Browse Source

framerate counter

distfuncs
alistair 4 years ago
parent
commit
a8ee16e2f7
  1. 2
      Makefile
  2. 24
      colours.c
  3. 1
      colours.h
  4. 18
      main.c

2
Makefile

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
LINKS = -lSDL2 -lm -Wall
LINKS = -lSDL2 -lm -Wall -g
all: colours.o
gcc main.c $(LINKS) colours.o -o blackpink

24
colours.c

@ -1,5 +1,10 @@ @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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++) {

1
colours.h

@ -1,5 +1,4 @@ @@ -1,5 +1,4 @@
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>

18
main.c

@ -1,5 +1,8 @@ @@ -1,5 +1,8 @@
#include "main.h"
#include "queue.h"
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_timer.h>
int keyboardstate[322] = {}; // 322 is the number of SDLK_DOWN events
int exitnow = 0;
@ -64,7 +67,7 @@ leave: @@ -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) { @@ -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) { @@ -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();

Loading…
Cancel
Save