testoverlay2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /********************************************************************************
  11. * *
  12. * Test of the overlay used for moved pictures, test more closed to real life. *
  13. * Running trojan moose :) Coded by Mike Gorchak. *
  14. * *
  15. ********************************************************************************/
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "SDL.h"
  20. #define MOOSEPIC_W 64
  21. #define MOOSEPIC_H 88
  22. #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
  23. #define MOOSEFRAMES_COUNT 10
  24. SDL_Color MooseColors[84] = {
  25. {49, 49, 49}
  26. , {66, 24, 0}
  27. , {66, 33, 0}
  28. , {66, 66, 66}
  29. ,
  30. {66, 115, 49}
  31. , {74, 33, 0}
  32. , {74, 41, 16}
  33. , {82, 33, 8}
  34. ,
  35. {82, 41, 8}
  36. , {82, 49, 16}
  37. , {82, 82, 82}
  38. , {90, 41, 8}
  39. ,
  40. {90, 41, 16}
  41. , {90, 57, 24}
  42. , {99, 49, 16}
  43. , {99, 66, 24}
  44. ,
  45. {99, 66, 33}
  46. , {99, 74, 33}
  47. , {107, 57, 24}
  48. , {107, 82, 41}
  49. ,
  50. {115, 57, 33}
  51. , {115, 66, 33}
  52. , {115, 66, 41}
  53. , {115, 74, 0}
  54. ,
  55. {115, 90, 49}
  56. , {115, 115, 115}
  57. , {123, 82, 0}
  58. , {123, 99, 57}
  59. ,
  60. {132, 66, 41}
  61. , {132, 74, 41}
  62. , {132, 90, 8}
  63. , {132, 99, 33}
  64. ,
  65. {132, 99, 66}
  66. , {132, 107, 66}
  67. , {140, 74, 49}
  68. , {140, 99, 16}
  69. ,
  70. {140, 107, 74}
  71. , {140, 115, 74}
  72. , {148, 107, 24}
  73. , {148, 115, 82}
  74. ,
  75. {148, 123, 74}
  76. , {148, 123, 90}
  77. , {156, 115, 33}
  78. , {156, 115, 90}
  79. ,
  80. {156, 123, 82}
  81. , {156, 132, 82}
  82. , {156, 132, 99}
  83. , {156, 156, 156}
  84. ,
  85. {165, 123, 49}
  86. , {165, 123, 90}
  87. , {165, 132, 82}
  88. , {165, 132, 90}
  89. ,
  90. {165, 132, 99}
  91. , {165, 140, 90}
  92. , {173, 132, 57}
  93. , {173, 132, 99}
  94. ,
  95. {173, 140, 107}
  96. , {173, 140, 115}
  97. , {173, 148, 99}
  98. , {173, 173, 173}
  99. ,
  100. {181, 140, 74}
  101. , {181, 148, 115}
  102. , {181, 148, 123}
  103. , {181, 156, 107}
  104. ,
  105. {189, 148, 123}
  106. , {189, 156, 82}
  107. , {189, 156, 123}
  108. , {189, 156, 132}
  109. ,
  110. {189, 189, 189}
  111. , {198, 156, 123}
  112. , {198, 165, 132}
  113. , {206, 165, 99}
  114. ,
  115. {206, 165, 132}
  116. , {206, 173, 140}
  117. , {206, 206, 206}
  118. , {214, 173, 115}
  119. ,
  120. {214, 173, 140}
  121. , {222, 181, 148}
  122. , {222, 189, 132}
  123. , {222, 189, 156}
  124. ,
  125. {222, 222, 222}
  126. , {231, 198, 165}
  127. , {231, 231, 231}
  128. , {239, 206, 173}
  129. };
  130. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  131. static void
  132. quit(int rc)
  133. {
  134. SDL_Quit();
  135. exit(rc);
  136. }
  137. /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
  138. /* NOTE: These RGB conversion functions are not intended for speed,
  139. only as examples.
  140. */
  141. void
  142. RGBtoYUV(Uint8 * rgb, int *yuv, int monochrome, int luminance)
  143. {
  144. if (monochrome) {
  145. #if 1 /* these are the two formulas that I found on the FourCC site... */
  146. yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
  147. yuv[1] = 128;
  148. yuv[2] = 128;
  149. #else
  150. yuv[0] = (int)(0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
  151. yuv[1] = 128;
  152. yuv[2] = 128;
  153. #endif
  154. } else {
  155. #if 1 /* these are the two formulas that I found on the FourCC site... */
  156. yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
  157. yuv[1] = (int)((rgb[2] - yuv[0]) * 0.565 + 128);
  158. yuv[2] = (int)((rgb[0] - yuv[0]) * 0.713 + 128);
  159. #else
  160. yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
  161. yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
  162. yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
  163. #endif
  164. }
  165. if (luminance != 100) {
  166. yuv[0] = yuv[0] * luminance / 100;
  167. if (yuv[0] > 255)
  168. yuv[0] = 255;
  169. }
  170. }
  171. void
  172. ConvertRGBtoYV12(Uint8 *rgb, Uint8 *out, int w, int h,
  173. int monochrome, int luminance)
  174. {
  175. int x, y;
  176. int yuv[3];
  177. Uint8 *op[3];
  178. op[0] = out;
  179. op[1] = op[0] + w*h;
  180. op[2] = op[1] + w*h/4;
  181. for (y = 0; y < h; ++y) {
  182. for (x = 0; x < w; ++x) {
  183. RGBtoYUV(rgb, yuv, monochrome, luminance);
  184. *(op[0]++) = yuv[0];
  185. if (x % 2 == 0 && y % 2 == 0) {
  186. *(op[1]++) = yuv[2];
  187. *(op[2]++) = yuv[1];
  188. }
  189. rgb += 3;
  190. }
  191. }
  192. }
  193. static void
  194. PrintUsage(char *argv0)
  195. {
  196. SDL_Log("Usage: %s [arg] [arg] [arg] ...\n", argv0);
  197. SDL_Log("\n");
  198. SDL_Log("Where 'arg' is any of the following options:\n");
  199. SDL_Log("\n");
  200. SDL_Log(" -fps <frames per second>\n");
  201. SDL_Log(" -nodelay\n");
  202. SDL_Log(" -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
  203. SDL_Log(" -scale <scale factor> (initial scale of the overlay)\n");
  204. SDL_Log(" -help (shows this help)\n");
  205. SDL_Log("\n");
  206. SDL_Log("Press ESC to exit, or SPACE to freeze the movie while application running.\n");
  207. SDL_Log("\n");
  208. }
  209. int
  210. main(int argc, char **argv)
  211. {
  212. Uint8 *RawMooseData;
  213. SDL_RWops *handle;
  214. int window_w;
  215. int window_h;
  216. SDL_Window *window;
  217. SDL_Renderer *renderer;
  218. Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
  219. SDL_Texture *MooseTexture;
  220. SDL_Rect displayrect;
  221. SDL_Event event;
  222. int paused = 0;
  223. int i, j;
  224. int fps = 12;
  225. int fpsdelay;
  226. int nodelay = 0;
  227. Uint32 pixel_format = SDL_PIXELFORMAT_YV12;
  228. int scale = 5;
  229. SDL_bool done = SDL_FALSE;
  230. /* Enable standard application logging */
  231. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  232. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  233. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  234. return 3;
  235. }
  236. while (argc > 1) {
  237. if (strcmp(argv[1], "-fps") == 0) {
  238. if (argv[2]) {
  239. fps = atoi(argv[2]);
  240. if (fps == 0) {
  241. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  242. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  243. quit(10);
  244. }
  245. if ((fps < 0) || (fps > 1000)) {
  246. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  247. "The -fps option must be in range from 1 to 1000, default is 12.\n");
  248. quit(10);
  249. }
  250. argv += 2;
  251. argc -= 2;
  252. } else {
  253. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  254. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  255. quit(10);
  256. }
  257. } else if (strcmp(argv[1], "-nodelay") == 0) {
  258. nodelay = 1;
  259. argv += 1;
  260. argc -= 1;
  261. } else if (strcmp(argv[1], "-scale") == 0) {
  262. if (argv[2]) {
  263. scale = atoi(argv[2]);
  264. if (scale == 0) {
  265. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  266. "The -scale option requires an argument [from 1 to 50], default is 5.\n");
  267. quit(10);
  268. }
  269. if ((scale < 0) || (scale > 50)) {
  270. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  271. "The -scale option must be in range from 1 to 50, default is 5.\n");
  272. quit(10);
  273. }
  274. argv += 2;
  275. argc -= 2;
  276. } else {
  277. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  278. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  279. quit(10);
  280. }
  281. } else if ((strcmp(argv[1], "-help") == 0)
  282. || (strcmp(argv[1], "-h") == 0)) {
  283. PrintUsage(argv[0]);
  284. quit(0);
  285. } else {
  286. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
  287. quit(10);
  288. }
  289. break;
  290. }
  291. RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
  292. if (RawMooseData == NULL) {
  293. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
  294. free(RawMooseData);
  295. quit(1);
  296. }
  297. /* load the trojan moose images */
  298. handle = SDL_RWFromFile("moose.dat", "rb");
  299. if (handle == NULL) {
  300. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
  301. free(RawMooseData);
  302. quit(2);
  303. }
  304. SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
  305. SDL_RWclose(handle);
  306. /* Create the window and renderer */
  307. window_w = MOOSEPIC_W * scale;
  308. window_h = MOOSEPIC_H * scale;
  309. window = SDL_CreateWindow("Happy Moose",
  310. SDL_WINDOWPOS_UNDEFINED,
  311. SDL_WINDOWPOS_UNDEFINED,
  312. window_w, window_h,
  313. SDL_WINDOW_RESIZABLE);
  314. if (!window) {
  315. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
  316. free(RawMooseData);
  317. quit(4);
  318. }
  319. renderer = SDL_CreateRenderer(window, -1, 0);
  320. if (!renderer) {
  321. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
  322. free(RawMooseData);
  323. quit(4);
  324. }
  325. MooseTexture = SDL_CreateTexture(renderer, pixel_format, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
  326. if (!MooseTexture) {
  327. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
  328. free(RawMooseData);
  329. quit(5);
  330. }
  331. /* Uncomment this to check vertex color with a YUV texture */
  332. /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
  333. for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
  334. Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
  335. Uint8 *rgb;
  336. Uint8 *frame;
  337. rgb = MooseFrameRGB;
  338. frame = RawMooseData + i * MOOSEFRAME_SIZE;
  339. for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
  340. rgb[0] = MooseColors[frame[j]].r;
  341. rgb[1] = MooseColors[frame[j]].g;
  342. rgb[2] = MooseColors[frame[j]].b;
  343. rgb += 3;
  344. }
  345. ConvertRGBtoYV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
  346. }
  347. free(RawMooseData);
  348. /* set the start frame */
  349. i = 0;
  350. if (nodelay) {
  351. fpsdelay = 0;
  352. } else {
  353. fpsdelay = 1000 / fps;
  354. }
  355. displayrect.x = 0;
  356. displayrect.y = 0;
  357. displayrect.w = window_w;
  358. displayrect.h = window_h;
  359. /* Ignore key up events, they don't even get filtered */
  360. SDL_EventState(SDL_KEYUP, SDL_IGNORE);
  361. /* Loop, waiting for QUIT or RESIZE */
  362. while (!done) {
  363. while (SDL_PollEvent(&event)) {
  364. switch (event.type) {
  365. case SDL_WINDOWEVENT:
  366. if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
  367. SDL_RenderSetViewport(renderer, NULL);
  368. displayrect.w = window_w = event.window.data1;
  369. displayrect.h = window_h = event.window.data2;
  370. }
  371. break;
  372. case SDL_MOUSEBUTTONDOWN:
  373. displayrect.x = event.button.x - window_w / 2;
  374. displayrect.y = event.button.y - window_h / 2;
  375. break;
  376. case SDL_MOUSEMOTION:
  377. if (event.motion.state) {
  378. displayrect.x = event.motion.x - window_w / 2;
  379. displayrect.y = event.motion.y - window_h / 2;
  380. }
  381. break;
  382. case SDL_KEYDOWN:
  383. if (event.key.keysym.sym == SDLK_SPACE) {
  384. paused = !paused;
  385. break;
  386. }
  387. if (event.key.keysym.sym != SDLK_ESCAPE) {
  388. break;
  389. }
  390. case SDL_QUIT:
  391. done = SDL_TRUE;
  392. break;
  393. }
  394. }
  395. SDL_Delay(fpsdelay);
  396. if (!paused) {
  397. i = (i + 1) % MOOSEFRAMES_COUNT;
  398. SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W*SDL_BYTESPERPIXEL(pixel_format));
  399. }
  400. SDL_RenderClear(renderer);
  401. SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
  402. SDL_RenderPresent(renderer);
  403. }
  404. SDL_DestroyRenderer(renderer);
  405. quit(0);
  406. return 0;
  407. }
  408. /* vi: set ts=4 sw=4 expandtab: */