Added timer struct, added support for pausing

master
Martin Felis 2017-01-30 22:39:55 +01:00
parent 7ce0b3c000
commit b47aa4426c
5 changed files with 47 additions and 14 deletions

View File

@ -2,6 +2,9 @@
#include "math_types.h"
struct Timer;
extern Timer* gTimer;
struct Renderer;
extern Renderer* gRenderer;

8
src/Timer.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
struct Timer {
float mCurrentTime = 0.0f;
float mFrameTime = 0.0f;
float mDeltaTime = 0.0f;
bool mPaused = false;
};

View File

@ -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;
@ -137,7 +138,24 @@ int main(void)
last = bx::getHPCounter();
}
module_manager.Update((float)(frameTime / freq));
// update time that was passed without module reloading
int64_t now = bx::getHPCounter();
int64_t module_update = now - pre_module_check;
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();

View File

@ -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

View File

@ -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) {