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

1170 lines
36 KiB

  1. /**
  2. * Original code: automated SDL platform test written by Edgar Simo "bobbens"
  3. * Extended and extensively updated by aschiffler at ferzkopp dot net
  4. */
  5. #include <stdio.h>
  6. #include "SDL.h"
  7. #include "SDL_test.h"
  8. /* ================= Test Case Implementation ================== */
  9. #define TESTRENDER_SCREEN_W 80
  10. #define TESTRENDER_SCREEN_H 60
  11. #define RENDER_COMPARE_FORMAT SDL_PIXELFORMAT_ARGB8888
  12. #define RENDER_COMPARE_AMASK 0xff000000 /**< Alpha bit mask. */
  13. #define RENDER_COMPARE_RMASK 0x00ff0000 /**< Red bit mask. */
  14. #define RENDER_COMPARE_GMASK 0x0000ff00 /**< Green bit mask. */
  15. #define RENDER_COMPARE_BMASK 0x000000ff /**< Blue bit mask. */
  16. #define ALLOWABLE_ERROR_OPAQUE 0
  17. #define ALLOWABLE_ERROR_BLENDED 64
  18. /* Test window and renderer */
  19. SDL_Window *window = NULL;
  20. SDL_Renderer *renderer = NULL;
  21. /* Prototypes for helper functions */
  22. static int _clearScreen(void);
  23. static void _compare(SDL_Surface *reference, int allowable_error);
  24. static int _hasTexAlpha(void);
  25. static int _hasTexColor(void);
  26. static SDL_Texture *_loadTestFace(void);
  27. static int _hasBlendModes(void);
  28. static int _hasDrawColor(void);
  29. static int _isSupported(int code);
  30. /**
  31. * Create software renderer for tests
  32. */
  33. void InitCreateRenderer(void *arg)
  34. {
  35. int posX = 100, posY = 100, width = 320, height = 240;
  36. renderer = NULL;
  37. window = SDL_CreateWindow("render_testCreateRenderer", posX, posY, width, height, 0);
  38. SDLTest_AssertPass("SDL_CreateWindow()");
  39. SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result");
  40. if (window == NULL) {
  41. return;
  42. }
  43. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  44. SDLTest_AssertPass("SDL_CreateRenderer()");
  45. SDLTest_AssertCheck(renderer != NULL, "Check SDL_CreateRenderer result");
  46. if (renderer == NULL) {
  47. SDL_DestroyWindow(window);
  48. return;
  49. }
  50. }
  51. /*
  52. * Destroy renderer for tests
  53. */
  54. void CleanupDestroyRenderer(void *arg)
  55. {
  56. if (renderer != NULL) {
  57. SDL_DestroyRenderer(renderer);
  58. renderer = NULL;
  59. SDLTest_AssertPass("SDL_DestroyRenderer()");
  60. }
  61. if (window != NULL) {
  62. SDL_DestroyWindow(window);
  63. window = NULL;
  64. SDLTest_AssertPass("SDL_DestroyWindow");
  65. }
  66. }
  67. /**
  68. * @brief Tests call to SDL_GetNumRenderDrivers
  69. *
  70. * \sa
  71. * http://wiki.libsdl.org/SDL_GetNumRenderDrivers
  72. */
  73. int render_testGetNumRenderDrivers(void *arg)
  74. {
  75. int n;
  76. n = SDL_GetNumRenderDrivers();
  77. SDLTest_AssertCheck(n >= 1, "Number of renderers >= 1, reported as %i", n);
  78. return TEST_COMPLETED;
  79. }
  80. /**
  81. * @brief Tests the SDL primitives for rendering.
  82. *
  83. * \sa
  84. * http://wiki.libsdl.org/SDL_SetRenderDrawColor
  85. * http://wiki.libsdl.org/SDL_RenderFillRect
  86. * http://wiki.libsdl.org/SDL_RenderDrawLine
  87. *
  88. */
  89. int render_testPrimitives(void *arg)
  90. {
  91. int ret;
  92. int x, y;
  93. SDL_Rect rect;
  94. SDL_Surface *referenceSurface = NULL;
  95. int checkFailCount1;
  96. int checkFailCount2;
  97. /* Clear surface. */
  98. _clearScreen();
  99. /* Need drawcolor or just skip test. */
  100. SDLTest_AssertCheck(_hasDrawColor(), "_hasDrawColor");
  101. /* Draw a rectangle. */
  102. rect.x = 40;
  103. rect.y = 0;
  104. rect.w = 40;
  105. rect.h = 80;
  106. ret = SDL_SetRenderDrawColor(renderer, 13, 73, 200, SDL_ALPHA_OPAQUE);
  107. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  108. ret = SDL_RenderFillRect(renderer, &rect);
  109. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
  110. /* Draw a rectangle. */
  111. rect.x = 10;
  112. rect.y = 10;
  113. rect.w = 60;
  114. rect.h = 40;
  115. ret = SDL_SetRenderDrawColor(renderer, 200, 0, 100, SDL_ALPHA_OPAQUE);
  116. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  117. ret = SDL_RenderFillRect(renderer, &rect);
  118. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
  119. /* Draw some points like so:
  120. * X.X.X.X..
  121. * .X.X.X.X.
  122. * X.X.X.X.. */
  123. checkFailCount1 = 0;
  124. checkFailCount2 = 0;
  125. for (y = 0; y < 3; y++) {
  126. for (x = y % 2; x < TESTRENDER_SCREEN_W; x += 2) {
  127. ret = SDL_SetRenderDrawColor(renderer, x * y, x * y / 2, x * y / 3, SDL_ALPHA_OPAQUE);
  128. if (ret != 0) {
  129. checkFailCount1++;
  130. }
  131. ret = SDL_RenderDrawPoint(renderer, x, y);
  132. if (ret != 0) {
  133. checkFailCount2++;
  134. }
  135. }
  136. }
  137. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
  138. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderDrawPoint, expected: 0, got: %i", checkFailCount2);
  139. /* Draw some lines. */
  140. ret = SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE);
  141. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor");
  142. ret = SDL_RenderDrawLine(renderer, 0, 30, TESTRENDER_SCREEN_W, 30);
  143. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
  144. ret = SDL_SetRenderDrawColor(renderer, 55, 55, 5, SDL_ALPHA_OPAQUE);
  145. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  146. ret = SDL_RenderDrawLine(renderer, 40, 30, 40, 60);
  147. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
  148. ret = SDL_SetRenderDrawColor(renderer, 5, 105, 105, SDL_ALPHA_OPAQUE);
  149. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  150. ret = SDL_RenderDrawLine(renderer, 0, 0, 29, 29);
  151. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
  152. ret = SDL_RenderDrawLine(renderer, 29, 30, 0, 59);
  153. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
  154. ret = SDL_RenderDrawLine(renderer, 79, 0, 50, 29);
  155. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
  156. ret = SDL_RenderDrawLine(renderer, 79, 59, 50, 30);
  157. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);
  158. /* See if it's the same. */
  159. referenceSurface = SDLTest_ImagePrimitives();
  160. _compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  161. /* Make current */
  162. SDL_RenderPresent(renderer);
  163. /* Clean up. */
  164. SDL_FreeSurface(referenceSurface);
  165. referenceSurface = NULL;
  166. return TEST_COMPLETED;
  167. }
  168. /**
  169. * @brief Tests the SDL primitives with alpha for rendering.
  170. *
  171. * \sa
  172. * http://wiki.libsdl.org/SDL_SetRenderDrawColor
  173. * http://wiki.libsdl.org/SDL_SetRenderDrawBlendMode
  174. * http://wiki.libsdl.org/SDL_RenderFillRect
  175. */
  176. int render_testPrimitivesBlend(void *arg)
  177. {
  178. int ret;
  179. int i, j;
  180. SDL_Rect rect;
  181. SDL_Surface *referenceSurface = NULL;
  182. int checkFailCount1;
  183. int checkFailCount2;
  184. int checkFailCount3;
  185. /* Clear surface. */
  186. _clearScreen();
  187. /* Need drawcolor and blendmode or just skip test. */
  188. SDLTest_AssertCheck(_hasDrawColor(), "_hasDrawColor");
  189. SDLTest_AssertCheck(_hasBlendModes(), "_hasBlendModes");
  190. /* Create some rectangles for each blend mode. */
  191. ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  192. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  193. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  194. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
  195. ret = SDL_RenderFillRect(renderer, NULL);
  196. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
  197. rect.x = 10;
  198. rect.y = 25;
  199. rect.w = 40;
  200. rect.h = 25;
  201. ret = SDL_SetRenderDrawColor(renderer, 240, 10, 10, 75);
  202. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  203. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
  204. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
  205. ret = SDL_RenderFillRect(renderer, &rect);
  206. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
  207. rect.x = 30;
  208. rect.y = 40;
  209. rect.w = 45;
  210. rect.h = 15;
  211. ret = SDL_SetRenderDrawColor(renderer, 10, 240, 10, 100);
  212. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  213. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  214. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
  215. ret = SDL_RenderFillRect(renderer, &rect);
  216. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
  217. rect.x = 25;
  218. rect.y = 25;
  219. rect.w = 25;
  220. rect.h = 25;
  221. ret = SDL_SetRenderDrawColor(renderer, 10, 10, 240, 125);
  222. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  223. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  224. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
  225. ret = SDL_RenderFillRect(renderer, &rect);
  226. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderFillRect, expected: 0, got: %i", ret);
  227. /* Draw blended lines, lines for everyone. */
  228. checkFailCount1 = 0;
  229. checkFailCount2 = 0;
  230. checkFailCount3 = 0;
  231. for (i = 0; i < TESTRENDER_SCREEN_W; i += 2) {
  232. ret = SDL_SetRenderDrawColor(renderer, 60 + 2 * i, 240 - 2 * i, 50, 3 * i);
  233. if (ret != 0) {
  234. checkFailCount1++;
  235. }
  236. ret = SDL_SetRenderDrawBlendMode(renderer, (((i / 2) % 3) == 0) ? SDL_BLENDMODE_BLEND : (((i / 2) % 3) == 1) ? SDL_BLENDMODE_ADD
  237. : SDL_BLENDMODE_NONE);
  238. if (ret != 0) {
  239. checkFailCount2++;
  240. }
  241. ret = SDL_RenderDrawLine(renderer, 0, 0, i, 59);
  242. if (ret != 0) {
  243. checkFailCount3++;
  244. }
  245. }
  246. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
  247. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetRenderDrawBlendMode, expected: 0, got: %i", checkFailCount2);
  248. SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_RenderDrawLine, expected: 0, got: %i", checkFailCount3);
  249. checkFailCount1 = 0;
  250. checkFailCount2 = 0;
  251. checkFailCount3 = 0;
  252. for (i = 0; i < TESTRENDER_SCREEN_H; i += 2) {
  253. ret = SDL_SetRenderDrawColor(renderer, 60 + 2 * i, 240 - 2 * i, 50, 3 * i);
  254. if (ret != 0) {
  255. checkFailCount1++;
  256. }
  257. ret = SDL_SetRenderDrawBlendMode(renderer, (((i / 2) % 3) == 0) ? SDL_BLENDMODE_BLEND : (((i / 2) % 3) == 1) ? SDL_BLENDMODE_ADD
  258. : SDL_BLENDMODE_NONE);
  259. if (ret != 0) {
  260. checkFailCount2++;
  261. }
  262. ret = SDL_RenderDrawLine(renderer, 0, 0, 79, i);
  263. if (ret != 0) {
  264. checkFailCount3++;
  265. }
  266. }
  267. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
  268. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetRenderDrawBlendMode, expected: 0, got: %i", checkFailCount2);
  269. SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_RenderDrawLine, expected: 0, got: %i", checkFailCount3);
  270. /* Draw points. */
  271. checkFailCount1 = 0;
  272. checkFailCount2 = 0;
  273. checkFailCount3 = 0;
  274. for (j = 0; j < TESTRENDER_SCREEN_H; j += 3) {
  275. for (i = 0; i < TESTRENDER_SCREEN_W; i += 3) {
  276. ret = SDL_SetRenderDrawColor(renderer, j * 4, i * 3, j * 4, i * 3);
  277. if (ret != 0) {
  278. checkFailCount1++;
  279. }
  280. ret = SDL_SetRenderDrawBlendMode(renderer, ((((i + j) / 3) % 3) == 0) ? SDL_BLENDMODE_BLEND : ((((i + j) / 3) % 3) == 1) ? SDL_BLENDMODE_ADD
  281. : SDL_BLENDMODE_NONE);
  282. if (ret != 0) {
  283. checkFailCount2++;
  284. }
  285. ret = SDL_RenderDrawPoint(renderer, i, j);
  286. if (ret != 0) {
  287. checkFailCount3++;
  288. }
  289. }
  290. }
  291. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
  292. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetRenderDrawBlendMode, expected: 0, got: %i", checkFailCount2);
  293. SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_RenderDrawPoint, expected: 0, got: %i", checkFailCount3);
  294. /* See if it's the same. */
  295. referenceSurface = SDLTest_ImagePrimitivesBlend();
  296. _compare(referenceSurface, ALLOWABLE_ERROR_BLENDED);
  297. /* Make current */
  298. SDL_RenderPresent(renderer);
  299. /* Clean up. */
  300. SDL_FreeSurface(referenceSurface);
  301. referenceSurface = NULL;
  302. return TEST_COMPLETED;
  303. }
  304. /**
  305. * @brief Tests some blitting routines.
  306. *
  307. * \sa
  308. * http://wiki.libsdl.org/SDL_RenderCopy
  309. * http://wiki.libsdl.org/SDL_DestroyTexture
  310. */
  311. int render_testBlit(void *arg)
  312. {
  313. int ret;
  314. SDL_Rect rect;
  315. SDL_Texture *tface;
  316. SDL_Surface *referenceSurface = NULL;
  317. Uint32 tformat;
  318. int taccess, tw, th;
  319. int i, j, ni, nj;
  320. int checkFailCount1;
  321. /* Clear surface. */
  322. _clearScreen();
  323. /* Need drawcolor or just skip test. */
  324. SDLTest_AssertCheck(_hasDrawColor(), "_hasDrawColor)");
  325. /* Create face surface. */
  326. tface = _loadTestFace();
  327. SDLTest_AssertCheck(tface != NULL, "Verify _loadTestFace() result");
  328. if (tface == NULL) {
  329. return TEST_ABORTED;
  330. }
  331. /* Constant values. */
  332. ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
  333. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
  334. rect.w = tw;
  335. rect.h = th;
  336. ni = TESTRENDER_SCREEN_W - tw;
  337. nj = TESTRENDER_SCREEN_H - th;
  338. /* Loop blit. */
  339. checkFailCount1 = 0;
  340. for (j = 0; j <= nj; j += 4) {
  341. for (i = 0; i <= ni; i += 4) {
  342. /* Blitting. */
  343. rect.x = i;
  344. rect.y = j;
  345. ret = SDL_RenderCopy(renderer, tface, NULL, &rect);
  346. if (ret != 0) {
  347. checkFailCount1++;
  348. }
  349. }
  350. }
  351. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount1);
  352. /* See if it's the same */
  353. referenceSurface = SDLTest_ImageBlit();
  354. _compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  355. /* Make current */
  356. SDL_RenderPresent(renderer);
  357. /* Clean up. */
  358. SDL_DestroyTexture(tface);
  359. SDL_FreeSurface(referenceSurface);
  360. referenceSurface = NULL;
  361. return TEST_COMPLETED;
  362. }
  363. /**
  364. * @brief Blits doing color tests.
  365. *
  366. * \sa
  367. * http://wiki.libsdl.org/SDL_SetTextureColorMod
  368. * http://wiki.libsdl.org/SDL_RenderCopy
  369. * http://wiki.libsdl.org/SDL_DestroyTexture
  370. */
  371. int render_testBlitColor(void *arg)
  372. {
  373. int ret;
  374. SDL_Rect rect;
  375. SDL_Texture *tface;
  376. SDL_Surface *referenceSurface = NULL;
  377. Uint32 tformat;
  378. int taccess, tw, th;
  379. int i, j, ni, nj;
  380. int checkFailCount1;
  381. int checkFailCount2;
  382. /* Clear surface. */
  383. _clearScreen();
  384. /* Create face surface. */
  385. tface = _loadTestFace();
  386. SDLTest_AssertCheck(tface != NULL, "Verify _loadTestFace() result");
  387. if (tface == NULL) {
  388. return TEST_ABORTED;
  389. }
  390. /* Constant values. */
  391. ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
  392. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
  393. rect.w = tw;
  394. rect.h = th;
  395. ni = TESTRENDER_SCREEN_W - tw;
  396. nj = TESTRENDER_SCREEN_H - th;
  397. /* Test blitting with color mod. */
  398. checkFailCount1 = 0;
  399. checkFailCount2 = 0;
  400. for (j = 0; j <= nj; j += 4) {
  401. for (i = 0; i <= ni; i += 4) {
  402. /* Set color mod. */
  403. ret = SDL_SetTextureColorMod(tface, (255 / nj) * j, (255 / ni) * i, (255 / nj) * j);
  404. if (ret != 0) {
  405. checkFailCount1++;
  406. }
  407. /* Blitting. */
  408. rect.x = i;
  409. rect.y = j;
  410. ret = SDL_RenderCopy(renderer, tface, NULL, &rect);
  411. if (ret != 0) {
  412. checkFailCount2++;
  413. }
  414. }
  415. }
  416. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
  417. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount2);
  418. /* See if it's the same. */
  419. referenceSurface = SDLTest_ImageBlitColor();
  420. _compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  421. /* Make current */
  422. SDL_RenderPresent(renderer);
  423. /* Clean up. */
  424. SDL_DestroyTexture(tface);
  425. SDL_FreeSurface(referenceSurface);
  426. referenceSurface = NULL;
  427. return TEST_COMPLETED;
  428. }
  429. /**
  430. * @brief Tests blitting with alpha.
  431. *
  432. * \sa
  433. * http://wiki.libsdl.org/SDL_SetTextureAlphaMod
  434. * http://wiki.libsdl.org/SDL_RenderCopy
  435. * http://wiki.libsdl.org/SDL_DestroyTexture
  436. */
  437. int render_testBlitAlpha(void *arg)
  438. {
  439. int ret;
  440. SDL_Rect rect;
  441. SDL_Texture *tface;
  442. SDL_Surface *referenceSurface = NULL;
  443. Uint32 tformat;
  444. int taccess, tw, th;
  445. int i, j, ni, nj;
  446. int checkFailCount1;
  447. int checkFailCount2;
  448. /* Clear surface. */
  449. _clearScreen();
  450. /* Need alpha or just skip test. */
  451. SDLTest_AssertCheck(_hasTexAlpha(), "_hasTexAlpha");
  452. /* Create face surface. */
  453. tface = _loadTestFace();
  454. SDLTest_AssertCheck(tface != NULL, "Verify _loadTestFace() result");
  455. if (tface == NULL) {
  456. return TEST_ABORTED;
  457. }
  458. /* Constant values. */
  459. ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
  460. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
  461. rect.w = tw;
  462. rect.h = th;
  463. ni = TESTRENDER_SCREEN_W - tw;
  464. nj = TESTRENDER_SCREEN_H - th;
  465. /* Test blitting with alpha mod. */
  466. checkFailCount1 = 0;
  467. checkFailCount2 = 0;
  468. for (j = 0; j <= nj; j += 4) {
  469. for (i = 0; i <= ni; i += 4) {
  470. /* Set alpha mod. */
  471. ret = SDL_SetTextureAlphaMod(tface, (255 / ni) * i);
  472. if (ret != 0) {
  473. checkFailCount1++;
  474. }
  475. /* Blitting. */
  476. rect.x = i;
  477. rect.y = j;
  478. ret = SDL_RenderCopy(renderer, tface, NULL, &rect);
  479. if (ret != 0) {
  480. checkFailCount2++;
  481. }
  482. }
  483. }
  484. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureAlphaMod, expected: 0, got: %i", checkFailCount1);
  485. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount2);
  486. /* See if it's the same. */
  487. referenceSurface = SDLTest_ImageBlitAlpha();
  488. _compare(referenceSurface, ALLOWABLE_ERROR_BLENDED);
  489. /* Make current */
  490. SDL_RenderPresent(renderer);
  491. /* Clean up. */
  492. SDL_DestroyTexture(tface);
  493. SDL_FreeSurface(referenceSurface);
  494. referenceSurface = NULL;
  495. return TEST_COMPLETED;
  496. }
  497. /* Helper functions */
  498. /**
  499. * @brief Tests a blend mode.
  500. *
  501. * \sa
  502. * http://wiki.libsdl.org/SDL_SetTextureBlendMode
  503. * http://wiki.libsdl.org/SDL_RenderCopy
  504. */
  505. static void
  506. _testBlitBlendMode(SDL_Texture *tface, int mode)
  507. {
  508. int ret;
  509. Uint32 tformat;
  510. int taccess, tw, th;
  511. int i, j, ni, nj;
  512. SDL_Rect rect;
  513. int checkFailCount1;
  514. int checkFailCount2;
  515. /* Clear surface. */
  516. _clearScreen();
  517. /* Constant values. */
  518. ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
  519. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
  520. rect.w = tw;
  521. rect.h = th;
  522. ni = TESTRENDER_SCREEN_W - tw;
  523. nj = TESTRENDER_SCREEN_H - th;
  524. /* Test blend mode. */
  525. checkFailCount1 = 0;
  526. checkFailCount2 = 0;
  527. for (j = 0; j <= nj; j += 4) {
  528. for (i = 0; i <= ni; i += 4) {
  529. /* Set blend mode. */
  530. ret = SDL_SetTextureBlendMode(tface, (SDL_BlendMode)mode);
  531. if (ret != 0) {
  532. checkFailCount1++;
  533. }
  534. /* Blitting. */
  535. rect.x = i;
  536. rect.y = j;
  537. ret = SDL_RenderCopy(renderer, tface, NULL, &rect);
  538. if (ret != 0) {
  539. checkFailCount2++;
  540. }
  541. }
  542. }
  543. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureBlendMode, expected: 0, got: %i", checkFailCount1);
  544. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount2);
  545. }
  546. /**
  547. * @brief Tests some more blitting routines.
  548. *
  549. * \sa
  550. * http://wiki.libsdl.org/SDL_SetTextureColorMod
  551. * http://wiki.libsdl.org/SDL_SetTextureAlphaMod
  552. * http://wiki.libsdl.org/SDL_SetTextureBlendMode
  553. * http://wiki.libsdl.org/SDL_DestroyTexture
  554. */
  555. int render_testBlitBlend(void *arg)
  556. {
  557. int ret;
  558. SDL_Rect rect;
  559. SDL_Texture *tface;
  560. SDL_Surface *referenceSurface = NULL;
  561. Uint32 tformat;
  562. int taccess, tw, th;
  563. int i, j, ni, nj;
  564. int mode;
  565. int checkFailCount1;
  566. int checkFailCount2;
  567. int checkFailCount3;
  568. int checkFailCount4;
  569. SDLTest_AssertCheck(_hasBlendModes(), "_hasBlendModes");
  570. SDLTest_AssertCheck(_hasTexColor(), "_hasTexColor");
  571. SDLTest_AssertCheck(_hasTexAlpha(), "_hasTexAlpha");
  572. /* Create face surface. */
  573. tface = _loadTestFace();
  574. SDLTest_AssertCheck(tface != NULL, "Verify _loadTestFace() result");
  575. if (tface == NULL) {
  576. return TEST_ABORTED;
  577. }
  578. /* Constant values. */
  579. ret = SDL_QueryTexture(tface, &tformat, &taccess, &tw, &th);
  580. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_QueryTexture, expected 0, got %i", ret);
  581. rect.w = tw;
  582. rect.h = th;
  583. ni = TESTRENDER_SCREEN_W - tw;
  584. nj = TESTRENDER_SCREEN_H - th;
  585. /* Set alpha mod. */
  586. ret = SDL_SetTextureAlphaMod(tface, 100);
  587. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetTextureAlphaMod, expected: 0, got: %i", ret);
  588. /* Test None. */
  589. _testBlitBlendMode(tface, SDL_BLENDMODE_NONE);
  590. referenceSurface = SDLTest_ImageBlitBlendNone();
  591. /* Compare, then Present */
  592. _compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  593. SDL_RenderPresent(renderer);
  594. SDL_FreeSurface(referenceSurface);
  595. referenceSurface = NULL;
  596. /* Test Blend. */
  597. _testBlitBlendMode(tface, SDL_BLENDMODE_BLEND);
  598. referenceSurface = SDLTest_ImageBlitBlend();
  599. /* Compare, then Present */
  600. _compare(referenceSurface, ALLOWABLE_ERROR_BLENDED);
  601. SDL_RenderPresent(renderer);
  602. SDL_FreeSurface(referenceSurface);
  603. referenceSurface = NULL;
  604. /* Test Add. */
  605. _testBlitBlendMode(tface, SDL_BLENDMODE_ADD);
  606. referenceSurface = SDLTest_ImageBlitBlendAdd();
  607. /* Compare, then Present */
  608. _compare(referenceSurface, ALLOWABLE_ERROR_BLENDED);
  609. SDL_RenderPresent(renderer);
  610. SDL_FreeSurface(referenceSurface);
  611. referenceSurface = NULL;
  612. /* Test Mod. */
  613. _testBlitBlendMode(tface, SDL_BLENDMODE_MOD);
  614. referenceSurface = SDLTest_ImageBlitBlendMod();
  615. /* Compare, then Present */
  616. _compare(referenceSurface, ALLOWABLE_ERROR_BLENDED);
  617. SDL_RenderPresent(renderer);
  618. SDL_FreeSurface(referenceSurface);
  619. referenceSurface = NULL;
  620. /* Clear surface. */
  621. _clearScreen();
  622. /* Loop blit. */
  623. checkFailCount1 = 0;
  624. checkFailCount2 = 0;
  625. checkFailCount3 = 0;
  626. checkFailCount4 = 0;
  627. for (j = 0; j <= nj; j += 4) {
  628. for (i = 0; i <= ni; i += 4) {
  629. /* Set color mod. */
  630. ret = SDL_SetTextureColorMod(tface, (255 / nj) * j, (255 / ni) * i, (255 / nj) * j);
  631. if (ret != 0) {
  632. checkFailCount1++;
  633. }
  634. /* Set alpha mod. */
  635. ret = SDL_SetTextureAlphaMod(tface, (100 / ni) * i);
  636. if (ret != 0) {
  637. checkFailCount2++;
  638. }
  639. /* Crazy blending mode magic. */
  640. mode = (i / 4 * j / 4) % 4;
  641. if (mode == 0) {
  642. mode = SDL_BLENDMODE_NONE;
  643. } else if (mode == 1) {
  644. mode = SDL_BLENDMODE_BLEND;
  645. } else if (mode == 2) {
  646. mode = SDL_BLENDMODE_ADD;
  647. } else if (mode == 3) {
  648. mode = SDL_BLENDMODE_MOD;
  649. }
  650. ret = SDL_SetTextureBlendMode(tface, (SDL_BlendMode)mode);
  651. if (ret != 0) {
  652. checkFailCount3++;
  653. }
  654. /* Blitting. */
  655. rect.x = i;
  656. rect.y = j;
  657. ret = SDL_RenderCopy(renderer, tface, NULL, &rect);
  658. if (ret != 0) {
  659. checkFailCount4++;
  660. }
  661. }
  662. }
  663. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
  664. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetTextureAlphaMod, expected: 0, got: %i", checkFailCount2);
  665. SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_SetTextureBlendMode, expected: 0, got: %i", checkFailCount3);
  666. SDLTest_AssertCheck(checkFailCount4 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount4);
  667. /* Clean up. */
  668. SDL_DestroyTexture(tface);
  669. /* Check to see if final image matches. */
  670. referenceSurface = SDLTest_ImageBlitBlendAll();
  671. _compare(referenceSurface, ALLOWABLE_ERROR_BLENDED);
  672. /* Make current */
  673. SDL_RenderPresent(renderer);
  674. SDL_FreeSurface(referenceSurface);
  675. referenceSurface = NULL;
  676. return TEST_COMPLETED;
  677. }
  678. /**
  679. * @brief Checks to see if functionality is supported. Helper function.
  680. */
  681. static int
  682. _isSupported(int code)
  683. {
  684. return code == 0;
  685. }
  686. /**
  687. * @brief Test to see if we can vary the draw color. Helper function.
  688. *
  689. * \sa
  690. * http://wiki.libsdl.org/SDL_SetRenderDrawColor
  691. * http://wiki.libsdl.org/SDL_GetRenderDrawColor
  692. */
  693. static int
  694. _hasDrawColor(void)
  695. {
  696. int ret, fail;
  697. Uint8 r, g, b, a;
  698. fail = 0;
  699. /* Set color. */
  700. ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
  701. if (!_isSupported(ret)) {
  702. fail = 1;
  703. }
  704. ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  705. if (!_isSupported(ret)) {
  706. fail = 1;
  707. }
  708. /* Restore natural. */
  709. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  710. if (!_isSupported(ret)) {
  711. fail = 1;
  712. }
  713. /* Something failed, consider not available. */
  714. if (fail) {
  715. return 0;
  716. }
  717. /* Not set properly, consider failed. */
  718. else if ((r != 100) || (g != 100) || (b != 100) || (a != 100)) {
  719. return 0;
  720. }
  721. return 1;
  722. }
  723. /**
  724. * @brief Test to see if we can vary the blend mode. Helper function.
  725. *
  726. * \sa
  727. * http://wiki.libsdl.org/SDL_SetRenderDrawBlendMode
  728. * http://wiki.libsdl.org/SDL_GetRenderDrawBlendMode
  729. */
  730. static int
  731. _hasBlendModes(void)
  732. {
  733. int fail;
  734. int ret;
  735. SDL_BlendMode mode;
  736. fail = 0;
  737. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  738. if (!_isSupported(ret)) {
  739. fail = 1;
  740. }
  741. ret = SDL_GetRenderDrawBlendMode(renderer, &mode);
  742. if (!_isSupported(ret)) {
  743. fail = 1;
  744. }
  745. ret = (mode != SDL_BLENDMODE_BLEND);
  746. if (!_isSupported(ret)) {
  747. fail = 1;
  748. }
  749. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
  750. if (!_isSupported(ret)) {
  751. fail = 1;
  752. }
  753. ret = SDL_GetRenderDrawBlendMode(renderer, &mode);
  754. if (!_isSupported(ret)) {
  755. fail = 1;
  756. }
  757. ret = (mode != SDL_BLENDMODE_ADD);
  758. if (!_isSupported(ret)) {
  759. fail = 1;
  760. }
  761. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_MOD);
  762. if (!_isSupported(ret)) {
  763. fail = 1;
  764. }
  765. ret = SDL_GetRenderDrawBlendMode(renderer, &mode);
  766. if (!_isSupported(ret)) {
  767. fail = 1;
  768. }
  769. ret = (mode != SDL_BLENDMODE_MOD);
  770. if (!_isSupported(ret)) {
  771. fail = 1;
  772. }
  773. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  774. if (!_isSupported(ret)) {
  775. fail = 1;
  776. }
  777. ret = SDL_GetRenderDrawBlendMode(renderer, &mode);
  778. if (!_isSupported(ret)) {
  779. fail = 1;
  780. }
  781. ret = (mode != SDL_BLENDMODE_NONE);
  782. if (!_isSupported(ret)) {
  783. fail = 1;
  784. }
  785. return !fail;
  786. }
  787. /**
  788. * @brief Loads the test image 'Face' as texture. Helper function.
  789. *
  790. * \sa
  791. * http://wiki.libsdl.org/SDL_CreateTextureFromSurface
  792. */
  793. static SDL_Texture *
  794. _loadTestFace(void)
  795. {
  796. SDL_Surface *face;
  797. SDL_Texture *tface;
  798. face = SDLTest_ImageFace();
  799. if (face == NULL) {
  800. return NULL;
  801. }
  802. tface = SDL_CreateTextureFromSurface(renderer, face);
  803. if (tface == NULL) {
  804. SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
  805. }
  806. SDL_FreeSurface(face);
  807. return tface;
  808. }
  809. /**
  810. * @brief Test to see if can set texture color mode. Helper function.
  811. *
  812. * \sa
  813. * http://wiki.libsdl.org/SDL_SetTextureColorMod
  814. * http://wiki.libsdl.org/SDL_GetTextureColorMod
  815. * http://wiki.libsdl.org/SDL_DestroyTexture
  816. */
  817. static int
  818. _hasTexColor(void)
  819. {
  820. int fail;
  821. int ret;
  822. SDL_Texture *tface;
  823. Uint8 r, g, b;
  824. /* Get test face. */
  825. tface = _loadTestFace();
  826. if (tface == NULL) {
  827. return 0;
  828. }
  829. /* See if supported. */
  830. fail = 0;
  831. ret = SDL_SetTextureColorMod(tface, 100, 100, 100);
  832. if (!_isSupported(ret)) {
  833. fail = 1;
  834. }
  835. ret = SDL_GetTextureColorMod(tface, &r, &g, &b);
  836. if (!_isSupported(ret)) {
  837. fail = 1;
  838. }
  839. /* Clean up. */
  840. SDL_DestroyTexture(tface);
  841. if (fail) {
  842. return 0;
  843. } else if ((r != 100) || (g != 100) || (b != 100)) {
  844. return 0;
  845. }
  846. return 1;
  847. }
  848. /**
  849. * @brief Test to see if we can vary the alpha of the texture. Helper function.
  850. *
  851. * \sa
  852. * http://wiki.libsdl.org/SDL_SetTextureAlphaMod
  853. * http://wiki.libsdl.org/SDL_GetTextureAlphaMod
  854. * http://wiki.libsdl.org/SDL_DestroyTexture
  855. */
  856. static int
  857. _hasTexAlpha(void)
  858. {
  859. int fail;
  860. int ret;
  861. SDL_Texture *tface;
  862. Uint8 a;
  863. /* Get test face. */
  864. tface = _loadTestFace();
  865. if (tface == NULL) {
  866. return 0;
  867. }
  868. /* See if supported. */
  869. fail = 0;
  870. ret = SDL_SetTextureAlphaMod(tface, 100);
  871. if (!_isSupported(ret)) {
  872. fail = 1;
  873. }
  874. ret = SDL_GetTextureAlphaMod(tface, &a);
  875. if (!_isSupported(ret)) {
  876. fail = 1;
  877. }
  878. /* Clean up. */
  879. SDL_DestroyTexture(tface);
  880. if (fail) {
  881. return 0;
  882. } else if (a != 100) {
  883. return 0;
  884. }
  885. return 1;
  886. }
  887. /**
  888. * @brief Compares screen pixels with image pixels. Helper function.
  889. *
  890. * @param s Image to compare against.
  891. *
  892. * \sa
  893. * http://wiki.libsdl.org/SDL_RenderReadPixels
  894. * http://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom
  895. * http://wiki.libsdl.org/SDL_FreeSurface
  896. */
  897. static void
  898. _compare(SDL_Surface *referenceSurface, int allowable_error)
  899. {
  900. int result;
  901. SDL_Rect rect;
  902. Uint8 *pixels;
  903. SDL_Surface *testSurface;
  904. /* Read pixels. */
  905. pixels = (Uint8 *)SDL_malloc(4 * TESTRENDER_SCREEN_W * TESTRENDER_SCREEN_H);
  906. SDLTest_AssertCheck(pixels != NULL, "Validate allocated temp pixel buffer");
  907. if (pixels == NULL) {
  908. return;
  909. }
  910. /* Explicitly specify the rect in case the window isn't the expected size... */
  911. rect.x = 0;
  912. rect.y = 0;
  913. rect.w = TESTRENDER_SCREEN_W;
  914. rect.h = TESTRENDER_SCREEN_H;
  915. result = SDL_RenderReadPixels(renderer, &rect, RENDER_COMPARE_FORMAT, pixels, 80 * 4);
  916. SDLTest_AssertCheck(result == 0, "Validate result from SDL_RenderReadPixels, expected: 0, got: %i", result);
  917. /* Create surface. */
  918. testSurface = SDL_CreateRGBSurfaceFrom(pixels, TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, 32, TESTRENDER_SCREEN_W * 4,
  919. RENDER_COMPARE_RMASK, RENDER_COMPARE_GMASK, RENDER_COMPARE_BMASK, RENDER_COMPARE_AMASK);
  920. SDLTest_AssertCheck(testSurface != NULL, "Verify result from SDL_CreateRGBSurfaceFrom is not NULL");
  921. /* Compare surface. */
  922. result = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  923. SDLTest_AssertCheck(result == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", result);
  924. /* Clean up. */
  925. SDL_free(pixels);
  926. SDL_FreeSurface(testSurface);
  927. }
  928. /**
  929. * @brief Clears the screen. Helper function.
  930. *
  931. * \sa
  932. * http://wiki.libsdl.org/SDL_SetRenderDrawColor
  933. * http://wiki.libsdl.org/SDL_RenderClear
  934. * http://wiki.libsdl.org/SDL_RenderPresent
  935. * http://wiki.libsdl.org/SDL_SetRenderDrawBlendMode
  936. */
  937. static int
  938. _clearScreen(void)
  939. {
  940. int ret;
  941. /* Set color. */
  942. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  943. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  944. /* Clear screen. */
  945. ret = SDL_RenderClear(renderer);
  946. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderClear, expected: 0, got: %i", ret);
  947. /* Make current */
  948. SDL_RenderPresent(renderer);
  949. /* Set defaults. */
  950. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  951. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawBlendMode, expected: 0, got: %i", ret);
  952. ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  953. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_SetRenderDrawColor, expected: 0, got: %i", ret);
  954. return 0;
  955. }
  956. /* ================= Test References ================== */
  957. /* Render test cases */
  958. static const SDLTest_TestCaseReference renderTest1 = {
  959. (SDLTest_TestCaseFp)render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED
  960. };
  961. static const SDLTest_TestCaseReference renderTest2 = {
  962. (SDLTest_TestCaseFp)render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED
  963. };
  964. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  965. static const SDLTest_TestCaseReference renderTest3 = {
  966. (SDLTest_TestCaseFp)render_testPrimitivesBlend, "render_testPrimitivesBlend", "Tests rendering primitives with blending", TEST_DISABLED
  967. };
  968. static const SDLTest_TestCaseReference renderTest4 = {
  969. (SDLTest_TestCaseFp)render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED
  970. };
  971. static const SDLTest_TestCaseReference renderTest5 = {
  972. (SDLTest_TestCaseFp)render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED
  973. };
  974. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  975. static const SDLTest_TestCaseReference renderTest6 = {
  976. (SDLTest_TestCaseFp)render_testBlitAlpha, "render_testBlitAlpha", "Tests blitting with alpha", TEST_DISABLED
  977. };
  978. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  979. static const SDLTest_TestCaseReference renderTest7 = {
  980. (SDLTest_TestCaseFp)render_testBlitBlend, "render_testBlitBlend", "Tests blitting with blending", TEST_DISABLED
  981. };
  982. /* Sequence of Render test cases */
  983. static const SDLTest_TestCaseReference *renderTests[] = {
  984. &renderTest1, &renderTest2, &renderTest3, &renderTest4, &renderTest5, &renderTest6, &renderTest7, NULL
  985. };
  986. /* Render test suite (global) */
  987. SDLTest_TestSuiteReference renderTestSuite = {
  988. "Render",
  989. InitCreateRenderer,
  990. renderTests,
  991. CleanupDestroyRenderer
  992. };