Add draw functions

This commit is contained in:
Dmitry Marakasov 2013-09-18 03:05:45 +04:00
parent aa615060e4
commit df56f312ed
3 changed files with 158 additions and 0 deletions

View File

@ -19,6 +19,8 @@
3. This notice may not be removed or altered from any source distribution.
*/
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2pp/Renderer.hh>
@ -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_Point> 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_Point> 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_Rect> 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_Rect> 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");
}
}

View File

@ -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);
};
}

View File

@ -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);