a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 931610c121c6bc87d5cd6e58f514975f6fd46acf
parent d4de91463dc441cd190bff9b2f0006429459b58f
Author: amin <dev@aminmesbah.com>
Date:   Tue, 16 Apr 2019 17:59:01 +0000

Define player speed in terms of meters per second

FossilOrigin-Name: 0b6ff424630a4657585aedc40d92986bc2a016309079cb022c6431d676c56a89
Diffstat:
Msrc/game.c | 9++++++---
Msrc/platform_linux.c | 30++++++++++++++++++++----------
Msrc/platform_linux.h | 6------
3 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -93,10 +93,12 @@ void game_init(struct GameState *game_state, v2u framebuffer) } +// NOTE(amin): For now updating and rendering are interleaved. We simulate the +// world at the same rate we render it. Our timestep is not fixed. We may want +// to change this in the future. void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffer) { - rect viewport; - + rect viewport = {0}; f32 ppm = 0.0f; { f32 screen_aspect_ratio = (f32)framebuffer.width / (f32)framebuffer.height; @@ -188,7 +190,8 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe // update player { - f32 movement_speed = 0.2f; + f32 max_meters_per_second = 5.0f; + f32 movement_speed = max_meters_per_second * dt; struct GameInput input = game_state->input; if (input.key_up) diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -69,23 +69,30 @@ int main(void) struct GameState game_state = {0}; glfwSetWindowUserPointer(window, &game_state); - game_init(&game_state, glmth_v2u_init(PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT)); + game_init(&game_state, (v2u) {PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT}); #ifdef PLATFORM_HOTLOAD_GAME_CODE struct GameCode game_code = load_game_code(PLATFORM_GAME_LIB_PATH); game_code.game_load_opengl_symbols(); #endif - uint64_t lag = 0; - uint64_t previous_ms = (glfwGetTimerValue() * PLATFORM_SECOND) / glfwGetTimerFrequency(); + // NOTE(amin): lag should always be very small, so it's fine to use a float + // to store it. We will not run out of precision. + uint64_t previous_wall_clock_time = glfwGetTimerValue(); + uint64_t timer_frequency = glfwGetTimerFrequency(); + float dt = 0.0f; while (!glfwWindowShouldClose(window)) { - uint64_t current_ms = (glfwGetTimerValue() * PLATFORM_SECOND) / glfwGetTimerFrequency(); - uint64_t elapsed_ms = current_ms - previous_ms; - previous_ms = current_ms; - lag += elapsed_ms; - //printf("%" PRIu64 ", %" PRIu64 ", %f\n", elapsed_ms, lag, PLATFORM_MS_PER_UPDATE); + uint64_t current_wall_clock_time = glfwGetTimerValue(); + + { + uint64_t previous_frame_duration = current_wall_clock_time - previous_wall_clock_time; + previous_wall_clock_time = current_wall_clock_time; + dt = (float)previous_frame_duration / (float)timer_frequency; + // TODO: Debug periodic dropped frame on linux when fullscreen + printf("ms per frame: %f\n", dt * 1000.0f); + } int32_t framebuffer_width = PLATFORM_SCR_WIDTH; int32_t framebuffer_height = PLATFORM_SCR_HEIGHT; @@ -104,11 +111,14 @@ int main(void) // TODO: fall back to backup? } } - game_code.game_update_and_render(&game_state, lag/PLATFORM_SECOND, glmth_v2u_init(framebuffer_width, framebuffer_height)); + // TODO: Don't make a v2u out of two i32s + game_code.game_update_and_render(&game_state, dt, (v2u) {framebuffer_width, framebuffer_height}); #else - game_update_and_render(&game_state, lag/PLATFORM_SECOND, glmth_v2u_init(framebuffer_width, framebuffer_height)); + // TODO: Don't make a v2u out of two i32s + game_update_and_render(&game_state, dt, (v2u) {framebuffer_width, framebuffer_height}); #endif // PLATFORM_HOTLOAD_GAME_CODE + // TODO: Do something sane when vsync is disabled glfwSwapBuffers(window); glfwPollEvents(); } diff --git a/src/platform_linux.h b/src/platform_linux.h @@ -1,12 +1,6 @@ #define PLATFORM_SCR_WIDTH 600u #define PLATFORM_SCR_HEIGHT 600u -#define PLATFORM_SECOND 1000.0f -#define PLATFORM_FPS 60 -#define PLATFORM_MS_PER_FRAME (PLATFORM_SECOND / PLATFORM_FPS) -#define PLATFORM_UPDATES_PER_SECOND 120 -#define PLATFORM_MS_PER_UPDATE (PLATFORM_SECOND / PLATFORM_UPDATES_PER_SECOND) - #ifdef PLATFORM_HOTLOAD_GAME_CODE #define PLATFORM_GAME_LIB_PATH "./out/release/game.so" struct GameCode