1 /*
2 
3 TestGfxBlit.c: test program to check custom RGBA blitter
4 
5 (C) A. Schiffler, December 2007, zlib License
6 
7 */
8 
9 #ifdef WIN32
10 #include <windows.h>
11 #endif
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <time.h>
17 
18 #include "SDL.h"
19 
20 #ifdef WIN32
21 #include <windows.h>
22 #include "SDL_framerate.h"
23 #include "SDL_gfxPrimitives.h"
24 #include "SDL_gfxBlitFunc.h"
25 #else
26 #include "SDL/SDL_framerate.h"
27 #include "SDL/SDL_gfxPrimitives.h"
28 #include "SDL/SDL_gfxBlitFunc.h"
29 #endif
30 
31 
HandleEvent()32 void HandleEvent()
33 {
34 	SDL_Event event;
35 
36 	/* Check for events */
37 	while ( SDL_PollEvent(&event) ) {
38 		switch (event.type) {
39 			 case SDL_KEYDOWN:
40 			 case SDL_QUIT:
41 				 exit(0);
42 				 break;
43 		}
44 	}
45 }
46 
ClearScreen(SDL_Surface * screen)47 void ClearScreen(SDL_Surface *screen)
48 {
49 	int i;
50 	/* Set the screen to gray */
51 	if ( SDL_LockSurface(screen) == 0 ) {
52 		Uint32 gray1, gray2;
53 		Uint8 *pixels;
54 		gray1 = SDL_MapRGB(screen->format, 64, 64, 64);
55 		gray2 = SDL_MapRGB(screen->format, 32, 32, 32);
56 		pixels = (Uint8 *)screen->pixels;
57 		for ( i=0; i<screen->h; ++i ) {
58 			if ((i % 32)<16) {
59 				memset(pixels, gray1, screen->w*screen->format->BytesPerPixel);
60 			} else {
61 				memset(pixels, gray2, screen->w*screen->format->BytesPerPixel);
62 		 }
63 			pixels += screen->pitch;
64 		}
65 		SDL_UnlockSurface(screen);
66 	}
67 }
68 
69 
Draw(SDL_Surface * screen)70 void Draw(SDL_Surface *screen)
71 {
72 	int rate,x,y,s;
73 	SDL_Rect dest,clip;
74 	SDL_Surface *texture_image;
75 	SDL_Surface *texture_target1;
76 	SDL_Surface *texture_target2;
77 	FPSmanager fpsm;
78 	Uint32 rmask, gmask, bmask, amask;
79 	int width_half = screen->w/2;
80 	int height_half = screen->h/2;
81 	Uint32 text_color = 0xffffffff;
82 
83 	/* Define masks for 32bit surface */
84 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
85 	rmask = 0xff000000;
86 	gmask = 0x00ff0000;
87 	bmask = 0x0000ff00;
88 	amask = 0x000000ff;
89 #else
90 	rmask = 0x000000ff;
91 	gmask = 0x0000ff00;
92 	bmask = 0x00ff0000;
93 	amask = 0xff000000;
94 #endif
95 
96 	/* Create semi-transparent textured surface */
97 	s=64;
98 	texture_image = SDL_DisplayFormatAlpha(SDL_CreateRGBSurface(SDL_SWSURFACE, s, s, 32, rmask, gmask, bmask, amask));
99 	/* Add some color */
100 	boxRGBA(texture_image, 0,  0, s/2, s/2, 255, 0, 0, 255);
101 	boxRGBA(texture_image, s/2, 0, s, s/2, 0, 255, 0, 255);
102 	boxRGBA(texture_image, 0, s/2, s/2, s, 0, 0, 255, 255);
103 	boxRGBA(texture_image, s/2, s/2, s, s, 255, 255, 255, 255);
104 	/* Make 75%-transparent */
105 	SDL_gfxSetAlpha(texture_image, 96);
106 	/* Set alpha channel use to per-pixel blending */
107 	SDL_SetAlpha(texture_image, SDL_SRCALPHA, 255);
108 
109 	/* Create an all transparent surface */
110 	texture_target1 = SDL_DisplayFormatAlpha(SDL_CreateRGBSurface(SDL_SWSURFACE, 256, 256, 32, rmask, gmask, bmask, amask));
111 	/* Make 75%-transparent */
112 	SDL_gfxSetAlpha(texture_target1, 64);
113 	/* Set alpha channel use to per-pixel blending */
114 	SDL_SetAlpha(texture_target1, SDL_SRCALPHA, 255);
115 
116 	/* Create an all transparent surface (2) */
117 	texture_target2 = SDL_DisplayFormatAlpha(SDL_CreateRGBSurface(SDL_SWSURFACE, 256, 256, 32, rmask, gmask, bmask, amask));
118 	/* Make 75%-transparent */
119 	SDL_gfxSetAlpha(texture_target2, 64);
120 	/* Set alpha channel use to per-pixel blending */
121 	SDL_SetAlpha(texture_target2, SDL_SRCALPHA, 255);
122 
123 	/* Define clipping region for left box */
124 	clip.x = width_half-256-10 ;
125 	clip.y = height_half-256/2 ;
126 	clip.w = 256;
127 	clip.h = 256;
128 
129 	/* Initialize Framerate manager */
130 	SDL_initFramerate(&fpsm);
131 
132 	/* Set/switch framerate */
133 	rate=5;
134 	SDL_setFramerate(&fpsm,rate);
135 
136 	/* --- Drawing loop */
137 	while (1) {
138 
139 		/* Event handler */
140 		HandleEvent();
141 
142 		/* Black screen */
143 		ClearScreen(screen);
144 
145 		/* Random position of new texture */
146 		x=(rand() % (256+2*s))-s;
147 		y=(rand() % (256+2*s))-s;
148 
149 		/* Same for comparison texture */
150 		dest.x = x;
151 		dest.y = y;
152 		dest.w = texture_image->w;
153 		dest.h = texture_image->h;
154 		SDL_BlitSurface(texture_image, NULL, texture_target1, &dest);
155 
156 		/* Blit image into the target using custom Blit function. */
157 		dest.x = x;
158 		dest.y = y;
159 		dest.w = texture_image->w;
160 		dest.h = texture_image->h;
161 		SDL_gfxBlitRGBA(texture_image, NULL, texture_target2, &dest);
162 
163 		/* Draw comparison target on screen (left) */
164 		dest.x = width_half-256-10;
165 		dest.y = height_half-256/2;
166 		dest.w = 256;
167 		dest.h = 256;
168 		SDL_BlitSurface(texture_target1, NULL, screen, &dest);
169 
170 		/* Draw combiner target on screen (right) */
171 		dest.x = width_half+10;
172 		dest.y = height_half-256/2;
173 		dest.w = 256;
174 		dest.h = 256;
175 		SDL_BlitSurface(texture_target2, NULL, screen, &dest);
176 
177 		/* Draw some frames with titles */
178 		rectangleColor(screen, width_half-256-10-1, height_half-256/2-1, width_half-256-10-1+257, height_half-256/2-1+257,  text_color);
179 		rectangleColor(screen, width_half+10-1, height_half-256/2-1, width_half+10-1+257, height_half-256/2-1+257, text_color);
180 		stringColor(screen, width_half-256-10-1, height_half-256/2-1-36, "     SDL Standard Blitter     ", text_color);
181 		stringColor(screen, width_half-256-10-1, height_half-256/2-1-24, "Image    --sdlBlit-->  Target1", text_color);
182 		stringColor(screen, width_half-256-10-1, height_half-256/2-1-12, "Target1  --sdlBlit-->  Screen", text_color);
183 		stringColor(screen, width_half+10-1, height_half-256/2-1-36, " SDL_gfx Compositing Blitter", text_color);
184 		stringColor(screen, width_half+10-1, height_half-256/2-1-24, "Image    --gfxBlit-->  Target2", text_color);
185 		stringColor(screen, width_half+10-1, height_half-256/2-1-12, "Target2  --sdlBlit-->  Screen", text_color);
186 
187 		stringColor(screen, width_half-256-10-1, height_half-256/2-1-60, "gfxBlitRGBA Demo: Target1/2 A=64 (25%), Image A=96 (37%)", text_color);
188 
189 		/* Display by flipping screens */
190 		SDL_Flip(screen);
191 
192 		/* Delay to fix rate */
193 		SDL_framerateDelay(&fpsm);
194 	}
195 }
196 
197 /* ======== */
198 
main(int argc,char * argv[])199 int main(int argc, char *argv[])
200 {
201 	SDL_Surface *screen;
202 	int w, h;
203 	int desired_bpp;
204 	Uint32 video_flags;
205 
206 	/* Title */
207 	fprintf (stderr,"gfxBlitRGBA test\n");
208 
209 	/* Set default options and check command-line */
210 	w = 640;
211 	h = 480;
212 	desired_bpp = 0;
213 	video_flags = 0;
214 	while ( argc > 1 ) {
215 		if ( strcmp(argv[1], "-width") == 0 ) {
216 			if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
217 				argv += 2;
218 				argc -= 2;
219 			} else {
220 				fprintf(stderr,
221 					"The -width option requires an argument\n");
222 				exit(1);
223 			}
224 		} else
225 			if ( strcmp(argv[1], "-height") == 0 ) {
226 				if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
227 					argv += 2;
228 					argc -= 2;
229 				} else {
230 					fprintf(stderr,
231 						"The -height option requires an argument\n");
232 					exit(1);
233 				}
234 			} else
235 				if ( strcmp(argv[1], "-bpp") == 0 ) {
236 					if ( argv[2] ) {
237 						desired_bpp = atoi(argv[2]);
238 						argv += 2;
239 						argc -= 2;
240 					} else {
241 						fprintf(stderr,
242 							"The -bpp option requires an argument\n");
243 						exit(1);
244 					}
245 				} else
246 					if ( strcmp(argv[1], "-warp") == 0 ) {
247 						video_flags |= SDL_HWPALETTE;
248 						argv += 1;
249 						argc -= 1;
250 					} else
251 						if ( strcmp(argv[1], "-hw") == 0 ) {
252 							video_flags |= SDL_HWSURFACE;
253 							argv += 1;
254 							argc -= 1;
255 						} else
256 							if ( strcmp(argv[1], "-fullscreen") == 0 ) {
257 								video_flags |= SDL_FULLSCREEN;
258 								argv += 1;
259 								argc -= 1;
260 							} else
261 								break;
262 	}
263 
264 	/* Force double buffering */
265 	video_flags |= SDL_DOUBLEBUF;
266 
267 	/* Initialize SDL */
268 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
269 		fprintf(stderr,
270 			"Couldn't initialize SDL: %s\n", SDL_GetError());
271 		exit(1);
272 	}
273 	atexit(SDL_Quit);			/* Clean up on exit */
274 
275 	/* Initialize the display */
276 	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
277 	if ( screen == NULL ) {
278 		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
279 			w, h, desired_bpp, SDL_GetError());
280 		exit(1);
281 	}
282 
283 	/* Show some info */
284 	printf("Set %dx%dx%d mode\n",
285 		screen->w, screen->h, screen->format->BitsPerPixel);
286 	printf("Video surface located in %s memory.\n",
287 		(screen->flags&SDL_HWSURFACE) ? "video" : "system");
288 
289 	/* Check for double buffering */
290 	if ( screen->flags & SDL_DOUBLEBUF ) {
291 		printf("Double-buffering enabled - good!\n");
292 	}
293 
294 	/* Set the window manager title bar */
295 	SDL_WM_SetCaption("gfxBlitRGBA", "gfxBlitRGBA");
296 
297 	/* Do all the drawing work */
298 	Draw (screen);
299 
300 	return(0);
301 }
302