gedeng/cpp/Application.cpp

43 lines
1.4 KiB
C++
Raw Normal View History

#include "Gedeng/Application.h"
2021-04-30 22:40:42 +02:00
#include "Gedeng/Input.h"
#include "Gedeng/Logger.h"
2021-04-30 22:40:42 +02:00
#include "Gedeng/RenderBackend.h"
#include "Gedeng/Time.h"
namespace Gedeng {
Application::Application(unsigned long ms_per_update, unsigned int window_size_x, unsigned int window_size_y,
String window_name)
: MS_PER_UPDATE(ms_per_update), WINDOW_SIZE_X(window_size_x), WINDOW_SIZE_Y(window_size_y),
WINDOW_NAME(window_name) {
2021-04-30 22:40:42 +02:00
// Setup Rendering
RenderBackend::initialize_window(WINDOW_SIZE_X, WINDOW_SIZE_Y, WINDOW_NAME);
2021-04-30 22:40:42 +02:00
Input::initialize(RenderBackend::get_window());
}
2021-04-30 22:40:42 +02:00
void Application::run() {
unsigned long previous_time_ms = Time::get_time_ms();
unsigned long time_until_fixed_update_ms = 0.0;
2021-04-30 22:40:42 +02:00
while (!RenderBackend::does_window_want_to_close()) {
unsigned long current_time_ms = Time::get_time_ms();
unsigned long elapsed_time_ms = current_time_ms - previous_time_ms;
previous_time_ms = current_time_ms;
time_until_fixed_update_ms += elapsed_time_ms;
2021-04-30 22:40:42 +02:00
Input::poll_input();
// Update fixed time step
while (time_until_fixed_update_ms >= MS_PER_UPDATE) {
fixed_update(static_cast<double>(MS_PER_UPDATE) / 1000.0);
time_until_fixed_update_ms -= MS_PER_UPDATE;
}
dynamic_update(static_cast<double>(elapsed_time_ms) / 1000.0);
RenderBackend::render();
}
}
} // namespace Gedeng