testautomation_render.c 43 KB

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