added fps; added time measurement of run loop; extra sleep commented out because of buggy update behaviour when used
This commit is contained in:
parent
97c837f9e9
commit
6202c7dba7
@ -17,6 +17,10 @@ Application Application::app;
|
|||||||
void Application::run() {
|
void Application::run() {
|
||||||
bool quit {false};
|
bool quit {false};
|
||||||
while (!quit) {
|
while (!quit) {
|
||||||
|
// measure start time for this tick
|
||||||
|
auto start = SDL_GetTicks();
|
||||||
|
|
||||||
|
// handle events
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
if (SDL_PollEvent(&event)) {
|
if (SDL_PollEvent(&event)) {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
@ -30,12 +34,29 @@ void Application::run() {
|
|||||||
quit = handle_event(event);
|
quit = handle_event(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update windows
|
||||||
for (const Window& window : Window::windows()) {
|
for (const Window& window : Window::windows()) {
|
||||||
window.update();
|
window.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// run any custom actions
|
||||||
for (RunLoopAction action : _actions) {
|
for (RunLoopAction action : _actions) {
|
||||||
action();
|
action();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// measure end time for this tick
|
||||||
|
auto end = SDL_GetTicks();
|
||||||
|
|
||||||
|
// calculate how long this tick took, and how much is left
|
||||||
|
// if we want to maintain a steady fps
|
||||||
|
auto elapsed = end - start;
|
||||||
|
auto available = 1000 / _fps - elapsed;
|
||||||
|
|
||||||
|
// if there's any time left, sleep that much
|
||||||
|
if (available > 0) {
|
||||||
|
// SDL_Delay(available);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,11 +36,15 @@ namespace sc {
|
|||||||
void add_run_loop_action(RunLoopAction action);
|
void add_run_loop_action(RunLoopAction action);
|
||||||
void remove_run_loop_action(RunLoopAction action);
|
void remove_run_loop_action(RunLoopAction action);
|
||||||
|
|
||||||
|
int fps() const { return _fps; }
|
||||||
|
void fps(int fps) { _fps = fps; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Application app;
|
static Application app;
|
||||||
|
|
||||||
std::map<SDL_EventType, std::vector<EventHandler>> _event_handlers;
|
std::map<SDL_EventType, std::vector<EventHandler>> _event_handlers;
|
||||||
std::vector<RunLoopAction> _actions;
|
std::vector<RunLoopAction> _actions;
|
||||||
|
int _fps {60};
|
||||||
|
|
||||||
bool handle_event(const SDL_Event& event);
|
bool handle_event(const SDL_Event& event);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user