1 /*
2
3 TestFramerate.c: test/sample program for framerate manager
4
5 (C) A. Schiffler, August 2001, zlib License
6
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <math.h>
13 #include <time.h>
14
15 #include "SDL.h"
16
17 #ifdef WIN32
18 #include <windows.h>
19 #include "SDL_framerate.h"
20 #include "SDL_gfxPrimitives.h"
21 #else
22 #include "SDL/SDL_framerate.h"
23 #include "SDL/SDL_gfxPrimitives.h"
24 #endif
25
26 #define WIDTH 640
27 #define HEIGHT 480
28
HandleEvent()29 void HandleEvent()
30 {
31 SDL_Event event;
32
33 /* Check for events */
34 while ( SDL_PollEvent(&event) ) {
35 switch (event.type) {
36 case SDL_KEYDOWN:
37 case SDL_QUIT:
38 exit(0);
39 break;
40 }
41 }
42 }
43
ClearScreen(SDL_Surface * screen)44 void ClearScreen(SDL_Surface *screen)
45 {
46 int i;
47 /* Set the screen to black */
48 if ( SDL_LockSurface(screen) == 0 ) {
49 Uint32 black;
50 Uint8 *pixels;
51 black = SDL_MapRGB(screen->format, 0, 0, 0);
52 pixels = (Uint8 *)screen->pixels;
53 for ( i=0; i<screen->h; ++i ) {
54 memset(pixels, black,
55 screen->w*screen->format->BytesPerPixel);
56 pixels += screen->pitch;
57 }
58 SDL_UnlockSurface(screen);
59 }
60 }
61
Draw(SDL_Surface * screen)62 void Draw(SDL_Surface *screen)
63 {
64 int i,rate,x,y,dx,dy,r,g,b;
65 Uint32 time_passed = 0;
66 FPSmanager fpsm;
67 char message[64];
68 char message2[64];
69
70 /* Initialize variables */
71 srand((int)time(NULL));
72 i=0;
73 x=screen->w/2;
74 y=screen->h/2;
75 dx=7;
76 dy=5;
77 r=g=b=255;
78
79 SDL_initFramerate(&fpsm);
80
81 rate = SDL_getFramerate(&fpsm);
82 sprintf(message, "Framerate set to %i Hz ...",rate);
83
84 while (1) {
85
86 /* Set/switch framerate */
87 i -= 1;
88 if (i<0) {
89 /* Set new rate */
90 rate=5+5*(rand() % 10);
91 SDL_setFramerate(&fpsm,rate);
92 sprintf(message, "Framerate set to %i Hz ...",rate);
93
94 /* New timeout */
95 i=2*rate;
96 /* New Color */
97 r=rand() & 255;
98 g=rand() & 255;
99 b=rand() & 255;
100 }
101
102 /* Black screen */
103 ClearScreen(screen);
104
105 /* Messages */
106 stringRGBA (screen, WIDTH/2-4*strlen(message),HEIGHT-12,message,255,255,255,255);
107 if (time_passed > 0) {
108 sprintf(message2, "Delay is %i ms / Measured framerate %i Hz ...", time_passed, 1000 / time_passed);
109 stringRGBA (screen, WIDTH/2-4*strlen(message2),HEIGHT-24,message2,255,255,255,255);
110 }
111
112 /* Move */
113 x += dx;
114 y += dy;
115
116 /* Reflect */
117 if ((x<0) || (x>screen->w)) { dx=-dx; }
118 if ((y<0) || (y>screen->h)) { dy=-dy; }
119
120 /* Draw */
121 filledCircleRGBA (screen,x,y,30,r,g,b,255);
122 circleRGBA(screen,x,y,30,255,255,255,255);
123
124 /* Display by flipping screens */
125 SDL_Flip(screen);
126
127 /* Check events */
128 HandleEvent();
129
130 /* Delay to fix rate */
131 time_passed = SDL_framerateDelay(&fpsm);
132 }
133 }
134
main(int argc,char * argv[])135 int main(int argc, char *argv[])
136 {
137 SDL_Surface *screen;
138 Uint8 video_bpp;
139 Uint32 videoflags;
140 char title[64];
141
142 /* Generate title strings */
143 sprintf (title, "TestFramerate - v%i.%i.%i", SDL_GFXPRIMITIVES_MAJOR, SDL_GFXPRIMITIVES_MINOR, SDL_GFXPRIMITIVES_MICRO);
144
145 /* Initialize SDL */
146 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
147 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
148 exit(1);
149 }
150 atexit(SDL_Quit);
151
152 video_bpp = 32;
153 videoflags = SDL_SWSURFACE | SDL_SRCALPHA | SDL_RESIZABLE;
154 while ( argc > 1 ) {
155 --argc;
156 if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
157 video_bpp = atoi(argv[argc]);
158 --argc;
159 } else
160 if ( strcmp(argv[argc], "-hw") == 0 ) {
161 videoflags |= SDL_HWSURFACE;
162 } else
163 if ( strcmp(argv[argc], "-warp") == 0 ) {
164 videoflags |= SDL_HWPALETTE;
165 } else
166 if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
167 videoflags |= SDL_FULLSCREEN;
168 } else {
169 fprintf(stderr,
170 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
171 argv[0]);
172 exit(1);
173 }
174 }
175
176 /* Force double buffering */
177 videoflags |= SDL_DOUBLEBUF;
178
179 /* Set video mode */
180 if ( (screen=SDL_SetVideoMode(WIDTH, HEIGHT, video_bpp, videoflags)) == NULL ) {
181 fprintf(stderr, "Couldn't set %ix%i %i bpp video mode: %s\n",WIDTH,HEIGHT,video_bpp,SDL_GetError());
182 exit(2);
183 }
184
185 /* Use alpha blending */
186 SDL_SetAlpha(screen, SDL_SRCALPHA, 0);
187
188 /* Set title for window */
189 SDL_WM_SetCaption(title, title);
190
191 /* Do all the drawing work */
192 Draw (screen);
193
194 return(0);
195 }
196