PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/external/qemu/distrib/sdl-1.2.15/test/testpalette.c

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C | 342 lines | 241 code | 45 blank | 56 comment | 60 complexity | 81353dbf19ccc97bf437f6c6c2515cc9 MD5 | raw file
  1. /*
  2. * testpalette.c
  3. *
  4. * A simple test of runtime palette modification for animation
  5. * (using the SDL_SetPalette() API).
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <math.h>
  11. /* This isn't in the Windows headers */
  12. #ifndef M_PI
  13. #define M_PI 3.14159265358979323846
  14. #endif
  15. #include "SDL.h"
  16. /* screen size */
  17. #define SCRW 640
  18. #define SCRH 480
  19. #define NBOATS 5
  20. #define SPEED 2
  21. #ifndef MIN
  22. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  23. #endif
  24. #ifndef MAX
  25. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  26. #endif
  27. /*
  28. * wave colours: Made by taking a narrow cross-section of a wave picture
  29. * in Gimp, saving in PPM ascii format and formatting with Emacs macros.
  30. */
  31. static SDL_Color wavemap[] = {
  32. {0,2,103}, {0,7,110}, {0,13,117}, {0,19,125},
  33. {0,25,133}, {0,31,141}, {0,37,150}, {0,43,158},
  34. {0,49,166}, {0,55,174}, {0,61,182}, {0,67,190},
  35. {0,73,198}, {0,79,206}, {0,86,214}, {0,96,220},
  36. {5,105,224}, {12,112,226}, {19,120,227}, {26,128,229},
  37. {33,135,230}, {40,143,232}, {47,150,234}, {54,158,236},
  38. {61,165,238}, {68,173,239}, {75,180,241}, {82,188,242},
  39. {89,195,244}, {96,203,246}, {103,210,248}, {112,218,250},
  40. {124,224,250}, {135,226,251}, {146,229,251}, {156,231,252},
  41. {167,233,252}, {178,236,252}, {189,238,252}, {200,240,252},
  42. {211,242,252}, {222,244,252}, {233,247,252}, {242,249,252},
  43. {237,250,252}, {209,251,252}, {174,251,252}, {138,252,252},
  44. {102,251,252}, {63,250,252}, {24,243,252}, {7,225,252},
  45. {4,203,252}, {3,181,252}, {2,158,252}, {1,136,251},
  46. {0,111,248}, {0,82,234}, {0,63,213}, {0,50,192},
  47. {0,39,172}, {0,28,152}, {0,17,132}, {0,7,114}
  48. };
  49. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  50. static void quit(int rc)
  51. {
  52. SDL_Quit();
  53. exit(rc);
  54. }
  55. static void sdlerr(char *when)
  56. {
  57. fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
  58. quit(1);
  59. }
  60. /* create a background surface */
  61. static SDL_Surface *make_bg(SDL_Surface *screen, int startcol)
  62. {
  63. int i;
  64. SDL_Surface *bg = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
  65. 8, 0, 0, 0, 0);
  66. if(!bg)
  67. sdlerr("creating background surface");
  68. /* set the palette to the logical screen palette so that blits
  69. won't be translated */
  70. SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
  71. /* Make a wavy background pattern using colours 0-63 */
  72. if(SDL_LockSurface(bg) < 0)
  73. sdlerr("locking background");
  74. for(i = 0; i < SCRH; i++) {
  75. Uint8 *p = (Uint8 *)bg->pixels + i * bg->pitch;
  76. int j, d;
  77. d = 0;
  78. for(j = 0; j < SCRW; j++) {
  79. int v = MAX(d, -2);
  80. v = MIN(v, 2);
  81. if(i > 0)
  82. v += p[-bg->pitch] + 65 - startcol;
  83. p[j] = startcol + (v & 63);
  84. d += ((rand() >> 3) % 3) - 1;
  85. }
  86. }
  87. SDL_UnlockSurface(bg);
  88. return(bg);
  89. }
  90. /*
  91. * Return a surface flipped horisontally. Only works for 8bpp;
  92. * extension to arbitrary bitness is left as an exercise for the reader.
  93. */
  94. static SDL_Surface *hflip(SDL_Surface *s)
  95. {
  96. int i;
  97. SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
  98. 0, 0, 0, 0);
  99. /* copy palette */
  100. SDL_SetColors(z, s->format->palette->colors,
  101. 0, s->format->palette->ncolors);
  102. if(SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
  103. sdlerr("locking flip images");
  104. for(i = 0; i < s->h; i++) {
  105. int j;
  106. Uint8 *from = (Uint8 *)s->pixels + i * s->pitch;
  107. Uint8 *to = (Uint8 *)z->pixels + i * z->pitch + s->w - 1;
  108. for(j = 0; j < s->w; j++)
  109. to[-j] = from[j];
  110. }
  111. SDL_UnlockSurface(z);
  112. SDL_UnlockSurface(s);
  113. return z;
  114. }
  115. int main(int argc, char **argv)
  116. {
  117. SDL_Color cmap[256];
  118. SDL_Surface *screen;
  119. SDL_Surface *bg;
  120. SDL_Surface *boat[2];
  121. unsigned vidflags = 0;
  122. unsigned start;
  123. int fade_max = 400;
  124. int fade_level, fade_dir;
  125. int boatcols, frames, i, red;
  126. int boatx[NBOATS], boaty[NBOATS], boatdir[NBOATS];
  127. int gamma_fade = 0;
  128. int gamma_ramp = 0;
  129. if(SDL_Init(SDL_INIT_VIDEO) < 0)
  130. sdlerr("initialising SDL");
  131. while(--argc) {
  132. ++argv;
  133. if(strcmp(*argv, "-hw") == 0)
  134. vidflags |= SDL_HWSURFACE;
  135. else if(strcmp(*argv, "-fullscreen") == 0)
  136. vidflags |= SDL_FULLSCREEN;
  137. else if(strcmp(*argv, "-nofade") == 0)
  138. fade_max = 1;
  139. else if(strcmp(*argv, "-gamma") == 0)
  140. gamma_fade = 1;
  141. else if(strcmp(*argv, "-gammaramp") == 0)
  142. gamma_ramp = 1;
  143. else {
  144. fprintf(stderr,
  145. "usage: testpalette "
  146. " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
  147. quit(1);
  148. }
  149. }
  150. /* Ask explicitly for 8bpp and a hardware palette */
  151. if((screen = SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
  152. fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
  153. SCRW, SCRH, SDL_GetError());
  154. quit(1);
  155. }
  156. if (vidflags & SDL_FULLSCREEN) SDL_ShowCursor (SDL_FALSE);
  157. if((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
  158. sdlerr("loading sail.bmp");
  159. /* We've chosen magenta (#ff00ff) as colour key for the boat */
  160. SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
  161. SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
  162. boatcols = boat[0]->format->palette->ncolors;
  163. boat[1] = hflip(boat[0]);
  164. SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
  165. SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
  166. /*
  167. * First set the physical screen palette to black, so the user won't
  168. * see our initial drawing on the screen.
  169. */
  170. memset(cmap, 0, sizeof(cmap));
  171. SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, 256);
  172. /*
  173. * Proper palette management is important when playing games with the
  174. * colormap. We have divided the palette as follows:
  175. *
  176. * index 0..(boatcols-1): used for the boat
  177. * index boatcols..(boatcols+63): used for the waves
  178. */
  179. SDL_SetPalette(screen, SDL_LOGPAL,
  180. boat[0]->format->palette->colors, 0, boatcols);
  181. SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
  182. /*
  183. * Now the logical screen palette is set, and will remain unchanged.
  184. * The boats already have the same palette so fast blits can be used.
  185. */
  186. memcpy(cmap, screen->format->palette->colors, 256 * sizeof(SDL_Color));
  187. /* save the index of the red colour for later */
  188. red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
  189. bg = make_bg(screen, boatcols); /* make a nice wavy background surface */
  190. /* initial screen contents */
  191. if(SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
  192. sdlerr("blitting background to screen");
  193. SDL_Flip(screen); /* actually put the background on screen */
  194. /* determine initial boat placements */
  195. for(i = 0; i < NBOATS; i++) {
  196. boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
  197. boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
  198. boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
  199. }
  200. start = SDL_GetTicks();
  201. frames = 0;
  202. fade_dir = 1;
  203. fade_level = 0;
  204. do {
  205. SDL_Event e;
  206. SDL_Rect updates[NBOATS];
  207. SDL_Rect r;
  208. int redphase;
  209. /* A small event loop: just exit on any key or mouse button event */
  210. while(SDL_PollEvent(&e)) {
  211. if(e.type == SDL_KEYDOWN || e.type == SDL_QUIT
  212. || e.type == SDL_MOUSEBUTTONDOWN) {
  213. if(fade_dir < 0)
  214. fade_level = 0;
  215. fade_dir = -1;
  216. }
  217. }
  218. /* move boats */
  219. for(i = 0; i < NBOATS; i++) {
  220. int old_x = boatx[i];
  221. /* update boat position */
  222. boatx[i] += boatdir[i] * SPEED;
  223. if(boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
  224. boatdir[i] = -boatdir[i];
  225. /* paint over the old boat position */
  226. r.x = old_x;
  227. r.y = boaty[i];
  228. r.w = boat[0]->w;
  229. r.h = boat[0]->h;
  230. if(SDL_BlitSurface(bg, &r, screen, &r) < 0)
  231. sdlerr("blitting background");
  232. /* construct update rectangle (bounding box of old and new pos) */
  233. updates[i].x = MIN(old_x, boatx[i]);
  234. updates[i].y = boaty[i];
  235. updates[i].w = boat[0]->w + SPEED;
  236. updates[i].h = boat[0]->h;
  237. /* clip update rectangle to screen */
  238. if(updates[i].x < 0) {
  239. updates[i].w += updates[i].x;
  240. updates[i].x = 0;
  241. }
  242. if(updates[i].x + updates[i].w > SCRW)
  243. updates[i].w = SCRW - updates[i].x;
  244. }
  245. for(i = 0; i < NBOATS; i++) {
  246. /* paint boat on new position */
  247. r.x = boatx[i];
  248. r.y = boaty[i];
  249. if(SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
  250. screen, &r) < 0)
  251. sdlerr("blitting boat");
  252. }
  253. /* cycle wave palette */
  254. for(i = 0; i < 64; i++)
  255. cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
  256. if(fade_dir) {
  257. /* Fade the entire palette in/out */
  258. fade_level += fade_dir;
  259. if(gamma_fade) {
  260. /* Fade linearly in gamma level (lousy) */
  261. float level = (float)fade_level / fade_max;
  262. if(SDL_SetGamma(level, level, level) < 0)
  263. sdlerr("setting gamma");
  264. } else if(gamma_ramp) {
  265. /* Fade using gamma ramp (better) */
  266. Uint16 ramp[256];
  267. for(i = 0; i < 256; i++)
  268. ramp[i] = (i * fade_level / fade_max) << 8;
  269. if(SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
  270. sdlerr("setting gamma ramp");
  271. } else {
  272. /* Fade using direct palette manipulation (best) */
  273. memcpy(cmap, screen->format->palette->colors,
  274. boatcols * sizeof(SDL_Color));
  275. for(i = 0; i < boatcols + 64; i++) {
  276. cmap[i].r = cmap[i].r * fade_level / fade_max;
  277. cmap[i].g = cmap[i].g * fade_level / fade_max;
  278. cmap[i].b = cmap[i].b * fade_level / fade_max;
  279. }
  280. }
  281. if(fade_level == fade_max)
  282. fade_dir = 0;
  283. }
  284. /* pulse the red colour (done after the fade, for a night effect) */
  285. redphase = frames % 64;
  286. cmap[red].r = (int)(255 * sin(redphase * M_PI / 63));
  287. SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
  288. /* update changed areas of the screen */
  289. SDL_UpdateRects(screen, NBOATS, updates);
  290. frames++;
  291. } while(fade_level > 0);
  292. printf("%d frames, %.2f fps\n",
  293. frames, 1000.0 * frames / (SDL_GetTicks() - start));
  294. if (vidflags & SDL_FULLSCREEN) SDL_ShowCursor (SDL_TRUE);
  295. SDL_Quit();
  296. return 0;
  297. }