From 1129b8ac89f1e614f69793227ccec90157708aea Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 23 Dec 2013 18:54:47 +0100 Subject: [PATCH] Vector : SDL_Point optimization --- src/graphics/Canvas.cpp | 10 ++++++++++ src/graphics/Canvas.h | 2 ++ src/graphics/Vector.h | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/graphics/Canvas.cpp b/src/graphics/Canvas.cpp index 45997c6..e4befc0 100644 --- a/src/graphics/Canvas.cpp +++ b/src/graphics/Canvas.cpp @@ -95,6 +95,16 @@ void Canvas::Cross(Vector pos, int extent) { Vector(pos.x, pos.y + extent)); } +void Canvas::Triangle(Vector v1, Vector v2, Vector v3) { + SDL_Point points[4] = { v1, v2, v3, v1 }; + SDL_RenderDrawPoints(canv, points, 4); +} + +void Canvas::Quad(Vector v1, Vector v2, Vector v3, Vector v4) { + SDL_Point points[5] = { v1, v2, v3, v4, v1 }; + SDL_RenderDrawPoints(canv, points, 5); +} + namespace { diff --git a/src/graphics/Canvas.h b/src/graphics/Canvas.h index 032c7db..c9a6488 100644 --- a/src/graphics/Canvas.h +++ b/src/graphics/Canvas.h @@ -38,6 +38,8 @@ public: void Dot(Vector pos); void Cross(Vector pos, int extent); + void Triangle(Vector, Vector, Vector); + void Quad(Vector, Vector, Vector, Vector); void Grid(Vector pos, Vector size, Vector step); void Grid(Vector pos, Vector size, Vector step); diff --git a/src/graphics/Vector.h b/src/graphics/Vector.h index b5c6d7d..34d8297 100644 --- a/src/graphics/Vector.h +++ b/src/graphics/Vector.h @@ -3,6 +3,7 @@ #include #include +#include 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 +: public SDL_Point { + +public: + constexpr Vector() : SDL_Point({0, 0}) { } + constexpr Vector(int x, int y) : SDL_Point({x, y}) { } + + template + constexpr Vector(Vector other) + : SDL_Point({int(other.x), int(other.y)}) { } + +public: + Vector &operator +=(Vector other) { + x += other.x; + y += other.y; + return *this; + } + Vector &operator -=(Vector other) { + x -= other.x; + y -= other.y; + return *this; + } + + SDL_Point ToPoint() const { + return *this; + } + +}; + template constexpr Vector operator -(Vector v) { -- 2.39.2