From df56f312ed7ff73f5afa3167b0c6c754c5ea06e7 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Wed, 18 Sep 2013 03:05:45 +0400 Subject: [PATCH] Add draw functions --- SDL2pp/Renderer.cc | 111 +++++++++++++++++++++++++++++++++++++++++++++ SDL2pp/Renderer.hh | 20 ++++++++ demo/demo.cc | 27 +++++++++++ 3 files changed, 158 insertions(+) diff --git a/SDL2pp/Renderer.cc b/SDL2pp/Renderer.cc index 575eb20..6e5b554 100644 --- a/SDL2pp/Renderer.cc +++ b/SDL2pp/Renderer.cc @@ -19,6 +19,8 @@ 3. This notice may not be removed or altered from any source distribution. */ +#include + #include #include @@ -77,4 +79,113 @@ void Renderer::SetTarget(Texture& texture) { throw Exception("SDL_SetRenderTarget failed"); } +void Renderer::SetDrawBlendMode(SDL_BlendMode blendMode) { + if (SDL_SetRenderDrawBlendMode(renderer_, blendMode) != 0) + throw Exception("SDL_SetRenderDrawBlendMode failed"); +} + +void Renderer::DrawPoint(int x, int y) { + if (SDL_RenderDrawPoint(renderer_, x, y) != 0) + throw Exception("SDL_RenderDrawPoint failed"); +} + +void Renderer::DrawPoint(const Point& p) { + if (p.IsNull()) + return; + DrawPoint(p.GetX(), p.GetY()); +} + +void Renderer::DrawPoints(const Point* points, int count) { + std::vector sdl_points; + sdl_points.reserve(count); + for (const Point* p = points; p != points + count; ++p) + if (!p->IsNull()) + sdl_points.emplace_back(*p->Get()); + + if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0) + throw Exception("SDL_RenderDrawPoints failed"); +} + +void Renderer::DrawLine(int x1, int y1, int x2, int y2) { + if (SDL_RenderDrawLine(renderer_, x1, y1, x2, y2) != 0) + throw Exception("SDL_RenderDrawLine failed"); +} + +void Renderer::DrawLine(const Point& p1, const Point& p2) { + if (p1.IsNull() || p2.IsNull()) + return; + DrawLine(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY()); +} + +void Renderer::DrawLines(const Point* points, int count) { + std::vector sdl_points; + sdl_points.reserve(count); + for (const Point* p = points; p != points + count; ++p) + if (!p->IsNull()) + sdl_points.emplace_back(*p->Get()); + + if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0) + throw Exception("SDL_RenderDrawLines failed"); +} + +void Renderer::DrawRect(int x1, int y1, int x2, int y2) { + SDL_Rect rect = {x1, y1, x2 - x1 + 1, y2 - y1 + 1}; + if (SDL_RenderDrawRect(renderer_, &rect) != 0) + throw Exception("SDL_RenderDrawRect failed"); +} + +void Renderer::DrawRect(const Point& p1, const Point& p2) { + if (p1.IsNull() || p2.IsNull()) + return; + DrawRect(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY()); +} + +void Renderer::DrawRect(const Rect& r) { + if (r.IsNull()) + return; + if (SDL_RenderDrawRect(renderer_, r.Get()) != 0) + throw Exception("SDL_RenderDrawRect failed"); +} + +void Renderer::DrawRects(const Rect* rects, int count) { + std::vector sdl_rects; + sdl_rects.reserve(count); + for (const Rect* r = rects; r != rects + count; ++r) + if (!r->IsNull()) + sdl_rects.emplace_back(*r->Get()); + + if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0) + throw Exception("SDL_RenderDrawRects failed"); +} + +void Renderer::FillRect(int x1, int y1, int x2, int y2) { + SDL_Rect rect = {x1, y1, x2 - x1 + 1, y2 - y1 + 1}; + if (SDL_RenderDrawRect(renderer_, &rect) != 0) + throw Exception("SDL_RenderFillRect failed"); +} + +void Renderer::FillRect(const Point& p1, const Point& p2) { + if (p1.IsNull() || p2.IsNull()) + return; + FillRect(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY()); +} + +void Renderer::FillRect(const Rect& r) { + if (r.IsNull()) + return; + if (SDL_RenderDrawRect(renderer_, r.Get()) != 0) + throw Exception("SDL_RenderFillRect failed"); +} + +void Renderer::FillRects(const Rect* rects, int count) { + std::vector sdl_rects; + sdl_rects.reserve(count); + for (const Rect* r = rects; r != rects + count; ++r) + if (!r->IsNull()) + sdl_rects.emplace_back(*r->Get()); + + if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0) + throw Exception("SDL_RenderFillRects failed"); +} + } diff --git a/SDL2pp/Renderer.hh b/SDL2pp/Renderer.hh index ee95222..7d7c37e 100644 --- a/SDL2pp/Renderer.hh +++ b/SDL2pp/Renderer.hh @@ -57,6 +57,26 @@ public: void SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255); void SetTarget(Texture& texture); + + void SetDrawBlendMode(SDL_BlendMode blendMode); + + void DrawPoint(int x, int y); + void DrawPoint(const Point& p); + void DrawPoints(const Point* points, int count); + + void DrawLine(int x1, int y1, int x2, int y2); + void DrawLine(const Point& p1, const Point& p2); + void DrawLines(const Point* points, int count); + + void DrawRect(int x1, int y1, int x2, int y2); + void DrawRect(const Point& p1, const Point& p2); + void DrawRect(const Rect& r); + void DrawRects(const Rect* rects, int count); + + void FillRect(int x1, int y1, int x2, int y2); + void FillRect(const Point& p1, const Point& p2); + void FillRect(const Rect& r); + void FillRects(const Rect* rects, int count); }; } diff --git a/demo/demo.cc b/demo/demo.cc index e0ca75f..7e23a6a 100644 --- a/demo/demo.cc +++ b/demo/demo.cc @@ -42,6 +42,8 @@ int main() { sprite.Update(Rect::Null(), pixels, 4 * 4); sprite.SetBlendMode(SDL_BLENDMODE_BLEND); + render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); + while (1) { // Process events SDL_Event event; @@ -51,8 +53,33 @@ int main() { } // Render + render.SetDrawColor(0, 0, 0); render.Clear(); + // Render lines + render.SetDrawColor(255, 0, 0); + render.DrawLine(10, 10, 630, 10); + render.SetDrawColor(0, 255, 0); + render.DrawLine(630, 10, 630, 470); + render.SetDrawColor(0, 0, 255); + render.DrawLine(630, 470, 10, 470); + render.SetDrawColor(255, 255, 255); + render.DrawLine(10, 470, 10, 10); + + render.SetDrawColor(255, 255, 255, 127); + render.FillRect(0, 0, 20, 20); + render.SetDrawColor(255, 255, 255); + render.DrawRect(0, 0, 20, 20); + + // Pixel-perfectness test + render.SetDrawColor(192, 192, 192); + render.DrawLine(6, 2, 6, 10); + render.DrawLine(2, 6, 10, 6); + + render.SetDrawColor(255, 255, 255); + render.DrawRect(5, 5, 7, 7); + render.DrawRect(3, 3, 9, 9); + // Render 4 smaller squares sprite.SetAlphaMod(0xff); render.Copy(sprite, Rect::Null(), Rect(80, 0, 240, 240), SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);