diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c2c2be..c07b60e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,12 @@ file(GLOB_RECURSE SourceFiles ) add_executable(${BuildDIR}/${EXEName} ${SourceFiles}) + +set_target_properties(${BuildDIR}/${EXEName} PROPERTIES + CXX_STANDARD 17 + CXX_EXTENSIONS OFF +) + target_link_libraries(${BuildDIR}/${EXEName} SDL2 SDL2_image diff --git a/TODO.txt b/TODO.txt index 0de1cbd..2f4469c 100644 --- a/TODO.txt +++ b/TODO.txt @@ -10,6 +10,12 @@ x -> complete **TODO** [-] Comment the code [?] Overload operators in the logger and support for streams +[?] Vector classes (2d, 3d, 4d) + [x] Operators for each + [x] Scalar addition, subtraction, addition and division + [x] Dot product + [x] Cross product (3d) + [ ] Other functions for max, min, magnitude, unitvec all that [ ] Entity system [ ] Entity manager [ ] Ability to make entities and manage textures between them diff --git a/bin/crumpet-engine b/bin/crumpet-engine index 35a47bf..ba06b26 100755 Binary files a/bin/crumpet-engine and b/bin/crumpet-engine differ diff --git a/src/crumpet-engine/entitymanager/entitybase.h b/src/crumpet-engine/entitymanager/entitybase.h index 76a4994..d7a3df0 100644 --- a/src/crumpet-engine/entitymanager/entitybase.h +++ b/src/crumpet-engine/entitymanager/entitybase.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include "../math.h" #include "../resourcemanager/resourcemanager.h" @@ -15,5 +15,5 @@ public: private: std::string textureRef; std::string textureSource; - Vec2 textureDimensions; + Vec2 textureDimensions; }; diff --git a/src/crumpet-engine/math.h b/src/crumpet-engine/math.h index 8ea2aba..1d2ff6d 100644 --- a/src/crumpet-engine/math.h +++ b/src/crumpet-engine/math.h @@ -11,32 +11,210 @@ inline float ToDegree(const float Radian) { return (Radian * RAD2DEG); } +template struct Vec4 { - int x, y, z, w; - Vec4(int x, int y, int z, int w) : x(x), y(y), z(z), w(w) {} -}; - -struct Vec4f { - float x, y, z, w; - Vec4f(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {} + T x, y, z, w; + template + Vec4(P x, P y, P z, P w) : x(x), y(y), z(z), w(w) {} + template + Vec4(P all) : x(all), y(all), z(all), w(all) {} + Vec4() : x(0), y(0), z(0), w(0) {} + inline Vec4& dot(const Vec3& v) { + return (x * v.x + y * v.y + z * v.z + w * v.w); + } + inline const Vec4& operator+() { + return *this; + } + inline Vec4& operator-() { + return Vec4(-x, -y, -z, -w); + } + inline Vec4& operator+(const Vec4& v) { + return new Vec4(x + v.x, y + v.y, z + v.z, w + v.w); + } + inline Vec4& operator-(const Vec4& v) { + return new Vec4(x - v.x, y - v.y, z - v.z, w - v.w); + } + inline Vec4& operator*(const Vec4& v) { + return new Vec4(x * v.x, y * v.y, z * v.z, w * v.w); + } + inline Vec4& operator/(const Vec4& v) { + return new Vec4(x / v.x, y / v.y, z / v.z, w / v.w); + } + inline Vec4& operator+=(const Vec4& v) { + x+=v.x; y+=v.y; z+=v.z; w+=v.w; + return *this; + } + inline Vec4& operator-=(const Vec4& v) { + x-=v.x; y-=v.y; z-=v.z; w-=v.w; + return *this; + } + inline Vec4& operator*=(const Vec4& v) { + x*=v.x; y*=v.y; z*=v.z; w*=v.w; + return *this; + } + inline Vec4& operator/=(const Vec4& v) { + x/=v.x; y/=v.y; z/=v.z; w/=v.w; + return *this; + } + template + inline Vec4& operator+=(P s) { + x+=s; y+=s; z+=s; w+=s; + return *this; + } + template + inline Vec4& operator-=(P s) { + x-=s; y-=s; z-=s; w-=s; + return *this; + } + template + inline Vec4& operator*=(P s) { + x*=s; y*=s; z*=s; w*=s; + return *this; + } + template + inline Vec4& operator/=(P s) { + x/=s; y/=s; z/=s; w/=s; + return *this; + } }; +template struct Vec3 { - int x, y, z; - Vec3(int x, int y, int z) : x(x), y(y), z(z) {} -}; - -struct Vec3f { - float x, y, z; - Vec3f(float x, float y, float z) : x(x), y(y), z(z) {} + T x, y, z; + template + Vec3(P x, P y, P z) : x(x), y(y), z(z) {} + template + Vec3(P all) : x(all), y(all), z(all) {} + Vec3() : x(0), y(0), z(0) {} + inline Vec3& cross(const Vec3& v) { + return new Vec3( + (y * v.z - z * v.y), + (x * v.z - z * v.x), + (x * v.y - y * v.x) + ); + } + inline Vec3& dot(const Vec3& v) { + return (x * v.x + y * v.y + z * v.z); + } + inline const Vec3& operator+() { + return *this; + } + inline Vec3& operator-() { + return Vec3(-x, -y, -z); + } + inline Vec3& operator+(const Vec3& v) { + return new Vec3(x + v.x, y + v.y, z + v.z); + } + inline Vec3& operator-(const Vec3& v) { + return new Vec3(x - v.x, y - v.y, z - v.z); + } + inline Vec3& operator*(const Vec3& v) { + return new Vec3(x * v.x, y * v.y, z * v.z); + } + inline Vec3& operator/(const Vec3& v) { + return new Vec3(x / v.x, y / v.y, z / v.z); + } + inline Vec3& operator+=(const Vec3& v) { + x+=v.x; y+=v.y; z+=v.z; + return *this; + } + inline Vec3& operator-=(const Vec3& v) { + x-=v.x; y-=v.y; z-=v.z; + return *this; + } + inline Vec3& operator*=(const Vec3& v) { + x*=v.x; y*=v.y; z*=v.z; + return *this; + } + inline Vec3& operator/=(const Vec3& v) { + x/=v.x; y/=v.y; z/=v.z; + return *this; + } + template + inline Vec3& operator+=(P s) { + x+=s; y+=s; z+=s; + return *this; + } + template + inline Vec3& operator-=(P s) { + x-=s; y-=s; z-=s; + return *this; + } + template + inline Vec3& operator*=(P s) { + x*=s; y*=s; z*=s; + return *this; + } + template + inline Vec3& operator/=(P s) { + x/=s; y/=s; z/=s; + return *this; + } }; +template struct Vec2 { - int x, y; - Vec2(int x, int y) : x(x), y(y) {} -}; - -struct Vec2f { - float x, y; - Vec2f(float x, float y) : x(x), y(y) {} + T x, y; + template + Vec2(P x, P y) : x(x), y(y) {} + template + Vec2(P all) : x(all), y(all) {} + Vec2() : x(0), y(0) {} + inline const Vec2& operator+() { + return *this; + } + inline Vec2& dot(const Vec3& v) { + return (x * v.x + y * v.y); + } + inline Vec2& operator-() { + return Vec3(-x, -y); + } + inline Vec2& operator+(const Vec2& v) { + return new Vec2(x + v.x, y + v.y); + } + inline Vec2& operator-(const Vec2& v) { + return new Vec2(x - v.x, y - v.y); + } + inline Vec2& operator*(const Vec2& v) { + return new Vec2(x * v.x, y * v.y); + } + inline Vec2& operator/(const Vec2& v) { + return new Vec2(x / v.x, y / v.y); + } + inline Vec2& operator+=(const Vec2& v) { + x+=v.x; y+=v.y; + return *this; + } + inline Vec2& operator-=(const Vec2& v) { + x-=v.x; y-=v.y; + return *this; + } + inline Vec2& operator*=(const Vec2& v) { + x*=v.x; y*=v.y; + return *this; + } + inline Vec2& operator/=(const Vec2& v) { + x/=v.x; y/=v.y; + return *this; + } + template + inline Vec2& operator+=(P s) { + x+=s; y+=s; + return *this; + } + template + inline Vec2& operator-=(P s) { + x-=s; y-=s; + return *this; + } + template + inline Vec2& operator*=(P s) { + x*=s; y*=s; + return *this; + } + template + inline Vec2& operator/=(P s) { + x/=s; y/=s; + return *this; + } }; diff --git a/src/crumpet-engine/rect.cpp b/src/crumpet-engine/rect.cpp index c29189f..115a01a 100644 --- a/src/crumpet-engine/rect.cpp +++ b/src/crumpet-engine/rect.cpp @@ -63,7 +63,7 @@ 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) { +bool Rect::Contains(Vec2* point) { return (point->x >= x && point->x <= (x + w) && point->y >= y && point->y <= (y + h)); } @@ -72,13 +72,13 @@ bool Rect::Contains(int x, int y, int w, int h) { return Contains(&tempRect); } -Vec2* Rect::Position() { - Vec2* res = new Vec2(x, y); +Vec2* Rect::Position() { + Vec2* res = new Vec2(x, y); return res; } -Vec2* Rect::Center() { - Vec2* res = new Vec2(x + (w / 2), y + (h / 2)); +Vec2* Rect::Center() { + Vec2* res = new Vec2(x + (w / 2), y + (h / 2)); return res; } @@ -142,17 +142,17 @@ void Rect::SetRect(int x, int y, int w, int h) { this->rect->h = h; } -void Rect::SetSize(Vec2* size) { +void Rect::SetSize(Vec2* size) { this->x = size->x; this->y = size->y; } -void Rect::SetPos(Vec2* pos) { +void Rect::SetPos(Vec2* pos) { this->w = pos->x; this->h = pos->y; } -void Rect::Translate(Vec2* offset) { +void Rect::Translate(Vec2* offset) { this->x += offset->x; this->y += offset->y; } diff --git a/src/crumpet-engine/rect.h b/src/crumpet-engine/rect.h index 5125b50..bcf123f 100644 --- a/src/crumpet-engine/rect.h +++ b/src/crumpet-engine/rect.h @@ -35,11 +35,11 @@ public: // bool Intersects(int x, int y, int w, int h); bool Contains(Rect* rect); - bool Contains(Vec2* point); + bool Contains(Vec2* point); bool Contains(int x, int y, int w, int h); - Vec2* Position(); - Vec2* Center(); + Vec2* Position(); + Vec2* Center(); int CenterX(); int CenterY(); @@ -47,7 +47,7 @@ public: int Right(); int Top(); int Bottom(); - int Perimiter(); + int Perimiter(); int Area(); int GetX(); @@ -56,9 +56,9 @@ public: int GetH(); void SetRect(int x, int y, int w, int h); - void SetSize(Vec2* size); - void SetPos(Vec2* pos); - void Translate(Vec2* offset); + void SetSize(Vec2* size); + void SetPos(Vec2* pos); + void Translate(Vec2* offset); void TranslateX(int x); void TranslateY(int y); diff --git a/src/main.cpp b/src/main.cpp index 580bf09..2c47099 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,9 +4,6 @@ #include int main(int argc, char** argv) { - - // Logger.Log() << "Okay, this is epic" << 1234; - Game game; game.renderer.createWindow("Crumpet Engine", 600, 400, SCREEN_MODE_VSYNC);