| 1 |
/* |
| 2 |
|
| 3 |
TestFramerate.c: test/sample program for framerate manager |
| 4 |
|
| 5 |
Copyright (C) 2012-2014 Andreas Schiffler |
| 6 |
|
| 7 |
This software is provided 'as-is', without any express or implied |
| 8 |
warranty. In no event will the authors be held liable for any damages |
| 9 |
arising from the use of this software. |
| 10 |
|
| 11 |
Permission is granted to anyone to use this software for any purpose, |
| 12 |
including commercial applications, and to alter it and redistribute it |
| 13 |
freely. |
| 14 |
*/ |
| 15 |
|
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <math.h> |
| 21 |
#include <time.h> |
| 22 |
|
| 23 |
#include "SDL_test_common.h" |
| 24 |
|
| 25 |
#include "SDL2_gfxPrimitives.h" |
| 26 |
#include "SDL2_framerate.h" |
| 27 |
|
| 28 |
static SDLTest_CommonState *state; |
| 29 |
|
| 30 |
/* Screen size */ |
| 31 |
#define WIDTH DEFAULT_WINDOW_WIDTH |
| 32 |
#define HEIGHT DEFAULT_WINDOW_HEIGHT |
| 33 |
|
| 34 |
/* Pause flag */ |
| 35 |
int pause = 0; |
| 36 |
|
| 37 |
/* Done flag */ |
| 38 |
int done = 0; |
| 39 |
|
| 40 |
/* Current message */ |
| 41 |
char *messageText; |
| 42 |
|
| 43 |
void ClearScreen(SDL_Renderer *renderer) |
| 44 |
{ |
| 45 |
SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0xFF); |
| 46 |
SDL_RenderClear(renderer); |
| 47 |
} |
| 48 |
|
| 49 |
/* Global draw state */ |
| 50 |
int timeout = 0; |
| 51 |
int x = WIDTH / 2; |
| 52 |
int y = HEIGHT / 2; |
| 53 |
int dx = 7; |
| 54 |
int dy = 5; |
| 55 |
int r = 255; |
| 56 |
int g = 255; |
| 57 |
int b = 255; |
| 58 |
Uint32 time_passed = 0; |
| 59 |
|
| 60 |
void Draw(SDL_Renderer *renderer, FPSmanager *fpsm) |
| 61 |
{ |
| 62 |
int newRate; |
| 63 |
int currentRate; |
| 64 |
|
| 65 |
/* FPSmanager: get current rate */ |
| 66 |
currentRate = SDL_getFramerate(fpsm); |
| 67 |
|
| 68 |
/* Set/switch framerate */ |
| 69 |
timeout--; |
| 70 |
if (timeout < 0) { |
| 71 |
/* New rate */ |
| 72 |
newRate = 5 + 5 * (rand() % 10); |
| 73 |
|
| 74 |
/* Update timeout */ |
| 75 |
timeout = 2 * newRate; |
| 76 |
|
| 77 |
/* New Color */ |
| 78 |
r = rand() & 255; |
| 79 |
g = rand() & 255; |
| 80 |
b = rand() & 255; |
| 81 |
|
| 82 |
/* FPSmanager: set new rate */ |
| 83 |
SDL_setFramerate(fpsm, newRate); |
| 84 |
} |
| 85 |
|
| 86 |
/* Black screen */ |
| 87 |
ClearScreen(renderer); |
| 88 |
|
| 89 |
/* Move */ |
| 90 |
x += dx; |
| 91 |
y += dy; |
| 92 |
|
| 93 |
/* Reflect */ |
| 94 |
if ((x<0) || (x>WIDTH)) { dx=-dx; } |
| 95 |
if ((y<0) || (y>HEIGHT)) { dy=-dy; } |
| 96 |
|
| 97 |
/* Draw */ |
| 98 |
filledCircleRGBA (renderer,x,y,30,r,g,b,255); |
| 99 |
circleRGBA(renderer,x,y,30,255,255,255,255); |
| 100 |
|
| 101 |
/* Report set rate and current delay (from last iteration) */ |
| 102 |
if (time_passed > 0) { |
| 103 |
SDL_snprintf(messageText, 1024, "Set rate: %4iHz Last delay=%4ims / Calc.Rate=%4iHz", currentRate, time_passed, 1000 / time_passed); |
| 104 |
stringRGBA (renderer, WIDTH/2 - 4*strlen(messageText),HEIGHT-24,messageText,255,255,255,255); |
| 105 |
} |
| 106 |
|
| 107 |
/* Simulate some random delay from drawing, business logic, network interaction, etc. */ |
| 108 |
SDL_Delay(1 + rand() % 10); |
| 109 |
|
| 110 |
/* Display */ |
| 111 |
SDL_RenderPresent(renderer); |
| 112 |
|
| 113 |
/* FPSmanager: Delay to a fixed rate */ |
| 114 |
time_passed = SDL_framerateDelay(fpsm); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
/* ====== Main */ |
| 120 |
|
| 121 |
int main(int argc, char *argv[]) |
| 122 |
{ |
| 123 |
int i; |
| 124 |
SDL_Event event; |
| 125 |
Uint32 then, now, frames; |
| 126 |
FPSmanager fpsm; |
| 127 |
|
| 128 |
/* Initialize test framework */ |
| 129 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
| 130 |
if (!state) { |
| 131 |
return 1; |
| 132 |
} |
| 133 |
|
| 134 |
SDL_Log("SDL2_gfx %i.%i.%i: testframerate", SDL2_GFXPRIMITIVES_MAJOR, SDL2_GFXPRIMITIVES_MINOR, SDL2_GFXPRIMITIVES_MICRO); |
| 135 |
SDL_Log("Platform: %s", SDL_GetPlatform()); |
| 136 |
|
| 137 |
for (i = 1; i < argc;) { |
| 138 |
int consumed; |
| 139 |
|
| 140 |
consumed = SDLTest_CommonArg(state, i); |
| 141 |
if (consumed == 0) { |
| 142 |
consumed = -1; |
| 143 |
if (SDL_strcasecmp(argv[i], "--x") == 0) { |
| 144 |
if (argv[i + 1]) { |
| 145 |
x = SDL_atoi(argv[i + 1]); |
| 146 |
if (x < 0) x = 0; |
| 147 |
if (x >= WIDTH) x = WIDTH - 1; |
| 148 |
consumed = 2; |
| 149 |
} |
| 150 |
} |
| 151 |
else if (SDL_strcasecmp(argv[i], "--y") == 0) { |
| 152 |
if (argv[i + 1]) { |
| 153 |
y = SDL_atoi(argv[i + 1]); |
| 154 |
if (y < 0) y = 0; |
| 155 |
if (y >= HEIGHT) y = HEIGHT - 1; |
| 156 |
consumed = 2; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (consumed < 0) { |
| 162 |
SDL_Log("Usage: %s %s [--x #] [--y #]\n", |
| 163 |
argv[0], SDLTest_CommonUsage(state)); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
i += consumed; |
| 167 |
} |
| 168 |
if (!SDLTest_CommonInit(state)) { |
| 169 |
return 2; |
| 170 |
} |
| 171 |
|
| 172 |
/* Create the windows and initialize the renderers */ |
| 173 |
for (i = 0; i < state->num_windows; ++i) { |
| 174 |
SDL_Renderer *renderer = state->renderers[i]; |
| 175 |
SDL_RendererInfo info; |
| 176 |
SDL_GetRendererInfo(state->renderers[i], &info); |
| 177 |
SDL_Log("Renderer %i: %s %s", i, info.name, (info.flags | SDL_RENDERER_ACCELERATED) ? "(Accelerated)" : ""); |
| 178 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 179 |
SDL_RenderClear(renderer); |
| 180 |
} |
| 181 |
|
| 182 |
/* Initialize random number generator */ |
| 183 |
srand((unsigned int)time(NULL)); |
| 184 |
|
| 185 |
/* Create common message buffer */ |
| 186 |
messageText = (char *)SDL_malloc(1024); |
| 187 |
if (messageText == NULL) { |
| 188 |
return -1; |
| 189 |
} |
| 190 |
|
| 191 |
/* Initialize framerate manager */ |
| 192 |
SDL_initFramerate(&fpsm); |
| 193 |
|
| 194 |
/* Main render loop */ |
| 195 |
frames = 0; |
| 196 |
then = SDL_GetTicks(); |
| 197 |
done = 0; |
| 198 |
while (!done) { |
| 199 |
/* Check for events */ |
| 200 |
++frames; |
| 201 |
while (SDL_PollEvent(&event) && !done) { |
| 202 |
SDLTest_CommonEvent(state, &event, &done); |
| 203 |
} |
| 204 |
|
| 205 |
/* Do all the drawing work */ |
| 206 |
Draw(state->renderers[0], &fpsm); |
| 207 |
} |
| 208 |
|
| 209 |
/* Print out some timing information */ |
| 210 |
now = SDL_GetTicks(); |
| 211 |
if (now > then) { |
| 212 |
double fps = ((double) frames * 1000) / (now - then); |
| 213 |
SDL_Log("%2.2f frames per second\n", fps); |
| 214 |
} |
| 215 |
|
| 216 |
/* Free common message buffer */ |
| 217 |
if (messageText) { |
| 218 |
free(messageText); |
| 219 |
} |
| 220 |
|
| 221 |
/* Shutdown SDL */ |
| 222 |
SDLTest_CommonQuit(state); |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|