File: testframerate.c

package info (click to toggle)
libsdl2-gfx 1.0.4%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,032 kB
  • sloc: ansic: 13,426; sh: 12,663; makefile: 128
file content (242 lines) | stat: -rwxr-xr-x 5,915 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* 

TestFramerate.c: test/sample program for framerate manager 

Copyright (C) 2012-2014 Andreas Schiffler

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#include "SDL_test_common.h"

#include "SDL2_gfxPrimitives.h"
#include "SDL2_framerate.h"

static SDLTest_CommonState *state;

/* Screen size */
#define WIDTH	DEFAULT_WINDOW_WIDTH
#define HEIGHT	DEFAULT_WINDOW_HEIGHT

/* Pause flag */
int pause = 0;

/* Done flag */
int done = 0;

/* Current message */
char *messageText;

void ClearScreen(SDL_Renderer *renderer)
{
	SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0xFF);
	SDL_RenderClear(renderer);
}

/* Global draw state */
int timeout = 0;
int x = WIDTH / 2;
int y = HEIGHT / 2;
int dx = 7;
int dy = 5;
int r = 255; 
int g = 255;
int b = 255;
Uint32 time_passed = 0;

void Draw(SDL_Renderer *renderer, FPSmanager *fpsm)
{
	int newRate;
	int currentRate;

		/* FPSmanager: get current rate */
		currentRate = SDL_getFramerate(fpsm);

		/* Set/switch framerate */
		timeout--;
		if (timeout < 0) {
			/* New rate */
			newRate = 5 + 5 * (rand() % 10);

			/* Update timeout */
			timeout = 2 * newRate;

			/* New Color */
			r = rand() & 255;
			g = rand() & 255;
			b = rand() & 255;

			/* FPSmanager: set new rate */
			SDL_setFramerate(fpsm, newRate);
		}

		/* Black screen */
		ClearScreen(renderer);

		/* Move */
		x += dx;
		y += dy;

		/* Reflect */
		if ((x<0) || (x>WIDTH)) { dx=-dx; }
		if ((y<0) || (y>HEIGHT)) { dy=-dy; }

		/* Draw */
		filledCircleRGBA (renderer,x,y,30,r,g,b,255);
		circleRGBA(renderer,x,y,30,255,255,255,255);

		/* Report set rate and current delay (from last iteration) */
		if (time_passed > 0) {
			SDL_snprintf(messageText, 1024, "Set rate: %4iHz   Last delay=%4ims / Calc.Rate=%4iHz", currentRate, time_passed, 1000 / time_passed); 
			stringRGBA (renderer, WIDTH/2 - 4*strlen(messageText),HEIGHT-24,messageText,255,255,255,255);
		}

		/* Simulate some random delay from drawing, business logic, network interaction, etc. */
		SDL_Delay(1 + rand() % 10);

		/* Display */
		SDL_RenderPresent(renderer);

		/* FPSmanager: Delay to a fixed rate */                   
		time_passed = SDL_framerateDelay(fpsm);  
}



/* ====== Main */

int main(int argc, char *argv[])
{
	int i;
	SDL_Event event;
	Uint32 then, now, frames;
	Uint32 seconds = -1;
	FPSmanager fpsm;

	/* Initialize test framework */
	state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
	if (!state) {
		return 1;
	}

    SDL_Log("SDL2_gfx %i.%i.%i: testframerate", SDL2_GFXPRIMITIVES_MAJOR, SDL2_GFXPRIMITIVES_MINOR, SDL2_GFXPRIMITIVES_MICRO);
    SDL_Log("Platform: %s", SDL_GetPlatform());

	for (i = 1; i < argc;) {
		int consumed;

		consumed = SDLTest_CommonArg(state, i);
		if (consumed == 0) {
                   consumed = -1;
                   if (SDL_strcasecmp(argv[i], "--x") == 0) {
                      if (argv[i + 1]) {
						  x = SDL_atoi(argv[i + 1]);
                         if (x < 0) x = 0;
                         if (x >= WIDTH) x = WIDTH - 1;
                         consumed = 2;
                      }
				   }
                   else if (SDL_strcasecmp(argv[i], "--y") == 0) {
                      if (argv[i + 1]) {
                         y = SDL_atoi(argv[i + 1]);
                         if (y < 0) y = 0;
                         if (y >= HEIGHT) y = HEIGHT - 1;
                         consumed = 2;
                      }
                   }
                   else if (SDL_strcasecmp(argv[i], "--seconds") == 0) {
                      if (argv[i + 1]) {
                         seconds = SDL_atoi(argv[i + 1]);
                         if (seconds < 0) seconds = 0;
                         SDL_Log("Will run for up to %d seconds\n", seconds);
                         consumed = 2;
                      }
                   }
                }
                
		if (consumed < 0) {
			SDL_Log("Usage: %s %s [--x #] [--y #] [--seconds #]\n",
				argv[0], SDLTest_CommonUsage(state));
			return 1;
		}
		i += consumed;
	}
	if (!SDLTest_CommonInit(state)) {
		return 2;
	}

	/* Create the windows and initialize the renderers */
	for (i = 0; i < state->num_windows; ++i) {
		SDL_Renderer *renderer = state->renderers[i];
                SDL_RendererInfo info;
                SDL_GetRendererInfo(state->renderers[i], &info);		                
                SDL_Log("Renderer %i: %s %s", i, info.name, (info.flags | SDL_RENDERER_ACCELERATED) ? "(Accelerated)" : "");		
		SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
		SDL_RenderClear(renderer);
	}

	/* Initialize random number generator */
	srand((unsigned int)time(NULL));

	/* Create common message buffer */
	messageText = (char *)SDL_malloc(1024);
	if (messageText == NULL) {
		return -1;
	}

	/* Initialize framerate manager */
	SDL_initFramerate(&fpsm);

	/* Main render loop */
	frames = 0;
	then = SDL_GetTicks();
	done = 0;
	while (!done) {
		/* Check for events */
		++frames;
		while (SDL_PollEvent(&event) && !done) {
			SDLTest_CommonEvent(state, &event, &done);
		}

        /* Do all the drawing work */
		Draw(state->renderers[0], &fpsm);

		if (seconds >= 0) {
			now = SDL_GetTicks();
			if (now > then + (seconds * 1000)) {
				done = 1;
			}
		}
	}

	/* Print out some timing information */
	now = SDL_GetTicks();
	if (now > then) {
		double fps = ((double) frames * 1000) / (now - then);
		SDL_Log("%2.2f frames per second\n", fps);
	}

	/* Free common message buffer */
	if (messageText) {
		free(messageText);
	}

	/* Shutdown SDL */
	SDLTest_CommonQuit(state);

	return 0;
}