diff options
| author | iamcheeseman <[email protected]> | 2026-05-11 00:26:05 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-05-11 00:26:05 -0400 |
| commit | 18a5d0af4664a1e50d1e69a5ecbcd91a34246ed4 (patch) | |
| tree | b9d9d67f8b9e03f5b9ae992214ab15e3c4da87cc /dc | |
| parent | d0a1b1c9eb2b2b55ce7c2dd5d39f1e7f645f2513 (diff) | |
improve main game loop
Diffstat (limited to 'dc')
| -rw-r--r-- | dc/dc.c | 108 |
1 files changed, 58 insertions, 50 deletions
@@ -12,6 +12,59 @@ #define TILE_SIZE 16 +struct ty_image img; + +void tick(void) +{ + double start = ty_get_time(); + + ty_draw_clear(TY_COLOR_BLACK); + + for (int y = 0; y < SCREEN_HEIGHT; y += 16) { + for (int x = 0; x < SCREEN_WIDTH; x += 16) { + ty_draw_image(img, ty_vec2i(x, y)); + } + } + + ty_draw_rect( + (struct ty_recti){ + SCREEN_WIDTH*0.5 + sin(ty_get_time() * 10) * 50 - 25, + SCREEN_HEIGHT*0.5-25, + 50, 50, + }, + TY_COLOR_RED + ); + + ty_draw_line( + ty_vec2i(0, 0), + ty_vec2i(100, 100), + ty_color(0xFF, 0xF0, 0x80) + ); + + ty_draw_line( + ty_vec2i(100, 0), + ty_vec2i(0, 100), + ty_color(0x80, 0xF0, 0xFF) + ); + + ty_draw_line( + ty_vec2i(150, 0), + ty_vec2i(150, 100), + ty_color(0x80, 0xF0, 0xFF) + ); + + ty_draw_line( + ty_vec2i(100, 50), + ty_vec2i(200, 50), + ty_color(0xFF, 0xF0, 0x80) + ); + + ty_draw_end(); + + double frame_time = ty_get_time() - start; + ty_log_debug("%g ms, %g FPS", frame_time * 1000, 1.0 / frame_time); +} + int main(void) { struct ty_hints hints = { @@ -26,59 +79,14 @@ int main(void) void *data = qoi_read("test_img.qoi", &desc, 3); if (!data) ty_log_fatal(TY_ERR_IO, "failed to read image"); - struct ty_image img = ty_create_image(desc.width, desc.height, data); + img = ty_create_image(desc.width, desc.height, data); free(data); while (ty_is_game_running()) { - clock_t start = clock(); - - ty_draw_clear(TY_COLOR_BLACK); - - for (int y = 0; y < SCREEN_HEIGHT; y += 16) { - for (int x = 0; x < SCREEN_WIDTH; x += 16) { - ty_draw_image(img, ty_vec2i(x, y)); - } - } - - ty_draw_rect( - (struct ty_recti){ - SCREEN_WIDTH*0.5 + sin(ty_get_time() * 10) * 50 - 25, - SCREEN_HEIGHT*0.5-25, - 50, 50, - }, - TY_COLOR_RED - ); - - ty_draw_line( - ty_vec2i(0, 0), - ty_vec2i(100, 100), - ty_color(0xFF, 0xF0, 0x80) - ); - - ty_draw_line( - ty_vec2i(100, 0), - ty_vec2i(0, 100), - ty_color(0x80, 0xF0, 0xFF) - ); - - ty_draw_line( - ty_vec2i(150, 0), - ty_vec2i(150, 100), - ty_color(0x80, 0xF0, 0xFF) - ); - - ty_draw_line( - ty_vec2i(100, 50), - ty_vec2i(200, 50), - ty_color(0xFF, 0xF0, 0x80) - ); - - ty_draw_end(); - ty_tick(); - - ty_sleep(1000 / hints.ticrate); - - ty_log_debug("%g ms", (float)(clock() - start) / CLOCKS_PER_SEC); + int ticks = ty_tick(); + for (int i = 0; i < ticks; i++) + tick(); + // ty_sleep(1000 / hints.ticrate); } ty_free_image(img); |
