diff options
author | kdx <kikoodx@paranoici.org> | 2023-07-12 16:58:49 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-07-12 16:58:52 +0200 |
commit | 2a4c2abab474d0e7ed5f427f23baf9c08719ebb2 (patch) | |
tree | 327d1cd88e898deafd65b557b989963dd856db72 | |
parent | 665faef6136045e5cee7e17846dc01fa569a7687 (diff) | |
download | fld-2a4c2abab474d0e7ed5f427f23baf9c08719ebb2.tar.gz |
run
-rw-r--r-- | demo.cpp | 24 | ||||
-rw-r--r-- | src/FLD.hpp | 27 | ||||
-rw-r--r-- | src/cycleEvents.cpp | 15 | ||||
-rw-r--r-- | src/drawBegin.cpp | 4 | ||||
-rw-r--r-- | src/drawEnd.cpp | 8 | ||||
-rw-r--r-- | src/init.cpp | 21 | ||||
-rw-r--r-- | src/mainLoop.cpp | 35 | ||||
-rw-r--r-- | src/sdlError.cpp (renamed from src/sdl_throw.cpp) | 2 |
8 files changed, 103 insertions, 33 deletions
@@ -1,18 +1,24 @@ #include "FLD.hpp" -int -main() +static int +main_loop(FLD& fld, [[maybe_unused]] void *udata) { - FLD fld (FLD::Config{ - .width=256, - .height=256, - .title="uwu", - }); - if (fld.init()) - return 1; if (fld.drawBegin()) return 1; if (fld.drawEnd()) return 1; return 0; } + +int +main() +{ + FLD fld ({ + .width = 256, + .height = 256, + .title = "uwu", + }); + if (fld.init(main_loop)) + return 1; + return 0; +} diff --git a/src/FLD.hpp b/src/FLD.hpp index 65111f8..c404996 100644 --- a/src/FLD.hpp +++ b/src/FLD.hpp @@ -21,7 +21,21 @@ public: READONLY(SDL_Renderer *, renderer, nullptr); READONLY(SDL_Texture *, target, nullptr); READONLY(unsigned long, tick, 0); +public: + using Error = int; + using MainLoop = int (*)(FLD& fld, void *udata); + + FLD(const Config& config); + FLD(const Config&& config); + ~FLD(); + [[nodiscard]] Error init(); + [[nodiscard]] Error init(MainLoop main_loop, void *udata = nullptr); + void run(MainLoop main_loop, void *udata = nullptr); + + [[nodiscard]] Error drawBegin() const; + [[nodiscard]] Error drawEnd(); private: + bool shouldQuit = false; struct { bool managed = false; Uint64 min_dt; @@ -32,20 +46,11 @@ private: int off_x = 0; int off_y = 0; } transform; -public: - using Error = int; - - FLD(const Config& config); - FLD(const Config&& config); - ~FLD(); - [[nodiscard]] Error init(); - [[nodiscard]] Error drawBegin() const; - [[nodiscard]] Error drawEnd(); -private: - Error sdl_error() const; + Error sdlError() const; [[nodiscard]] Error initBasepath() const; [[nodiscard]] Error initWindow(); [[nodiscard]] Error initRenderer(); [[nodiscard]] Error initTarget(); + void cycleEvents(); }; diff --git a/src/cycleEvents.cpp b/src/cycleEvents.cpp new file mode 100644 index 0000000..6f2e6dc --- /dev/null +++ b/src/cycleEvents.cpp @@ -0,0 +1,15 @@ +#include "FLD.hpp" +#include <SDL2/SDL_events.h> + +void +FLD::cycleEvents() +{ + SDL_Event e; + while (SDL_PollEvent(&e)) switch (e.type) { + case SDL_QUIT: + shouldQuit = true; + break; + default: + break; + } +} diff --git a/src/drawBegin.cpp b/src/drawBegin.cpp index fc25ae3..2991917 100644 --- a/src/drawBegin.cpp +++ b/src/drawBegin.cpp @@ -4,8 +4,8 @@ FLD::Error FLD::drawBegin() const { if (SDL_SetRenderTarget(_renderer, _target) < 0) - return sdl_error(); + return sdlError(); if (SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND)) - return sdl_error(); + return sdlError(); return 0; } diff --git a/src/drawEnd.cpp b/src/drawEnd.cpp index ead7bed..1db58cd 100644 --- a/src/drawEnd.cpp +++ b/src/drawEnd.cpp @@ -5,11 +5,11 @@ FLD::Error FLD::drawEnd() { if (SDL_SetRenderTarget(_renderer, nullptr) < 0) - return sdl_error(); + return sdlError(); if (SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0xff) < 0) - return sdl_error(); + return sdlError(); if (SDL_RenderClear(_renderer) < 0) - return sdl_error(); + return sdlError(); int win_w, win_h; SDL_GetWindowSize(_window, &win_w, &win_h); @@ -31,7 +31,7 @@ FLD::drawEnd() .h = static_cast<int>(config.height * transform.scale) }; if (SDL_RenderCopy(_renderer, _target, nullptr, &dest) < 0) - return sdl_error(); + return sdlError(); if (fps.managed) { fps.next_time += fps.min_dt; diff --git a/src/init.cpp b/src/init.cpp index d5f5dfd..2cd1b2f 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -3,10 +3,19 @@ #include <unistd.h> FLD::Error +FLD::init(MainLoop main_loop, void *udata) +{ + if (init()) + return sdlError(); + run(main_loop, udata); + return 0; +} + +FLD::Error FLD::init() { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) - return sdl_error(); + return sdlError(); if (auto err = initBasepath()) return err; if (auto err = initWindow()) @@ -20,7 +29,7 @@ FLD::init() if (config.hide_cursor) { if (SDL_ShowCursor(SDL_FALSE) < 0) - sdl_error(); + sdlError(); } fps.managed = (config.target_fps > 0); @@ -37,7 +46,7 @@ FLD::initBasepath() const { char *const basepath = SDL_GetBasePath(); if (basepath == nullptr) - return sdl_error(); + return sdlError(); const int chdir_rv = chdir(basepath); SDL_free(basepath); if (chdir_rv < 0) @@ -55,7 +64,7 @@ FLD::initWindow() config.height * config.scale, SDL_WINDOW_RESIZABLE); if (_window == nullptr) - return sdl_error(); + return sdlError(); return 0; } @@ -68,7 +77,7 @@ FLD::initRenderer() _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED); #endif if (_renderer == nullptr) - return sdl_error(); + return sdlError(); return 0; } @@ -81,6 +90,6 @@ FLD::initTarget() config.width, config.height); if (_target == nullptr) - return sdl_error(); + return sdlError(); return 0; } diff --git a/src/mainLoop.cpp b/src/mainLoop.cpp new file mode 100644 index 0000000..ea78b8d --- /dev/null +++ b/src/mainLoop.cpp @@ -0,0 +1,35 @@ +#include "FLD.hpp" + +#ifdef __EMSCRIPTEN__ + +static FLD::MainLoop _main_loop; +static FLD *_fld; +static void *_udata; + +static void +_em_loop() +{ + _main_loop(*_fld, _udata); +} + +void +FLD::run(MainLoop main_loop, void *udata) +{ + _main_loop = main_loop; + _fld = this; + _udata = udata; + emscripten_set_main_loop(_em_loop, 0, 1); +} + +#else + +void +FLD::run(MainLoop main_loop, void *udata) +{ + while (!shouldQuit) { + cycleEvents(); + if (main_loop(*this, udata)) + break; + } +} +#endif diff --git a/src/sdl_throw.cpp b/src/sdlError.cpp index 4d66e23..e77edcc 100644 --- a/src/sdl_throw.cpp +++ b/src/sdlError.cpp @@ -2,7 +2,7 @@ #include <SDL2/SDL_log.h> FLD::Error -FLD::sdl_error() const +FLD::sdlError() const { SDL_Log("SDL runtime error: %s", SDL_GetError()); return -1; |