2 Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple program: Fill a colormap with gray and stripe it down the screen,
14 Then move an alpha valued sprite around the screen.
24 #define FRAME_TICKS (1000/30) /* 30 frames/second */
26 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
34 /* Fill the screen with a gradient */
36 FillBackground(SDL_Surface * screen)
42 /* Set the surface pixels and refresh! */
43 if (SDL_LockSurface(screen) < 0) {
44 fprintf(stderr, "Couldn't lock the display surface: %s\n",
48 buffer = (Uint8 *) screen->pixels;
49 switch (screen->format->BytesPerPixel) {
52 for (i = 0; i < screen->h; ++i) {
53 memset(buffer, (i * 255) / screen->h,
54 screen->w * screen->format->BytesPerPixel);
55 buffer += screen->pitch;
59 for (i = 0; i < screen->h; ++i) {
63 gradient = ((i * 255) / screen->h);
64 color = (Uint16) SDL_MapRGB(screen->format,
65 gradient, gradient, gradient);
66 buffer16 = (Uint16 *) buffer;
67 for (k = 0; k < screen->w; k++) {
70 buffer += screen->pitch;
74 for (i = 0; i < screen->h; ++i) {
78 gradient = ((i * 255) / screen->h);
79 color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
80 buffer32 = (Uint32 *) buffer;
81 for (k = 0; k < screen->w; k++) {
84 buffer += screen->pitch;
89 SDL_UnlockSurface(screen);
90 SDL_UpdateRect(screen, 0, 0, 0, 0);
93 /* Create a "light" -- a yellowish surface with variable alpha */
95 CreateLight(int radius)
97 Uint8 trans, alphamask;
108 /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */
109 /* Note: this isn't any faster than a 32 bit alpha surface */
110 alphamask = 0x0000000F;
111 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2 * radius, 2 * radius, 16,
112 0x0000F000, 0x00000F00, 0x000000F0,
117 /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */
118 alphamask = 0x000000FF;
119 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2 * radius, 2 * radius, 32,
120 0xFF000000, 0x00FF0000, 0x0000FF00,
123 fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError());
128 /* Fill with a light yellow-orange color */
129 skip = light->pitch - (light->w * light->format->BytesPerPixel);
131 buf = (Uint16 *) light->pixels;
133 buf = (Uint32 *) light->pixels;
135 /* Get a tranparent pixel value - we'll add alpha later */
136 pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0);
137 for (y = 0; y < light->h; ++y) {
138 for (x = 0; x < light->w; ++x) {
141 buf += skip; /* Almost always 0, but just in case... */
144 /* Calculate alpha values for the surface. */
146 buf = (Uint16 *) light->pixels;
148 buf = (Uint32 *) light->pixels;
150 for (y = 0; y < light->h; ++y) {
151 for (x = 0; x < light->w; ++x) {
152 /* Slow distance formula (from center of light) */
153 xdist = x - (light->w / 2);
154 ydist = y - (light->h / 2);
155 range = (int) sqrt(xdist * xdist + ydist * ydist);
157 /* Scale distance to range of transparency (0-255) */
158 if (range > radius) {
161 /* Increasing transparency with distance */
162 trans = (Uint8) ((range * alphamask) / radius);
164 /* Lights are very transparent */
165 addition = (alphamask + 1) / 8;
166 if ((int) trans + addition > alphamask) {
172 /* We set the alpha component as the right N bits */
173 *buf++ |= (255 - trans);
175 buf += skip; /* Almost always 0, but just in case... */
177 /* Enable RLE acceleration of this alpha surface */
178 SDL_SetAlpha(light, SDL_SRCALPHA | SDL_RLEACCEL, 0);
184 static Uint32 flashes = 0;
185 static Uint32 flashtime = 0;
188 FlashLight(SDL_Surface * screen, SDL_Surface * light, int x, int y)
194 /* Easy, center light */
195 position.x = x - (light->w / 2);
196 position.y = y - (light->h / 2);
197 position.w = light->w;
198 position.h = light->h;
199 ticks1 = SDL_GetTicks();
200 SDL_BlitSurface(light, NULL, screen, &position);
201 ticks2 = SDL_GetTicks();
202 SDL_UpdateRects(screen, 1, &position);
205 /* Update time spend doing alpha blitting */
206 flashtime += (ticks2 - ticks1);
209 static int sprite_visible = 0;
210 static SDL_Surface *sprite;
211 static SDL_Surface *backing;
212 static SDL_Rect position;
213 static int x_vel, y_vel;
214 static int alpha_vel;
217 LoadSprite(SDL_Surface * screen, char *file)
219 SDL_Surface *converted;
221 /* Load the sprite image */
222 sprite = SDL_LoadBMP(file);
223 if (sprite == NULL) {
224 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
228 /* Set transparent pixel as the pixel at (0,0) */
229 if (sprite->format->palette) {
230 SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, *(Uint8 *) sprite->pixels);
233 /* Convert sprite to video format */
234 converted = SDL_DisplayFormat(sprite);
235 SDL_FreeSurface(sprite);
236 if (converted == NULL) {
237 fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
242 /* Create the background */
243 backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
245 if (backing == NULL) {
246 fprintf(stderr, "Couldn't create background: %s\n", SDL_GetError());
247 SDL_FreeSurface(sprite);
251 /* Convert background to video format */
252 converted = SDL_DisplayFormat(backing);
253 SDL_FreeSurface(backing);
254 if (converted == NULL) {
255 fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
256 SDL_FreeSurface(sprite);
261 /* Set the initial position of the sprite */
262 position.x = (screen->w - sprite->w) / 2;
263 position.y = (screen->h - sprite->h) / 2;
264 position.w = sprite->w;
265 position.h = sprite->h;
270 /* We're ready to roll. :) */
275 AttractSprite(Uint16 x, Uint16 y)
277 x_vel = ((int) x - position.x) / 10;
278 y_vel = ((int) y - position.y) / 10;
282 MoveSprite(SDL_Surface * screen, SDL_Surface * light)
287 /* Erase the sprite if it was visible */
288 if (sprite_visible) {
289 updates[0] = position;
290 SDL_BlitSurface(backing, NULL, screen, &updates[0]);
299 /* Since the sprite is off the screen, we can do other drawing
300 without being overwritten by the saved area behind the sprite.
305 SDL_GetMouseState(&x, &y);
306 FlashLight(screen, light, x, y);
309 /* Move the sprite, bounce at the wall */
311 if ((position.x < 0) || (position.x >= screen->w)) {
316 if ((position.y < 0) || (position.y >= screen->h)) {
321 /* Update transparency (fade in and out) */
322 SDL_GetSurfaceAlphaMod(sprite, &alpha);
323 if (((int) alpha + alpha_vel) < 0) {
324 alpha_vel = -alpha_vel;
325 } else if (((int) alpha + alpha_vel) > 255) {
326 alpha_vel = -alpha_vel;
328 SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8) (alpha + alpha_vel));
330 /* Save the area behind the sprite */
331 updates[1] = position;
332 SDL_BlitSurface(screen, &updates[1], backing, NULL);
334 /* Blit the sprite onto the screen */
335 updates[1] = position;
336 SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
339 SDL_UpdateRects(screen, 2, updates);
343 main(int argc, char *argv[])
345 const SDL_VideoInfo *info;
354 Uint32 ticks, lastticks;
358 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
359 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
363 /* Alpha blending doesn't work well at 8-bit color */
372 info = SDL_GetVideoInfo();
373 if (info->vfmt->BitsPerPixel > 8) {
374 video_bpp = info->vfmt->BitsPerPixel;
377 fprintf(stderr, "forced 16 bpp mode\n");
379 videoflags = SDL_SWSURFACE;
380 for (i = 1; argv[i]; ++i) {
381 if (strcmp(argv[i], "-bpp") == 0) {
382 video_bpp = atoi(argv[++i]);
383 if (video_bpp <= 8) {
385 fprintf(stderr, "forced 16 bpp mode\n");
387 } else if (strcmp(argv[i], "-hw") == 0) {
388 videoflags |= SDL_HWSURFACE;
389 } else if (strcmp(argv[i], "-warp") == 0) {
390 videoflags |= SDL_HWPALETTE;
391 } else if (strcmp(argv[i], "-width") == 0 && argv[i + 1]) {
393 } else if (strcmp(argv[i], "-height") == 0 && argv[i + 1]) {
395 } else if (strcmp(argv[i], "-resize") == 0) {
396 videoflags |= SDL_RESIZABLE;
397 } else if (strcmp(argv[i], "-noframe") == 0) {
398 videoflags |= SDL_NOFRAME;
399 } else if (strcmp(argv[i], "-fullscreen") == 0) {
400 videoflags |= SDL_FULLSCREEN;
403 "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
410 if ((screen = SDL_SetVideoMode(w, h, video_bpp, videoflags)) == NULL) {
411 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
412 w, h, video_bpp, SDL_GetError());
415 FillBackground(screen);
417 /* Create the light */
418 light = CreateLight(82);
423 /* Load the sprite */
424 if (LoadSprite(screen, "icon.bmp") < 0) {
425 SDL_FreeSurface(light);
429 /* Print out information about our surfaces */
430 printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
431 if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
432 printf("Screen is in video memory\n");
434 printf("Screen is in system memory\n");
436 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
437 printf("Screen has double-buffering enabled\n");
439 if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
440 printf("Sprite is in video memory\n");
442 printf("Sprite is in system memory\n");
445 /* Run a sample blit to trigger blit acceleration */
446 MoveSprite(screen, NULL);
447 if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
448 printf("Sprite blit uses hardware alpha acceleration\n");
450 printf("Sprite blit dosn't uses hardware alpha acceleration\n");
453 /* Set a clipping rectangle to clip the outside edge of the screen */
458 clip.w = screen->w - (2 * 32);
459 clip.h = screen->h - (2 * 32);
460 SDL_SetClipRect(screen, &clip);
463 /* Wait for a keystroke */
464 lastticks = SDL_GetTicks();
468 /* Update the frame -- move the sprite */
470 MoveSprite(screen, light);
473 MoveSprite(screen, NULL);
476 /* Slow down the loop to 30 frames/second */
477 ticks = SDL_GetTicks();
478 if ((ticks - lastticks) < FRAME_TICKS) {
479 #ifdef CHECK_SLEEP_GRANULARITY
480 fprintf(stderr, "Sleeping %d ticks\n",
481 FRAME_TICKS - (ticks - lastticks));
483 SDL_Delay(FRAME_TICKS - (ticks - lastticks));
484 #ifdef CHECK_SLEEP_GRANULARITY
485 fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks() - ticks));
490 /* Check for events */
491 while (SDL_PollEvent(&event)) {
492 switch (event.type) {
493 case SDL_VIDEORESIZE:
495 SDL_SetVideoMode(event.resize.w, event.resize.h,
496 video_bpp, videoflags);
498 FillBackground(screen);
501 /* Attract sprite while mouse is held down */
502 case SDL_MOUSEMOTION:
503 if (event.motion.state != 0) {
504 AttractSprite(event.motion.x, event.motion.y);
508 case SDL_MOUSEBUTTONDOWN:
509 if (event.button.button == 1) {
510 AttractSprite(event.button.x, event.button.y);
515 area.x = event.button.x - 16;
516 area.y = event.button.y - 16;
519 SDL_FillRect(screen, &area,
520 SDL_MapRGB(screen->format, 0, 0, 0));
521 SDL_UpdateRects(screen, 1, &area);
525 if (event.key.keysym.sym == SDLK_ESCAPE) {
528 if (event.key.keysym.sym == SDLK_RETURN) {
529 SDL_WM_ToggleFullScreen(screen);
540 SDL_FreeSurface(light);
541 SDL_FreeSurface(sprite);
542 SDL_FreeSurface(backing);
544 /* Print out some timing information */
546 printf("%d alpha blits, ~%4.4f ms per blit\n",
547 flashes, (float) flashtime / flashes);