]> git.localhorst.tv Git - space.git/commitdiff
Vector<int> : SDL_Point optimization
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 23 Dec 2013 17:54:47 +0000 (18:54 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 23 Dec 2013 17:54:47 +0000 (18:54 +0100)
src/graphics/Canvas.cpp
src/graphics/Canvas.h
src/graphics/Vector.h

index 45997c6b3d3ad92cac6b5c38e6ff29ec570b3b71..e4befc0bf04886003dc3d8f04ce409c0d5f15c46 100644 (file)
@@ -95,6 +95,16 @@ void Canvas::Cross(Vector<int> pos, int extent) {
                Vector<int>(pos.x, pos.y + extent));
 }
 
+void Canvas::Triangle(Vector<int> v1, Vector<int> v2, Vector<int> v3) {
+       SDL_Point points[4] = { v1, v2, v3, v1 };
+       SDL_RenderDrawPoints(canv, points, 4);
+}
+
+void Canvas::Quad(Vector<int> v1, Vector<int> v2, Vector<int> v3, Vector<int> v4) {
+       SDL_Point points[5] = { v1, v2, v3, v4, v1 };
+       SDL_RenderDrawPoints(canv, points, 5);
+}
+
 
 namespace {
 
index 032c7dbb94f88b21f415d7caeddfdf93b0447fea..c9a6488ced4e621612fe61f7c5cc32cfd71f3ff0 100644 (file)
@@ -38,6 +38,8 @@ public:
 
        void Dot(Vector<int> pos);
        void Cross(Vector<int> pos, int extent);
+       void Triangle(Vector<int>, Vector<int>, Vector<int>);
+       void Quad(Vector<int>, Vector<int>, Vector<int>, Vector<int>);
 
        void Grid(Vector<int> pos, Vector<int> size, Vector<int> step);
        void Grid(Vector<int> pos, Vector<int> size, Vector<float> step);
index b5c6d7d11dd9f9452c0a65b781a511b8fa9603ad..34d829713e5dea735d339b0ed56cafdb3a409d11 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <algorithm>
 #include <ostream>
+#include <SDL.h>
 
 
 namespace space {
@@ -29,12 +30,50 @@ public:
                return *this;
        }
 
+       SDL_Point ToPoint() const {
+               SDL_Point p;
+               p.x = x;
+               p.y = y;
+               return p;
+       }
+
 public:
        Scalar x;
        Scalar y;
 
 };
 
+/// specialization with same layout as SDL_Point
+template<>
+class Vector<int>
+: public SDL_Point {
+
+public:
+       constexpr Vector() : SDL_Point({0, 0}) { }
+       constexpr Vector(int x, int y) : SDL_Point({x, y}) { }
+
+       template<class Other>
+       constexpr Vector(Vector<Other> other)
+       : SDL_Point({int(other.x), int(other.y)}) { }
+
+public:
+       Vector<int> &operator +=(Vector<int> other) {
+               x += other.x;
+               y += other.y;
+               return *this;
+       }
+       Vector<int> &operator -=(Vector<int> other) {
+               x -= other.x;
+               y -= other.y;
+               return *this;
+       }
+
+       SDL_Point ToPoint() const {
+               return *this;
+       }
+
+};
+
 
 template<class Scalar>
 constexpr Vector<Scalar> operator -(Vector<Scalar> v) {