From 69887f68697fdd4a7e844677155d80e364a2630e Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 31 Dec 2022 01:25:48 +0100 Subject: Extracted drawable sub-scene holder to a Frame class --- kurator/CMakeLists.txt | 1 + kurator/src/ScenarioEditor.cpp | 43 ++++++------------------- kurator/src/ScenarioEditor.h | 8 ++--- kurator/src/SceneFrame.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++ kurator/src/SceneFrame.h | 29 +++++++++++++++++ 5 files changed, 116 insertions(+), 38 deletions(-) create mode 100644 kurator/src/SceneFrame.cpp create mode 100644 kurator/src/SceneFrame.h diff --git a/kurator/CMakeLists.txt b/kurator/CMakeLists.txt index 989ab6f..c91d507 100644 --- a/kurator/CMakeLists.txt +++ b/kurator/CMakeLists.txt @@ -9,6 +9,7 @@ add_executable( src/main.cpp src/ScenarioEditor.cpp src/SceneBuilder.cpp + src/SceneFrame.cpp src/Session.cpp src/Skybox.cpp src/Summary.cpp diff --git a/kurator/src/ScenarioEditor.cpp b/kurator/src/ScenarioEditor.cpp index 7025b49..45e594f 100644 --- a/kurator/src/ScenarioEditor.cpp +++ b/kurator/src/ScenarioEditor.cpp @@ -19,30 +19,20 @@ namespace kurator ScenarioEditor::ScenarioEditor(std::shared_ptr _session) : session {std::move(_session)}, - buffer {LoadRenderTexture(GetScreenWidth(), GetScreenHeight())}, teams {2}, anvils {3}, warbringers {2}, eclipses {3}, - distance {12000}, - battle {generate()} + distance {12000} { -} - - -ScenarioEditor::~ScenarioEditor() -{ - UnloadRenderTexture(buffer); + regenerate(); } void ScenarioEditor::update(float) { - if (IsWindowResized()) { - UnloadRenderTexture(buffer); - buffer = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); - } + frame.update(); if (ImGui::Begin("Scenario Editor")) { bool update = false; update |= ImGui::SliderInt("Teams", &teams, 2, 6); @@ -51,7 +41,7 @@ ScenarioEditor::update(float) update |= ImGui::SliderInt("Eclipses", &eclipses, 0, 20); update |= ImGui::SliderFloat("Distance", &distance, 10000, 20000); if (update) - battle = generate(); + regenerate(); if (ImGui::Button("Start")) start_battle(); ImGui::SameLine(); @@ -68,21 +58,7 @@ void ScenarioEditor::draw() const { ClearBackground(BLACK); - if (battle) { - BeginTextureMode(buffer); - battle->draw(); - EndTextureMode(); - DrawTextureRec( - buffer.texture, - { - 0, - static_cast(GetScreenHeight()), - static_cast(GetScreenWidth()), - static_cast(-GetScreenHeight()) - }, - {0, 0}, - {255, 255, 255, 150}); - } + frame.draw(); } @@ -90,15 +66,16 @@ void ScenarioEditor::start_battle() { session->push(std::move(battle)); - battle = generate(); + regenerate(); } -std::shared_ptr -ScenarioEditor::generate() const +void +ScenarioEditor::regenerate() { auto scenario = campaign::scenarios::example(teams, anvils, warbringers, eclipses, distance); - return std::make_shared(session, std::move(scenario)); + battle = std::make_shared(session, std::move(scenario)); + frame.reset(battle); } diff --git a/kurator/src/ScenarioEditor.h b/kurator/src/ScenarioEditor.h index ec83965..cec4783 100644 --- a/kurator/src/ScenarioEditor.h +++ b/kurator/src/ScenarioEditor.h @@ -2,9 +2,8 @@ #include -#include - #include "Scene.h" +#include "SceneFrame.h" #include "Session.h" @@ -16,20 +15,19 @@ class ScenarioEditor : public Scene { public: explicit ScenarioEditor(std::shared_ptr _session); - ~ScenarioEditor(); void update(float dt) override; void draw() const override; void start_battle(); private: - std::shared_ptr generate() const; + void regenerate(); std::shared_ptr session; - RenderTexture2D buffer; int teams; int anvils; int warbringers; int eclipses; float distance; std::shared_ptr battle; + SceneFrame frame; }; diff --git a/kurator/src/SceneFrame.cpp b/kurator/src/SceneFrame.cpp new file mode 100644 index 0000000..ebc781c --- /dev/null +++ b/kurator/src/SceneFrame.cpp @@ -0,0 +1,73 @@ +#include "SceneFrame.h" + +#include +#include + +#include + +#include "Scene.h" + + +namespace kurator +{ + + +SceneFrame::SceneFrame(std::shared_ptr _scene) : + scene {std::move(_scene)}, + buffer {LoadRenderTexture(GetScreenWidth(), GetScreenHeight())} +{ +} + + +SceneFrame::~SceneFrame() +{ + UnloadRenderTexture(buffer); +} + + +void +SceneFrame::update() +{ + if (IsWindowResized()) { + UnloadRenderTexture(buffer); + buffer = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); + } +} + + +void +SceneFrame::draw() const +{ + if (!scene) + return; + BeginTextureMode(buffer); + scene->draw(); + EndTextureMode(); + DrawTextureRec( + buffer.texture, + { + 0, + static_cast(GetScreenHeight()), + static_cast(GetScreenWidth()), + static_cast(-GetScreenHeight()) + }, + {0, 0}, + {255, 255, 255, 150}); +} + + +void +SceneFrame::reset(std::shared_ptr _scene) +{ + scene = std::move(_scene); +} + + +std::shared_ptr +SceneFrame::view() const +{ + return scene; +} + + +} // namespace kurator diff --git a/kurator/src/SceneFrame.h b/kurator/src/SceneFrame.h new file mode 100644 index 0000000..9bc3feb --- /dev/null +++ b/kurator/src/SceneFrame.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include + +#include "Scene.h" + + +namespace kurator +{ + + +class SceneFrame +{ +public: + explicit SceneFrame(std::shared_ptr _scene=nullptr); + ~SceneFrame(); + void update(); + void draw() const; + void reset(std::shared_ptr _scene); + std::shared_ptr view() const; +private: + std::shared_ptr scene; + RenderTexture2D buffer; +}; + + +} // namespace kurator -- cgit v1.1