gedeng/cpp/Application.cpp
karl 482116018f Add Parallax Demo + some bugfixes
The initialization order in the Application was wrong, so some code has been moved from run to the constructor.
2021-05-03 22:23:15 +02:00

43 lines
1.4 KiB
C++

#include "Gedeng/Application.h"
#include "Gedeng/Input.h"
#include "Gedeng/Logger.h"
#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) {
// Setup Rendering
RenderBackend::initialize_window(WINDOW_SIZE_X, WINDOW_SIZE_Y, WINDOW_NAME);
Input::initialize(RenderBackend::get_window());
}
void Application::run() {
unsigned long previous_time_ms = Time::get_time_ms();
unsigned long time_until_fixed_update_ms = 0.0;
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;
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