From 67f654f72ecac4a46835d20ba147f3f28071ff98 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 5 Jan 2025 10:23:41 +0100 Subject: [PATCH] readd seconds stuff --- src/app/app.gen.c | 2 +- src/app/app.gen.h | 2 +- src/app/app_win32.c | 38 +++++++++++++++++++------------------- src/os/os_unix.c | 13 +++++++++++++ src/os/os_win32.c | 20 ++++++++++++++++++++ todo.txt | 2 +- 6 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/app/app.gen.c b/src/app/app.gen.c index bd08a40..9ef6f61 100644 --- a/src/app/app.gen.c +++ b/src/app/app.gen.c @@ -227,7 +227,7 @@ app_key_t w32_map_wparam_to_app_key(WPARAM wparam) { default: {return app_key_null;} break; } } -#endif/*D:\dev\wasm\src/app/app.meta.c*/ +#endif/*C:\dev\wasm\src/app/app.meta.c*/ type_t type__app_mouse_button_t = { type_kind_enum, s8_const_lit("app_mouse_button_t"), sizeof(app_mouse_button_t), .members = (type_member_t[]){ {.name = s8_const_lit("app_mouse_button_null"), .value = app_mouse_button_null}, diff --git a/src/app/app.gen.h b/src/app/app.gen.h index 5512a15..6920818 100644 --- a/src/app/app.gen.h +++ b/src/app/app.gen.h @@ -70,7 +70,7 @@ app_key_page_up, app_key_page_down, app_key_count, } app_key_t; -/*D:\dev\wasm\src/app/app.meta.c*/ +/*C:\dev\wasm\src/app/app.meta.c*/ typedef enum { app_mouse_button_null, app_mouse_button_left, diff --git a/src/app/app_win32.c b/src/app/app_win32.c index 43eb777..373c81b 100644 --- a/src/app/app_win32.c +++ b/src/app/app_win32.c @@ -40,6 +40,20 @@ fn f64 w32_get_dpr(HWND window_handle) { return result; } +fn f64 w32_seconds_now(void) { + static int64_t counts_per_second; + if (counts_per_second == 0) { + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + counts_per_second = freq.QuadPart; + } + + LARGE_INTEGER time; + QueryPerformanceCounter(&time); + f64 result = (f64)time.QuadPart / (f64)counts_per_second; + return result; +} + /////////////////////////////// // event processing @@ -240,20 +254,6 @@ void w32_try_resizing_canvas(w32_canvas_t *canvas, HWND window_handle) { } } -fn f64 os_seconds_now(void) { - static int64_t counts_per_second; - if (counts_per_second == 0) { - LARGE_INTEGER freq; - QueryPerformanceFrequency(&freq); - counts_per_second = freq.QuadPart; - } - - LARGE_INTEGER time; - QueryPerformanceCounter(&time); - f64 result = (f64)time.QuadPart / (f64)counts_per_second; - return result; -} - int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { tcx.temp = ma_create(ma_default_reserve_size); @@ -339,7 +339,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n refresh_rate = (f64)devmodew.dmDisplayFrequency; } - f64 time_frame_start = os_seconds_now(); + f64 time_frame_start = w32_seconds_now(); f64 time_delta = 1.0 / refresh_rate; f64 time_total = 0.0; f64 time_update = 0.0; @@ -373,7 +373,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n /////////////////////////////// // end of frame timings - f64 time_update_partial = os_seconds_now() - time_frame_start; + f64 time_update_partial = w32_seconds_now() - time_frame_start; time_update = time_update_partial; if (time_update < time_delta) { consecutive_missed_frames = 0; @@ -393,9 +393,9 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n // busy loop if we dont have good scheduling // or we woke up early - time_update = os_seconds_now() - time_frame_start; + time_update = w32_seconds_now() - time_frame_start; while (time_update < time_delta) { - time_update = os_seconds_now() - time_frame_start; + time_update = w32_seconds_now() - time_frame_start; } } else { missed_frames += 1; @@ -409,7 +409,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n // probably want the locked frame rate and total should reflect that, so choosing // time_delta seems the correct choice but not really sure what is correct. time_total += time_delta; - time_frame_start = os_seconds_now(); + time_frame_start = w32_seconds_now(); } return 0; diff --git a/src/os/os_unix.c b/src/os/os_unix.c index be25369..92c9cfa 100644 --- a/src/os/os_unix.c +++ b/src/os/os_unix.c @@ -10,3 +10,16 @@ fn os_date_t os_local_time_now(void) { result.year = lt->tm_year; return result; } + +fn u64 os_get_microseconds(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + u64 result = t.tv_sec*million(1) + (t.tv_nsec/thousand(1)); + return result; +} + +fn f64 os_get_milliseconds(void) { + u64 micros = os_get_microseconds(); + f64 result = (f64)micros / 1000.0; + return result; +} \ No newline at end of file diff --git a/src/os/os_win32.c b/src/os/os_win32.c index 15782a2..32e5e5c 100644 --- a/src/os/os_win32.c +++ b/src/os/os_win32.c @@ -11,3 +11,23 @@ fn os_date_t os_local_time_now(void) { result.year = lt.wYear; return result; } + +fn f64 os_seconds_now(void) { + static int64_t counts_per_second; + if (counts_per_second == 0) { + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + counts_per_second = freq.QuadPart; + } + + LARGE_INTEGER time; + QueryPerformanceCounter(&time); + f64 result = (f64)time.QuadPart / (f64)counts_per_second; + return result; +} + +fn f64 os_get_milliseconds(void) { + f64 secs = os_seconds_now(); + f64 result = secs * 1000; + return result; +} \ No newline at end of file diff --git a/todo.txt b/todo.txt index 2eed321..f3e48ee 100644 --- a/todo.txt +++ b/todo.txt @@ -17,7 +17,7 @@ [ ] gfx [ ] simple 2d [ ] revisit api - [ ] change name, hard to type + [x] change name, hard to type [ ] core [x] remove dates and time from core