Rectangle class complete

This commit is contained in:
plane000
2018-10-06 16:07:04 +01:00
parent d5b9d88a30
commit ff67b7fe8c
7 changed files with 219 additions and 13 deletions

View File

@@ -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

View File

@@ -0,0 +1,9 @@
#include "camera.h"
Camera::Camera() {
}
Camera::~Camera() {
}

View File

@@ -125,6 +125,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="camera.cpp" />
<ClCompile Include="entity.cpp" />
<ClCompile Include="game.cpp" />
<ClCompile Include="gameWorld.cpp" />

View File

@@ -10,6 +10,7 @@
<ClCompile Include="gameWorld.cpp" />
<ClCompile Include="level.cpp" />
<ClCompile Include="rect.cpp" />
<ClCompile Include="camera.cpp" />
</ItemGroup>
<ItemGroup>
<Filter Include="headers">

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -1,7 +1,65 @@
#pragma once
#include <string>
#include <SDL.h>
#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;
};