#include "application.h" #include "renderer/renderer.h" Application* Application::_instance = nullptr; #include void Application::event(const SDL_Event ¤t_event) { switch (current_event.type) { case SDL_QUIT: running = false; break; } scene->event(current_event); } void Application::update(float delta) { scene->update(delta); } void Application::render() { scene->render(); } void Application::main_loop() { std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); //handle input SDL_Event current_event; while (SDL_PollEvent(¤t_event)) { event(current_event); } update(frame_delta); render(); std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed_seconds = end - start; double t = elapsed_seconds.count(); double tfps = 1.0 / static_cast(target_fps); double remaining = tfps - t; if (remaining > 0) { Uint32 fms = static_cast(remaining * 1000.0); frame_delta = tfps; SDL_Delay(fms); } else { frame_delta = t; } } Application::Application() { running = true; target_fps = 60; scene = nullptr; _instance = this; } Application::~Application() { _instance = nullptr; } Application* Application::get_singleton() { return _instance; }