Slightly better run loop. Raylib is limited in this regard.

This commit is contained in:
2026-03-06 16:31:51 -08:00
parent d5cfff72b1
commit 69bbe78ecc

View File

@@ -288,11 +288,30 @@ void save_activities(FILE *f, char *file_path, U32 current_activity, activity *a
} }
} }
#define ACTIVE_FPS 60 global B32 dirty = true;
#define IDLE_FPS 1
#define IDLE_TIME 5 /* seconds */
int main(int argc, char *argv[]) internal void mark_dirty()
{
dirty = true;
}
internal void clear_dirty()
{
dirty = false;
}
internal B32 is_dirty(void)
{
return dirty;
}
internal void poll_input()
{
if(GetMouseDelta().x != 0 || GetMouseDelta().y != 0 || IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsMouseButtonReleased(MOUSE_BUTTON_LEFT) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) || GetKeyPressed() != 0 || IsWindowResized())
mark_dirty();
}
S32 main(int argc, char *argv[])
{ {
// TODO: Move into global state. // TODO: Move into global state.
activity activities[256]; // TODO: This should be a dynamic array. activity activities[256]; // TODO: This should be a dynamic array.
@@ -323,8 +342,6 @@ int main(int argc, char *argv[])
InitWindow(state.window_w, state.window_h, "Time Tracker"); InitWindow(state.window_w, state.window_h, "Time Tracker");
SetWindowState(FLAG_MSAA_4X_HINT|FLAG_WINDOW_RESIZABLE); SetWindowState(FLAG_MSAA_4X_HINT|FLAG_WINDOW_RESIZABLE);
SetTargetFPS(ACTIVE_FPS);
state.font = LoadFontFromMemory(".ttf", opensans_ttf, opensans_ttf_len, 96, NULL, 0); state.font = LoadFontFromMemory(".ttf", opensans_ttf, opensans_ttf_len, 96, NULL, 0);
if(!IsFontValid(state.font)) { if(!IsFontValid(state.font)) {
fprintf(stderr, "Unable to load font\n"); fprintf(stderr, "Unable to load font\n");
@@ -343,17 +360,10 @@ int main(int argc, char *argv[])
double last_input_s = now(); double last_input_s = now();
while(!WindowShouldClose()) { while(!WindowShouldClose()) {
poll_input();
U64 now_s = now(); U64 now_s = now();
if(GetMouseDelta().x != 0 || GetMouseDelta().y != 0 || GetMouseWheelMove() != 0 || GetKeyPressed() != 0 || IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) {
last_input_s = now_s;
SetTargetFPS(ACTIVE_FPS);
}
if(now_s - last_input_s > IDLE_TIME) {
SetTargetFPS(IDLE_FPS);
}
if(now_s >= upper_bound_s) { if(now_s >= upper_bound_s) {
t = today(); t = today();
ts = start_of_day_seconds(t); ts = start_of_day_seconds(t);
@@ -474,6 +484,9 @@ int main(int argc, char *argv[])
DrawTextEx(state.font, buffer, (Vector2){ start_x + 5.0f, 40.0f + (F32)(activity_type_d.y) }, state.font_size - 10.0f, 2, WHITE); DrawTextEx(state.font, buffer, (Vector2){ start_x + 5.0f, 40.0f + (F32)(activity_type_d.y) }, state.font_size - 10.0f, 2, WHITE);
} }
} }
DrawFPS(20, 20);
EndDrawing(); EndDrawing();
// Reset statistics as we will accumulate seconds again. // Reset statistics as we will accumulate seconds again.
@@ -486,6 +499,13 @@ int main(int argc, char *argv[])
save_activities(f, file_path, current_activity, activities); save_activities(f, file_path, current_activity, activities);
last_save = now_s; last_save = now_s;
} }
if(is_dirty()) {
clear_dirty();
SetTargetFPS(120); // FPS cap
} else {
SetTargetFPS(20);
}
} }
save_activities(f, file_path, current_activity, activities); save_activities(f, file_path, current_activity, activities);