cleaned up app, renamed internal input update methods

This commit is contained in:
Noel Berry 2022-02-12 00:17:21 -08:00
parent 4fcd29b82f
commit b1e33f2627
3 changed files with 43 additions and 47 deletions

View File

@ -23,17 +23,23 @@ Renderer* App::Internal::renderer = nullptr;
namespace namespace
{ {
// A dummy Frame Buffer that represents the Back Buffer // Global App State
// it doesn't actually contain any textures or details. Config app_config;
bool app_is_running = false;
bool app_is_exiting = false;
u64 app_time_last;
u64 app_time_accumulator = 0;
TargetRef app_backbuffer;
// A dummy Target that represents the Back Buffer.
// It doesn't contain any data, rather it forwards calls along to the actual BackBuffer.
class BackBuffer final : public Target class BackBuffer final : public Target
{ {
public: public:
Attachments empty_textures; Attachments empty_textures;
Attachments& textures() override { BLAH_ASSERT(false, "Backbuffer doesn't have any textures"); return empty_textures; } Attachments& textures() override { BLAH_ASSERT(false, "Backbuffer doesn't have any textures"); return empty_textures; }
const Attachments& textures() const override { BLAH_ASSERT(false, "Backbuffer doesn't have any textures"); return empty_textures; } const Attachments& textures() const override { BLAH_ASSERT(false, "Backbuffer doesn't have any textures"); return empty_textures; }
int width() const override { int width() const override { int w, h; App::Internal::platform->get_draw_size(&w, &h); return w; }
int w, h; App::Internal::platform->get_draw_size(&w, &h); return w;
}
int height() const override { int w, h; App::Internal::platform->get_draw_size(&w, &h); return h; } int height() const override { int w, h; App::Internal::platform->get_draw_size(&w, &h); return h; }
void clear(Color color, float depth, u8 stencil, ClearMask mask) override void clear(Color color, float depth, u8 stencil, ClearMask mask) override
{ {
@ -42,14 +48,6 @@ namespace
App::Internal::renderer->clear_backbuffer(color, depth, stencil, mask); App::Internal::renderer->clear_backbuffer(color, depth, stencil, mask);
} }
}; };
// Global App State
Config app_config;
bool app_is_running = false;
bool app_is_exiting = false;
u64 app_time_last;
u64 app_time_accumulator = 0;
TargetRef app_backbuffer;
} }
bool App::run(const Config* c) bool App::run(const Config* c)
@ -86,48 +84,49 @@ bool App::run(const Config* c)
app_backbuffer = TargetRef(new BackBuffer()); app_backbuffer = TargetRef(new BackBuffer());
// initialize the system // initialize the system
Internal::platform = Platform::try_make_platform(app_config);
if (!Internal::platform)
{ {
Log::error("Failed to create Platform module"); Internal::platform = Platform::try_make_platform(app_config);
App::Internal::shutdown(); if (!Internal::platform)
return false; {
} Log::error("Failed to create Platform module");
App::Internal::shutdown();
return false;
}
if (!Internal::platform->init(app_config)) if (!Internal::platform->init(app_config))
{ {
Log::error("Failed to initialize Platform module"); Log::error("Failed to initialize Platform module");
App::Internal::shutdown(); App::Internal::shutdown();
return false; return false;
}
} }
// initialize graphics // initialize graphics
Internal::renderer = Renderer::try_make_renderer(app_config.renderer_type);
if (Internal::renderer == nullptr)
{ {
Log::error("Renderer module was not found"); Internal::renderer = Renderer::try_make_renderer(app_config.renderer_type);
App::Internal::shutdown(); if (Internal::renderer == nullptr)
return false; {
Log::error("Renderer module was not found");
App::Internal::shutdown();
return false;
}
if (!Internal::renderer->init())
{
Log::error("Failed to initialize Renderer module");
App::Internal::shutdown();
return false;
}
} }
if (!Internal::renderer->init()) // input + poll the platform once
{
Log::error("Failed to initialize Renderer module");
App::Internal::shutdown();
return false;
}
// input
Input::Internal::init(); Input::Internal::init();
Input::Internal::step_state();
// prepare by updating input & platform once
Input::Internal::update_state();
Internal::platform->update(Input::state); Internal::platform->update(Input::state);
// startup // startup
if (app_config.on_startup != nullptr) if (app_config.on_startup != nullptr)
app_config.on_startup(); app_config.on_startup();
app_time_last = Internal::platform->ticks(); app_time_last = Internal::platform->ticks();
app_time_accumulator = 0; app_time_accumulator = 0;
@ -135,8 +134,6 @@ bool App::run(const Config* c)
Internal::platform->ready(); Internal::platform->ready();
// Begin main loop // Begin main loop
// Emscripten requires the main loop be separated into its own call
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
emscripten_set_main_loop(App::Internal::iterate, 0, 1); emscripten_set_main_loop(App::Internal::iterate, 0, 1);
#else #else
@ -147,7 +144,6 @@ bool App::run(const Config* c)
// shutdown // shutdown
if (app_config.on_shutdown != nullptr) if (app_config.on_shutdown != nullptr)
app_config.on_shutdown(); app_config.on_shutdown();
App::Internal::shutdown(); App::Internal::shutdown();
return true; return true;
} }
@ -202,7 +198,7 @@ void App::Internal::iterate()
Time::previous_seconds = Time::seconds; Time::previous_seconds = Time::seconds;
Time::seconds += Time::delta; Time::seconds += Time::delta;
Input::Internal::update_state(); Input::Internal::step_state();
platform->update(Input::state); platform->update(Input::state);
Input::Internal::update_bindings(); Input::Internal::update_bindings();
renderer->update(); renderer->update();

View File

@ -44,7 +44,7 @@ void Input::Internal::shutdown()
init(); init();
} }
void Input::Internal::update_state() void Input::Internal::step_state()
{ {
// cycle states // cycle states
Input::last_state = Input::state; Input::last_state = Input::state;

View File

@ -27,7 +27,7 @@ namespace Blah
void init(); void init();
// Steps the input state // Steps the input state
void update_state(); void step_state();
// Updates bindings // Updates bindings
void update_bindings(); void update_bindings();