From 15ba7976d44eefb5db43db95c99c868b1f119636 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 21 May 2022 15:31:39 +0200 Subject: Moved view and window management to App class --- App.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ App.h | 17 +++++++++++++++++ CMakeLists.txt | 1 + main.cpp | 19 +++++++++---------- 4 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 App.cpp create mode 100644 App.h diff --git a/App.cpp b/App.cpp new file mode 100644 index 0000000..f897ebb --- /dev/null +++ b/App.cpp @@ -0,0 +1,41 @@ +#include "App.h" + +#include +#include + +#include + +#include "View.h" + + +App::App() : + m_view {} +{ + InitWindow(800, 600, "Derelict"); + SetWindowState(FLAG_WINDOW_RESIZABLE); + SetTargetFPS(60); +} + + +App::~App() +{ + m_view.reset(); + CloseWindow(); +} + + +void +App::assign(std::unique_ptr view) +{ + m_view = std::move(view); +} + + +void +App::loop() +{ + if (m_view) { + m_view->update(GetFrameTime()); + m_view->draw(); + } +} diff --git a/App.h b/App.h new file mode 100644 index 0000000..0ace948 --- /dev/null +++ b/App.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "View.h" + + +class App +{ +public: + App(); + ~App(); + void assign(std::unique_ptr view); + void loop(); +private: + std::unique_ptr m_view; +}; diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f8bb00..8945b6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ find_package(raylib 3 REQUIRED) find_package(nlohmann_json 3.2 REQUIRED) add_executable( ${PROJECT_NAME} + App.cpp DumpSource.cpp main.cpp Reader.cpp diff --git a/main.cpp b/main.cpp index b419c2e..a0c4285 100644 --- a/main.cpp +++ b/main.cpp @@ -1,27 +1,26 @@ +#include #include #include +#include "App.h" #include "DumpSource.h" #include "Reader.h" #include "View.h" +App app; + + int main(int, char*[]) { - InitWindow(800, 600, "Derelict"); { - SetWindowState(FLAG_WINDOW_RESIZABLE); - SetTargetFPS(60); DumpSource source("sample.json"); auto [grids, timeline] = Reader::read(source); - View view(std::move(grids), std::move(timeline)); - while (!WindowShouldClose()) - { - view.update(GetFrameTime()); - view.draw(); - } + auto view = std::make_unique(std::move(grids), std::move(timeline)); + app.assign(std::move(view)); } - CloseWindow(); + while (!WindowShouldClose()) + app.loop(); } -- cgit v1.1