🛠️🐜 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.

417 lines
13 KiB

  1. /*
  2. Copyright (C) 1997-2023 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. #include "testutils.h"
  23. #define MOOSEPIC_W 64
  24. #define MOOSEPIC_H 88
  25. #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
  26. #define MOOSEFRAMES_COUNT 10
  27. SDL_Color MooseColors[84] = {
  28. {49, 49, 49, SDL_ALPHA_OPAQUE}
  29. , {66, 24, 0, SDL_ALPHA_OPAQUE}
  30. , {66, 33, 0, SDL_ALPHA_OPAQUE}
  31. , {66, 66, 66, SDL_ALPHA_OPAQUE}
  32. ,
  33. {66, 115, 49, SDL_ALPHA_OPAQUE}
  34. , {74, 33, 0, SDL_ALPHA_OPAQUE}
  35. , {74, 41, 16, SDL_ALPHA_OPAQUE}
  36. , {82, 33, 8, SDL_ALPHA_OPAQUE}
  37. ,
  38. {82, 41, 8, SDL_ALPHA_OPAQUE}
  39. , {82, 49, 16, SDL_ALPHA_OPAQUE}
  40. , {82, 82, 82, SDL_ALPHA_OPAQUE}
  41. , {90, 41, 8, SDL_ALPHA_OPAQUE}
  42. ,
  43. {90, 41, 16, SDL_ALPHA_OPAQUE}
  44. , {90, 57, 24, SDL_ALPHA_OPAQUE}
  45. , {99, 49, 16, SDL_ALPHA_OPAQUE}
  46. , {99, 66, 24, SDL_ALPHA_OPAQUE}
  47. ,
  48. {99, 66, 33, SDL_ALPHA_OPAQUE}
  49. , {99, 74, 33, SDL_ALPHA_OPAQUE}
  50. , {107, 57, 24, SDL_ALPHA_OPAQUE}
  51. , {107, 82, 41, SDL_ALPHA_OPAQUE}
  52. ,
  53. {115, 57, 33, SDL_ALPHA_OPAQUE}
  54. , {115, 66, 33, SDL_ALPHA_OPAQUE}
  55. , {115, 66, 41, SDL_ALPHA_OPAQUE}
  56. , {115, 74, 0, SDL_ALPHA_OPAQUE}
  57. ,
  58. {115, 90, 49, SDL_ALPHA_OPAQUE}
  59. , {115, 115, 115, SDL_ALPHA_OPAQUE}
  60. , {123, 82, 0, SDL_ALPHA_OPAQUE}
  61. , {123, 99, 57, SDL_ALPHA_OPAQUE}
  62. ,
  63. {132, 66, 41, SDL_ALPHA_OPAQUE}
  64. , {132, 74, 41, SDL_ALPHA_OPAQUE}
  65. , {132, 90, 8, SDL_ALPHA_OPAQUE}
  66. , {132, 99, 33, SDL_ALPHA_OPAQUE}
  67. ,
  68. {132, 99, 66, SDL_ALPHA_OPAQUE}
  69. , {132, 107, 66, SDL_ALPHA_OPAQUE}
  70. , {140, 74, 49, SDL_ALPHA_OPAQUE}
  71. , {140, 99, 16, SDL_ALPHA_OPAQUE}
  72. ,
  73. {140, 107, 74, SDL_ALPHA_OPAQUE}
  74. , {140, 115, 74, SDL_ALPHA_OPAQUE}
  75. , {148, 107, 24, SDL_ALPHA_OPAQUE}
  76. , {148, 115, 82, SDL_ALPHA_OPAQUE}
  77. ,
  78. {148, 123, 74, SDL_ALPHA_OPAQUE}
  79. , {148, 123, 90, SDL_ALPHA_OPAQUE}
  80. , {156, 115, 33, SDL_ALPHA_OPAQUE}
  81. , {156, 115, 90, SDL_ALPHA_OPAQUE}
  82. ,
  83. {156, 123, 82, SDL_ALPHA_OPAQUE}
  84. , {156, 132, 82, SDL_ALPHA_OPAQUE}
  85. , {156, 132, 99, SDL_ALPHA_OPAQUE}
  86. , {156, 156, 156, SDL_ALPHA_OPAQUE}
  87. ,
  88. {165, 123, 49, SDL_ALPHA_OPAQUE}
  89. , {165, 123, 90, SDL_ALPHA_OPAQUE}
  90. , {165, 132, 82, SDL_ALPHA_OPAQUE}
  91. , {165, 132, 90, SDL_ALPHA_OPAQUE}
  92. ,
  93. {165, 132, 99, SDL_ALPHA_OPAQUE}
  94. , {165, 140, 90, SDL_ALPHA_OPAQUE}
  95. , {173, 132, 57, SDL_ALPHA_OPAQUE}
  96. , {173, 132, 99, SDL_ALPHA_OPAQUE}
  97. ,
  98. {173, 140, 107, SDL_ALPHA_OPAQUE}
  99. , {173, 140, 115, SDL_ALPHA_OPAQUE}
  100. , {173, 148, 99, SDL_ALPHA_OPAQUE}
  101. , {173, 173, 173, SDL_ALPHA_OPAQUE}
  102. ,
  103. {181, 140, 74, SDL_ALPHA_OPAQUE}
  104. , {181, 148, 115, SDL_ALPHA_OPAQUE}
  105. , {181, 148, 123, SDL_ALPHA_OPAQUE}
  106. , {181, 156, 107, SDL_ALPHA_OPAQUE}
  107. ,
  108. {189, 148, 123, SDL_ALPHA_OPAQUE}
  109. , {189, 156, 82, SDL_ALPHA_OPAQUE}
  110. , {189, 156, 123, SDL_ALPHA_OPAQUE}
  111. , {189, 156, 132, SDL_ALPHA_OPAQUE}
  112. ,
  113. {189, 189, 189, SDL_ALPHA_OPAQUE}
  114. , {198, 156, 123, SDL_ALPHA_OPAQUE}
  115. , {198, 165, 132, SDL_ALPHA_OPAQUE}
  116. , {206, 165, 99, SDL_ALPHA_OPAQUE}
  117. ,
  118. {206, 165, 132, SDL_ALPHA_OPAQUE}
  119. , {206, 173, 140, SDL_ALPHA_OPAQUE}
  120. , {206, 206, 206, SDL_ALPHA_OPAQUE}
  121. , {214, 173, 115, SDL_ALPHA_OPAQUE}
  122. ,
  123. {214, 173, 140, SDL_ALPHA_OPAQUE}
  124. , {222, 181, 148, SDL_ALPHA_OPAQUE}
  125. , {222, 189, 132, SDL_ALPHA_OPAQUE}
  126. , {222, 189, 156, SDL_ALPHA_OPAQUE}
  127. ,
  128. {222, 222, 222, SDL_ALPHA_OPAQUE}
  129. , {231, 198, 165, SDL_ALPHA_OPAQUE}
  130. , {231, 231, 231, SDL_ALPHA_OPAQUE}
  131. , {239, 206, 173, SDL_ALPHA_OPAQUE}
  132. };
  133. Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
  134. SDL_Texture *MooseTexture;
  135. SDL_Rect displayrect;
  136. int window_w;
  137. int window_h;
  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. char *filename = NULL;
  229. /* Enable standard application logging */
  230. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  231. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  232. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  233. return 3;
  234. }
  235. while (argc > 1) {
  236. if (SDL_strcmp(argv[1], "-fps") == 0) {
  237. if (argv[2]) {
  238. fps = SDL_atoi(argv[2]);
  239. if (fps == 0) {
  240. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  241. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  242. quit(10);
  243. }
  244. if ((fps < 0) || (fps > 1000)) {
  245. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  246. "The -fps option must be in range from 1 to 1000, default is 12.\n");
  247. quit(10);
  248. }
  249. argv += 2;
  250. argc -= 2;
  251. } else {
  252. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  253. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  254. quit(10);
  255. }
  256. } else if (SDL_strcmp(argv[1], "-nodelay") == 0) {
  257. nodelay = 1;
  258. argv += 1;
  259. argc -= 1;
  260. } else if (SDL_strcmp(argv[1], "-scale") == 0) {
  261. if (argv[2]) {
  262. scale = SDL_atoi(argv[2]);
  263. if (scale == 0) {
  264. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  265. "The -scale option requires an argument [from 1 to 50], default is 5.\n");
  266. quit(10);
  267. }
  268. if ((scale < 0) || (scale > 50)) {
  269. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  270. "The -scale option must be in range from 1 to 50, default is 5.\n");
  271. quit(10);
  272. }
  273. argv += 2;
  274. argc -= 2;
  275. } else {
  276. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  277. "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
  278. quit(10);
  279. }
  280. } else if ((SDL_strcmp(argv[1], "-help") == 0)
  281. || (SDL_strcmp(argv[1], "-h") == 0)) {
  282. PrintUsage(argv[0]);
  283. quit(0);
  284. } else {
  285. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
  286. quit(10);
  287. }
  288. break;
  289. }
  290. RawMooseData = (Uint8 *) SDL_malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
  291. if (RawMooseData == NULL) {
  292. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
  293. quit(1);
  294. }
  295. /* load the trojan moose images */
  296. filename = GetResourceFilename(NULL, "moose.dat");
  297. if (filename == NULL) {
  298. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
  299. return -1;
  300. }
  301. handle = SDL_RWFromFile(filename, "rb");
  302. SDL_free(filename);
  303. if (handle == NULL) {
  304. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
  305. SDL_free(RawMooseData);
  306. quit(2);
  307. }
  308. SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
  309. SDL_RWclose(handle);
  310. /* Create the window and renderer */
  311. window_w = MOOSEPIC_W * scale;
  312. window_h = MOOSEPIC_H * scale;
  313. window = SDL_CreateWindow("Happy Moose",
  314. SDL_WINDOWPOS_UNDEFINED,
  315. SDL_WINDOWPOS_UNDEFINED,
  316. window_w, window_h,
  317. SDL_WINDOW_RESIZABLE);
  318. if (!window) {
  319. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
  320. SDL_free(RawMooseData);
  321. quit(4);
  322. }
  323. renderer = SDL_CreateRenderer(window, -1, 0);
  324. if (!renderer) {
  325. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
  326. SDL_free(RawMooseData);
  327. quit(4);
  328. }
  329. MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
  330. if (!MooseTexture) {
  331. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
  332. SDL_free(RawMooseData);
  333. quit(5);
  334. }
  335. /* Uncomment this to check vertex color with a YUV texture */
  336. /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
  337. for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
  338. Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
  339. Uint8 *rgb;
  340. Uint8 *frame;
  341. rgb = MooseFrameRGB;
  342. frame = RawMooseData + i * MOOSEFRAME_SIZE;
  343. for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
  344. rgb[0] = MooseColors[frame[j]].r;
  345. rgb[1] = MooseColors[frame[j]].g;
  346. rgb[2] = MooseColors[frame[j]].b;
  347. rgb += 3;
  348. }
  349. ConvertRGBtoYUV(SDL_PIXELFORMAT_YV12, MooseFrameRGB, MOOSEPIC_W*3, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H,
  350. SDL_GetYUVConversionModeForResolution(MOOSEPIC_W, MOOSEPIC_H),
  351. 0, 100);
  352. }
  353. SDL_free(RawMooseData);
  354. /* set the start frame */
  355. i = 0;
  356. if (nodelay) {
  357. fpsdelay = 0;
  358. } else {
  359. fpsdelay = 1000 / fps;
  360. }
  361. displayrect.x = 0;
  362. displayrect.y = 0;
  363. displayrect.w = window_w;
  364. displayrect.h = window_h;
  365. /* Ignore key up events, they don't even get filtered */
  366. SDL_EventState(SDL_KEYUP, SDL_IGNORE);
  367. /* Loop, waiting for QUIT or RESIZE */
  368. #ifdef __EMSCRIPTEN__
  369. emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
  370. #else
  371. while (!done) {
  372. loop();
  373. }
  374. #endif
  375. SDL_DestroyRenderer(renderer);
  376. quit(0);
  377. return 0;
  378. }
  379. /* vi: set ts=4 sw=4 expandtab: */