mirror of
https://github.com/NoelFB/blah.git
synced 2025-02-18 12:48:27 +08:00
a few notes and cleanup from previous merge
This commit is contained in:
parent
66e8b34d37
commit
3fa9f99791
138
src/core/app.cpp
138
src/core/app.cpp
@ -14,15 +14,6 @@
|
|||||||
|
|
||||||
using namespace Blah;
|
using namespace Blah;
|
||||||
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
static Config app_config;
|
|
||||||
static bool app_is_running = false;
|
|
||||||
static bool app_is_exiting = false;
|
|
||||||
static uint64_t time_last;
|
|
||||||
static uint64_t time_accumulator = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Config::Config()
|
Config::Config()
|
||||||
{
|
{
|
||||||
name = nullptr;
|
name = nullptr;
|
||||||
@ -41,78 +32,86 @@ Config::Config()
|
|||||||
on_error = nullptr;
|
on_error = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace
|
||||||
|
{
|
||||||
|
Config app_config;
|
||||||
|
bool app_is_running = false;
|
||||||
|
bool app_is_exiting = false;
|
||||||
|
uint64_t time_last;
|
||||||
|
uint64_t time_accumulator = 0;
|
||||||
|
|
||||||
void loop_iteration() {
|
void app_iterate()
|
||||||
// poll system events
|
|
||||||
PlatformBackend::frame();
|
|
||||||
|
|
||||||
// update at a fixed timerate
|
|
||||||
// TODO: allow a non-fixed step update?
|
|
||||||
{
|
{
|
||||||
uint64_t time_target = (uint64_t)((1.0f / app_config.target_framerate) * 1000);
|
// poll system events
|
||||||
uint64_t time_curr = PlatformBackend::time();
|
PlatformBackend::frame();
|
||||||
uint64_t time_diff = time_curr - time_last;
|
|
||||||
time_last = time_curr;
|
|
||||||
time_accumulator += time_diff;
|
|
||||||
|
|
||||||
// do not let us run too fast
|
// update at a fixed timerate
|
||||||
while (time_accumulator < time_target)
|
// TODO: allow a non-fixed step update?
|
||||||
{
|
{
|
||||||
PlatformBackend::sleep((int)(time_target - time_accumulator));
|
uint64_t time_target = (uint64_t)((1.0f / app_config.target_framerate) * 1000);
|
||||||
|
uint64_t time_curr = PlatformBackend::time();
|
||||||
time_curr = PlatformBackend::time();
|
uint64_t time_diff = time_curr - time_last;
|
||||||
time_diff = time_curr - time_last;
|
|
||||||
time_last = time_curr;
|
time_last = time_curr;
|
||||||
time_accumulator += time_diff;
|
time_accumulator += time_diff;
|
||||||
}
|
|
||||||
|
|
||||||
// Do not allow us to fall behind too many updates
|
// do not let us run too fast
|
||||||
// (otherwise we'll get spiral of death)
|
while (time_accumulator < time_target)
|
||||||
uint64_t time_maximum = app_config.max_updates * time_target;
|
|
||||||
if (time_accumulator > time_maximum)
|
|
||||||
time_accumulator = time_maximum;
|
|
||||||
|
|
||||||
// do as many updates as we can
|
|
||||||
while (time_accumulator >= time_target)
|
|
||||||
{
|
|
||||||
time_accumulator -= time_target;
|
|
||||||
|
|
||||||
Time::delta = (1.0f / app_config.target_framerate);
|
|
||||||
|
|
||||||
if (Time::pause_timer > 0)
|
|
||||||
{
|
{
|
||||||
Time::pause_timer -= Time::delta;
|
PlatformBackend::sleep((int)(time_target - time_accumulator));
|
||||||
if (Time::pause_timer <= -0.0001f)
|
|
||||||
Time::delta = -Time::pause_timer;
|
time_curr = PlatformBackend::time();
|
||||||
else
|
time_diff = time_curr - time_last;
|
||||||
continue;
|
time_last = time_curr;
|
||||||
|
time_accumulator += time_diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
Time::milliseconds += time_target;
|
// Do not allow us to fall behind too many updates
|
||||||
Time::previous_elapsed = Time::elapsed;
|
// (otherwise we'll get spiral of death)
|
||||||
Time::elapsed += Time::delta;
|
uint64_t time_maximum = app_config.max_updates * time_target;
|
||||||
|
if (time_accumulator > time_maximum)
|
||||||
|
time_accumulator = time_maximum;
|
||||||
|
|
||||||
InputBackend::frame();
|
// do as many updates as we can
|
||||||
GraphicsBackend::frame();
|
while (time_accumulator >= time_target)
|
||||||
|
{
|
||||||
|
time_accumulator -= time_target;
|
||||||
|
|
||||||
if (app_config.on_update != nullptr)
|
Time::delta = (1.0f / app_config.target_framerate);
|
||||||
app_config.on_update();
|
|
||||||
|
if (Time::pause_timer > 0)
|
||||||
|
{
|
||||||
|
Time::pause_timer -= Time::delta;
|
||||||
|
if (Time::pause_timer <= -0.0001f)
|
||||||
|
Time::delta = -Time::pause_timer;
|
||||||
|
else
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Time::milliseconds += time_target;
|
||||||
|
Time::previous_elapsed = Time::elapsed;
|
||||||
|
Time::elapsed += Time::delta;
|
||||||
|
|
||||||
|
InputBackend::frame();
|
||||||
|
GraphicsBackend::frame();
|
||||||
|
|
||||||
|
if (app_config.on_update != nullptr)
|
||||||
|
app_config.on_update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// render
|
||||||
|
{
|
||||||
|
GraphicsBackend::before_render();
|
||||||
|
|
||||||
|
if (app_config.on_render != nullptr)
|
||||||
|
app_config.on_render();
|
||||||
|
|
||||||
|
GraphicsBackend::after_render();
|
||||||
|
PlatformBackend::present();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// render
|
|
||||||
{
|
|
||||||
GraphicsBackend::before_render();
|
|
||||||
|
|
||||||
if (app_config.on_render != nullptr)
|
|
||||||
app_config.on_render();
|
|
||||||
|
|
||||||
GraphicsBackend::after_render();
|
|
||||||
PlatformBackend::present();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} // namespace
|
|
||||||
|
|
||||||
bool App::run(const Config* c)
|
bool App::run(const Config* c)
|
||||||
{
|
{
|
||||||
@ -154,13 +153,14 @@ bool App::run(const Config* c)
|
|||||||
// display window
|
// display window
|
||||||
PlatformBackend::ready();
|
PlatformBackend::ready();
|
||||||
|
|
||||||
|
// Begin main loop
|
||||||
|
// Emscripten requires the main loop be separated into its own call
|
||||||
|
|
||||||
#ifdef __EMSCRIPTEN__
|
#ifdef __EMSCRIPTEN__
|
||||||
emscripten_set_main_loop(loop_iteration, 0, 1);
|
emscripten_set_main_loop(app_iterate, 0, 1);
|
||||||
#else
|
#else
|
||||||
while (!app_is_exiting)
|
while (!app_is_exiting)
|
||||||
{
|
app_iterate();
|
||||||
loop_iteration();
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// shutdown
|
// shutdown
|
||||||
|
@ -147,6 +147,8 @@ void PlatformBackend::ready()
|
|||||||
{
|
{
|
||||||
#ifndef __EMSCRIPTEN__
|
#ifndef __EMSCRIPTEN__
|
||||||
// enable V-Sync
|
// enable V-Sync
|
||||||
|
// TODO:
|
||||||
|
// This should be a toggle or controllable in some way
|
||||||
if (App::renderer() == Renderer::OpenGL)
|
if (App::renderer() == Renderer::OpenGL)
|
||||||
SDL_GL_SetSwapInterval(1);
|
SDL_GL_SetSwapInterval(1);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user