SDL_render.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. /* The SDL 2D rendering system */
  20. #include "SDL_assert.h"
  21. #include "SDL_hints.h"
  22. #include "SDL_log.h"
  23. #include "SDL_render.h"
  24. #include "SDL_sysrender.h"
  25. #include "software/SDL_render_sw_c.h"
  26. #define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
  27. #define CHECK_RENDERER_MAGIC(renderer, retval) \
  28. if (!renderer || renderer->magic != &renderer_magic) { \
  29. SDL_SetError("Invalid renderer"); \
  30. return retval; \
  31. }
  32. #define CHECK_TEXTURE_MAGIC(texture, retval) \
  33. if (!texture || texture->magic != &texture_magic) { \
  34. SDL_SetError("Invalid texture"); \
  35. return retval; \
  36. }
  37. #if !SDL_RENDER_DISABLED
  38. static const SDL_RenderDriver *render_drivers[] = {
  39. #if SDL_VIDEO_RENDER_D3D
  40. &D3D_RenderDriver,
  41. #endif
  42. #if SDL_VIDEO_RENDER_OGL
  43. &GL_RenderDriver,
  44. #endif
  45. #if SDL_VIDEO_RENDER_OGL_ES2
  46. &GLES2_RenderDriver,
  47. #endif
  48. #if SDL_VIDEO_RENDER_OGL_ES
  49. &GLES_RenderDriver,
  50. #endif
  51. #if SDL_VIDEO_RENDER_DIRECTFB
  52. &DirectFB_RenderDriver,
  53. #endif
  54. #if SDL_VIDEO_RENDER_PSP
  55. &PSP_RenderDriver,
  56. #endif
  57. &SW_RenderDriver
  58. };
  59. #endif /* !SDL_RENDER_DISABLED */
  60. static char renderer_magic;
  61. static char texture_magic;
  62. static int UpdateLogicalSize(SDL_Renderer *renderer);
  63. int
  64. SDL_GetNumRenderDrivers(void)
  65. {
  66. #if !SDL_RENDER_DISABLED
  67. return SDL_arraysize(render_drivers);
  68. #else
  69. return 0;
  70. #endif
  71. }
  72. int
  73. SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  74. {
  75. #if !SDL_RENDER_DISABLED
  76. if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  77. return SDL_SetError("index must be in the range of 0 - %d",
  78. SDL_GetNumRenderDrivers() - 1);
  79. }
  80. *info = render_drivers[index]->info;
  81. return 0;
  82. #else
  83. return SDL_SetError("SDL not built with rendering support");
  84. #endif
  85. }
  86. static int
  87. SDL_RendererEventWatch(void *userdata, SDL_Event *event)
  88. {
  89. SDL_Renderer *renderer = (SDL_Renderer *)userdata;
  90. if (event->type == SDL_WINDOWEVENT) {
  91. SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
  92. if (window == renderer->window) {
  93. if (renderer->WindowEvent) {
  94. renderer->WindowEvent(renderer, &event->window);
  95. }
  96. if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
  97. if (renderer->logical_w) {
  98. UpdateLogicalSize(renderer);
  99. } else {
  100. /* Window was resized, reset viewport */
  101. int w, h;
  102. if (renderer->GetOutputSize) {
  103. renderer->GetOutputSize(renderer, &w, &h);
  104. } else {
  105. SDL_GetWindowSize(renderer->window, &w, &h);
  106. }
  107. if (renderer->target) {
  108. renderer->viewport_backup.x = 0;
  109. renderer->viewport_backup.y = 0;
  110. renderer->viewport_backup.w = w;
  111. renderer->viewport_backup.h = h;
  112. } else {
  113. renderer->viewport.x = 0;
  114. renderer->viewport.y = 0;
  115. renderer->viewport.w = w;
  116. renderer->viewport.h = h;
  117. renderer->UpdateViewport(renderer);
  118. }
  119. }
  120. } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
  121. renderer->hidden = SDL_TRUE;
  122. } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
  123. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
  124. renderer->hidden = SDL_FALSE;
  125. }
  126. } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
  127. renderer->hidden = SDL_TRUE;
  128. } else if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
  129. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
  130. renderer->hidden = SDL_FALSE;
  131. }
  132. }
  133. }
  134. } else if (event->type == SDL_MOUSEMOTION) {
  135. if (renderer->logical_w) {
  136. event->motion.x -= renderer->viewport.x;
  137. event->motion.y -= renderer->viewport.y;
  138. event->motion.x = (int)(event->motion.x / renderer->scale.x);
  139. event->motion.y = (int)(event->motion.y / renderer->scale.y);
  140. if (event->motion.xrel > 0) {
  141. event->motion.xrel = SDL_max(1, (int)(event->motion.xrel / renderer->scale.x));
  142. } else if (event->motion.xrel < 0) {
  143. event->motion.xrel = SDL_min(-1, (int)(event->motion.xrel / renderer->scale.x));
  144. }
  145. if (event->motion.yrel > 0) {
  146. event->motion.yrel = SDL_max(1, (int)(event->motion.yrel / renderer->scale.y));
  147. } else if (event->motion.yrel < 0) {
  148. event->motion.yrel = SDL_min(-1, (int)(event->motion.yrel / renderer->scale.y));
  149. }
  150. }
  151. } else if (event->type == SDL_MOUSEBUTTONDOWN ||
  152. event->type == SDL_MOUSEBUTTONUP) {
  153. if (renderer->logical_w) {
  154. event->button.x -= renderer->viewport.x;
  155. event->button.y -= renderer->viewport.y;
  156. event->button.x = (int)(event->button.x / renderer->scale.x);
  157. event->button.y = (int)(event->button.y / renderer->scale.y);
  158. }
  159. }
  160. return 0;
  161. }
  162. int
  163. SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
  164. SDL_Window **window, SDL_Renderer **renderer)
  165. {
  166. *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
  167. SDL_WINDOWPOS_UNDEFINED,
  168. width, height, window_flags);
  169. if (!*window) {
  170. *renderer = NULL;
  171. return -1;
  172. }
  173. *renderer = SDL_CreateRenderer(*window, -1, 0);
  174. if (!*renderer) {
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. SDL_Renderer *
  180. SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
  181. {
  182. #if !SDL_RENDER_DISABLED
  183. SDL_Renderer *renderer = NULL;
  184. int n = SDL_GetNumRenderDrivers();
  185. const char *hint;
  186. if (!window) {
  187. SDL_SetError("Invalid window");
  188. return NULL;
  189. }
  190. if (SDL_GetRenderer(window)) {
  191. SDL_SetError("Renderer already associated with window");
  192. return NULL;
  193. }
  194. hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
  195. if (hint) {
  196. if (*hint == '0') {
  197. flags &= ~SDL_RENDERER_PRESENTVSYNC;
  198. } else {
  199. flags |= SDL_RENDERER_PRESENTVSYNC;
  200. }
  201. }
  202. if (index < 0) {
  203. hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
  204. if (hint) {
  205. for (index = 0; index < n; ++index) {
  206. const SDL_RenderDriver *driver = render_drivers[index];
  207. if (SDL_strcasecmp(hint, driver->info.name) == 0) {
  208. /* Create a new renderer instance */
  209. renderer = driver->CreateRenderer(window, flags);
  210. break;
  211. }
  212. }
  213. }
  214. if (!renderer) {
  215. for (index = 0; index < n; ++index) {
  216. const SDL_RenderDriver *driver = render_drivers[index];
  217. if ((driver->info.flags & flags) == flags) {
  218. /* Create a new renderer instance */
  219. renderer = driver->CreateRenderer(window, flags);
  220. if (renderer) {
  221. /* Yay, we got one! */
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. if (index == n) {
  228. SDL_SetError("Couldn't find matching render driver");
  229. return NULL;
  230. }
  231. } else {
  232. if (index >= SDL_GetNumRenderDrivers()) {
  233. SDL_SetError("index must be -1 or in the range of 0 - %d",
  234. SDL_GetNumRenderDrivers() - 1);
  235. return NULL;
  236. }
  237. /* Create a new renderer instance */
  238. renderer = render_drivers[index]->CreateRenderer(window, flags);
  239. }
  240. if (renderer) {
  241. renderer->magic = &renderer_magic;
  242. renderer->window = window;
  243. renderer->scale.x = 1.0f;
  244. renderer->scale.y = 1.0f;
  245. if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
  246. renderer->hidden = SDL_TRUE;
  247. } else {
  248. renderer->hidden = SDL_FALSE;
  249. }
  250. SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
  251. SDL_RenderSetViewport(renderer, NULL);
  252. SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
  253. SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
  254. "Created renderer: %s", renderer->info.name);
  255. }
  256. return renderer;
  257. #else
  258. SDL_SetError("SDL not built with rendering support");
  259. return NULL;
  260. #endif
  261. }
  262. SDL_Renderer *
  263. SDL_CreateSoftwareRenderer(SDL_Surface * surface)
  264. {
  265. #if !SDL_RENDER_DISABLED
  266. SDL_Renderer *renderer;
  267. renderer = SW_CreateRendererForSurface(surface);
  268. if (renderer) {
  269. renderer->magic = &renderer_magic;
  270. renderer->scale.x = 1.0f;
  271. renderer->scale.y = 1.0f;
  272. SDL_RenderSetViewport(renderer, NULL);
  273. }
  274. return renderer;
  275. #else
  276. SDL_SetError("SDL not built with rendering support");
  277. return NULL;
  278. #endif /* !SDL_RENDER_DISABLED */
  279. }
  280. SDL_Renderer *
  281. SDL_GetRenderer(SDL_Window * window)
  282. {
  283. return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
  284. }
  285. int
  286. SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
  287. {
  288. CHECK_RENDERER_MAGIC(renderer, -1);
  289. *info = renderer->info;
  290. return 0;
  291. }
  292. int
  293. SDL_GetRendererOutputSize(SDL_Renderer * renderer, int *w, int *h)
  294. {
  295. CHECK_RENDERER_MAGIC(renderer, -1);
  296. if (renderer->target) {
  297. return SDL_QueryTexture(renderer->target, NULL, NULL, w, h);
  298. } else if (renderer->GetOutputSize) {
  299. return renderer->GetOutputSize(renderer, w, h);
  300. } else if (renderer->window) {
  301. SDL_GetWindowSize(renderer->window, w, h);
  302. return 0;
  303. } else {
  304. /* This should never happen */
  305. return SDL_SetError("Renderer doesn't support querying output size");
  306. }
  307. }
  308. static SDL_bool
  309. IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
  310. {
  311. Uint32 i;
  312. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  313. if (renderer->info.texture_formats[i] == format) {
  314. return SDL_TRUE;
  315. }
  316. }
  317. return SDL_FALSE;
  318. }
  319. static Uint32
  320. GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
  321. {
  322. Uint32 i;
  323. if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  324. /* Look for an exact match */
  325. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  326. if (renderer->info.texture_formats[i] == format) {
  327. return renderer->info.texture_formats[i];
  328. }
  329. }
  330. } else {
  331. SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
  332. /* We just want to match the first format that has the same channels */
  333. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  334. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  335. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
  336. return renderer->info.texture_formats[i];
  337. }
  338. }
  339. }
  340. return renderer->info.texture_formats[0];
  341. }
  342. SDL_Texture *
  343. SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
  344. {
  345. SDL_Texture *texture;
  346. CHECK_RENDERER_MAGIC(renderer, NULL);
  347. if (!format) {
  348. format = renderer->info.texture_formats[0];
  349. }
  350. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  351. SDL_SetError("Palettized textures are not supported");
  352. return NULL;
  353. }
  354. if (w <= 0 || h <= 0) {
  355. SDL_SetError("Texture dimensions can't be 0");
  356. return NULL;
  357. }
  358. if ((renderer->info.max_texture_width && w > renderer->info.max_texture_width) ||
  359. (renderer->info.max_texture_height && h > renderer->info.max_texture_height)) {
  360. SDL_SetError("Texture dimensions are limited to %dx%d", renderer->info.max_texture_width, renderer->info.max_texture_height);
  361. return NULL;
  362. }
  363. texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  364. if (!texture) {
  365. SDL_OutOfMemory();
  366. return NULL;
  367. }
  368. texture->magic = &texture_magic;
  369. texture->format = format;
  370. texture->access = access;
  371. texture->w = w;
  372. texture->h = h;
  373. texture->r = 255;
  374. texture->g = 255;
  375. texture->b = 255;
  376. texture->a = 255;
  377. texture->renderer = renderer;
  378. texture->next = renderer->textures;
  379. if (renderer->textures) {
  380. renderer->textures->prev = texture;
  381. }
  382. renderer->textures = texture;
  383. if (IsSupportedFormat(renderer, format)) {
  384. if (renderer->CreateTexture(renderer, texture) < 0) {
  385. SDL_DestroyTexture(texture);
  386. return 0;
  387. }
  388. } else {
  389. texture->native = SDL_CreateTexture(renderer,
  390. GetClosestSupportedFormat(renderer, format),
  391. access, w, h);
  392. if (!texture->native) {
  393. SDL_DestroyTexture(texture);
  394. return NULL;
  395. }
  396. /* Swap textures to have texture before texture->native in the list */
  397. texture->native->next = texture->next;
  398. if (texture->native->next) {
  399. texture->native->next->prev = texture->native;
  400. }
  401. texture->prev = texture->native->prev;
  402. if (texture->prev) {
  403. texture->prev->next = texture;
  404. }
  405. texture->native->prev = texture;
  406. texture->next = texture->native;
  407. renderer->textures = texture;
  408. if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
  409. texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
  410. if (!texture->yuv) {
  411. SDL_DestroyTexture(texture);
  412. return NULL;
  413. }
  414. } else if (access == SDL_TEXTUREACCESS_STREAMING) {
  415. /* The pitch is 4 byte aligned */
  416. texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
  417. texture->pixels = SDL_calloc(1, texture->pitch * h);
  418. if (!texture->pixels) {
  419. SDL_DestroyTexture(texture);
  420. return NULL;
  421. }
  422. }
  423. }
  424. return texture;
  425. }
  426. SDL_Texture *
  427. SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
  428. {
  429. const SDL_PixelFormat *fmt;
  430. SDL_bool needAlpha;
  431. Uint32 i;
  432. Uint32 format;
  433. SDL_Texture *texture;
  434. CHECK_RENDERER_MAGIC(renderer, NULL);
  435. if (!surface) {
  436. SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  437. return NULL;
  438. }
  439. /* See what the best texture format is */
  440. fmt = surface->format;
  441. if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
  442. needAlpha = SDL_TRUE;
  443. } else {
  444. needAlpha = SDL_FALSE;
  445. }
  446. format = renderer->info.texture_formats[0];
  447. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  448. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  449. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
  450. format = renderer->info.texture_formats[i];
  451. break;
  452. }
  453. }
  454. texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
  455. surface->w, surface->h);
  456. if (!texture) {
  457. return NULL;
  458. }
  459. if (format == surface->format->format) {
  460. if (SDL_MUSTLOCK(surface)) {
  461. SDL_LockSurface(surface);
  462. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  463. SDL_UnlockSurface(surface);
  464. } else {
  465. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  466. }
  467. } else {
  468. SDL_PixelFormat *dst_fmt;
  469. SDL_Surface *temp = NULL;
  470. /* Set up a destination surface for the texture update */
  471. dst_fmt = SDL_AllocFormat(format);
  472. temp = SDL_ConvertSurface(surface, dst_fmt, 0);
  473. SDL_FreeFormat(dst_fmt);
  474. if (temp) {
  475. SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
  476. SDL_FreeSurface(temp);
  477. } else {
  478. SDL_DestroyTexture(texture);
  479. return NULL;
  480. }
  481. }
  482. {
  483. Uint8 r, g, b, a;
  484. SDL_BlendMode blendMode;
  485. SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  486. SDL_SetTextureColorMod(texture, r, g, b);
  487. SDL_GetSurfaceAlphaMod(surface, &a);
  488. SDL_SetTextureAlphaMod(texture, a);
  489. if (SDL_GetColorKey(surface, NULL) == 0) {
  490. /* We converted to a texture with alpha format */
  491. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  492. } else {
  493. SDL_GetSurfaceBlendMode(surface, &blendMode);
  494. SDL_SetTextureBlendMode(texture, blendMode);
  495. }
  496. }
  497. return texture;
  498. }
  499. int
  500. SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
  501. int *w, int *h)
  502. {
  503. CHECK_TEXTURE_MAGIC(texture, -1);
  504. if (format) {
  505. *format = texture->format;
  506. }
  507. if (access) {
  508. *access = texture->access;
  509. }
  510. if (w) {
  511. *w = texture->w;
  512. }
  513. if (h) {
  514. *h = texture->h;
  515. }
  516. return 0;
  517. }
  518. int
  519. SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
  520. {
  521. SDL_Renderer *renderer;
  522. CHECK_TEXTURE_MAGIC(texture, -1);
  523. renderer = texture->renderer;
  524. if (r < 255 || g < 255 || b < 255) {
  525. texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  526. } else {
  527. texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  528. }
  529. texture->r = r;
  530. texture->g = g;
  531. texture->b = b;
  532. if (texture->native) {
  533. return SDL_SetTextureColorMod(texture->native, r, g, b);
  534. } else if (renderer->SetTextureColorMod) {
  535. return renderer->SetTextureColorMod(renderer, texture);
  536. } else {
  537. return 0;
  538. }
  539. }
  540. int
  541. SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
  542. Uint8 * b)
  543. {
  544. CHECK_TEXTURE_MAGIC(texture, -1);
  545. if (r) {
  546. *r = texture->r;
  547. }
  548. if (g) {
  549. *g = texture->g;
  550. }
  551. if (b) {
  552. *b = texture->b;
  553. }
  554. return 0;
  555. }
  556. int
  557. SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
  558. {
  559. SDL_Renderer *renderer;
  560. CHECK_TEXTURE_MAGIC(texture, -1);
  561. renderer = texture->renderer;
  562. if (alpha < 255) {
  563. texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  564. } else {
  565. texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  566. }
  567. texture->a = alpha;
  568. if (texture->native) {
  569. return SDL_SetTextureAlphaMod(texture->native, alpha);
  570. } else if (renderer->SetTextureAlphaMod) {
  571. return renderer->SetTextureAlphaMod(renderer, texture);
  572. } else {
  573. return 0;
  574. }
  575. }
  576. int
  577. SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
  578. {
  579. CHECK_TEXTURE_MAGIC(texture, -1);
  580. if (alpha) {
  581. *alpha = texture->a;
  582. }
  583. return 0;
  584. }
  585. int
  586. SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
  587. {
  588. SDL_Renderer *renderer;
  589. CHECK_TEXTURE_MAGIC(texture, -1);
  590. renderer = texture->renderer;
  591. texture->blendMode = blendMode;
  592. if (texture->native) {
  593. return SDL_SetTextureBlendMode(texture->native, blendMode);
  594. } else if (renderer->SetTextureBlendMode) {
  595. return renderer->SetTextureBlendMode(renderer, texture);
  596. } else {
  597. return 0;
  598. }
  599. }
  600. int
  601. SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
  602. {
  603. CHECK_TEXTURE_MAGIC(texture, -1);
  604. if (blendMode) {
  605. *blendMode = texture->blendMode;
  606. }
  607. return 0;
  608. }
  609. static int
  610. SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
  611. const void *pixels, int pitch)
  612. {
  613. SDL_Texture *native = texture->native;
  614. SDL_Rect full_rect;
  615. if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
  616. return -1;
  617. }
  618. full_rect.x = 0;
  619. full_rect.y = 0;
  620. full_rect.w = texture->w;
  621. full_rect.h = texture->h;
  622. rect = &full_rect;
  623. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  624. /* We can lock the texture and copy to it */
  625. void *native_pixels;
  626. int native_pitch;
  627. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  628. return -1;
  629. }
  630. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  631. rect->w, rect->h, native_pixels, native_pitch);
  632. SDL_UnlockTexture(native);
  633. } else {
  634. /* Use a temporary buffer for updating */
  635. void *temp_pixels;
  636. int temp_pitch;
  637. temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  638. temp_pixels = SDL_malloc(rect->h * temp_pitch);
  639. if (!temp_pixels) {
  640. return SDL_OutOfMemory();
  641. }
  642. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  643. rect->w, rect->h, temp_pixels, temp_pitch);
  644. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  645. SDL_free(temp_pixels);
  646. }
  647. return 0;
  648. }
  649. static int
  650. SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
  651. const void *pixels, int pitch)
  652. {
  653. SDL_Texture *native = texture->native;
  654. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  655. /* We can lock the texture and copy to it */
  656. void *native_pixels;
  657. int native_pitch;
  658. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  659. return -1;
  660. }
  661. SDL_ConvertPixels(rect->w, rect->h,
  662. texture->format, pixels, pitch,
  663. native->format, native_pixels, native_pitch);
  664. SDL_UnlockTexture(native);
  665. } else {
  666. /* Use a temporary buffer for updating */
  667. void *temp_pixels;
  668. int temp_pitch;
  669. temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  670. temp_pixels = SDL_malloc(rect->h * temp_pitch);
  671. if (!temp_pixels) {
  672. return SDL_OutOfMemory();
  673. }
  674. SDL_ConvertPixels(rect->w, rect->h,
  675. texture->format, pixels, pitch,
  676. native->format, temp_pixels, temp_pitch);
  677. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  678. SDL_free(temp_pixels);
  679. }
  680. return 0;
  681. }
  682. int
  683. SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
  684. const void *pixels, int pitch)
  685. {
  686. SDL_Renderer *renderer;
  687. SDL_Rect full_rect;
  688. CHECK_TEXTURE_MAGIC(texture, -1);
  689. if (!pixels) {
  690. return SDL_InvalidParamError("pixels");
  691. }
  692. if (!pitch) {
  693. return SDL_InvalidParamError("pitch");
  694. }
  695. if (!rect) {
  696. full_rect.x = 0;
  697. full_rect.y = 0;
  698. full_rect.w = texture->w;
  699. full_rect.h = texture->h;
  700. rect = &full_rect;
  701. }
  702. if (texture->yuv) {
  703. return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
  704. } else if (texture->native) {
  705. return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
  706. } else {
  707. renderer = texture->renderer;
  708. return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  709. }
  710. }
  711. static int
  712. SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
  713. const Uint8 *Yplane, int Ypitch,
  714. const Uint8 *Uplane, int Upitch,
  715. const Uint8 *Vplane, int Vpitch)
  716. {
  717. SDL_Texture *native = texture->native;
  718. SDL_Rect full_rect;
  719. if (SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch) < 0) {
  720. return -1;
  721. }
  722. full_rect.x = 0;
  723. full_rect.y = 0;
  724. full_rect.w = texture->w;
  725. full_rect.h = texture->h;
  726. rect = &full_rect;
  727. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  728. /* We can lock the texture and copy to it */
  729. void *native_pixels;
  730. int native_pitch;
  731. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  732. return -1;
  733. }
  734. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  735. rect->w, rect->h, native_pixels, native_pitch);
  736. SDL_UnlockTexture(native);
  737. } else {
  738. /* Use a temporary buffer for updating */
  739. void *temp_pixels;
  740. int temp_pitch;
  741. temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  742. temp_pixels = SDL_malloc(rect->h * temp_pitch);
  743. if (!temp_pixels) {
  744. return SDL_OutOfMemory();
  745. }
  746. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  747. rect->w, rect->h, temp_pixels, temp_pitch);
  748. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  749. SDL_free(temp_pixels);
  750. }
  751. return 0;
  752. }
  753. int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
  754. const Uint8 *Yplane, int Ypitch,
  755. const Uint8 *Uplane, int Upitch,
  756. const Uint8 *Vplane, int Vpitch)
  757. {
  758. SDL_Renderer *renderer;
  759. SDL_Rect full_rect;
  760. CHECK_TEXTURE_MAGIC(texture, -1);
  761. if (!Yplane) {
  762. return SDL_InvalidParamError("Yplane");
  763. }
  764. if (!Ypitch) {
  765. return SDL_InvalidParamError("Ypitch");
  766. }
  767. if (!Uplane) {
  768. return SDL_InvalidParamError("Uplane");
  769. }
  770. if (!Upitch) {
  771. return SDL_InvalidParamError("Upitch");
  772. }
  773. if (!Vplane) {
  774. return SDL_InvalidParamError("Vplane");
  775. }
  776. if (!Vpitch) {
  777. return SDL_InvalidParamError("Vpitch");
  778. }
  779. if (texture->format != SDL_PIXELFORMAT_YV12 &&
  780. texture->format != SDL_PIXELFORMAT_IYUV) {
  781. return SDL_SetError("Texture format must by YV12 or IYUV");
  782. }
  783. if (!rect) {
  784. full_rect.x = 0;
  785. full_rect.y = 0;
  786. full_rect.w = texture->w;
  787. full_rect.h = texture->h;
  788. rect = &full_rect;
  789. }
  790. if (texture->yuv) {
  791. return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  792. } else {
  793. SDL_assert(!texture->native);
  794. renderer = texture->renderer;
  795. SDL_assert(renderer->UpdateTextureYUV);
  796. if (renderer->UpdateTextureYUV) {
  797. return renderer->UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  798. } else {
  799. return SDL_Unsupported();
  800. }
  801. }
  802. }
  803. static int
  804. SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
  805. void **pixels, int *pitch)
  806. {
  807. return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
  808. }
  809. static int
  810. SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
  811. void **pixels, int *pitch)
  812. {
  813. texture->locked_rect = *rect;
  814. *pixels = (void *) ((Uint8 *) texture->pixels +
  815. rect->y * texture->pitch +
  816. rect->x * SDL_BYTESPERPIXEL(texture->format));
  817. *pitch = texture->pitch;
  818. return 0;
  819. }
  820. int
  821. SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
  822. void **pixels, int *pitch)
  823. {
  824. SDL_Renderer *renderer;
  825. SDL_Rect full_rect;
  826. CHECK_TEXTURE_MAGIC(texture, -1);
  827. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  828. return SDL_SetError("SDL_LockTexture(): texture must be streaming");
  829. }
  830. if (!rect) {
  831. full_rect.x = 0;
  832. full_rect.y = 0;
  833. full_rect.w = texture->w;
  834. full_rect.h = texture->h;
  835. rect = &full_rect;
  836. }
  837. if (texture->yuv) {
  838. return SDL_LockTextureYUV(texture, rect, pixels, pitch);
  839. } else if (texture->native) {
  840. return SDL_LockTextureNative(texture, rect, pixels, pitch);
  841. } else {
  842. renderer = texture->renderer;
  843. return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
  844. }
  845. }
  846. static void
  847. SDL_UnlockTextureYUV(SDL_Texture * texture)
  848. {
  849. SDL_Texture *native = texture->native;
  850. void *native_pixels;
  851. int native_pitch;
  852. SDL_Rect rect;
  853. rect.x = 0;
  854. rect.y = 0;
  855. rect.w = texture->w;
  856. rect.h = texture->h;
  857. if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
  858. return;
  859. }
  860. SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
  861. rect.w, rect.h, native_pixels, native_pitch);
  862. SDL_UnlockTexture(native);
  863. }
  864. static void
  865. SDL_UnlockTextureNative(SDL_Texture * texture)
  866. {
  867. SDL_Texture *native = texture->native;
  868. void *native_pixels;
  869. int native_pitch;
  870. const SDL_Rect *rect = &texture->locked_rect;
  871. const void* pixels = (void *) ((Uint8 *) texture->pixels +
  872. rect->y * texture->pitch +
  873. rect->x * SDL_BYTESPERPIXEL(texture->format));
  874. int pitch = texture->pitch;
  875. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  876. return;
  877. }
  878. SDL_ConvertPixels(rect->w, rect->h,
  879. texture->format, pixels, pitch,
  880. native->format, native_pixels, native_pitch);
  881. SDL_UnlockTexture(native);
  882. }
  883. void
  884. SDL_UnlockTexture(SDL_Texture * texture)
  885. {
  886. SDL_Renderer *renderer;
  887. CHECK_TEXTURE_MAGIC(texture, );
  888. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  889. return;
  890. }
  891. if (texture->yuv) {
  892. SDL_UnlockTextureYUV(texture);
  893. } else if (texture->native) {
  894. SDL_UnlockTextureNative(texture);
  895. } else {
  896. renderer = texture->renderer;
  897. renderer->UnlockTexture(renderer, texture);
  898. }
  899. }
  900. SDL_bool
  901. SDL_RenderTargetSupported(SDL_Renderer *renderer)
  902. {
  903. if (!renderer || !renderer->SetRenderTarget) {
  904. return SDL_FALSE;
  905. }
  906. return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
  907. }
  908. int
  909. SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  910. {
  911. if (!SDL_RenderTargetSupported(renderer)) {
  912. return SDL_Unsupported();
  913. }
  914. if (texture == renderer->target) {
  915. /* Nothing to do! */
  916. return 0;
  917. }
  918. /* texture == NULL is valid and means reset the target to the window */
  919. if (texture) {
  920. CHECK_TEXTURE_MAGIC(texture, -1);
  921. if (renderer != texture->renderer) {
  922. return SDL_SetError("Texture was not created with this renderer");
  923. }
  924. if (texture->access != SDL_TEXTUREACCESS_TARGET) {
  925. return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
  926. }
  927. if (texture->native) {
  928. /* Always render to the native texture */
  929. texture = texture->native;
  930. }
  931. }
  932. if (texture && !renderer->target) {
  933. /* Make a backup of the viewport */
  934. renderer->viewport_backup = renderer->viewport;
  935. renderer->clip_rect_backup = renderer->clip_rect;
  936. renderer->scale_backup = renderer->scale;
  937. renderer->logical_w_backup = renderer->logical_w;
  938. renderer->logical_h_backup = renderer->logical_h;
  939. }
  940. renderer->target = texture;
  941. if (renderer->SetRenderTarget(renderer, texture) < 0) {
  942. return -1;
  943. }
  944. if (texture) {
  945. renderer->viewport.x = 0;
  946. renderer->viewport.y = 0;
  947. renderer->viewport.w = texture->w;
  948. renderer->viewport.h = texture->h;
  949. renderer->scale.x = 1.0f;
  950. renderer->scale.y = 1.0f;
  951. renderer->logical_w = texture->w;
  952. renderer->logical_h = texture->h;
  953. } else {
  954. renderer->viewport = renderer->viewport_backup;
  955. renderer->clip_rect = renderer->clip_rect_backup;
  956. renderer->scale = renderer->scale_backup;
  957. renderer->logical_w = renderer->logical_w_backup;
  958. renderer->logical_h = renderer->logical_h_backup;
  959. }
  960. if (renderer->UpdateViewport(renderer) < 0) {
  961. return -1;
  962. }
  963. if (renderer->UpdateClipRect(renderer) < 0) {
  964. return -1;
  965. }
  966. /* All set! */
  967. return 0;
  968. }
  969. SDL_Texture *
  970. SDL_GetRenderTarget(SDL_Renderer *renderer)
  971. {
  972. return renderer->target;
  973. }
  974. static int
  975. UpdateLogicalSize(SDL_Renderer *renderer)
  976. {
  977. int w, h;
  978. float want_aspect;
  979. float real_aspect;
  980. float scale;
  981. SDL_Rect viewport;
  982. if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
  983. return -1;
  984. }
  985. want_aspect = (float)renderer->logical_w / renderer->logical_h;
  986. real_aspect = (float)w / h;
  987. /* Clear the scale because we're setting viewport in output coordinates */
  988. SDL_RenderSetScale(renderer, 1.0f, 1.0f);
  989. if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
  990. /* The aspect ratios are the same, just scale appropriately */
  991. scale = (float)w / renderer->logical_w;
  992. SDL_RenderSetViewport(renderer, NULL);
  993. } else if (want_aspect > real_aspect) {
  994. /* We want a wider aspect ratio than is available - letterbox it */
  995. scale = (float)w / renderer->logical_w;
  996. viewport.x = 0;
  997. viewport.w = w;
  998. viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
  999. viewport.y = (h - viewport.h) / 2;
  1000. SDL_RenderSetViewport(renderer, &viewport);
  1001. } else {
  1002. /* We want a narrower aspect ratio than is available - use side-bars */
  1003. scale = (float)h / renderer->logical_h;
  1004. viewport.y = 0;
  1005. viewport.h = h;
  1006. viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
  1007. viewport.x = (w - viewport.w) / 2;
  1008. SDL_RenderSetViewport(renderer, &viewport);
  1009. }
  1010. /* Set the new scale */
  1011. SDL_RenderSetScale(renderer, scale, scale);
  1012. return 0;
  1013. }
  1014. int
  1015. SDL_RenderSetLogicalSize(SDL_Renderer * renderer, int w, int h)
  1016. {
  1017. CHECK_RENDERER_MAGIC(renderer, -1);
  1018. if (!w || !h) {
  1019. /* Clear any previous logical resolution */
  1020. renderer->logical_w = 0;
  1021. renderer->logical_h = 0;
  1022. SDL_RenderSetViewport(renderer, NULL);
  1023. SDL_RenderSetScale(renderer, 1.0f, 1.0f);
  1024. return 0;
  1025. }
  1026. renderer->logical_w = w;
  1027. renderer->logical_h = h;
  1028. return UpdateLogicalSize(renderer);
  1029. }
  1030. void
  1031. SDL_RenderGetLogicalSize(SDL_Renderer * renderer, int *w, int *h)
  1032. {
  1033. CHECK_RENDERER_MAGIC(renderer, );
  1034. if (w) {
  1035. *w = renderer->logical_w;
  1036. }
  1037. if (h) {
  1038. *h = renderer->logical_h;
  1039. }
  1040. }
  1041. int
  1042. SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
  1043. {
  1044. CHECK_RENDERER_MAGIC(renderer, -1);
  1045. if (rect) {
  1046. renderer->viewport.x = (int)SDL_floor(rect->x * renderer->scale.x);
  1047. renderer->viewport.y = (int)SDL_floor(rect->y * renderer->scale.y);
  1048. renderer->viewport.w = (int)SDL_ceil(rect->w * renderer->scale.x);
  1049. renderer->viewport.h = (int)SDL_ceil(rect->h * renderer->scale.y);
  1050. } else {
  1051. renderer->viewport.x = 0;
  1052. renderer->viewport.y = 0;
  1053. if (SDL_GetRendererOutputSize(renderer, &renderer->viewport.w, &renderer->viewport.h) < 0) {
  1054. return -1;
  1055. }
  1056. }
  1057. return renderer->UpdateViewport(renderer);
  1058. }
  1059. void
  1060. SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
  1061. {
  1062. CHECK_RENDERER_MAGIC(renderer, );
  1063. if (rect) {
  1064. rect->x = (int)(renderer->viewport.x / renderer->scale.x);
  1065. rect->y = (int)(renderer->viewport.y / renderer->scale.y);
  1066. rect->w = (int)(renderer->viewport.w / renderer->scale.x);
  1067. rect->h = (int)(renderer->viewport.h / renderer->scale.y);
  1068. }
  1069. }
  1070. int
  1071. SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  1072. {
  1073. CHECK_RENDERER_MAGIC(renderer, -1)
  1074. if (rect) {
  1075. renderer->clip_rect.x = (int)SDL_floor(rect->x * renderer->scale.x);
  1076. renderer->clip_rect.y = (int)SDL_floor(rect->y * renderer->scale.y);
  1077. renderer->clip_rect.w = (int)SDL_ceil(rect->w * renderer->scale.x);
  1078. renderer->clip_rect.h = (int)SDL_ceil(rect->h * renderer->scale.y);
  1079. } else {
  1080. SDL_zero(renderer->clip_rect);
  1081. }
  1082. return renderer->UpdateClipRect(renderer);
  1083. }
  1084. void
  1085. SDL_RenderGetClipRect(SDL_Renderer * renderer, SDL_Rect * rect)
  1086. {
  1087. CHECK_RENDERER_MAGIC(renderer, )
  1088. if (rect) {
  1089. rect->x = (int)(renderer->clip_rect.x / renderer->scale.x);
  1090. rect->y = (int)(renderer->clip_rect.y / renderer->scale.y);
  1091. rect->w = (int)(renderer->clip_rect.w / renderer->scale.x);
  1092. rect->h = (int)(renderer->clip_rect.h / renderer->scale.y);
  1093. }
  1094. }
  1095. int
  1096. SDL_RenderSetScale(SDL_Renderer * renderer, float scaleX, float scaleY)
  1097. {
  1098. CHECK_RENDERER_MAGIC(renderer, -1);
  1099. renderer->scale.x = scaleX;
  1100. renderer->scale.y = scaleY;
  1101. return 0;
  1102. }
  1103. void
  1104. SDL_RenderGetScale(SDL_Renderer * renderer, float *scaleX, float *scaleY)
  1105. {
  1106. CHECK_RENDERER_MAGIC(renderer, );
  1107. if (scaleX) {
  1108. *scaleX = renderer->scale.x;
  1109. }
  1110. if (scaleY) {
  1111. *scaleY = renderer->scale.y;
  1112. }
  1113. }
  1114. int
  1115. SDL_SetRenderDrawColor(SDL_Renderer * renderer,
  1116. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1117. {
  1118. CHECK_RENDERER_MAGIC(renderer, -1);
  1119. renderer->r = r;
  1120. renderer->g = g;
  1121. renderer->b = b;
  1122. renderer->a = a;
  1123. return 0;
  1124. }
  1125. int
  1126. SDL_GetRenderDrawColor(SDL_Renderer * renderer,
  1127. Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  1128. {
  1129. CHECK_RENDERER_MAGIC(renderer, -1);
  1130. if (r) {
  1131. *r = renderer->r;
  1132. }
  1133. if (g) {
  1134. *g = renderer->g;
  1135. }
  1136. if (b) {
  1137. *b = renderer->b;
  1138. }
  1139. if (a) {
  1140. *a = renderer->a;
  1141. }
  1142. return 0;
  1143. }
  1144. int
  1145. SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  1146. {
  1147. CHECK_RENDERER_MAGIC(renderer, -1);
  1148. renderer->blendMode = blendMode;
  1149. return 0;
  1150. }
  1151. int
  1152. SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
  1153. {
  1154. CHECK_RENDERER_MAGIC(renderer, -1);
  1155. *blendMode = renderer->blendMode;
  1156. return 0;
  1157. }
  1158. int
  1159. SDL_RenderClear(SDL_Renderer * renderer)
  1160. {
  1161. CHECK_RENDERER_MAGIC(renderer, -1);
  1162. /* Don't draw while we're hidden */
  1163. if (renderer->hidden) {
  1164. return 0;
  1165. }
  1166. return renderer->RenderClear(renderer);
  1167. }
  1168. int
  1169. SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
  1170. {
  1171. SDL_Point point;
  1172. point.x = x;
  1173. point.y = y;
  1174. return SDL_RenderDrawPoints(renderer, &point, 1);
  1175. }
  1176. static int
  1177. RenderDrawPointsWithRects(SDL_Renderer * renderer,
  1178. const SDL_Point * points, int count)
  1179. {
  1180. SDL_FRect *frects;
  1181. int i;
  1182. int status;
  1183. frects = SDL_stack_alloc(SDL_FRect, count);
  1184. if (!frects) {
  1185. return SDL_OutOfMemory();
  1186. }
  1187. for (i = 0; i < count; ++i) {
  1188. frects[i].x = points[i].x * renderer->scale.x;
  1189. frects[i].y = points[i].y * renderer->scale.y;
  1190. frects[i].w = renderer->scale.x;
  1191. frects[i].h = renderer->scale.y;
  1192. }
  1193. status = renderer->RenderFillRects(renderer, frects, count);
  1194. SDL_stack_free(frects);
  1195. return status;
  1196. }
  1197. int
  1198. SDL_RenderDrawPoints(SDL_Renderer * renderer,
  1199. const SDL_Point * points, int count)
  1200. {
  1201. SDL_FPoint *fpoints;
  1202. int i;
  1203. int status;
  1204. CHECK_RENDERER_MAGIC(renderer, -1);
  1205. if (!points) {
  1206. return SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
  1207. }
  1208. if (count < 1) {
  1209. return 0;
  1210. }
  1211. /* Don't draw while we're hidden */
  1212. if (renderer->hidden) {
  1213. return 0;
  1214. }
  1215. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  1216. return RenderDrawPointsWithRects(renderer, points, count);
  1217. }
  1218. fpoints = SDL_stack_alloc(SDL_FPoint, count);
  1219. if (!fpoints) {
  1220. return SDL_OutOfMemory();
  1221. }
  1222. for (i = 0; i < count; ++i) {
  1223. fpoints[i].x = points[i].x * renderer->scale.x;
  1224. fpoints[i].y = points[i].y * renderer->scale.y;
  1225. }
  1226. status = renderer->RenderDrawPoints(renderer, fpoints, count);
  1227. SDL_stack_free(fpoints);
  1228. return status;
  1229. }
  1230. int
  1231. SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
  1232. {
  1233. SDL_Point points[2];
  1234. points[0].x = x1;
  1235. points[0].y = y1;
  1236. points[1].x = x2;
  1237. points[1].y = y2;
  1238. return SDL_RenderDrawLines(renderer, points, 2);
  1239. }
  1240. static int
  1241. RenderDrawLinesWithRects(SDL_Renderer * renderer,
  1242. const SDL_Point * points, int count)
  1243. {
  1244. SDL_FRect *frect;
  1245. SDL_FRect *frects;
  1246. SDL_FPoint fpoints[2];
  1247. int i, nrects;
  1248. int status;
  1249. frects = SDL_stack_alloc(SDL_FRect, count-1);
  1250. if (!frects) {
  1251. return SDL_OutOfMemory();
  1252. }
  1253. status = 0;
  1254. nrects = 0;
  1255. for (i = 0; i < count-1; ++i) {
  1256. if (points[i].x == points[i+1].x) {
  1257. int minY = SDL_min(points[i].y, points[i+1].y);
  1258. int maxY = SDL_max(points[i].y, points[i+1].y);
  1259. frect = &frects[nrects++];
  1260. frect->x = points[i].x * renderer->scale.x;
  1261. frect->y = minY * renderer->scale.y;
  1262. frect->w = renderer->scale.x;
  1263. frect->h = (maxY - minY + 1) * renderer->scale.y;
  1264. } else if (points[i].y == points[i+1].y) {
  1265. int minX = SDL_min(points[i].x, points[i+1].x);
  1266. int maxX = SDL_max(points[i].x, points[i+1].x);
  1267. frect = &frects[nrects++];
  1268. frect->x = minX * renderer->scale.x;
  1269. frect->y = points[i].y * renderer->scale.y;
  1270. frect->w = (maxX - minX + 1) * renderer->scale.x;
  1271. frect->h = renderer->scale.y;
  1272. } else {
  1273. /* FIXME: We can't use a rect for this line... */
  1274. fpoints[0].x = points[i].x * renderer->scale.x;
  1275. fpoints[0].y = points[i].y * renderer->scale.y;
  1276. fpoints[1].x = points[i+1].x * renderer->scale.x;
  1277. fpoints[1].y = points[i+1].y * renderer->scale.y;
  1278. status += renderer->RenderDrawLines(renderer, fpoints, 2);
  1279. }
  1280. }
  1281. status += renderer->RenderFillRects(renderer, frects, nrects);
  1282. SDL_stack_free(frects);
  1283. if (status < 0) {
  1284. status = -1;
  1285. }
  1286. return status;
  1287. }
  1288. int
  1289. SDL_RenderDrawLines(SDL_Renderer * renderer,
  1290. const SDL_Point * points, int count)
  1291. {
  1292. SDL_FPoint *fpoints;
  1293. int i;
  1294. int status;
  1295. CHECK_RENDERER_MAGIC(renderer, -1);
  1296. if (!points) {
  1297. return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
  1298. }
  1299. if (count < 2) {
  1300. return 0;
  1301. }
  1302. /* Don't draw while we're hidden */
  1303. if (renderer->hidden) {
  1304. return 0;
  1305. }
  1306. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  1307. return RenderDrawLinesWithRects(renderer, points, count);
  1308. }
  1309. fpoints = SDL_stack_alloc(SDL_FPoint, count);
  1310. if (!fpoints) {
  1311. return SDL_OutOfMemory();
  1312. }
  1313. for (i = 0; i < count; ++i) {
  1314. fpoints[i].x = points[i].x * renderer->scale.x;
  1315. fpoints[i].y = points[i].y * renderer->scale.y;
  1316. }
  1317. status = renderer->RenderDrawLines(renderer, fpoints, count);
  1318. SDL_stack_free(fpoints);
  1319. return status;
  1320. }
  1321. int
  1322. SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  1323. {
  1324. SDL_Rect full_rect;
  1325. SDL_Point points[5];
  1326. CHECK_RENDERER_MAGIC(renderer, -1);
  1327. /* If 'rect' == NULL, then outline the whole surface */
  1328. if (!rect) {
  1329. SDL_RenderGetViewport(renderer, &full_rect);
  1330. full_rect.x = 0;
  1331. full_rect.y = 0;
  1332. rect = &full_rect;
  1333. }
  1334. points[0].x = rect->x;
  1335. points[0].y = rect->y;
  1336. points[1].x = rect->x+rect->w-1;
  1337. points[1].y = rect->y;
  1338. points[2].x = rect->x+rect->w-1;
  1339. points[2].y = rect->y+rect->h-1;
  1340. points[3].x = rect->x;
  1341. points[3].y = rect->y+rect->h-1;
  1342. points[4].x = rect->x;
  1343. points[4].y = rect->y;
  1344. return SDL_RenderDrawLines(renderer, points, 5);
  1345. }
  1346. int
  1347. SDL_RenderDrawRects(SDL_Renderer * renderer,
  1348. const SDL_Rect * rects, int count)
  1349. {
  1350. int i;
  1351. CHECK_RENDERER_MAGIC(renderer, -1);
  1352. if (!rects) {
  1353. return SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
  1354. }
  1355. if (count < 1) {
  1356. return 0;
  1357. }
  1358. /* Don't draw while we're hidden */
  1359. if (renderer->hidden) {
  1360. return 0;
  1361. }
  1362. for (i = 0; i < count; ++i) {
  1363. if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
  1364. return -1;
  1365. }
  1366. }
  1367. return 0;
  1368. }
  1369. int
  1370. SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  1371. {
  1372. SDL_Rect full_rect = { 0, 0, 0, 0 };
  1373. CHECK_RENDERER_MAGIC(renderer, -1);
  1374. /* If 'rect' == NULL, then outline the whole surface */
  1375. if (!rect) {
  1376. SDL_RenderGetViewport(renderer, &full_rect);
  1377. full_rect.x = 0;
  1378. full_rect.y = 0;
  1379. rect = &full_rect;
  1380. }
  1381. return SDL_RenderFillRects(renderer, rect, 1);
  1382. }
  1383. int
  1384. SDL_RenderFillRects(SDL_Renderer * renderer,
  1385. const SDL_Rect * rects, int count)
  1386. {
  1387. SDL_FRect *frects;
  1388. int i;
  1389. int status;
  1390. CHECK_RENDERER_MAGIC(renderer, -1);
  1391. if (!rects) {
  1392. return SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
  1393. }
  1394. if (count < 1) {
  1395. return 0;
  1396. }
  1397. /* Don't draw while we're hidden */
  1398. if (renderer->hidden) {
  1399. return 0;
  1400. }
  1401. frects = SDL_stack_alloc(SDL_FRect, count);
  1402. if (!frects) {
  1403. return SDL_OutOfMemory();
  1404. }
  1405. for (i = 0; i < count; ++i) {
  1406. frects[i].x = rects[i].x * renderer->scale.x;
  1407. frects[i].y = rects[i].y * renderer->scale.y;
  1408. frects[i].w = rects[i].w * renderer->scale.x;
  1409. frects[i].h = rects[i].h * renderer->scale.y;
  1410. }
  1411. status = renderer->RenderFillRects(renderer, frects, count);
  1412. SDL_stack_free(frects);
  1413. return status;
  1414. }
  1415. int
  1416. SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  1417. const SDL_Rect * srcrect, const SDL_Rect * dstrect)
  1418. {
  1419. SDL_Rect real_srcrect = { 0, 0, 0, 0 };
  1420. SDL_Rect real_dstrect = { 0, 0, 0, 0 };
  1421. SDL_FRect frect;
  1422. CHECK_RENDERER_MAGIC(renderer, -1);
  1423. CHECK_TEXTURE_MAGIC(texture, -1);
  1424. if (renderer != texture->renderer) {
  1425. return SDL_SetError("Texture was not created with this renderer");
  1426. }
  1427. real_srcrect.x = 0;
  1428. real_srcrect.y = 0;
  1429. real_srcrect.w = texture->w;
  1430. real_srcrect.h = texture->h;
  1431. if (srcrect) {
  1432. if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  1433. return 0;
  1434. }
  1435. }
  1436. SDL_RenderGetViewport(renderer, &real_dstrect);
  1437. real_dstrect.x = 0;
  1438. real_dstrect.y = 0;
  1439. if (dstrect) {
  1440. if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
  1441. return 0;
  1442. }
  1443. real_dstrect = *dstrect;
  1444. }
  1445. if (texture->native) {
  1446. texture = texture->native;
  1447. }
  1448. /* Don't draw while we're hidden */
  1449. if (renderer->hidden) {
  1450. return 0;
  1451. }
  1452. frect.x = real_dstrect.x * renderer->scale.x;
  1453. frect.y = real_dstrect.y * renderer->scale.y;
  1454. frect.w = real_dstrect.w * renderer->scale.x;
  1455. frect.h = real_dstrect.h * renderer->scale.y;
  1456. return renderer->RenderCopy(renderer, texture, &real_srcrect, &frect);
  1457. }
  1458. int
  1459. SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  1460. const SDL_Rect * srcrect, const SDL_Rect * dstrect,
  1461. const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
  1462. {
  1463. SDL_Rect real_srcrect = { 0, 0, 0, 0 };
  1464. SDL_Rect real_dstrect = { 0, 0, 0, 0 };
  1465. SDL_Point real_center;
  1466. SDL_FRect frect;
  1467. SDL_FPoint fcenter;
  1468. CHECK_RENDERER_MAGIC(renderer, -1);
  1469. CHECK_TEXTURE_MAGIC(texture, -1);
  1470. if (renderer != texture->renderer) {
  1471. return SDL_SetError("Texture was not created with this renderer");
  1472. }
  1473. if (!renderer->RenderCopyEx) {
  1474. return SDL_SetError("Renderer does not support RenderCopyEx");
  1475. }
  1476. real_srcrect.x = 0;
  1477. real_srcrect.y = 0;
  1478. real_srcrect.w = texture->w;
  1479. real_srcrect.h = texture->h;
  1480. if (srcrect) {
  1481. if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  1482. return 0;
  1483. }
  1484. }
  1485. /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
  1486. if (dstrect) {
  1487. real_dstrect = *dstrect;
  1488. } else {
  1489. SDL_RenderGetViewport(renderer, &real_dstrect);
  1490. real_dstrect.x = 0;
  1491. real_dstrect.y = 0;
  1492. }
  1493. if (texture->native) {
  1494. texture = texture->native;
  1495. }
  1496. if(center) real_center = *center;
  1497. else {
  1498. real_center.x = real_dstrect.w/2;
  1499. real_center.y = real_dstrect.h/2;
  1500. }
  1501. frect.x = real_dstrect.x * renderer->scale.x;
  1502. frect.y = real_dstrect.y * renderer->scale.y;
  1503. frect.w = real_dstrect.w * renderer->scale.x;
  1504. frect.h = real_dstrect.h * renderer->scale.y;
  1505. fcenter.x = real_center.x * renderer->scale.x;
  1506. fcenter.y = real_center.y * renderer->scale.y;
  1507. return renderer->RenderCopyEx(renderer, texture, &real_srcrect, &frect, angle, &fcenter, flip);
  1508. }
  1509. int
  1510. SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  1511. Uint32 format, void * pixels, int pitch)
  1512. {
  1513. SDL_Rect real_rect;
  1514. CHECK_RENDERER_MAGIC(renderer, -1);
  1515. if (!renderer->RenderReadPixels) {
  1516. return SDL_Unsupported();
  1517. }
  1518. if (!format) {
  1519. format = SDL_GetWindowPixelFormat(renderer->window);
  1520. }
  1521. real_rect.x = renderer->viewport.x;
  1522. real_rect.y = renderer->viewport.y;
  1523. real_rect.w = renderer->viewport.w;
  1524. real_rect.h = renderer->viewport.h;
  1525. if (rect) {
  1526. if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1527. return 0;
  1528. }
  1529. if (real_rect.y > rect->y) {
  1530. pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  1531. }
  1532. if (real_rect.x > rect->x) {
  1533. int bpp = SDL_BYTESPERPIXEL(format);
  1534. pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  1535. }
  1536. }
  1537. return renderer->RenderReadPixels(renderer, &real_rect,
  1538. format, pixels, pitch);
  1539. }
  1540. void
  1541. SDL_RenderPresent(SDL_Renderer * renderer)
  1542. {
  1543. CHECK_RENDERER_MAGIC(renderer, );
  1544. /* Don't draw while we're hidden */
  1545. if (renderer->hidden) {
  1546. return;
  1547. }
  1548. renderer->RenderPresent(renderer);
  1549. }
  1550. void
  1551. SDL_DestroyTexture(SDL_Texture * texture)
  1552. {
  1553. SDL_Renderer *renderer;
  1554. CHECK_TEXTURE_MAGIC(texture, );
  1555. renderer = texture->renderer;
  1556. if (texture == renderer->target) {
  1557. SDL_SetRenderTarget(renderer, NULL);
  1558. }
  1559. texture->magic = NULL;
  1560. if (texture->next) {
  1561. texture->next->prev = texture->prev;
  1562. }
  1563. if (texture->prev) {
  1564. texture->prev->next = texture->next;
  1565. } else {
  1566. renderer->textures = texture->next;
  1567. }
  1568. if (texture->native) {
  1569. SDL_DestroyTexture(texture->native);
  1570. }
  1571. if (texture->yuv) {
  1572. SDL_SW_DestroyYUVTexture(texture->yuv);
  1573. }
  1574. SDL_free(texture->pixels);
  1575. renderer->DestroyTexture(renderer, texture);
  1576. SDL_free(texture);
  1577. }
  1578. void
  1579. SDL_DestroyRenderer(SDL_Renderer * renderer)
  1580. {
  1581. CHECK_RENDERER_MAGIC(renderer, );
  1582. SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
  1583. /* Free existing textures for this renderer */
  1584. while (renderer->textures) {
  1585. SDL_DestroyTexture(renderer->textures);
  1586. }
  1587. if (renderer->window) {
  1588. SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
  1589. }
  1590. /* It's no longer magical... */
  1591. renderer->magic = NULL;
  1592. /* Free the renderer instance */
  1593. renderer->DestroyRenderer(renderer);
  1594. }
  1595. int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
  1596. {
  1597. SDL_Renderer *renderer;
  1598. CHECK_TEXTURE_MAGIC(texture, -1);
  1599. renderer = texture->renderer;
  1600. if (texture->native) {
  1601. return SDL_GL_BindTexture(texture->native, texw, texh);
  1602. } else if (renderer && renderer->GL_BindTexture) {
  1603. return renderer->GL_BindTexture(renderer, texture, texw, texh);
  1604. } else {
  1605. return SDL_Unsupported();
  1606. }
  1607. }
  1608. int SDL_GL_UnbindTexture(SDL_Texture *texture)
  1609. {
  1610. SDL_Renderer *renderer;
  1611. CHECK_TEXTURE_MAGIC(texture, -1);
  1612. renderer = texture->renderer;
  1613. if (texture->native) {
  1614. return SDL_GL_UnbindTexture(texture->native);
  1615. } else if (renderer && renderer->GL_UnbindTexture) {
  1616. return renderer->GL_UnbindTexture(renderer, texture);
  1617. }
  1618. return SDL_Unsupported();
  1619. }
  1620. /* vi: set ts=4 sw=4 expandtab: */