1 /*
2 
3 TestShrink.c: test program for shrink routines
4 
5 (C) A. Schiffler, July 2006, zlib License
6 
7 */
8 
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include "SDL.h"
14 
15 #ifdef WIN32
16 #include <windows.h>
17 #include "SDL_rotozoom.h"
18 #else
19 #include "SDL/SDL_rotozoom.h"
20 #endif
21 
HandleEvent()22 void HandleEvent()
23 {
24 	SDL_Event event;
25 
26 	/* Check for events */
27 	while ( SDL_PollEvent(&event) ) {
28 		switch (event.type) {
29 			 case SDL_KEYDOWN:
30 			 case SDL_QUIT:
31 				 exit(0);
32 				 break;
33 		}
34 	}
35 }
36 
ClearScreen(SDL_Surface * screen)37 void ClearScreen(SDL_Surface *screen)
38 {
39 	int i;
40 	/* Set the screen to black */
41 	if ( SDL_LockSurface(screen) == 0 ) {
42 		Uint8 *pixels;
43 		pixels = (Uint8 *)screen->pixels;
44 		for ( i=0; i<screen->h; ++i ) {
45 			memset(pixels, 0,
46 				screen->w*screen->format->BytesPerPixel);
47 			pixels += screen->pitch;
48 		}
49 		SDL_UnlockSurface(screen);
50 	}
51 }
52 
53 
ShrinkPicture(SDL_Surface * screen,SDL_Surface * picture)54 void ShrinkPicture (SDL_Surface *screen, SDL_Surface *picture)
55 {
56 	SDL_Surface *shrink_picture;
57 	SDL_Rect dest;
58 	int factorx, factory;
59 
60 	for (factorx=1; factorx<6; factorx++) {
61 		for (factory=1; factory<6; factory++) {
62 
63 			HandleEvent();
64 
65 			ClearScreen(screen);
66 			if ((shrink_picture=shrinkSurface (picture, factorx, factory))!=NULL) {
67 				dest.x = (screen->w - shrink_picture->w)/2;;
68 				dest.y = (screen->h - shrink_picture->h)/2;
69 				dest.w = shrink_picture->w;
70 				dest.h = shrink_picture->h;
71 				if ( SDL_BlitSurface(shrink_picture, NULL, screen, &dest) < 0 ) {
72 					fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
73 					break;
74 				}
75 				SDL_FreeSurface(shrink_picture);
76 			}
77 
78 			/* Display by flipping screens */
79 			SDL_Flip(screen);
80 
81 			/* Pause for 0.25 sec */
82 			SDL_Delay(250);
83 
84 		}
85 	}
86 
87 }
88 
89 
Draw(SDL_Surface * screen,int start)90 void Draw (SDL_Surface *screen, int start)
91 {
92 	SDL_Surface *picture, *picture_again;
93 	char *bmpfile;
94 
95 	/* --------- 8 bit test -------- */
96 
97 	if (start<=1) {
98 
99 		/* Message */
100 		fprintf (stderr,"Loading 8bit square image\n");
101 
102 		/* Load the image into a surface */
103 		bmpfile = "sample8-box.bmp";
104 		fprintf(stderr, "Loading picture: %s\n", bmpfile);
105 		picture = SDL_LoadBMP(bmpfile);
106 		if ( picture == NULL ) {
107 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
108 			return;
109 		}
110 
111 
112 		fprintf (stderr,"1. shrink 8bit\n");
113 		ShrinkPicture(screen,picture);
114 
115 		/* Free the picture */
116 		SDL_FreeSurface(picture);
117 
118 	}
119 
120 	if (start<=2) {
121 
122 		/* Message */
123 		fprintf (stderr,"Loading 8bit image\n");
124 
125 		/* Load the image into a surface */
126 		bmpfile = "sample8.bmp";
127 		fprintf(stderr, "Loading picture: %s\n", bmpfile);
128 		picture = SDL_LoadBMP(bmpfile);
129 		if ( picture == NULL ) {
130 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
131 			return;
132 		}
133 
134 
135 		fprintf (stderr,"1. shrink 8bit\n");
136 		ShrinkPicture(screen,picture);
137 
138 		/* Free the picture */
139 		SDL_FreeSurface(picture);
140 
141 	}
142 
143 	/* -------- 24 bit test --------- */
144 
145 
146 	if (start<=3) {
147 
148 		/* Message */
149 		fprintf (stderr,"Loading 24bit square image\n");
150 
151 		/* Load the image into a surface */
152 		bmpfile = "sample24-box.bmp";
153 		fprintf(stderr, "Loading picture: %s\n", bmpfile);
154 		picture = SDL_LoadBMP(bmpfile);
155 		if ( picture == NULL ) {
156 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
157 			return;
158 		}
159 
160 		fprintf (stderr,"2. shrink 24bit\n");
161 		ShrinkPicture(screen,picture);
162 
163 		/* Free the picture */
164 		SDL_FreeSurface(picture);
165 
166 	}
167 
168 	if (start<=4) {
169 
170 		/* Message */
171 		fprintf (stderr,"Loading 24bit image\n");
172 
173 		/* Load the image into a surface */
174 		bmpfile = "sample24.bmp";
175 		fprintf(stderr, "Loading picture: %s\n", bmpfile);
176 		picture = SDL_LoadBMP(bmpfile);
177 		if ( picture == NULL ) {
178 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
179 			return;
180 		}
181 
182 		fprintf (stderr,"2. shrink 24bit\n");
183 		ShrinkPicture(screen,picture);
184 
185 		/* Free the picture */
186 		SDL_FreeSurface(picture);
187 
188 	}
189 
190 	/* -------- 32 bit test --------- */
191 
192 	if (start<=5) {
193 
194 		/* Message */
195 		fprintf (stderr,"Loading 24bit square image\n");
196 
197 		/* Load the image into a surface */
198 		bmpfile = "sample24-box.bmp";
199 		fprintf(stderr, "Loading picture: %s\n", bmpfile);
200 		picture = SDL_LoadBMP(bmpfile);
201 		if ( picture == NULL ) {
202 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
203 			return;
204 		}
205 
206 		/* New source surface is 32bit with defined RGBA ordering */
207 		/* Much faster to do this once rather than the routine on the fly */
208 		fprintf (stderr,"Converting 24bit image into 32bit RGBA surface ...\n");
209 		picture_again = SDL_CreateRGBSurface(SDL_SWSURFACE, picture->w, picture->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
210 		SDL_BlitSurface(picture,NULL,picture_again,NULL);
211 
212 		/* Message */
213 		fprintf (stderr,"3. shrink 32bit \n");
214 		ShrinkPicture(screen,picture_again);
215 
216 		/* Free the picture2 */
217 		SDL_FreeSurface(picture_again);
218 		SDL_FreeSurface(picture);
219 
220 	}
221 
222 	if (start<=6) {
223 
224 		/* Message */
225 		fprintf (stderr,"Loading 24bit image\n");
226 
227 		/* Load the image into a surface */
228 		bmpfile = "sample24.bmp";
229 		fprintf(stderr, "Loading picture: %s\n", bmpfile);
230 		picture = SDL_LoadBMP(bmpfile);
231 		if ( picture == NULL ) {
232 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
233 			return;
234 		}
235 
236 		/* New source surface is 32bit with defined RGBA ordering */
237 		/* Much faster to do this once rather than the routine on the fly */
238 		fprintf (stderr,"Converting 24bit image into 32bit RGBA surface ...\n");
239 		picture_again = SDL_CreateRGBSurface(SDL_SWSURFACE, picture->w, picture->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
240 		SDL_BlitSurface(picture,NULL,picture_again,NULL);
241 
242 		/* Message */
243 		fprintf (stderr,"3. shrink 32bit \n");
244 		ShrinkPicture(screen,picture_again);
245 
246 		/* Free the picture2 */
247 		SDL_FreeSurface(picture_again);
248 		SDL_FreeSurface(picture);
249 
250 	}
251 
252 	return;
253 }
254 
main(int argc,char * argv[])255 int main ( int argc, char *argv[] )
256 {
257 	SDL_Surface *screen;
258 	int w, h;
259 	int desired_bpp;
260 	Uint32 video_flags;
261 	int start;
262 
263 	/* Title */
264 	fprintf (stderr,"SDL_rotozoom test\n");
265 
266 	/* Set default options and check command-line */
267 	w = 640;
268 	h = 480;
269 	desired_bpp = 0;
270 	video_flags = 0;
271 	start = 1;
272 	while ( argc > 1 ) {
273 		if ( strcmp(argv[1], "-start") == 0 ) {
274 			if ( argv[2] && ((start = atoi(argv[2])) > 0) ) {
275 				argv += 2;
276 				argc -= 2;
277 			} else {
278 				fprintf(stderr,
279 					"The -start option requires an argument\n");
280 				exit(1);
281 			}
282 		} else
283 			if ( strcmp(argv[1], "-width") == 0 ) {
284 				if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
285 					argv += 2;
286 					argc -= 2;
287 				} else {
288 					fprintf(stderr,
289 						"The -width option requires an argument\n");
290 					exit(1);
291 				}
292 			} else
293 				if ( strcmp(argv[1], "-height") == 0 ) {
294 					if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
295 						argv += 2;
296 						argc -= 2;
297 					} else {
298 						fprintf(stderr,
299 							"The -height option requires an argument\n");
300 						exit(1);
301 					}
302 				} else
303 					if ( strcmp(argv[1], "-bpp") == 0 ) {
304 						if ( argv[2] ) {
305 							desired_bpp = atoi(argv[2]);
306 							argv += 2;
307 							argc -= 2;
308 						} else {
309 							fprintf(stderr,
310 								"The -bpp option requires an argument\n");
311 							exit(1);
312 						}
313 					} else
314 						if ( strcmp(argv[1], "-warp") == 0 ) {
315 							video_flags |= SDL_HWPALETTE;
316 							argv += 1;
317 							argc -= 1;
318 						} else
319 							if ( strcmp(argv[1], "-hw") == 0 ) {
320 								video_flags |= SDL_HWSURFACE;
321 								argv += 1;
322 								argc -= 1;
323 							} else
324 								if ( strcmp(argv[1], "-fullscreen") == 0 ) {
325 									video_flags |= SDL_FULLSCREEN;
326 									argv += 1;
327 									argc -= 1;
328 								} else
329 									if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "--help") == 0)) {
330 										printf ("Usage:\n");
331 										printf (" -start #	Set starting test number (1=8bit, 3=24bit, 5=32bit)\n");
332 										printf (" -width #	Screen width (Default: %i)\n",w);
333 										printf (" -height #	Screen height (Default: %i)\n",h);
334 										printf (" -bpp #	Screen bpp\n");
335 										printf (" -warp		Enable hardware palette\n");
336 										printf (" -hw		Enable hardware surface\n");
337 										printf (" -fullscreen	Enable fullscreen mode\n");
338 
339 									} else
340 										break;
341 	}
342 
343 	/* Force double buffering */
344 	video_flags |= SDL_DOUBLEBUF;
345 
346 	/* Initialize SDL */
347 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
348 		fprintf(stderr,
349 			"Couldn't initialize SDL: %s\n", SDL_GetError());
350 		exit(1);
351 	}
352 	atexit(SDL_Quit);			/* Clean up on exit */
353 
354 	/* Initialize the display */
355 	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
356 	if ( screen == NULL ) {
357 		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
358 			w, h, desired_bpp, SDL_GetError());
359 		exit(1);
360 	}
361 
362 	/* Show some info */
363 	printf("Set %dx%dx%d mode\n",
364 		screen->w, screen->h, screen->format->BitsPerPixel);
365 	printf("Video surface located in %s memory.\n",
366 		(screen->flags&SDL_HWSURFACE) ? "video" : "system");
367 
368 	/* Check for double buffering */
369 	if ( screen->flags & SDL_DOUBLEBUF ) {
370 		printf("Double-buffering enabled - good!\n");
371 	}
372 
373 	/* Set the window manager title bar */
374 	SDL_WM_SetCaption("SDL_rotozoom shrink test", "shrink");
375 
376 	/* Do all the drawing work */
377 	Draw (screen, start);
378 
379 	return(0);
380 }
381