From ff67b7fe8c6b8bf398591b6ae735e567db6be534 Mon Sep 17 00:00:00 2001 From: plane000 Date: Sat, 6 Oct 2018 16:07:04 +0100 Subject: [PATCH] Rectangle class complete --- TODO.txt | 16 +- crumpet-engine/camera.cpp | 9 ++ crumpet-engine/crumpet-engine.vcxproj | 1 + crumpet-engine/crumpet-engine.vcxproj.filters | 1 + crumpet-engine/entity.h | 4 +- crumpet-engine/rect.cpp | 139 +++++++++++++++++- crumpet-engine/rect.h | 62 +++++++- 7 files changed, 219 insertions(+), 13 deletions(-) create mode 100644 crumpet-engine/camera.cpp diff --git a/TODO.txt b/TODO.txt index 672275a..470af9c 100644 --- a/TODO.txt +++ b/TODO.txt @@ -8,15 +8,15 @@ CURRENT TODO LIST FOR THE DEVELOPMENT OF THE CRUMPET GAME ENGINE [ ] Time and other useful logging methods [ ] Empty constructor, instance method [ ] Rect class to extend SDL_Rect and add conversion capabilitys, colision and intersection to it - [ ] Camera class to use Rect* instead of SLD_Rect* - [ ] Add and subtract (+ -) operators to add rectangles and subtract them together - [ ] Equal and not equal (!= ==) operators to return true / flase if the rectangles match - [ ] ToString function for easy logging - [ ] Setposition and translate methods - [ ] Intersects, contains methods - [ ] x,y,w,h properties + [x] Camera class to use Rect* instead of SLD_Rect* + [x] Add and subtract (+ -) operators to add rectangles and subtract them together + [x] Equal and not equal (!= ==) operators to return true / flase if the rectangles match + [x] ToString function for easy logging + [x] Setposition and translate methods + [x] Intersects, contains methods + [x] x,y,w,h properties [ ] Switch other classes to use this instead of SDL_Rect* and make sure to update the render pipeline - [ ] Center point + [x] Center point [ ] Maybe a point class [ ] Game camera class and redo rendering pipeline [ ] Add rotation and flipping for entities and sprites diff --git a/crumpet-engine/camera.cpp b/crumpet-engine/camera.cpp new file mode 100644 index 0000000..3b5e737 --- /dev/null +++ b/crumpet-engine/camera.cpp @@ -0,0 +1,9 @@ +#include "camera.h" + +Camera::Camera() { + +} + +Camera::~Camera() { + +} diff --git a/crumpet-engine/crumpet-engine.vcxproj b/crumpet-engine/crumpet-engine.vcxproj index 2ebd585..fad3d64 100644 --- a/crumpet-engine/crumpet-engine.vcxproj +++ b/crumpet-engine/crumpet-engine.vcxproj @@ -125,6 +125,7 @@ + diff --git a/crumpet-engine/crumpet-engine.vcxproj.filters b/crumpet-engine/crumpet-engine.vcxproj.filters index 0f830fc..36fd345 100644 --- a/crumpet-engine/crumpet-engine.vcxproj.filters +++ b/crumpet-engine/crumpet-engine.vcxproj.filters @@ -10,6 +10,7 @@ + diff --git a/crumpet-engine/entity.h b/crumpet-engine/entity.h index d593a35..bc410b7 100644 --- a/crumpet-engine/entity.h +++ b/crumpet-engine/entity.h @@ -45,8 +45,8 @@ public: virtual ~Entity(); protected: - std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine"; - //std::string PATH = "E:/Games/crumpet-engine"; + // std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine"; + std::string PATH = "E:/Games/crumpet-engine"; private: std::string m_name; diff --git a/crumpet-engine/rect.cpp b/crumpet-engine/rect.cpp index aa4ff35..d1a09f3 100644 --- a/crumpet-engine/rect.cpp +++ b/crumpet-engine/rect.cpp @@ -1,9 +1,146 @@ #include "rect.h" Rect::Rect() { + Clear(); +} +Rect::Rect(int x, int y, int w, int h) { + SetRect(x, y, w, h); +} + +void Rect::Clear() { + SetRect(0, 0, 0, 0); +} + +SDL_Rect* Rect::ToSDLRect() { + rect->x = x; + rect->y = y; + rect->w = w; + rect->h = h; + + return rect; +} + +std::string Rect::ToString() { + std::string res = ""; + + res += "( "; + res += std::to_string(x); + res += ", "; + res += std::to_string(y); + res += ", "; + res += std::to_string(w); + res += ", "; + res += std::to_string(h); + res += ")"; + return res; +} + +bool Rect::Intersects(Rect* rect) { + int leftA = x; + int rightA = x + w; + int topA = y; + int bottomA = y + h; + + int leftB = rect->x; + int rightB = rect->x + rect->w; + int topB = rect->y; + int bottomB = rect->y + rect->h; + + if (bottomA <= topB) return false; + if (topA >= bottomB) return false; + if (rightA <= leftB) return false; + if (leftA >= rightB) return false; + + return true; +} + +bool Rect::Intersects(int x, int y, int w, int h) { + return Intersects(&CreateRect(x, y, w, h)); +} + +bool Rect::Contains(Rect* rect) { + return (rect->x >= x && rect->Right() <= (x + w) && rect->y >= y && rect->Bottom() <= (y + h)); +} + +bool Rect::Contains(Vec2* point) { + return (point->x >= x && point->x <= (x + w) && point->y >= y && point->y <= (y + h)); +} + +bool Rect::Contains(int x, int y, int w, int h) { + Rect tempRect(x, y, w, h); + return Contains(&tempRect); +} + +Vec2* Rect::Position() { + Vec2* res = new Vec2(x, y); + return res; +} + +Vec2* Rect::Center() { + Vec2* res = new Vec2(x + (w / 2), y + (h / 2)); + return res; +} + +int Rect::CenterX() { + return (x + (w / 2)); +} + +int Rect::CenterY() { + return (y + (h / 2)); +} + +int Rect::Left() { + return x; +} + +int Rect::Right() { + return (x + w); +} + +int Rect::Top() { + return y; +} + +int Rect::Bottom() { + return y + h; +} + +int Rect::Perimiter() { + return (w + w + h + h); +} + +int Rect::Area() { + return (w + h); +} + +void Rect::SetRect(int x, int y, int w, int h) { + +} + +void Rect::SetSize(Vec2* size) { + this->x = size->x; + this->y = size->y; +} + +void Rect::SetPos(Vec2* pos) { + this->w = pos->x; + this->h = pos->y; +} + +void Rect::Translate(Vec2* offset) { + this->x += offset->x; + this->y += offset->y; +} + +void Rect::TranslateX(int x) { + this->x += x; +} + +void Rect::TranslateY(int y) { + this->y += y; } Rect::~Rect() { - + rect = NULL; } diff --git a/crumpet-engine/rect.h b/crumpet-engine/rect.h index 2b1f85d..e2b6484 100644 --- a/crumpet-engine/rect.h +++ b/crumpet-engine/rect.h @@ -1,7 +1,65 @@ #pragma once + +#include +#include +#include "mathHelper.h" + class Rect { public: Rect(); - virtual ~Rect(); -}; + Rect(int x, int y, int w, int h); + void Clear(); + static Rect CreateRect(int x, int y, int w, int h) { + Rect tempRect(x, y, w, h); + return tempRect; + } + + Rect operator+(Rect* rect) { + return Rect(this->x + rect->x, this->y + this->x, w, h); + } + Rect operator-(Rect* rect) { + return Rect(this->x - rect->x, this->y - this->x, w, h); + } + bool operator==(const Rect* rect) { + return (x == rect->x && y == rect->y && w == rect->w && h == rect->h); + } + bool operator!=(const Rect* rect) { + return !(x == rect->x && y == rect->y && w == rect->w && h == rect->h); + } + + SDL_Rect* ToSDLRect(); + std::string ToString(); + + bool Intersects(Rect* rect); + bool Intersects(int x, int y, int w, int h); + + bool Contains(Rect* rect); + bool Contains(Vec2* point); + bool Contains(int x, int y, int w, int h); + + Vec2* Position(); + Vec2* Center(); + int CenterX(); + int CenterY(); + + int Left(); + int Right(); + int Top(); + int Bottom(); + int Perimiter(); + int Area(); + + void SetRect(int x, int y, int w, int h); + void SetSize(Vec2* size); + void SetPos(Vec2* pos); + void Translate(Vec2* offset); + void TranslateX(int x); + void TranslateY(int y); + + int x, y, w, h; + + virtual ~Rect(); +private: + SDL_Rect* rect; +};