testoverlay2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. Copyright (C) 1997-2019 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. #ifdef __EMSCRIPTEN__
  17. #include <emscripten/emscripten.h>
  18. #endif
  19. #include "SDL.h"
  20. #include "testyuv_cvt.h"
  21. #define MOOSEPIC_W 64
  22. #define MOOSEPIC_H 88
  23. #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
  24. #define MOOSEFRAMES_COUNT 10
  25. SDL_Color MooseColors[84] = {
  26. {49, 49, 49, SDL_ALPHA_OPAQUE}
  27. , {66, 24, 0, SDL_ALPHA_OPAQUE}
  28. , {66, 33, 0, SDL_ALPHA_OPAQUE}
  29. , {66, 66, 66, SDL_ALPHA_OPAQUE}
  30. ,
  31. {66, 115, 49, SDL_ALPHA_OPAQUE}
  32. , {74, 33, 0, SDL_ALPHA_OPAQUE}
  33. , {74, 41, 16, SDL_ALPHA_OPAQUE}
  34. , {82, 33, 8, SDL_ALPHA_OPAQUE}
  35. ,
  36. {82, 41, 8, SDL_ALPHA_OPAQUE}
  37. , {82, 49, 16, SDL_ALPHA_OPAQUE}
  38. , {82, 82, 82, SDL_ALPHA_OPAQUE}
  39. , {90, 41, 8, SDL_ALPHA_OPAQUE}
  40. ,
  41. {90, 41, 16, SDL_ALPHA_OPAQUE}
  42. , {90, 57, 24, SDL_ALPHA_OPAQUE}
  43. , {99, 49, 16, SDL_ALPHA_OPAQUE}
  44. , {99, 66, 24, SDL_ALPHA_OPAQUE}
  45. ,
  46. {99, 66, 33, SDL_ALPHA_OPAQUE}
  47. , {99, 74, 33, SDL_ALPHA_OPAQUE}
  48. , {107, 57, 24, SDL_ALPHA_OPAQUE}
  49. , {107, 82, 41, SDL_ALPHA_OPAQUE}
  50. ,
  51. {115, 57, 33, SDL_ALPHA_OPAQUE}
  52. , {115, 66, 33, SDL_ALPHA_OPAQUE}
  53. , {115, 66, 41, SDL_ALPHA_OPAQUE}
  54. , {115, 74, 0, SDL_ALPHA_OPAQUE}
  55. ,
  56. {115, 90, 49, SDL_ALPHA_OPAQUE}
  57. , {115, 115, 115, SDL_ALPHA_OPAQUE}
  58. , {123, 82, 0, SDL_ALPHA_OPAQUE}
  59. , {123, 99, 57, SDL_ALPHA_OPAQUE}
  60. ,
  61. {132, 66, 41, SDL_ALPHA_OPAQUE}
  62. , {132, 74, 41, SDL_ALPHA_OPAQUE}
  63. , {132, 90, 8, SDL_ALPHA_OPAQUE}
  64. , {132, 99, 33, SDL_ALPHA_OPAQUE}
  65. ,
  66. {132, 99, 66, SDL_ALPHA_OPAQUE}
  67. , {132, 107, 66, SDL_ALPHA_OPAQUE}
  68. , {140, 74, 49, SDL_ALPHA_OPAQUE}
  69. , {140, 99, 16, SDL_ALPHA_OPAQUE}
  70. ,
  71. {140, 107, 74, SDL_ALPHA_OPAQUE}
  72. , {140, 115, 74, SDL_ALPHA_OPAQUE}
  73. , {148, 107, 24, SDL_ALPHA_OPAQUE}
  74. , {148, 115, 82, SDL_ALPHA_OPAQUE}
  75. ,
  76. {148, 123, 74, SDL_ALPHA_OPAQUE}
  77. , {148, 123, 90, SDL_ALPHA_OPAQUE}
  78. , {156, 115, 33, SDL_ALPHA_OPAQUE}
  79. , {156, 115, 90, SDL_ALPHA_OPAQUE}
  80. ,
  81. {156, 123, 82, SDL_ALPHA_OPAQUE}
  82. , {156, 132, 82, SDL_ALPHA_OPAQUE}
  83. , {156, 132, 99, SDL_ALPHA_OPAQUE}
  84. , {156, 156, 156, SDL_ALPHA_OPAQUE}
  85. ,
  86. {165, 123, 49, SDL_ALPHA_OPAQUE}
  87. , {165, 123, 90, SDL_ALPHA_OPAQUE}
  88. , {165, 132, 82, SDL_ALPHA_OPAQUE}
  89. , {165, 132, 90, SDL_ALPHA_OPAQUE}
  90. ,
  91. {165, 132, 99, SDL_ALPHA_OPAQUE}
  92. , {165, 140, 90, SDL_ALPHA_OPAQUE}
  93. , {173, 132, 57, SDL_ALPHA_OPAQUE}
  94. , {173, 132, 99, SDL_ALPHA_OPAQUE}
  95. ,
  96. {173, 140, 107, SDL_ALPHA_OPAQUE}
  97. , {173, 140, 115, SDL_ALPHA_OPAQUE}
  98. , {173, 148, 99, SDL_ALPHA_OPAQUE}
  99. , {173, 173, 173, SDL_ALPHA_OPAQUE}
  100. ,
  101. {181, 140, 74, SDL_ALPHA_OPAQUE}
  102. , {181, 148, 115, SDL_ALPHA_OPAQUE}
  103. , {181, 148, 123, SDL_ALPHA_OPAQUE}
  104. , {181, 156, 107, SDL_ALPHA_OPAQUE}
  105. ,
  106. {189, 148, 123, SDL_ALPHA_OPAQUE}
  107. , {189, 156, 82, SDL_ALPHA_OPAQUE}
  108. , {189, 156, 123, SDL_ALPHA_OPAQUE}
  109. , {189, 156, 132, SDL_ALPHA_OPAQUE}
  110. ,
  111. {189, 189, 189, SDL_ALPHA_OPAQUE}
  112. , {198, 156, 123, SDL_ALPHA_OPAQUE}
  113. , {198, 165, 132, SDL_ALPHA_OPAQUE}
  114. , {206, 165, 99, SDL_ALPHA_OPAQUE}
  115. ,
  116. {206, 165, 132, SDL_ALPHA_OPAQUE}
  117. , {206, 173, 140, SDL_ALPHA_OPAQUE}
  118. , {206, 206, 206, SDL_ALPHA_OPAQUE}
  119. , {214, 173, 115, SDL_ALPHA_OPAQUE}
  120. ,
  121. {214, 173, 140, SDL_ALPHA_OPAQUE}
  122. , {222, 181, 148, SDL_ALPHA_OPAQUE}
  123. , {222, 189, 132, SDL_ALPHA_OPAQUE}
  124. , {222, 189, 156, SDL_ALPHA_OPAQUE}
  125. ,
  126. {222, 222, 222, SDL_ALPHA_OPAQUE}
  127. , {231, 198, 165, SDL_ALPHA_OPAQUE}
  128. , {231, 231, 231, SDL_ALPHA_OPAQUE}
  129. , {239, 206, 173, SDL_ALPHA_OPAQUE}
  130. };
  131. Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
  132. SDL_Texture *MooseTexture;
  133. SDL_Rect displayrect;
  134. int window_w;
  135. int window_h;
  136. SDL_Window *window;
  137. SDL_Renderer *renderer;
  138. int paused = 0;
  139. int i;
  140. SDL_bool done = SDL_FALSE;
  141. static int fpsdelay;
  142. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  143. static void
  144. quit(int rc)
  145. {
  146. SDL_Quit();
  147. exit(rc);
  148. }
  149. static void
  150. PrintUsage(char *argv0)
  151. {
  152. SDL_Log("Usage: %s [arg] [arg] [arg] ...\n", argv0);
  153. SDL_Log("\n");
  154. SDL_Log("Where 'arg' is any of the following options:\n");
  155. SDL_Log("\n");
  156. SDL_Log(" -fps <frames per second>\n");
  157. SDL_Log(" -nodelay\n");
  158. SDL_Log(" -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
  159. SDL_Log(" -scale <scale factor> (initial scale of the overlay)\n");
  160. SDL_Log(" -help (shows this help)\n");
  161. SDL_Log("\n");
  162. SDL_Log("Press ESC to exit, or SPACE to freeze the movie while application running.\n");
  163. SDL_Log("\n");
  164. }
  165. void
  166. loop()
  167. {
  168. SDL_Event event;
  169. while (SDL_PollEvent(&event)) {
  170. switch (event.type) {
  171. case SDL_WINDOWEVENT:
  172. if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
  173. SDL_RenderSetViewport(renderer, NULL);
  174. displayrect.w = window_w = event.window.data1;
  175. displayrect.h = window_h = event.window.data2;
  176. }
  177. break;
  178. case SDL_MOUSEBUTTONDOWN:
  179. displayrect.x = event.button.x - window_w / 2;
  180. displayrect.y = event.button.y - window_h / 2;
  181. break;
  182. case SDL_MOUSEMOTION:
  183. if (event.motion.state) {
  184. displayrect.x = event.motion.x - window_w / 2;
  185. displayrect.y = event.motion.y - window_h / 2;
  186. }
  187. break;
  188. case SDL_KEYDOWN:
  189. if (event.key.keysym.sym == SDLK_SPACE) {
  190. paused = !paused;
  191. break;
  192. }
  193. if (event.key.keysym.sym != SDLK_ESCAPE) {
  194. break;
  195. }
  196. case SDL_QUIT:
  197. done = SDL_TRUE;
  198. break;
  199. }
  200. }
  201. #ifndef __EMSCRIPTEN__
  202. SDL_Delay(fpsdelay);
  203. #endif
  204. if (!paused) {
  205. i = (i + 1) % MOOSEFRAMES_COUNT;
  206. SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W);
  207. }
  208. SDL_RenderClear(renderer);
  209. SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
  210. SDL_RenderPresent(renderer);
  211. #ifdef __EMSCRIPTEN__
  212. if (done) {
  213. emscripten_cancel_main_loop();
  214. }
  215. #endif
  216. }
  217. int
  218. main(int argc, char **argv)
  219. {
  220. Uint8 *RawMooseData;
  221. SDL_RWops *handle;
  222. SDL_Window *window;
  223. int j;
  224. int fps = 12;
  225. int nodelay = 0;
  226. int scale = 5;
  227. /* Enable standard application logging */
  228. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  229. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  230. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  231. return 3;
  232. }
  233. while (argc > 1) {
  234. if (SDL_strcmp(argv[1], "-fps") == 0) {
  235. if (argv[2]) {
  236. fps = SDL_atoi(argv[2]);
  237. if (fps == 0) {
  238. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  239. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  240. quit(10);
  241. }
  242. if ((fps < 0) || (fps > 1000)) {
  243. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  244. "The -fps option must be in range from 1 to 1000, default is 12.\n");
  245. quit(10);
  246. }
  247. argv += 2;
  248. argc -= 2;
  249. } else {
  250. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  251. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  252. quit(10);
  253. }
  254. } else if (SDL_strcmp(argv[1], "-nodelay") == 0) {
  255. nodelay = 1;
  256. argv += 1;
  257. argc -= 1;
  258. } else if (SDL_strcmp(argv[1], "-scale") == 0) {
  259. if (argv[2]) {
  260. scale = SDL_atoi(argv[2]);
  261. if (scale == 0) {
  262. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  263. "The -scale option requires an argument [from 1 to 50], default is 5.\n");
  264. quit(10);
  265. }
  266. if ((scale < 0) || (scale > 50)) {
  267. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  268. "The -scale option must be in range from 1 to 50, default is 5.\n");
  269. quit(10);
  270. }
  271. argv += 2;
  272. argc -= 2;
  273. } else {
  274. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  275. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  276. quit(10);
  277. }
  278. } else if ((SDL_strcmp(argv[1], "-help") == 0)
  279. || (SDL_strcmp(argv[1], "-h") == 0)) {
  280. PrintUsage(argv[0]);
  281. quit(0);
  282. } else {
  283. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
  284. quit(10);
  285. }
  286. break;
  287. }
  288. RawMooseData = (Uint8 *) SDL_malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
  289. if (RawMooseData == NULL) {
  290. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
  291. quit(1);
  292. }
  293. /* load the trojan moose images */
  294. handle = SDL_RWFromFile("moose.dat", "rb");
  295. if (handle == NULL) {
  296. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
  297. SDL_free(RawMooseData);
  298. quit(2);
  299. }
  300. SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
  301. SDL_RWclose(handle);
  302. /* Create the window and renderer */
  303. window_w = MOOSEPIC_W * scale;
  304. window_h = MOOSEPIC_H * scale;
  305. window = SDL_CreateWindow("Happy Moose",
  306. SDL_WINDOWPOS_UNDEFINED,
  307. SDL_WINDOWPOS_UNDEFINED,
  308. window_w, window_h,
  309. SDL_WINDOW_RESIZABLE);
  310. if (!window) {
  311. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
  312. SDL_free(RawMooseData);
  313. quit(4);
  314. }
  315. renderer = SDL_CreateRenderer(window, -1, 0);
  316. if (!renderer) {
  317. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
  318. SDL_free(RawMooseData);
  319. quit(4);
  320. }
  321. MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
  322. if (!MooseTexture) {
  323. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
  324. SDL_free(RawMooseData);
  325. quit(5);
  326. }
  327. /* Uncomment this to check vertex color with a YUV texture */
  328. /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
  329. for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
  330. Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
  331. Uint8 *rgb;
  332. Uint8 *frame;
  333. rgb = MooseFrameRGB;
  334. frame = RawMooseData + i * MOOSEFRAME_SIZE;
  335. for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
  336. rgb[0] = MooseColors[frame[j]].r;
  337. rgb[1] = MooseColors[frame[j]].g;
  338. rgb[2] = MooseColors[frame[j]].b;
  339. rgb += 3;
  340. }
  341. ConvertRGBtoYUV(SDL_PIXELFORMAT_YV12, MooseFrameRGB, MOOSEPIC_W*3, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H,
  342. SDL_GetYUVConversionModeForResolution(MOOSEPIC_W, MOOSEPIC_H),
  343. 0, 100);
  344. }
  345. SDL_free(RawMooseData);
  346. /* set the start frame */
  347. i = 0;
  348. if (nodelay) {
  349. fpsdelay = 0;
  350. } else {
  351. fpsdelay = 1000 / fps;
  352. }
  353. displayrect.x = 0;
  354. displayrect.y = 0;
  355. displayrect.w = window_w;
  356. displayrect.h = window_h;
  357. /* Ignore key up events, they don't even get filtered */
  358. SDL_EventState(SDL_KEYUP, SDL_IGNORE);
  359. /* Loop, waiting for QUIT or RESIZE */
  360. #ifdef __EMSCRIPTEN__
  361. emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
  362. #else
  363. while (!done) {
  364. loop();
  365. }
  366. #endif
  367. SDL_DestroyRenderer(renderer);
  368. quit(0);
  369. return 0;
  370. }
  371. /* vi: set ts=4 sw=4 expandtab: */