feat(engine): Implement delta time calculation.
This commit is contained in:
parent
8dcd42c97b
commit
6a58b4a8ba
25
src/main.cpp
25
src/main.cpp
@ -1,6 +1,9 @@
|
|||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <SDL3/SDL_error.h>
|
#include <SDL3/SDL_error.h>
|
||||||
|
#include <SDL3/SDL_stdinc.h>
|
||||||
|
#include <SDL3/SDL_timer.h>
|
||||||
|
#include <SDL3/SDL_video.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
const int SCREEN_WIDTH = 800;
|
const int SCREEN_WIDTH = 800;
|
||||||
@ -59,7 +62,21 @@ int main(int argc, char* argv[]) {
|
|||||||
bool is_running = true;
|
bool is_running = true;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
|
|
||||||
|
/* Delta time calculations. */
|
||||||
|
Uint64 perf_frequency = SDL_GetPerformanceFrequency();
|
||||||
|
Uint64 last_counter = SDL_GetPerformanceCounter();
|
||||||
|
double delta_time = 0;
|
||||||
|
|
||||||
|
/* FPS counter for window title. */
|
||||||
|
char window_title[256];
|
||||||
|
double time_since_title_update = 0.0;
|
||||||
|
|
||||||
while(is_running) {
|
while(is_running) {
|
||||||
|
/* Calc delta time. */
|
||||||
|
Uint64 current_counter = SDL_GetPerformanceCounter();
|
||||||
|
delta_time = (double)(current_counter-last_counter) / (double)perf_frequency;
|
||||||
|
last_counter = current_counter;
|
||||||
|
|
||||||
/* Event handling. */
|
/* Event handling. */
|
||||||
while(SDL_PollEvent(&event)) {
|
while(SDL_PollEvent(&event)) {
|
||||||
if(event.type == SDL_EVENT_QUIT) {
|
if(event.type == SDL_EVENT_QUIT) {
|
||||||
@ -67,6 +84,14 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Update window title with FPS counter. */
|
||||||
|
time_since_title_update += delta_time;
|
||||||
|
if(time_since_title_update > 0.25) {
|
||||||
|
snprintf(window_title, 256, "Bettola - FPS: %.2f", 1.0/delta_time);
|
||||||
|
SDL_SetWindowTitle(window, window_title);
|
||||||
|
time_since_title_update = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Render that b*tch. */
|
/* Render that b*tch. */
|
||||||
glClearColor(0.1f, 0.1f, 0.3f, 1.0f); /* Dark blue or something. */
|
glClearColor(0.1f, 0.1f, 0.3f, 1.0f); /* Dark blue or something. */
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user