frame rate limiting

main
Henri Vasserman 2024-12-30 13:26:44 +02:00
parent d18f512213
commit 052d1d9c84
2 changed files with 12 additions and 2 deletions

2
font.c
View File

@ -17,7 +17,7 @@ void draw_text(SDL_Renderer *renderer, SDL_Texture *font,
SDL_FRect src = { .w = font_w, .h = font_h },
dst = { .x = x, .y = y, .w = font_w, .h = font_h };
for (size_t i = 0; i < len; i++) {
char c = text[i];
unsigned char c = text[i];
if (c == 0) break;
if (c == '\n') {
dst.x = x;

View File

@ -33,8 +33,14 @@ int main() {
Field_init(&f, rend, w, h);
struct float2 mouse_prev = { -1, -1 };
Uint64 last = SDL_GetTicks();
int running = 1;
while (running) {
Uint64 ticks = SDL_GetTicks();
Uint64 delta = ticks - last;
last = ticks;
const bool *keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_ESCAPE] || keys[SDL_SCANCODE_Q]) {
break;
@ -76,7 +82,7 @@ int main() {
draw_text(rend, font, ACCEL_PLATFORM_STR, sizeof(ACCEL_PLATFORM_STR), RC(1,1), 255, 255, 255);
size_t len = snprintf(buf, sizeof(buf),
"off.x: %d, off.y: %d", f.offset.x, f.offset.y);
"off.x: %d, off.y: %d, \xEBT: %" SDL_PRIu64, f.offset.x, f.offset.y, delta);
draw_text(rend, font, buf, len, RC(2, 1), 255, 255, 255);
char *q[] = {"TL", "TR", "BL", "BR"};
@ -89,6 +95,10 @@ int main() {
SDL_RenderPresent(rend);
SDL_PumpEvents();
if (delta < 16) {
SDL_Delay(16 - delta);
}
}
SDL_DestroyRenderer(rend);