From b47aa4426ccad623a46d33442e62b3aba9ac54a5 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Mon, 30 Jan 2017 22:39:55 +0100 Subject: [PATCH] Added timer struct, added support for pausing --- src/Globals.h | 3 +++ src/Timer.h | 8 ++++++++ src/main.cc | 38 +++++++++++++++++++++++++++---------- src/modules/RenderModule.cc | 2 -- src/modules/TestModule.cc | 10 ++++++++-- 5 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 src/Timer.h diff --git a/src/Globals.h b/src/Globals.h index f19a33a..179faf2 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -2,6 +2,9 @@ #include "math_types.h" +struct Timer; +extern Timer* gTimer; + struct Renderer; extern Renderer* gRenderer; diff --git a/src/Timer.h b/src/Timer.h new file mode 100644 index 0000000..caafa3e --- /dev/null +++ b/src/Timer.h @@ -0,0 +1,8 @@ +#pragma once + +struct Timer { + float mCurrentTime = 0.0f; + float mFrameTime = 0.0f; + float mDeltaTime = 0.0f; + bool mPaused = false; +}; diff --git a/src/main.cc b/src/main.cc index b3d89fd..5839020 100644 --- a/src/main.cc +++ b/src/main.cc @@ -12,13 +12,14 @@ #include "bgfx/bgfxplatform.h" #include "bx/timer.h" +#include "Timer.h" #include "RuntimeModuleManager.h" #include "imgui/imgui.h" #include "Globals.h" #include "Serializer.h" - +Timer* gTimer = nullptr; Renderer* gRenderer = nullptr; GLFWwindow* gWindow = nullptr; RuntimeModuleManager* gModuleManager = nullptr; @@ -99,6 +100,12 @@ int main(void) // imgui initialization. imguiCreate(); + // Timer + Timer timer; + gTimer = &timer; + timer.mCurrentTime = 0.0f; + timer.mDeltaTime = 0.0f; + printf("Initializing ModuleManager...\n"); RuntimeModuleManager module_manager; module_manager.RegisterModule("src/modules/libRenderModule.so"); @@ -117,14 +124,8 @@ int main(void) int64_t time_offset = bx::getHPCounter(); while(!glfwWindowShouldClose(gWindow)) { - int64_t now = bx::getHPCounter(); - static int64_t last = now; - const int64_t frameTime = now - last; - last = now; - const double freq = double(bx::getHPFrequency() ); - const double toMs = 1000.0/freq; - - float time = (float)( (now-time_offset)/double(bx::getHPFrequency() ) ); + static int64_t last = bx::getHPCounter(); + int64_t pre_module_check = bx::getHPCounter(); if (module_manager.CheckModulesChanged()) { std::cout << "Detected module update. Unloading all modules." << std::endl; @@ -136,8 +137,25 @@ int main(void) // to reloading of the modules. last = bx::getHPCounter(); } + + // update time that was passed without module reloading + int64_t now = bx::getHPCounter(); + int64_t module_update = now - pre_module_check; - module_manager.Update((float)(frameTime / freq)); + const int64_t frameTime = (now - last) - module_update; + last = now; + const double freq = double(bx::getHPFrequency() ); + const double toMs = 1000.0/freq; + + gTimer->mFrameTime = (float)(frameTime / freq); + if (!gTimer->mPaused) { + gTimer->mDeltaTime = gTimer->mFrameTime; + gTimer->mCurrentTime = gTimer->mCurrentTime + gTimer->mDeltaTime; + } else { + gTimer->mDeltaTime = 0.0f; + } + + module_manager.Update(gTimer->mDeltaTime); glfwPollEvents(); diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 72d7080..c0c5474 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -1172,8 +1172,6 @@ void Renderer::initialize(int width, int height) { reset = BGFX_RESET_VSYNC | BGFX_RESET_MAXANISOTROPY | BGFX_RESET_MSAA_X16; bgfx::reset(width, height, reset); - std::cout << "bla55aa" << std::endl; - bgfx::setViewClear(0 , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH , 0x303030ff diff --git a/src/modules/TestModule.cc b/src/modules/TestModule.cc index fa43b55..9298553 100644 --- a/src/modules/TestModule.cc +++ b/src/modules/TestModule.cc @@ -10,6 +10,7 @@ #include "RuntimeModuleManager.h" #include "Serializer.h" +#include "Timer.h" #include "modules/RenderModule.h" #include "modules/CharacterModule.h" @@ -144,8 +145,8 @@ void handle_keyboard (struct module_state *state, float dt) { direction += Vector3f (0.f, -1.f, 0.f); } - eye += direction * 5.f * dt; - poi += direction * 5.f * dt; + eye += direction * 5.f * gTimer->mFrameTime; + poi += direction * 5.f * gTimer->mFrameTime; active_camera->eye = eye; active_camera->poi = poi; @@ -178,6 +179,11 @@ void handle_keyboard (struct module_state *state, float dt) { controller.state[CharacterController::ControlStateJump] = true; } } + + // handle pause + if (glfwGetKey(gWindow, GLFW_KEY_P) == GLFW_PRESS) { + gTimer->mPaused = !gTimer->mPaused; + } } void update_character(module_state* state, float dt) {