diff --git a/App/App/src/main.cpp b/App/App/src/main.cpp index 20f3d10..5f435cc 100644 --- a/App/App/src/main.cpp +++ b/App/App/src/main.cpp @@ -1,8 +1,24 @@ #include "App.hh" +#include "Math.hh" +#include + +using namespace std; int main(int argc, char const *argv[]) { - App::App* app = new App::App(); - return app->getReturnCode(); + // App::App* app = new App::App(); + // return app->getReturnCode(); + Vector2 v1(1, 1.5); + Vector2 v2(3, -2); + Vector2 res = v1 + v2; + cout << res.x << ", " << res.y << endl; + res = v1 - v2; + cout << res.x << ", " << res.y << endl; + res = v1 * v2; + cout << res.x << ", " << res.y << endl; + res = v1 / v2; + cout << res.x << ", " << res.y << endl; + + return 0; } diff --git a/App/AppLib/include/AppLib/CowPtr.hh b/App/AppLib/include/AppLib/CowPtr.hh new file mode 100644 index 0000000..a1088ea --- /dev/null +++ b/App/AppLib/include/AppLib/CowPtr.hh @@ -0,0 +1,47 @@ +// Implementation from https://www.youtube.com/watch?v=9b9hWIH8-hE + +#include + +namespace Core { +template +class CowPtr +{ +public: + explicit CowPtr(T* t) : content(t) + { + } + + T& operator*() + { + detach(); + return *content; + } + const T& operator*() const + { + return *content; + } + + T* operator->() + { + detach(); + return content.operator->(); + } + + const T* operator->() const + { + return content.operator->(); + } + + std::shared_ptr content; + +private: + void detach() + { + T* tmp = content.get(); + if(!(tmp == 0 || content.unique())) + { + content = std::shared_ptr(new T(*tmp)); + } + } +}; +} \ No newline at end of file diff --git a/App/AppLib/include/AppLib/Math.hh b/App/AppLib/include/AppLib/Math.hh index b5d43b8..7c2179b 100644 --- a/App/AppLib/include/AppLib/Math.hh +++ b/App/AppLib/include/AppLib/Math.hh @@ -8,6 +8,23 @@ namespace Core class Vector2 { public: + Vector2(); + Vector2(float x, float y); + + Vector2 operator+(Vector2 const& other); + Vector2 operator-(Vector2 const& other); + Vector2 operator*(Vector2 const& other); + Vector2 operator/(Vector2 const& other); + Vector2 operator+=(Vector2 const& other); + Vector2 operator-=(Vector2 const& other); + Vector2 operator*=(Vector2 const& other); + Vector2 operator/=(Vector2 const& other); + + bool operator==(Vector2 const& other); + bool operator!=(Vector2 const& other); + + Vector2 operator*(float const& scalar); + float x; float y; }; @@ -18,7 +35,21 @@ namespace Core float x; float y; float z; - float w = 1; + float w; + + Vector3 operator+(Vector3 const& other); + Vector3 operator-(Vector3 const& other); + Vector3 operator*(Vector3 const& other); + Vector3 operator/(Vector3 const& other); + Vector3 operator+=(Vector3 const& other); + Vector3 operator-=(Vector3 const& other); + Vector3 operator*=(Vector3 const& other); + Vector3 operator/=(Vector3 const& other); + + bool operator==(Vector3 const& other); + bool operator!=(Vector3 const& other); + + Vector3 operator*(float const& scalar); }; } // namespace Core diff --git a/App/AppLib/src/Math.cpp b/App/AppLib/src/Math.cpp index 399f5ff..63280b9 100644 --- a/App/AppLib/src/Math.cpp +++ b/App/AppLib/src/Math.cpp @@ -2,4 +2,32 @@ namespace Core { + Vector2::Vector2() : x(0), y(0) + { + } + + Vector2::Vector2(float x, float y) : x(x), y(y) + { + } + + Vector2 Vector2::operator+(Vector2 const &other) + { + return Vector2(this->x+other.x, this->y+other.y); + } + + Vector2 Vector2::operator-(Vector2 const &other) + { + return Vector2(this->x-other.x, this->y-other.y); + } + + Vector2 Vector2::operator*(Vector2 const &other) + { + return Vector2(this->x*other.x, this->y*other.y); + } + + Vector2 Vector2::operator/(Vector2 const &other) + { + return Vector2(this->x/other.x, this->y/other.y); + } + } // namespace Core \ No newline at end of file