diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-06-15 17:35:13 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-06-15 17:35:13 +0100 |
commit | 321103ea84cf61f58a26d59700284d84f11833f5 (patch) | |
tree | db90e2d4694702d518dae28c5c7eb48e7ca46283 | |
parent | First cut reshuffling app/window/gl/render bits (diff) | |
download | ilt-321103ea84cf61f58a26d59700284d84f11833f5.tar.bz2 ilt-321103ea84cf61f58a26d59700284d84f11833f5.tar.xz ilt-321103ea84cf61f58a26d59700284d84f11833f5.zip |
Move main application loop into the library
-rw-r--r-- | application/main.cpp | 37 | ||||
-rw-r--r-- | ui/mainApplication.cpp | 34 | ||||
-rw-r--r-- | ui/mainApplication.h | 18 |
3 files changed, 56 insertions, 33 deletions
diff --git a/application/main.cpp b/application/main.cpp index 04c88f3..d58cf6d 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -1,7 +1,7 @@ +#include "ui/mainApplication.h" #include "ui/mainWindow.h" #include <array> #include <assetFactory/assetFactory.h> -#include <chrono> #include <collection.h> #include <game/activities/go.h> #include <game/activities/idle.h> @@ -32,16 +32,13 @@ static const int DISPLAY_WIDTH = 1280; static const int DISPLAY_HEIGHT = 1024; -class MainApplication : public GameState, public ApplicationBase { +class DummyMainApplication : public GameState, public MainApplication { public: - using Windows = Collection<Window>; - int run() { geoData = std::make_shared<GeoData>(GeoData::loadFromAsciiGrid("test/fixtures/height/SD19.asc")); - Windows windows; windows.create<MainWindow>(DISPLAY_WIDTH, DISPLAY_HEIGHT)->setContent<GameMainWindow>(); world.create<Terrain>(geoData); @@ -90,41 +87,15 @@ public: } } - auto t_start = std::chrono::high_resolution_clock::now(); - while (isRunning) { - processInputs(windows); - const auto t_end = std::chrono::high_resolution_clock::now(); - const auto t_passed = std::chrono::duration_cast<TickDuration>(t_end - t_start); - - world.apply(&WorldObject::tick, t_passed); - windows.apply(&Window::tick, t_passed); - windows.apply(&Window::refresh); - - t_start = t_end; - } + mainLoop(); world.objects.clear(); return 0; } - -private: - void - processInputs(const Windows & windows) - { - for (SDL_Event e; SDL_PollEvent(&e);) { - if (e.type == SDL_QUIT) { - isRunning = false; - return; - } - windows.applyOne(&Window::handleInput, e); - } - } - - bool isRunning {true}; }; int main(int, char **) { - return std::make_shared<MainApplication>()->run(); + return std::make_shared<DummyMainApplication>()->run(); } diff --git a/ui/mainApplication.cpp b/ui/mainApplication.cpp new file mode 100644 index 0000000..08f49a4 --- /dev/null +++ b/ui/mainApplication.cpp @@ -0,0 +1,34 @@ +#include "mainApplication.h" +#include "game/gamestate.h" +#include "game/worldobject.h" + +void +MainApplication::mainLoop() +{ + auto t_start = std::chrono::high_resolution_clock::now(); + while (isRunning) { + processInputs(); + const auto t_end = std::chrono::high_resolution_clock::now(); + const auto t_passed = std::chrono::duration_cast<TickDuration>(t_end - t_start); + + if (gameState) { + gameState->world.apply(&WorldObject::tick, t_passed); + } + windows.apply(&Window::tick, t_passed); + windows.apply(&Window::refresh); + + t_start = t_end; + } +} + +void +MainApplication::processInputs() +{ + for (SDL_Event e; SDL_PollEvent(&e);) { + if (e.type == SDL_QUIT) { + isRunning = false; + return; + } + windows.applyOne(&Window::handleInput, e); + } +} diff --git a/ui/mainApplication.h b/ui/mainApplication.h new file mode 100644 index 0000000..a6cb126 --- /dev/null +++ b/ui/mainApplication.h @@ -0,0 +1,18 @@ +#pragma once + +#include "applicationBase.h" +#include "collection.h" +#include "window.h" + +class MainApplication : public ApplicationBase { +public: + using Windows = Collection<Window>; + void mainLoop(); + +protected: + Windows windows; + +private: + void processInputs(); + bool isRunning {true}; +}; |