SDL_render.c 56 KB

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