diff options
author | kdx <kikoodx@paranoici.org> | 2023-07-12 16:30:45 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-07-12 16:30:45 +0200 |
commit | 665faef6136045e5cee7e17846dc01fa569a7687 (patch) | |
tree | 5fa31597496572516fdbfe82516769220925357d | |
parent | 42e295485cd2ed62b773de3fd6ec57b875033978 (diff) | |
download | fld-665faef6136045e5cee7e17846dc01fa569a7687.tar.gz |
drawBegin/drawEnd
-rw-r--r-- | demo.cpp | 4 | ||||
-rw-r--r-- | src/FLD.hpp | 19 | ||||
-rw-r--r-- | src/drawBegin.cpp | 11 | ||||
-rw-r--r-- | src/drawEnd.cpp | 48 | ||||
-rw-r--r-- | src/init.cpp | 6 |
5 files changed, 86 insertions, 2 deletions
@@ -10,5 +10,9 @@ main() }); if (fld.init()) return 1; + if (fld.drawBegin()) + return 1; + if (fld.drawEnd()) + return 1; return 0; } diff --git a/src/FLD.hpp b/src/FLD.hpp index cae4cf8..65111f8 100644 --- a/src/FLD.hpp +++ b/src/FLD.hpp @@ -2,7 +2,7 @@ #include <SDL2/SDL_render.h> #include <string> -#define READONLY(T, X, V) private: T _##X = V; public: T const& X = _##X +#define READONLY(T, X, V) private: T _##X = V; public: decltype(_##X) const& X = _##X class FLD { public: @@ -20,13 +20,28 @@ public: READONLY(SDL_Window *, window, nullptr); READONLY(SDL_Renderer *, renderer, nullptr); READONLY(SDL_Texture *, target, nullptr); + READONLY(unsigned long, tick, 0); +private: + struct { + bool managed = false; + Uint64 min_dt; + Uint64 next_time; + } fps; + struct { + float scale = 1.0; + int off_x = 0; + int off_y = 0; + } transform; public: using Error = int; FLD(const Config& config); FLD(const Config&& config); - [[nodiscard]] Error init(); ~FLD(); + [[nodiscard]] Error init(); + + [[nodiscard]] Error drawBegin() const; + [[nodiscard]] Error drawEnd(); private: Error sdl_error() const; [[nodiscard]] Error initBasepath() const; diff --git a/src/drawBegin.cpp b/src/drawBegin.cpp new file mode 100644 index 0000000..fc25ae3 --- /dev/null +++ b/src/drawBegin.cpp @@ -0,0 +1,11 @@ +#include "FLD.hpp" + +FLD::Error +FLD::drawBegin() const +{ + if (SDL_SetRenderTarget(_renderer, _target) < 0) + return sdl_error(); + if (SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND)) + return sdl_error(); + return 0; +} diff --git a/src/drawEnd.cpp b/src/drawEnd.cpp new file mode 100644 index 0000000..ead7bed --- /dev/null +++ b/src/drawEnd.cpp @@ -0,0 +1,48 @@ +#include "FLD.hpp" +#include <SDL2/SDL_timer.h> + +FLD::Error +FLD::drawEnd() +{ + if (SDL_SetRenderTarget(_renderer, nullptr) < 0) + return sdl_error(); + if (SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0xff) < 0) + return sdl_error(); + if (SDL_RenderClear(_renderer) < 0) + return sdl_error(); + + int win_w, win_h; + SDL_GetWindowSize(_window, &win_w, &win_h); + win_w = win_w ? win_w : 1; + win_h = win_h ? win_h : 1; + + const float ratio_w = win_w / static_cast<float>(config.width); + const float ratio_h = win_h / static_cast<float>(config.height); + transform.scale = (ratio_w < ratio_h) ? ratio_w : ratio_h; + if (transform.scale > 1.0f && config.pixel_perfect) + transform.scale = static_cast<int>(transform.scale); + transform.off_x = (win_w - config.width * transform.scale) / 2; + transform.off_y = (win_h - config.height * transform.scale) / 2; + + const SDL_Rect dest = { + .x = transform.off_x, + .y = transform.off_y, + .w = static_cast<int>(config.width * transform.scale), + .h = static_cast<int>(config.height * transform.scale) + }; + if (SDL_RenderCopy(_renderer, _target, nullptr, &dest) < 0) + return sdl_error(); + + if (fps.managed) { + fps.next_time += fps.min_dt; + const Uint64 cur_time = SDL_GetTicks64(); + if (fps.next_time <= cur_time) + fps.next_time = cur_time; + else + SDL_Delay(fps.next_time - cur_time); + } + + SDL_RenderPresent(_renderer); + _tick += 1; + return 0; +} diff --git a/src/init.cpp b/src/init.cpp index a6689a3..d5f5dfd 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -23,6 +23,12 @@ FLD::init() sdl_error(); } + fps.managed = (config.target_fps > 0); + if (fps.managed) { + fps.min_dt = 1000 / config.target_fps; + fps.next_time = SDL_GetTicks64(); + } + return 0; } |