Added a very primitive adaptive FPS loop.

This commit is contained in:
2026-03-05 22:42:02 -08:00
parent 97c77a8642
commit d5cfff72b1

View File

@@ -1,10 +1,10 @@
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// TODOS: // // TODOS: //
// [X] Lower the the time text lower below the activity title. // // [X] Lower the the time text lower below the activity title. //
// [ ] On timeline, hower, display additional activity chunk info. // // [X] On timeline, hower, display additional activity chunk info. //
// [X] Display distribution timeline below the activity line. // // [X] Display distribution timeline below the activity line. //
// [ ] Better file save error handling. // // [ ] Better file save error handling. //
// [ ] Make Linux and MacOS libraries for RayLib; store locally. // // [X] Make Linux and MacOS libraries for RayLib; store locally. //
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@@ -78,9 +78,7 @@ U64 now()
return (U64)time(NULL); return (U64)time(NULL);
} }
B32 activity_button(Rectangle rect, char *title, char *subtitle, B32 activity_button(Rectangle rect, char *title, char *subtitle, F32 font_size, Color font_color, Color background_color)
F32 font_size, Color font_color,
Color background_color)
{ {
B32 button_pressed = false; B32 button_pressed = false;
@@ -274,7 +272,7 @@ void save_activities(FILE *f, char *file_path, U32 current_activity, activity *a
// NOTE: We use "w+" because want to clear file contents before the next write. // NOTE: We use "w+" because want to clear file contents before the next write.
f = freopen(file_path, "w+", f); f = freopen(file_path, "w+", f);
if(f) { if(f) {
char write_buffer[128]; local_persist char write_buffer[128];
for(U32 i = 0; i <= current_activity; i++) { for(U32 i = 0; i <= current_activity; i++) {
snprintf(write_buffer, sizeof(write_buffer), "%s %llu %llu\n", activity_type_string_representation[activities[i].type], activities[i].began, activities[i].ended); snprintf(write_buffer, sizeof(write_buffer), "%s %llu %llu\n", activity_type_string_representation[activities[i].type], activities[i].began, activities[i].ended);
@@ -290,6 +288,10 @@ void save_activities(FILE *f, char *file_path, U32 current_activity, activity *a
} }
} }
#define ACTIVE_FPS 60
#define IDLE_FPS 1
#define IDLE_TIME 5 /* seconds */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// TODO: Move into global state. // TODO: Move into global state.
@@ -321,7 +323,7 @@ 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(30); 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)) {
@@ -338,11 +340,20 @@ int main(int argc, char *argv[])
state.font_size = 30; state.font_size = 30;
//DisableEventWaiting(); double last_input_s = now();
//EnableEventWaiting();
while(!WindowShouldClose()) { while(!WindowShouldClose()) {
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);