From 855da86feae1a5cc14dc2d486ccf115f484dbc2e Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 25 Apr 2022 20:22:07 -0700 Subject: Changeset: 16 Initial work on rendering sprites to screen --- source/App.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 25 deletions(-) (limited to 'source/App.cpp') diff --git a/source/App.cpp b/source/App.cpp index ac5b319..dc38342 100644 --- a/source/App.cpp +++ b/source/App.cpp @@ -2,44 +2,102 @@ #include -void App::Init() { - if (mInitialized) return; - mInitialized = true; - - mCurrentWorld = std::make_unique(); - auto& worldRoot = mCurrentWorld->GetRoot(); +App::App() { + auto& worldRoot = mWorld.GetRoot(); constexpr int kPlayerCount = 2; for (int i = 0; i < kPlayerCount; ++i) { - auto player = new Player(mCurrentWorld.get(), i); + auto player = new Player(&mWorld, i); worldRoot.AddChild(player); mPlayers.push_back(player); + }; + +#if defined(BRUSSEL_DEV_ENV) + SetGameRunning(false); + SetEditorVisible(true); +#else + SetGameRunning(true); +#endif + + // TODO this renders nothing, fix + mGameCamera.Move(glm::vec3(0, 0, 1)); + mGameCamera.LookAt(glm::vec3(0, 0, 0)); + mGameCamera.SetHasPerspective(false); + mEditorCamera.Move(glm::vec3(0, 0, 1)); + mEditorCamera.LookAt(glm::vec3(0, 0, 0)); + mGameCamera.SetHasPerspective(false); +} + +App::~App() { +} + +bool App::IsGameRunning() const { + return mGameRunning; +} + +void App::SetGameRunning(bool running) { + if (mGameRunning != running) { + mGameRunning = running; + if (running) { + mWorld.Awaken(); + } else { + mWorld.Resleep(); + } } +} - mCurrentWorld->Awaken(); - mEditor = std::make_unique(this, mCurrentWorld.get()); +bool App::IsEditorVisible() const { + return mEditorVisible; } -void App::Shutdown() { - if (!mInitialized) return; - mInitialized = false; - mEditor = nullptr; - mCurrentWorld->Resleep(); - mCurrentWorld = nullptr; - mPlayers.clear(); +void App::SetEditorVisible(bool visible) { + if (mEditorVisible != visible) { + mEditorVisible = visible; + if (visible) { + if (mEditor == nullptr) { + mEditor = std::make_unique(this, &mWorld); + } + } + } } void App::Show() { - if (mEditorShown) { + if (mEditorVisible) { mEditor->Show(); } } void App::Update() { + if (IsGameRunning()) { + mWorld.Update(); + } } -void App::Draw() { - mCurrentWorld->Draw(); +void App::Draw(float currentTime, float deltaTime) { + Camera* camera; + if (IsGameRunning()) { + camera = &mGameCamera; + } else { + camera = &mEditorCamera; + } + mRenderer.BeginFrame(*camera, currentTime, deltaTime); + + PodVector stack; + stack.push_back(&mWorld.GetRoot()); + + while (!stack.empty()) { + auto obj = stack.back(); + stack.pop_back(); + + for (auto child : obj->GetChildren()) { + stack.push_back(child); + } + + auto renderObjects = obj->GetRenderObjects(); + mRenderer.Draw(renderObjects.data(), renderObjects.size()); + } + + mRenderer.EndFrame(); } void App::HandleMouse(int button, int action) { @@ -60,15 +118,10 @@ void App::HandleKey(GLFWkeyboard* keyboard, int key, int action) { switch (key) { case GLFW_KEY_F3: { if (action == GLFW_PRESS) { - mEditorShown = !mEditorShown; + SetEditorVisible(!IsEditorVisible()); } return; } - - case GLFW_KEY_F11: { - // TODO fullscreen - return; - } } for (auto& player : mPlayers) { -- cgit v1.2.3-70-g09d2