sdl_test.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib SDL Test Source
  4. // Copyright (C) 2000 by Denton Woods
  5. // Copyright (C) 2001 Nelson Rush.
  6. // Last modified: 7/02/2001
  7. //
  8. // Filename: testil/sdltest/sdl_test.c
  9. //
  10. // Description: SDL test application for DevIL.
  11. //
  12. //-----------------------------------------------------------------------------
  13. #include <IL/il.h>
  14. #include <IL/ilu.h>
  15. #include <IL/ilut.h>
  16. #include <SDL.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #ifdef _WIN32
  21. #ifdef _DEBUG
  22. #pragma comment(linker, "/NODEFAULTLIB:msvcrt.lib")
  23. #endif//_DEBUG
  24. #endif
  25. /* Bring up a window and play with it */
  26. #define BENCHMARK_SDL
  27. #define NOTICE(X) printf("%s", X);
  28. void DrawPict(SDL_Surface *screen, char *bmpfile,
  29. int speedy, int flip, int nofade)
  30. {
  31. SDL_Surface *picture;
  32. SDL_Rect dest, update;
  33. int i, centered;
  34. int ncolors;
  35. SDL_Color *colors, *cmap;
  36. /* Load the image into a surface */
  37. if ( bmpfile == NULL ) {
  38. bmpfile = "sample.bmp"; /* Sample image */
  39. }
  40. fprintf(stderr, "Loading picture: %s\n", bmpfile);
  41. //picture = SDL_LoadBMP(bmpfile);
  42. picture = ilutSDLSurfaceLoadImage(bmpfile);
  43. if ( picture == NULL ) {
  44. fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
  45. SDL_GetError());
  46. return;
  47. }
  48. /* Set the display colors -- on a hicolor display this is a no-op */
  49. if ( picture->format->palette ) {
  50. ncolors = picture->format->palette->ncolors;
  51. colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
  52. cmap = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
  53. memcpy(colors, picture->format->palette->colors,
  54. ncolors*sizeof(SDL_Color));
  55. } else {
  56. int r, g, b;
  57. /* Allocate 256 color palette */
  58. ncolors = 256;
  59. colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
  60. cmap = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
  61. /* Set a 3,3,2 color cube */
  62. for ( r=0; r<8; ++r ) {
  63. for ( g=0; g<8; ++g ) {
  64. for ( b=0; b<4; ++b ) {
  65. i = ((r<<5)|(g<<2)|b);
  66. colors[i].r = r<<5;
  67. colors[i].g = g<<5;
  68. colors[i].b = b<<6;
  69. }
  70. }
  71. }
  72. }
  73. NOTICE("testwin: setting colors\n");
  74. if ( ! SDL_SetColors(screen, colors, 0, ncolors) &&
  75. (screen->format->palette != NULL) ) {
  76. fprintf(stderr,
  77. "Warning: Couldn't set all of the colors, but SDL will map the image\n"
  78. " (colormap fading will suffer - try the -warp option)\n"
  79. );
  80. }
  81. /* Set the screen to black (not really necessary) */
  82. if ( SDL_LockSurface(screen) == 0 ) {
  83. Uint32 black;
  84. Uint8 *pixels;
  85. black = SDL_MapRGB(screen->format, 0, 0, 0);
  86. pixels = (Uint8 *)screen->pixels;
  87. for ( i=0; i<screen->h; ++i ) {
  88. memset(pixels, black,
  89. screen->w*screen->format->BytesPerPixel);
  90. pixels += screen->pitch;
  91. }
  92. SDL_UnlockSurface(screen);
  93. SDL_UpdateRect(screen, 0, 0, 0, 0);
  94. }
  95. /* Display the picture */
  96. if ( speedy ) {
  97. SDL_Surface *displayfmt;
  98. fprintf(stderr, "Converting picture\n");
  99. displayfmt = SDL_DisplayFormat(picture);
  100. if ( displayfmt == NULL ) {
  101. fprintf(stderr,
  102. "Couldn't convert image: %s\n", SDL_GetError());
  103. goto done;
  104. }
  105. SDL_FreeSurface(picture);
  106. picture = displayfmt;
  107. }
  108. printf("(image surface located in %s memory)\n",
  109. (picture->flags&SDL_HWSURFACE) ? "video" : "system");
  110. centered = (screen->w - picture->w)/2;
  111. if ( centered < 0 ) {
  112. centered = 0;
  113. }
  114. dest.y = (screen->h - picture->h)/2;
  115. dest.w = picture->w;
  116. dest.h = picture->h;
  117. NOTICE("testwin: moving image\n");
  118. for ( i=0; i<=centered; ++i ) {
  119. dest.x = i;
  120. update = dest;
  121. if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) {
  122. fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
  123. break;
  124. }
  125. if ( flip ) {
  126. SDL_Flip(screen);
  127. } else {
  128. SDL_UpdateRects(screen, 1, &update);
  129. }
  130. }
  131. #ifdef SCREENSHOT
  132. if ( SDL_SaveBMP(screen, "screen.bmp") < 0 )
  133. printf("Couldn't save screen: %s\n", SDL_GetError());
  134. #endif
  135. #ifndef BENCHMARK_SDL
  136. /* Let it sit there for a while */
  137. SDL_Delay(5*1000);
  138. #endif
  139. /* Fade the colormap */
  140. if ( ! nofade ) {
  141. int maxstep;
  142. SDL_Color final;
  143. SDL_Color palcolors[256];
  144. struct {
  145. Sint16 r, g, b;
  146. } cdist[256];
  147. NOTICE("testwin: fading out...\n");
  148. memcpy(cmap, colors, ncolors*sizeof(SDL_Color));
  149. maxstep = 32-1;
  150. final.r = 0xFF;
  151. final.g = 0x00;
  152. final.b = 0x00;
  153. memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
  154. for ( i=0; i<ncolors; ++i ) {
  155. cdist[i].r = final.r-palcolors[i].r;
  156. cdist[i].g = final.g-palcolors[i].g;
  157. cdist[i].b = final.b-palcolors[i].b;
  158. }
  159. for ( i=0; i<=maxstep/2; ++i ) { /* halfway fade */
  160. int c;
  161. for ( c=0; c<ncolors; ++c ) {
  162. colors[c].r =
  163. palcolors[c].r+((cdist[c].r*i))/maxstep;
  164. colors[c].g =
  165. palcolors[c].g+((cdist[c].g*i))/maxstep;
  166. colors[c].b =
  167. palcolors[c].b+((cdist[c].b*i))/maxstep;
  168. }
  169. SDL_SetColors(screen, colors, 0, ncolors);
  170. SDL_Delay(1);
  171. }
  172. final.r = 0x00;
  173. final.g = 0x00;
  174. final.b = 0x00;
  175. memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
  176. for ( i=0; i<ncolors; ++i ) {
  177. cdist[i].r = final.r-palcolors[i].r;
  178. cdist[i].g = final.g-palcolors[i].g;
  179. cdist[i].b = final.b-palcolors[i].b;
  180. }
  181. maxstep /= 2;
  182. for ( i=0; i<=maxstep; ++i ) { /* finish fade out */
  183. int c;
  184. for ( c=0; c<ncolors; ++c ) {
  185. colors[c].r =
  186. palcolors[c].r+((cdist[c].r*i))/maxstep;
  187. colors[c].g =
  188. palcolors[c].g+((cdist[c].g*i))/maxstep;
  189. colors[c].b =
  190. palcolors[c].b+((cdist[c].b*i))/maxstep;
  191. }
  192. SDL_SetColors(screen, colors, 0, ncolors);
  193. SDL_Delay(1);
  194. }
  195. for ( i=0; i<ncolors; ++i ) {
  196. colors[i].r = final.r;
  197. colors[i].g = final.g;
  198. colors[i].b = final.b;
  199. }
  200. SDL_SetColors(screen, colors, 0, ncolors);
  201. NOTICE("testwin: fading in...\n");
  202. memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
  203. for ( i=0; i<ncolors; ++i ) {
  204. cdist[i].r = cmap[i].r-palcolors[i].r;
  205. cdist[i].g = cmap[i].g-palcolors[i].g;
  206. cdist[i].b = cmap[i].b-palcolors[i].b;
  207. }
  208. for ( i=0; i<=maxstep; ++i ) { /* 32 step fade in */
  209. int c;
  210. for ( c=0; c<ncolors; ++c ) {
  211. colors[c].r =
  212. palcolors[c].r+((cdist[c].r*i))/maxstep;
  213. colors[c].g =
  214. palcolors[c].g+((cdist[c].g*i))/maxstep;
  215. colors[c].b =
  216. palcolors[c].b+((cdist[c].b*i))/maxstep;
  217. }
  218. SDL_SetColors(screen, colors, 0, ncolors);
  219. SDL_Delay(1);
  220. }
  221. NOTICE("testwin: fading over\n");
  222. }
  223. done:
  224. /* Free the picture and return */
  225. SDL_FreeSurface(picture);
  226. free(colors); free(cmap);
  227. return;
  228. }
  229. int main(int argc, char *argv[])
  230. {
  231. SDL_Surface *screen;
  232. /* Options */
  233. int speedy, flip, nofade;
  234. int delay;
  235. int w, h;
  236. int desired_bpp;
  237. Uint32 video_flags;
  238. #ifdef BENCHMARK_SDL
  239. Uint32 then, now;
  240. #endif
  241. /* Set default options and check command-line */
  242. ilInit();
  243. speedy = 0;
  244. flip = 0;
  245. nofade = 0;
  246. delay = 1;
  247. w = 640;
  248. h = 480;
  249. desired_bpp = 0;
  250. video_flags = 0;
  251. while ( argc > 1 ) {
  252. if ( strcmp(argv[1], "-speedy") == 0 ) {
  253. speedy = 1;
  254. argv += 1;
  255. argc -= 1;
  256. } else
  257. if ( strcmp(argv[1], "-nofade") == 0 ) {
  258. nofade = 1;
  259. argv += 1;
  260. argc -= 1;
  261. } else
  262. if ( strcmp(argv[1], "-delay") == 0 ) {
  263. if ( argv[2] ) {
  264. delay = atoi(argv[2]);
  265. argv += 2;
  266. argc -= 2;
  267. } else {
  268. fprintf(stderr,
  269. "The -delay option requires an argument\n");
  270. exit(1);
  271. }
  272. } else
  273. if ( strcmp(argv[1], "-width") == 0 ) {
  274. if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
  275. argv += 2;
  276. argc -= 2;
  277. } else {
  278. fprintf(stderr,
  279. "The -width option requires an argument\n");
  280. exit(1);
  281. }
  282. } else
  283. if ( strcmp(argv[1], "-height") == 0 ) {
  284. if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
  285. argv += 2;
  286. argc -= 2;
  287. } else {
  288. fprintf(stderr,
  289. "The -height option requires an argument\n");
  290. exit(1);
  291. }
  292. } else
  293. if ( strcmp(argv[1], "-bpp") == 0 ) {
  294. if ( argv[2] ) {
  295. desired_bpp = atoi(argv[2]);
  296. argv += 2;
  297. argc -= 2;
  298. } else {
  299. fprintf(stderr,
  300. "The -bpp option requires an argument\n");
  301. exit(1);
  302. }
  303. } else
  304. if ( strcmp(argv[1], "-warp") == 0 ) {
  305. video_flags |= SDL_HWPALETTE;
  306. argv += 1;
  307. argc -= 1;
  308. } else
  309. if ( strcmp(argv[1], "-hw") == 0 ) {
  310. video_flags |= SDL_HWSURFACE;
  311. argv += 1;
  312. argc -= 1;
  313. } else
  314. if ( strcmp(argv[1], "-flip") == 0 ) {
  315. video_flags |= SDL_DOUBLEBUF;
  316. argv += 1;
  317. argc -= 1;
  318. } else
  319. if ( strcmp(argv[1], "-fullscreen") == 0 ) {
  320. video_flags |= SDL_FULLSCREEN;
  321. argv += 1;
  322. argc -= 1;
  323. } else
  324. break;
  325. }
  326. if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  327. fprintf(stderr,
  328. "Couldn't initialize SDL: %s\n", SDL_GetError());
  329. exit(1);
  330. }
  331. atexit(SDL_Quit); /* Clean up on exit */
  332. /* Initialize the display */
  333. screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
  334. if ( screen == NULL ) {
  335. fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
  336. w, h, desired_bpp, SDL_GetError());
  337. exit(1);
  338. }
  339. printf("Set %dx%dx%d mode\n",
  340. screen->w, screen->h, screen->format->BitsPerPixel);
  341. printf("(video surface located in %s memory)\n",
  342. (screen->flags&SDL_HWSURFACE) ? "video" : "system");
  343. if ( screen->flags & SDL_DOUBLEBUF ) {
  344. printf("Double-buffering enabled\n");
  345. flip = 1;
  346. }
  347. /* Set the window manager title bar */
  348. SDL_WM_SetCaption("SDL test window", "testwin");
  349. /* Do all the drawing work */
  350. #ifdef BENCHMARK_SDL
  351. then = SDL_GetTicks();
  352. DrawPict(screen, argv[1], speedy, flip, nofade);
  353. now = SDL_GetTicks();
  354. printf("Time: %d milliseconds\n", now-then);
  355. #else
  356. DrawPict(screen, argv[1], speedy, flip, nofade);
  357. #endif
  358. SDL_Delay(delay*1000);
  359. return(0);
  360. }