diff options
author | kdx <kikoodx@paranoici.org> | 2023-07-15 08:01:49 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-07-15 08:01:49 +0200 |
commit | f1a32aad576fb6b5bae80a391b28de445cfa24aa (patch) | |
tree | 40a5cea7716043f5b7cf67601627cb5315d85feb | |
parent | 638ff55913cf6a2a9fc09380a4efda04705d23da (diff) | |
download | fld-f1a32aad576fb6b5bae80a391b28de445cfa24aa.tar.gz |
drawLine && drawPoint
-rw-r--r-- | src/FLD.hpp | 26 | ||||
-rw-r--r-- | src/drawLine.cpp | 15 | ||||
-rw-r--r-- | src/drawPoint.cpp | 15 | ||||
-rw-r--r-- | src/drawRectangle.cpp | 10 |
4 files changed, 58 insertions, 8 deletions
diff --git a/src/FLD.hpp b/src/FLD.hpp index e08d9e6..584c418 100644 --- a/src/FLD.hpp +++ b/src/FLD.hpp @@ -23,13 +23,20 @@ public: float b = -1.0f; float a = -1.0f; }; + struct Point { + int x; + int y; + }; struct Rectangle { int x; int y; int w; int h; - bool center = false; - bool fill = false; + }; + enum DrawFlag { + NONE = 0, + FILL = 1, + CENTER = 2, }; const Config config; @@ -42,17 +49,28 @@ public: FLD(const Config& config); FLD(const Config&& config); ~FLD(); + [[nodiscard]] Error init(); - [[nodiscard]] Error init(MainLoop main_loop, void *udata = nullptr); + [[nodiscard]] Error init(MainLoop main_loop, void *udata=nullptr); + void run(MainLoop main_loop, void *udata = nullptr); [[nodiscard]] Error drawBegin() const; + [[nodiscard]] Error drawEnd(); + Error drawSetColor(float r, float g, float b, float a=-1); Error drawSetColor(const Color& c); + Error drawClear() const; - Error drawRectangle(const Rectangle& rect) const; + Error drawPoint(int x, int y) const; + Error drawPoint(const Point& p) const; + + Error drawLine(int x0, int y0, int x1, int y1) const; + Error drawLine(const Point& p0, const Point& p1) const; + + Error drawRectangle(const Rectangle& rect, unsigned flags=NONE) const; private: bool shouldQuit = false; struct { diff --git a/src/drawLine.cpp b/src/drawLine.cpp new file mode 100644 index 0000000..0c7e64d --- /dev/null +++ b/src/drawLine.cpp @@ -0,0 +1,15 @@ +#include "FLD.hpp" + +FLD::Error +FLD::drawLine(int x0, int y0, int x1, int y1) const +{ + if (SDL_RenderDrawLine(_renderer, x0, y0, x1, y1) < 0) + return sdlError(); + return 0; +} + +FLD::Error +FLD::drawLine(const Point& p0, const Point& p1) const +{ + return drawLine(p0.x, p0.y, p1.x, p1.y); +} diff --git a/src/drawPoint.cpp b/src/drawPoint.cpp new file mode 100644 index 0000000..fb24155 --- /dev/null +++ b/src/drawPoint.cpp @@ -0,0 +1,15 @@ +#include "FLD.hpp" + +FLD::Error +FLD::drawPoint(int x, int y) const +{ + if (SDL_RenderDrawPoint(_renderer, x, y) < 0) + return sdlError(); + return 0; +} + +FLD::Error +FLD::drawPoint(const Point& p) const +{ + return drawPoint(p.x, p.y); +} diff --git a/src/drawRectangle.cpp b/src/drawRectangle.cpp index fb21be9..fad2ccf 100644 --- a/src/drawRectangle.cpp +++ b/src/drawRectangle.cpp @@ -1,15 +1,17 @@ #include "FLD.hpp" FLD::Error -FLD::drawRectangle(const Rectangle &rect) const +FLD::drawRectangle(const Rectangle &rect, unsigned flags) const { + const bool fill = flags & FILL; + const bool center = flags & CENTER; const SDL_Rect srect { - .x = rect.x - rect.center * (rect.w / 2), - .y = rect.y - rect.center * (rect.h / 2), + .x = rect.x - center * (rect.w / 2), + .y = rect.y - center * (rect.h / 2), .w = rect.w, .h = rect.h }; - const auto fun = rect.fill ? SDL_RenderFillRect : SDL_RenderDrawRect; + const auto fun = fill ? SDL_RenderFillRect : SDL_RenderDrawRect; if (fun(_renderer, &srect) < 0) return sdlError(); return 0; |