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

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