SDL-FPSCounter as module

This commit is contained in:
VegOwOtenks 2025-03-02 09:54:54 +01:00
parent 35f48fd7c0
commit f2750b30a8
6 changed files with 9 additions and 73 deletions

3
.gitmodules vendored
View file

@ -7,3 +7,6 @@
[submodule "submodules/utilitiec"]
path = submodules/utilitiec
url = https://git.jossco.de/VegOwOtenks/utilitiec.git
[submodule "submodules/SDL_FPSCounter"]
path = submodules/SDL_FPSCounter
url = https://git.jossco.de/VegOwOtenks/SDL_FPSCounter.git

View file

@ -14,8 +14,10 @@ add_subdirectory(submodules/SDL_ttf EXCLUDE_FROM_ALL)
add_subdirectory(submodules/utilitiec EXCLUDE_FROM_ALL)
add_subdirectory(submodules/SDL_FPSCounter EXCLUDE_FROM_ALL)
# Create your game executable target as usual
add_executable(partikle WIN32 main.c SDL_FPSCounter.c)
add_executable(partikle WIN32 main.c)
# Link to the actual SDL3 library.
target_link_libraries(partikle PRIVATE SDL3_ttf::SDL3_ttf SDL3::SDL3 QuadTree allocator-interface rand m)
target_link_libraries(partikle PRIVATE SDL3_ttf::SDL3_ttf SDL3::SDL3 QuadTree allocator-interface rand m SDL_FPSCounter)

View file

@ -1,51 +0,0 @@
#include "SDL_FPSCounter.h"
#include <SDL3/SDL.h>
#include <stdlib.h>
int SDL_CreateFPSCounter(SDL_FPSCounter* counter, uint32_t window_length)
{
counter->window = malloc(sizeof(uint64_t) * window_length);
if (counter->window == NULL) {
SDL_OutOfMemory();
return -1;
}
memset(counter->window, 0, window_length * sizeof(uint64_t));
counter->frequency = SDL_GetPerformanceFrequency();
counter->window_index = 0;
counter->window_length = window_length;
return EXIT_SUCCESS;
}
void SDL_DestroyFPSCounter(SDL_FPSCounter* counter)
{
free(counter->window);
memset(counter, 0, sizeof *counter);
}
int SDL_FPSCounterTick(SDL_FPSCounter* counter)
{
counter->window[counter->window_index] = SDL_GetPerformanceCounter();
counter->window_index++;
counter->window_index = counter->window_index % counter->window_length;
return EXIT_SUCCESS;
}
uint64_t SDL_FPSCounterFPS(SDL_FPSCounter* counter)
{
uint64_t last = counter->window[counter->window_index];
uint64_t current;
uint64_t acc_difference = 0;
for (uint32_t i = (counter->window_index + 1) % counter->window_length; i != counter->window_index; i = (i + 1) % counter->window_length) {
current = counter->window[i];
acc_difference += current - last;
last = current;
}
uint64_t average_difference = acc_difference / (counter->window_length - 1);
return counter->frequency / average_difference;
}

View file

@ -1,19 +0,0 @@
#ifndef SDL_FPSCOUNTER_H
#define SDL_FPSCOUNTER_H
#include <stdint.h>
typedef struct SDL_FPSCounter_s {
uint64_t frequency;
uint32_t window_index;
uint32_t window_length;
uint64_t* window;
} SDL_FPSCounter;
int SDL_CreateFPSCounter(SDL_FPSCounter* counter, uint32_t window_length);
void SDL_DestroyFPSCounter(SDL_FPSCounter* counter);
int SDL_FPSCounterTick(SDL_FPSCounter* counter);
uint64_t SDL_FPSCounterFPS(SDL_FPSCounter* counter);
#endif

2
main.c
View file

@ -21,7 +21,7 @@
#include <SDL3/SDL_main.h>
#include <SDL3_ttf/SDL_ttf.h>
#include "SDL_FPSCounter.h"
#include "submodules/SDL_FPSCounter/src/SDL_FPSCounter.h"
#include "submodules/utilitiec/src/QuadTree/QuadTree.h"
#include "submodules/utilitiec/src/rand/xoshiro256.h"

@ -0,0 +1 @@
Subproject commit 5ff4e4ebb5e7905a94d35f6679c31162d053a7e4