2 /* Simple program: Fill a colormap with gray and stripe it down the screen,
3 Then move an alpha valued sprite around the screen.
13 #define FRAME_TICKS (1000/30) /* 30 frames/second */
15 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
16 static void quit(int rc)
22 /* Fill the screen with a gradient */
23 static void FillBackground(SDL_Surface *screen)
31 /* Set the surface pixels and refresh! */
32 if ( SDL_LockSurface(screen) < 0 ) {
33 fprintf(stderr, "Couldn't lock the display surface: %s\n",
37 buffer=(Uint8 *)screen->pixels;
38 if (screen->format->BytesPerPixel!=2) {
39 for ( i=0; i<screen->h; ++i ) {
40 memset(buffer,(i*255)/screen->h, screen->w*screen->format->BytesPerPixel);
41 buffer += screen->pitch;
46 for ( i=0; i<screen->h; ++i ) {
47 gradient=((i*255)/screen->h);
48 color = (Uint16)SDL_MapRGB(screen->format, gradient, gradient, gradient);
49 buffer16=(Uint16*)buffer;
50 for (k=0; k<screen->w; k++)
54 buffer += screen->pitch;
58 SDL_UnlockSurface(screen);
59 SDL_UpdateRect(screen, 0, 0, 0, 0);
62 /* Create a "light" -- a yellowish surface with variable alpha */
63 SDL_Surface *CreateLight(int radius)
65 Uint8 trans, alphamask;
76 /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */
77 /* Note: this isn't any faster than a 32 bit alpha surface */
78 alphamask = 0x0000000F;
79 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 16,
80 0x0000F000, 0x00000F00, 0x000000F0, alphamask);
84 /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */
85 alphamask = 0x000000FF;
86 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 32,
87 0xFF000000, 0x00FF0000, 0x0000FF00, alphamask);
88 if ( light == NULL ) {
89 fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError());
94 /* Fill with a light yellow-orange color */
95 skip = light->pitch-(light->w*light->format->BytesPerPixel);
97 buf = (Uint16 *)light->pixels;
99 buf = (Uint32 *)light->pixels;
101 /* Get a tranparent pixel value - we'll add alpha later */
102 pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0);
103 for ( y=0; y<light->h; ++y ) {
104 for ( x=0; x<light->w; ++x ) {
107 buf += skip; /* Almost always 0, but just in case... */
110 /* Calculate alpha values for the surface. */
112 buf = (Uint16 *)light->pixels;
114 buf = (Uint32 *)light->pixels;
116 for ( y=0; y<light->h; ++y ) {
117 for ( x=0; x<light->w; ++x ) {
118 /* Slow distance formula (from center of light) */
119 xdist = x-(light->w/2);
120 ydist = y-(light->h/2);
121 range = (int)sqrt(xdist*xdist+ydist*ydist);
123 /* Scale distance to range of transparency (0-255) */
124 if ( range > radius ) {
127 /* Increasing transparency with distance */
128 trans = (Uint8)((range*alphamask)/radius);
130 /* Lights are very transparent */
131 addition = (alphamask+1)/8;
132 if ( (int)trans+addition > alphamask ) {
138 /* We set the alpha component as the right N bits */
139 *buf++ |= (255-trans);
141 buf += skip; /* Almost always 0, but just in case... */
143 /* Enable RLE acceleration of this alpha surface */
144 SDL_SetAlpha(light, SDL_SRCALPHA|SDL_RLEACCEL, 0);
150 static Uint32 flashes = 0;
151 static Uint32 flashtime = 0;
153 void FlashLight(SDL_Surface *screen, SDL_Surface *light, int x, int y)
159 /* Easy, center light */
160 position.x = x-(light->w/2);
161 position.y = y-(light->h/2);
162 position.w = light->w;
163 position.h = light->h;
164 ticks1 = SDL_GetTicks();
165 SDL_BlitSurface(light, NULL, screen, &position);
166 ticks2 = SDL_GetTicks();
167 SDL_UpdateRects(screen, 1, &position);
170 /* Update time spend doing alpha blitting */
171 flashtime += (ticks2-ticks1);
174 static int sprite_visible = 0;
175 static SDL_Surface *sprite;
176 static SDL_Surface *backing;
177 static SDL_Rect position;
178 static int x_vel, y_vel;
179 static int alpha_vel;
181 int LoadSprite(SDL_Surface *screen, char *file)
183 SDL_Surface *converted;
185 /* Load the sprite image */
186 sprite = SDL_LoadBMP(file);
187 if ( sprite == NULL ) {
188 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
192 /* Set transparent pixel as the pixel at (0,0) */
193 if ( sprite->format->palette ) {
194 SDL_SetColorKey(sprite, SDL_SRCCOLORKEY,
195 *(Uint8 *)sprite->pixels);
198 /* Convert sprite to video format */
199 converted = SDL_DisplayFormat(sprite);
200 SDL_FreeSurface(sprite);
201 if ( converted == NULL ) {
202 fprintf(stderr, "Couldn't convert background: %s\n",
208 /* Create the background */
209 backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
211 if ( backing == NULL ) {
212 fprintf(stderr, "Couldn't create background: %s\n",
214 SDL_FreeSurface(sprite);
218 /* Convert background to video format */
219 converted = SDL_DisplayFormat(backing);
220 SDL_FreeSurface(backing);
221 if ( converted == NULL ) {
222 fprintf(stderr, "Couldn't convert background: %s\n",
224 SDL_FreeSurface(sprite);
229 /* Set the initial position of the sprite */
230 position.x = (screen->w-sprite->w)/2;
231 position.y = (screen->h-sprite->h)/2;
232 position.w = sprite->w;
233 position.h = sprite->h;
234 x_vel = 0; y_vel = 0;
237 /* We're ready to roll. :) */
241 void AttractSprite(Uint16 x, Uint16 y)
243 x_vel = ((int)x-position.x)/10;
244 y_vel = ((int)y-position.y)/10;
247 void MoveSprite(SDL_Surface *screen, SDL_Surface *light)
252 /* Erase the sprite if it was visible */
253 if ( sprite_visible ) {
254 updates[0] = position;
255 SDL_BlitSurface(backing, NULL, screen, &updates[0]);
257 updates[0].x = 0; updates[0].y = 0;
258 updates[0].w = 0; updates[0].h = 0;
262 /* Since the sprite is off the screen, we can do other drawing
263 without being overwritten by the saved area behind the sprite.
265 if ( light != NULL ) {
268 SDL_GetMouseState(&x, &y);
269 FlashLight(screen, light, x, y);
272 /* Move the sprite, bounce at the wall */
274 if ( (position.x < 0) || (position.x >= screen->w) ) {
279 if ( (position.y < 0) || (position.y >= screen->h) ) {
284 /* Update transparency (fade in and out) */
285 alpha = sprite->format->alpha;
286 if ( (alpha+alpha_vel) < 0 ) {
287 alpha_vel = -alpha_vel;
289 if ( (alpha+alpha_vel) > 255 ) {
290 alpha_vel = -alpha_vel;
292 SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8)(alpha+alpha_vel));
294 /* Save the area behind the sprite */
295 updates[1] = position;
296 SDL_BlitSurface(screen, &updates[1], backing, NULL);
298 /* Blit the sprite onto the screen */
299 updates[1] = position;
300 SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
303 SDL_UpdateRects(screen, 2, updates);
306 void WarpSprite(SDL_Surface *screen, int x, int y)
310 /* Erase, move, Draw, update */
311 updates[0] = position;
312 SDL_BlitSurface(backing, NULL, screen, &updates[0]);
313 position.x = x-sprite->w/2; /* Center about X */
314 position.y = y-sprite->h/2; /* Center about Y */
315 updates[1] = position;
316 SDL_BlitSurface(screen, &updates[1], backing, NULL);
317 updates[1] = position;
318 SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
319 SDL_UpdateRects(screen, 2, updates);
322 int main(int argc, char *argv[])
324 const SDL_VideoInfo *info;
333 Uint32 ticks, lastticks;
337 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
338 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
342 /* Alpha blending doesn't work well at 8-bit color */
351 info = SDL_GetVideoInfo();
352 if ( info->vfmt->BitsPerPixel > 8 ) {
353 video_bpp = info->vfmt->BitsPerPixel;
356 fprintf(stderr, "forced 16 bpp mode\n");
358 videoflags = SDL_SWSURFACE;
359 for ( i = 1; argv[i]; ++i ) {
360 if ( strcmp(argv[i], "-bpp") == 0 ) {
361 video_bpp = atoi(argv[++i]);
364 fprintf(stderr, "forced 16 bpp mode\n");
367 if ( strcmp(argv[i], "-hw") == 0 ) {
368 videoflags |= SDL_HWSURFACE;
370 if ( strcmp(argv[i], "-warp") == 0 ) {
371 videoflags |= SDL_HWPALETTE;
373 if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) {
376 if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) {
379 if ( strcmp(argv[i], "-resize") == 0 ) {
380 videoflags |= SDL_RESIZABLE;
382 if ( strcmp(argv[i], "-noframe") == 0 ) {
383 videoflags |= SDL_NOFRAME;
385 if ( strcmp(argv[i], "-fullscreen") == 0 ) {
386 videoflags |= SDL_FULLSCREEN;
389 "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
396 if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) {
397 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
398 w, h, video_bpp, SDL_GetError());
401 FillBackground(screen);
403 /* Create the light */
404 light = CreateLight(82);
405 if ( light == NULL ) {
409 /* Load the sprite */
410 if ( LoadSprite(screen, "icon.bmp") < 0 ) {
411 SDL_FreeSurface(light);
415 /* Print out information about our surfaces */
416 printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
417 if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
418 printf("Screen is in video memory\n");
420 printf("Screen is in system memory\n");
422 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
423 printf("Screen has double-buffering enabled\n");
425 if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
426 printf("Sprite is in video memory\n");
428 printf("Sprite is in system memory\n");
431 /* Run a sample blit to trigger blit acceleration */
432 MoveSprite(screen, NULL);
433 if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
434 printf("Sprite blit uses hardware alpha acceleration\n");
436 printf("Sprite blit dosn't uses hardware alpha acceleration\n");
439 /* Set a clipping rectangle to clip the outside edge of the screen */
443 clip.w = screen->w-(2*32);
444 clip.h = screen->h-(2*32);
445 SDL_SetClipRect(screen, &clip);
448 /* Wait for a keystroke */
449 lastticks = SDL_GetTicks();
453 /* Update the frame -- move the sprite */
454 if ( mouse_pressed ) {
455 MoveSprite(screen, light);
458 MoveSprite(screen, NULL);
461 /* Slow down the loop to 30 frames/second */
462 ticks = SDL_GetTicks();
463 if ( (ticks-lastticks) < FRAME_TICKS ) {
464 #ifdef CHECK_SLEEP_GRANULARITY
465 fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks));
467 SDL_Delay(FRAME_TICKS-(ticks-lastticks));
468 #ifdef CHECK_SLEEP_GRANULARITY
469 fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
474 /* Check for events */
475 while ( SDL_PollEvent(&event) ) {
476 switch (event.type) {
477 case SDL_VIDEORESIZE:
478 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
480 FillBackground(screen);
483 /* Attract sprite while mouse is held down */
484 case SDL_MOUSEMOTION:
485 if (event.motion.state != 0) {
486 AttractSprite(event.motion.x,
491 case SDL_MOUSEBUTTONDOWN:
492 if ( event.button.button == 1 ) {
493 AttractSprite(event.button.x,
499 area.x = event.button.x-16;
500 area.y = event.button.y-16;
503 SDL_FillRect(screen, &area, 0);
504 SDL_UpdateRects(screen,1,&area);
509 if ( event.key.keysym.sym == SDLK_ESCAPE ) {
513 // there is no ESC key at all
525 SDL_FreeSurface(light);
526 SDL_FreeSurface(sprite);
527 SDL_FreeSurface(backing);
529 /* Print out some timing information */
531 printf("%d alpha blits, ~%4.4f ms per blit\n",
532 flashes, (float)flashtime/flashes);