From a8ee16e2f770a55e169d7ae95aec80359ccabbf5 Mon Sep 17 00:00:00 2001 From: alistair Date: Sat, 17 Oct 2020 01:11:53 +1000 Subject: [PATCH] framerate counter --- Makefile | 2 +- colours.c | 24 ++++++++++++------------ colours.h | 1 - main.c | 18 ++++++++++++++++-- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 74615cd..8bcec78 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -LINKS = -lSDL2 -lm -Wall +LINKS = -lSDL2 -lm -Wall -g all: colours.o gcc main.c $(LINKS) colours.o -o blackpink diff --git a/colours.c b/colours.c index 0bb823f..d4aef96 100644 --- a/colours.c +++ b/colours.c @@ -1,5 +1,10 @@ #include "colours.h" + +int c_round(double a) { + return a - (int)a > 0.5 ? (int)a + 1 : (int)a; +} + struct colour get_random_color(void) { int red = rand() % 255; int blue = rand() % 255; @@ -116,6 +121,7 @@ double magic_hsv_function(int n, struct colour c) { return res; } + struct colour get_rgb_from_hsv(struct colour c) { double r = magic_hsv_function(5, c); @@ -123,9 +129,9 @@ struct colour get_rgb_from_hsv(struct colour c) { double b = magic_hsv_function(1, c); struct colour res; - res.r = round(r * 255); - res.g = round(g * 255); - res.b = round(b * 255); + res.r = c_round(r * 255); + res.g = c_round(g * 255); + res.b = c_round(b * 255); return res; } @@ -145,9 +151,9 @@ struct colour get_rgb_from_hsl(struct colour c) { double b = magic_hsl_function(4, c); struct colour res; - res.r = round(r * 255); - res.g = round(g * 255); - res.b = round(b * 255); + res.r = c_round(r * 255); + res.g = c_round(g * 255); + res.b = c_round(b * 255); return res; } @@ -209,12 +215,6 @@ void print_colour(struct colour c) { void test(int seed) { struct colour c = get_random_color(); - //print_colour(c); - struct colour hsl = get_hsl(c); - struct colour hsv = get_hsv(c); - /*printf("RGB: %d %d %d\n",c.r,c.g,c.b);*/ - /*printf("HSL: %f %f %f\n",hsl.h, hsl.s, hsl.l);*/ - /*printf("HSV: %f %f %f\n",hsv.h, hsv.s, hsv.v);*/ struct colour *adj = get_adjacent(c, 5, 4); for (int i = 0; i < 5; i++) { diff --git a/colours.h b/colours.h index 20af1ca..a44599f 100644 --- a/colours.h +++ b/colours.h @@ -1,5 +1,4 @@ #include -#include #include #include #include diff --git a/main.c b/main.c index 6e4585e..c6ac895 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,8 @@ #include "main.h" #include "queue.h" +#include +#include +#include int keyboardstate[322] = {}; // 322 is the number of SDLK_DOWN events int exitnow = 0; @@ -64,7 +67,7 @@ leave: int main(int argc, char **argv) { SDL_Window * win = make_window(); - ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); + ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); SDL_RenderSetLogicalSize(ren, B_INTERNAL_HEIGHT, B_INTERNAL_HEIGHT); SDL_Rect r; @@ -75,9 +78,12 @@ int main(int argc, char **argv) { SDL_Thread *input_thread = SDL_CreateThread(input_loop, "input", (void *)NULL); struct colour c = get_random_color(); + double elapsed; + Uint64 start, end; while (!exitnow) { /* clear the view */ + start = SDL_GetPerformanceCounter(); SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); SDL_RenderClear(ren); @@ -90,8 +96,16 @@ int main(int argc, char **argv) { SDL_SetRenderDrawColor(ren, c.r, c.g, c.b, c.a); SDL_RenderFillRect(ren, &r); - /* update the view */ SDL_RenderPresent(ren); + + end = SDL_GetPerformanceCounter(); + double el = (1000 * (end - start) / SDL_GetPerformanceFrequency()); + if (el > 0) { + elapsed = 1000 / el; + } + printf("framerate: %f\r", elapsed); + fflush(stdout); + } SDL_Quit();