🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

410 lines
13 KiB

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