1 /*
2
3 TestGfxTexture.c: test program for textured polygon routine
4 (Contributed by Kees Jongenburger)
5
6 (C) A. Schiffler, December 2006, zlib license
7
8 */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <time.h>
14
15 #include "SDL.h"
16
17 #ifdef WIN32
18 #include <windows.h>
19 #include "SDL_gfxPrimitives.h"
20 #include "SDL_framerate.h"
21 #else
22 #include "SDL/SDL_gfxPrimitives.h"
23 #include "SDL/SDL_framerate.h"
24 #endif
25
HandleEvent()26 void HandleEvent()
27 {
28 SDL_Event event;
29
30 /* Check for events */
31 while ( SDL_PollEvent(&event) ) {
32 switch (event.type) {
33 case SDL_KEYDOWN:
34 case SDL_QUIT:
35 exit(0);
36 break;
37 }
38 }
39 }
40
ClearScreen(SDL_Surface * screen)41 void ClearScreen(SDL_Surface *screen)
42 {
43 int i;
44 /* Set the screen to black */
45 if ( SDL_LockSurface(screen) == 0 ) {
46 Uint32 black;
47 Uint8 *pixels;
48 black = SDL_MapRGB(screen->format, 0, 0, 0);
49 pixels = (Uint8 *)screen->pixels;
50 for ( i=0; i<screen->h; ++i ) {
51 memset(pixels, black,
52 screen->w*screen->format->BytesPerPixel);
53 pixels += screen->pitch;
54 }
55 SDL_UnlockSurface(screen);
56 }
57 }
58
59 #define NUM_POINTS 150
60
Draw(SDL_Surface * screen)61 void Draw(SDL_Surface *screen)
62 {
63 int i,rate,x,y,dx,dy;
64 int psize = NUM_POINTS;
65 double sin_start = 0;
66 double sin_amp = 100;
67 Sint16 polygon_x[NUM_POINTS], polygon_y[NUM_POINTS];
68 Sint16 polygon_alpha_x[4], polygon_alpha_y[4];
69 SDL_Surface *texture;
70 SDL_Surface *texture_alpha;
71 FPSmanager fpsm;
72 int width_half = screen->w/2;
73 int height_half = screen->h/2;
74
75 /* Load texture surfaces */
76 texture = SDL_LoadBMP("texture.bmp");
77 texture_alpha = SDL_LoadBMP("texture_alpha.bmp");
78 SDL_SetAlpha(texture_alpha, SDL_SRCALPHA, 128);
79
80 /* Initialize variables */
81 srand((int)time(NULL));
82 i=0;
83 x=width_half;
84 y=height_half;
85 dx=7;
86 dy=5;
87
88 /* Initialize Framerate manager */
89 SDL_initFramerate(&fpsm);
90
91 /* Polygon for blended texture */
92 polygon_alpha_x[0]= 0;
93 polygon_alpha_y[0]= 0;
94 polygon_alpha_x[1]= width_half;
95 polygon_alpha_y[1]= 0;
96 polygon_alpha_x[2]= screen->w*2 / 3;
97 polygon_alpha_y[2]= screen->h;
98 polygon_alpha_x[3]= 0;
99 polygon_alpha_y[3]= screen->h;
100
101 /* Set/switch framerate */
102 rate=25;
103 SDL_setFramerate(&fpsm,rate);
104
105 /* Drawing loop */
106 while (1) {
107
108 /* Generate wave polygon */
109 sin_start++;
110 polygon_x[0]= 0;
111 polygon_y[0]= screen->h;
112 polygon_x[1]= 0;
113 polygon_y[1]= height_half;
114 for (i=2; i < psize -2 ; i++){
115 polygon_x[i]= (screen->w * (i-2)) / (psize -5) ;
116 polygon_y[i]= (Sint16)(sin(sin_start/100) * 200) + height_half - (Sint16)(sin((i + sin_start) / 20) * sin_amp);
117 }
118
119 polygon_x[psize-2]= screen->w;
120 polygon_y[psize-2]= height_half;
121 polygon_x[psize-1]= screen->w;
122 polygon_y[psize-1]= screen->h;
123
124 /* Event handler */
125 HandleEvent();
126
127 /* Black screen */
128 ClearScreen(screen);
129
130 /* Move */
131 x += dx;
132 y += dy;
133
134 /* Reflect */
135 if ((x<0) || (x>screen->w)) { dx=-dx; }
136 if ((y<0) || (y>screen->h)) { dy=-dy; }
137
138 /* Draw */
139 texturedPolygon(screen,polygon_x,polygon_y,psize,texture, -(screen->w * (Sint16)(sin_start-2)) / (psize - 5), -(Sint16)(sin(sin_start/100) * 200));
140 texturedPolygon(screen,polygon_alpha_x,polygon_alpha_y,4,texture_alpha,(Sint16)sin_start,-(Sint16)sin_start);
141
142 /* Display by flipping screens */
143 SDL_Flip(screen);
144
145 /* Delay to fix rate */
146 SDL_framerateDelay(&fpsm);
147 }
148 }
149
150 /* ======== */
151
main(int argc,char * argv[])152 int main ( int argc, char *argv[] )
153 {
154 SDL_Surface *screen;
155 int w, h;
156 int desired_bpp;
157 Uint32 video_flags;
158
159 /* Title */
160 fprintf (stderr,"texturedPolygon test\n");
161
162 /* Set default options and check command-line */
163 w = 640;
164 h = 480;
165 desired_bpp = 0;
166 video_flags = 0;
167 while ( argc > 1 ) {
168 if ( strcmp(argv[1], "-width") == 0 ) {
169 if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
170 argv += 2;
171 argc -= 2;
172 } else {
173 fprintf(stderr,
174 "The -width option requires an argument\n");
175 exit(1);
176 }
177 } else
178 if ( strcmp(argv[1], "-height") == 0 ) {
179 if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
180 argv += 2;
181 argc -= 2;
182 } else {
183 fprintf(stderr,
184 "The -height option requires an argument\n");
185 exit(1);
186 }
187 } else
188 if ( strcmp(argv[1], "-bpp") == 0 ) {
189 if ( argv[2] ) {
190 desired_bpp = atoi(argv[2]);
191 argv += 2;
192 argc -= 2;
193 } else {
194 fprintf(stderr,
195 "The -bpp option requires an argument\n");
196 exit(1);
197 }
198 } else
199 if ( strcmp(argv[1], "-warp") == 0 ) {
200 video_flags |= SDL_HWPALETTE;
201 argv += 1;
202 argc -= 1;
203 } else
204 if ( strcmp(argv[1], "-hw") == 0 ) {
205 video_flags |= SDL_HWSURFACE;
206 argv += 1;
207 argc -= 1;
208 } else
209 if ( strcmp(argv[1], "-fullscreen") == 0 ) {
210 video_flags |= SDL_FULLSCREEN;
211 argv += 1;
212 argc -= 1;
213 } else
214 break;
215 }
216
217 /* Force double buffering */
218 video_flags |= SDL_DOUBLEBUF;
219
220 /* Initialize SDL */
221 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
222 fprintf(stderr,
223 "Couldn't initialize SDL: %s\n", SDL_GetError());
224 exit(1);
225 }
226 atexit(SDL_Quit); /* Clean up on exit */
227
228 /* Initialize the display */
229 screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
230 if ( screen == NULL ) {
231 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
232 w, h, desired_bpp, SDL_GetError());
233 exit(1);
234 }
235
236 /* Show some info */
237 printf("Set %dx%dx%d mode\n",
238 screen->w, screen->h, screen->format->BitsPerPixel);
239 printf("Video surface located in %s memory.\n",
240 (screen->flags&SDL_HWSURFACE) ? "video" : "system");
241
242 /* Check for double buffering */
243 if ( screen->flags & SDL_DOUBLEBUF ) {
244 printf("Double-buffering enabled - good!\n");
245 }
246
247 /* Set the window manager title bar */
248 SDL_WM_SetCaption("texturedPolygon test", "texturedPolygon");
249
250 /* Do all the drawing work */
251 Draw (screen);
252
253 return(0);
254 }
255