From f138311d2d2e0cc9ba0496d523bb46f2c1c9fb73 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Wed, 20 Sep 2023 23:58:58 -0700 Subject: Copy from the PlasticSCM repo, replace vendored glm wtih conan --- source/30-game/App.cpp | 168 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 source/30-game/App.cpp (limited to 'source/30-game/App.cpp') diff --git a/source/30-game/App.cpp b/source/30-game/App.cpp new file mode 100644 index 0000000..8328589 --- /dev/null +++ b/source/30-game/App.cpp @@ -0,0 +1,168 @@ +#include "App.hpp" + +#include "ScopeGuard.hpp" +#include "Utils.hpp" + +#include +#include +#include +#include + +using namespace std::literals; + +App::App() + : mActiveCamera{ &mMainCamera } { + auto& worldRoot = mWorld.GetRoot(); + + constexpr int kPlayerCount = 2; + for (int i = 0; i < kPlayerCount; ++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 + + mMainCamera.name = "Main Camera"s; + mMainCamera.SetEyePos(glm::vec3(0, 0, 1)); + mMainCamera.SetTargetDirection(glm::vec3(0, 0, -1)); + mMainCamera.SetHasPerspective(false); + + do { + auto file = Utils::OpenCstdioFile("assets/GameRendererBindings.json", Utils::Read); + if (!file) break; + DEFER { fclose(file); }; + + char readerBuffer[65536]; + rapidjson::FileReadStream stream(file, readerBuffer, sizeof(readerBuffer)); + + rapidjson::Document root; + root.ParseStream(stream); + + mWorldRenderer.LoadBindings(root); + } while (false); +} + +App::~App() { +} + +Camera* App::GetActiveCamera() const { + return mActiveCamera; +} + +void App::BindActiveCamera(Camera* camera) { + mActiveCamera = camera; +} + +void App::UnbindActiveCamera() { + mActiveCamera = &mMainCamera; +} + +bool App::IsGameRunning() const { + return mGameRunning; +} + +void App::SetGameRunning(bool running) { + if (mGameRunning != running) { + mGameRunning = running; + if (running) { + mWorld.Awaken(); + } else { + mWorld.Resleep(); + } + } +} + +bool App::IsEditorVisible() const { + return mEditorVisible; +} + +void App::SetEditorVisible(bool visible) { + if (mEditorVisible != visible) { + if (visible) { +#if BRUSSEL_ENABLE_EDITOR + mEditorVisible = true; + if (mEditor == nullptr) { + mEditor = IEditor::CreateInstance(this); + } +#endif + } else { + mEditorVisible = false; + } + } +} + +void App::Show() { + if (mEditorVisible) { + mEditor->Show(); + } +} + +void App::Update() { + if (IsGameRunning()) { + mWorld.Update(); + } +} + +void App::Draw(float currentTime, float deltaTime) { + mWorldRenderer.BeginFrame(*mActiveCamera, 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(); + mWorldRenderer.Draw(renderObjects.data(), obj, renderObjects.size()); + } + + mWorldRenderer.EndFrame(); +} + +void App::HandleMouse(int button, int action) { +} + +void App::HandleMouseMotion(double xOff, double yOff) { +} + +void App::HandleKey(GLFWkeyboard* keyboard, int key, int action) { + if (!mKeyCaptureCallbacks.empty()) { + auto& callback = mKeyCaptureCallbacks.front(); + bool remove = callback(key, action); + if (remove) { + mKeyCaptureCallbacks.pop_front(); + } + } + + switch (key) { + case GLFW_KEY_F3: { + if (action == GLFW_PRESS) { + SetEditorVisible(!IsEditorVisible()); + } + return; + } + } + + for (auto& player : mPlayers) { + for (auto playerKeyboard : player->boundKeyboards) { + if (playerKeyboard == keyboard) { + player->HandleKeyInput(key, action); + } + } + } +} + +void App::PushKeyCaptureCallback(KeyCaptureCallback callback) { + mKeyCaptureCallbacks.push_back(std::move(callback)); +} -- cgit v1.2.3-70-g09d2