diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7229f4e..b3b54e2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -4,7 +4,7 @@ on: [push, pull_request, workflow_dispatch] jobs: build-windows-msvc: - runs-on: windows-2019 + runs-on: windows-latest steps: - uses: actions/checkout@v1 # this runs vcvarsall for us, so we get the MSVC toolchain in PATH. @@ -20,7 +20,7 @@ jobs: - name: run assetsys tests run: .\assetsys.exe build-windows-msvc-cpp: - runs-on: windows-2019 + runs-on: windows-latest steps: - uses: actions/checkout@v1 # this runs vcvarsall for us, so we get the MSVC toolchain in PATH. @@ -36,7 +36,7 @@ jobs: - name: run assetsys tests run: .\assetsys.exe build-windows-tcc: - runs-on: windows-2019 + runs-on: windows-latest steps: - uses: actions/checkout@v1 - uses: robinraju/release-downloader@v1.2 diff --git a/README.md b/README.md index 44e25d9..e3bc018 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Single-file public domain libraries for C/C++ (dual licensed under MIT). * [app.h](docs/app.md) - Small cross-platform base framework for graphical apps, for C/C++. * [assetsys.h](docs/assetsys.md) - File system abstraction to read from zip-files, for C/C++. +* [frametimer.h](docs/frametimer.md) - Framerate timer and frame rate limiter, for C/C++. * [hashtable.h](docs/hashtable.md) - Cache efficient hash table implementation for C/C++. * [http.h](docs/http.md) - Basic HTTP protocol implementation over sockets (no https). * [ini.h](docs/ini.md) - Simple ini-file reader for C/C++. @@ -23,7 +24,6 @@ More libs, work-in-progress, some are unfinished, some are complete but lacking * cstr.h - String interning and manipulation library for C/C++. * dialog.h - Loading and management of dialogs for a custom game dialog system. * dir.h - Directory listing functions for C/C++. -* frametimer.h - Framerate timer functionality, for C/C++. * id3tag.h - Read/write ID3 tags from/to mp3 files in C/C++. * img.h - Image processing functions for C/C++. * mus.h - Parsing library for MUS music files (as used in DOS games). diff --git a/docs/app.md b/docs/app.md index 6484361..f00d270 100644 --- a/docs/app.md +++ b/docs/app.md @@ -1,7 +1,7 @@ app.h ===== -Small cross-platform base framework for graphical apps. +Library: [app.h](../app.h) Example diff --git a/docs/frametimer.md b/docs/frametimer.md new file mode 100644 index 0000000..f27a2b6 --- /dev/null +++ b/docs/frametimer.md @@ -0,0 +1,172 @@ +frametimer.h +============ + +Library: [frametimer.h](../frametimer.h) + + +Example +------- + +Here's a sample program which creates a frame timer, limits update to 60 frames +per second, and retrieves delta time on each loop iteration. + + #define FRAMETIMER_IMPLEMENTATION + #include "frametimer.h" + + int main() { + frametimer_t* frametimer = frametimer_create( NULL ); + frametimer_lock_rate( frametimer, 60 ); + + int is_running = 1; + while( is_running ) { + float dt = frametimer_update( frametimer ); + + // ... + + } + + frametimer_destroy( frametimer ); + return 0; + } + + +API Documentation +----------------- + +frametimer.h is a small library for measuring the time between frame updates in +a game loop or similar application, and optionally limit the frame rate to a +specified number of frames per seconds. + +It is using high precision timers and wait functions to accomplish this. + + +### Customization + +There are a few different things in frametimer.h which are configurable by +#defines. The customizations only affect the implementation, so will only need +to be defined in the file where you have the #define FRAMETIMER_IMPLEMENTATION. + + +#### Custom memory allocators + +To store the internal data structures, frametimer.h needs to do dynamic allocation +by calling `malloc`. Programs might want to keep track of allocations done, or use +custom defined pools to allocate memory from. frametimer.h allows for specifying +custom memory allocation functions for `malloc` and `free`. +This is done with the following code: + + #define FRAMETIMER_IMPLEMENTATION + #define FRAMETIMER_MALLOC( ctx, size ) ( my_custom_malloc( ctx, size ) ) + #define FRAMETIMER_FREE( ctx, ptr ) ( my_custom_free( ctx, ptr ) ) + #include "frametimer.h" + +where `my_custom_malloc` and `my_custom_free` are your own memory +allocation/deallocation functions. The `ctx` parameter is an optional parameter of +type `void*`. When `frametimer_create` is called, you can pass in a `memctx` +parameter, which can be a pointer to anything you like, and which will be passed +through as the `ctx` parameter to every `FRAMETIMER_MALLOC`/`FRAMETIMER_FREE` call. +For example, if you are doing memory tracking, you can pass a pointer to your +tracking data as `memctx`, and in your custom allocation/deallocation function, you +can cast the `ctx` param back to the right type, and update the tracking data. + +If no custom allocator is defined, frametimer.h will default to `malloc` and `free` +from the C runtime library. + + +#### Custom unsigned 64-bit integer type + +frametimer.h allows for specifying the exact type of 64-bit unsigned integer to be used +internally. By default, it is defined as `unsigned long long`, but as this is not a +standard type on all compilers, you can redefine it by #defining FRAMETIMER_U64 before +including the implementation for frametimer.h. This is useful if you, for example, use +the types from `` in the rest of your program, and you want frametimer.h to +use compatible types. In this case, you would include frametimer.h using the following +code: + + #define FRAMETIMER_IMPLEMENTATION + #define FRAMETIMER_U64 uint64_t + #include "frametimer.h" + + +frametimer_create +----------------- + + frametimer_t* frametimer_create( void* memctx ); + +Creates a new frametimer instance. When no longer needed, it should be cleaned up by +calling `frametimer_destroy`. `memctx` is a pointer to user defined data which will be +passed through to the custom FRAMETIMER_MALLOC/FRAMETIMER_FREE calls. It can be NULL if +no user defined data is needed, or if no custom allocators are defined. If the allocation +fails when creating the frametimer, `frametimer_create` returns NULL. + + +frametimer_destroy +------------------ + + void frametimer_destroy( frametimer_t* frametimer ) + +Destroys a `frametimer_t` instance created by calling `frametimer_create`, releasing the +resources allocated by it. No further API calls are valid on a `frametimer_t` instance +after calling `frametimer_destroy` on it. + + +frametimer_lock_rate +-------------------- + + void frametimer_lock_rate( frametimer_t* frametimer, int fps ); + +Using frametimer, you can lock the update rate to a fixed number of frames per second. +In this case, calls to `frametimer_update` will measure the time elapsed since last +update, using high precision timers, and if it is less than the time between frames for +the given fps, `frametimer_update` will (non-busy) wait until the expected amount of time +has elapsed. The implementation is using a high quality, high resolution wait mechanic +for this, and (on windows) sets the highest timer resolution possible, ensuring the wait +does not "oversleep". + +Passing an `fps` parameter of 0 (or less), turns off the frame rate lock and reverts to +the default behaviour, which is free running frame rate. The update rate is capped at the +lower end to 5 fps, to protect against time steps that are too large to be practical, and +which can cause problems in game update code. + + +frametimer_update +----------------- + + float frametimer_update( frametimer_t* frametimer ); + +`frametimer_update` should be called once per frame update in your main loop. + +If running at a locked rate (`frametimer_lock_rate` having been called with an `fps` +param greater than 0), `frametimer_update` will measure the time elapsed since last +update, using high precision timers, and if it is less than the time between frames for +the given fps, `frametimer_update` will (non-busy) wait until the expected amount of time +has elapsed. The implementation is using a high quality, high resolution wait mechanic +for this, and (on windows) sets the highest timer resolution possible, ensuring the wait +does not "oversleep". + +When running at an unrestricted rate, `frametimer_update` will measure the time elapsed +since last update, using high precision timers. + +In both cases, `frametimer_update` will return a floating point delta time value, giving +the number of seconds elapsed since last frame. This value is guaranteed to never be +negative, and it is capped to never be greater than 0.2 seconds, equivalent to running +at 5 frames per second, to protect against time steps that are too large to be practical, +and which can cause problems in game update code. + + +frametimer_delta_time +--------------------- + + float frametimer_delta_time( frametimer_t* frametimer ); + +Returns the delta time for the last frame update. It returns the exact same value that +was returned from the last call to `frametimer_update`. + + +frametimer_frame_counter +------------------------ + + int frametimer_frame_counter( frametimer_t* frametimer ); + +Returns the total number of times that `frametimer_update` has been called. + diff --git a/frametimer.h b/frametimer.h index ab88ff0..8a3203f 100644 --- a/frametimer.h +++ b/frametimer.h @@ -3,7 +3,7 @@ Licensing information can be found at the end of the file. ------------------------------------------------------------------------------ -frametimer.h - v1.0 - Framerate timer and frame rate limiter functionality. +frametimer.h - v1.0 - Framerate timer and frame rate limiter, for C/C++ Do this: #define FRAMETIMER_IMPLEMENTATION