diff --git a/inc/lzy.h b/inc/lzy.h index f2a522b..a6d3088 100644 --- a/inc/lzy.h +++ b/inc/lzy.h @@ -7,7 +7,7 @@ extern "C" { #define LZY_UNUSED(x) (void)(x) -int LZY_Init(const char *title); +int LZY_Init(const char *title, int target_fps); void LZY_Quit(void); int LZY_DrawBegin(void); int LZY_DrawEnd(void); @@ -56,19 +56,40 @@ enum LZY_ScanCode { #endif #ifdef FXCG50 +#include #include #include +#include #include static color_t draw_color = C_BLACK; static int should_quit = 0; +static int timer = 0; +static volatile int has_ticked = 0; -int LZY_Init(const char *title) { +static int timer_callback(volatile int *); + +int LZY_Init(const char *title, int target_fps) { LZY_UNUSED(title); + if (target_fps > 0) { + timer = timer_configure(TIMER_ANY, 1000000 / target_fps, + GINT_CALL(timer_callback, &has_ticked)); + timer_start(timer); + } return 0; } -void LZY_Quit(void) {} +static int timer_callback(volatile int *arg) { + *arg += 1; + return 0; +} + +void LZY_Quit(void) { + if (timer) { + timer_stop(timer); + timer = 0; + } +} int LZY_DrawBegin(void) { return 0; @@ -76,11 +97,14 @@ int LZY_DrawBegin(void) { int LZY_DrawEnd(void) { dupdate(); + while (timer && !has_ticked) + sleep(); + has_ticked = 0; return 0; } void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b) { - draw_color = C_RGB(r << 3, g << 3, b << 3); + draw_color = C_RGB(r / 8, g / 8, b / 8); } void LZY_DrawSetColorNone(void) { @@ -122,8 +146,11 @@ static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; static int should_quit = 0; static int input[LZYK_COUNT] = {0}; +static int fps_limiter = 0; +static uint64_t min_dt; +static uint64_t next_time; -int LZY_Init(const char *title) { +int LZY_Init(const char *title, int target_fps) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { error = SDL_GetError(); return 1; @@ -147,6 +174,12 @@ int LZY_Init(const char *title) { return 3; } + fps_limiter = target_fps > 0; + if (fps_limiter) { + min_dt = 1000 / target_fps; + next_time = SDL_GetTicks64(); + } + return 0; } @@ -165,11 +198,21 @@ void LZY_Quit(void) { } int LZY_DrawBegin(void) { + if (fps_limiter) + next_time += min_dt; return 0; } int LZY_DrawEnd(void) { SDL_RenderPresent(renderer); + + if (fps_limiter) { + const uint64_t cur_time = SDL_GetTicks64(); + if (next_time <= cur_time) + next_time = cur_time; + else + SDL_Delay(next_time - cur_time); + } return 0; } diff --git a/src/main.c b/src/main.c index 23b7cc5..b967932 100644 --- a/src/main.c +++ b/src/main.c @@ -1,26 +1,28 @@ #define LZY_IMPLEMENTATION #include "lzy.h" +#include int main(void) { - if (LZY_Init("lzy example")) { + uint8_t c = 0; + + if (LZY_Init("lzy example", 30)) { LZY_Log(LZY_GetError()); LZY_Quit(); return 1; } LZY_Log("init was great success!"); - LZY_DrawSetColor(0xff, 0xff, 0x00); - - LZY_DrawBegin(); - LZY_DrawClear(); - LZY_DrawEnd(); - do { LZY_CycleEvents(); if (LZY_KeyDown(LZYK_LEFT)) - LZY_Log("pressing left"); + c -= 8; if (LZY_KeyDown(LZYK_RIGHT)) - LZY_Log("pressing right"); + c += 8; + + LZY_DrawBegin(); + LZY_DrawSetColor(c, c, c); + LZY_DrawClear(); + LZY_DrawEnd(); } while (!LZY_ShouldQuit()); LZY_Log("cya");