SDL_render_sw.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. #if !SDL_RENDER_DISABLED
  20. #include "../SDL_sysrender.h"
  21. #include "SDL_render_sw_c.h"
  22. #include "SDL_hints.h"
  23. #include "SDL_draw.h"
  24. #include "SDL_blendfillrect.h"
  25. #include "SDL_blendline.h"
  26. #include "SDL_blendpoint.h"
  27. #include "SDL_drawline.h"
  28. #include "SDL_drawpoint.h"
  29. #include "SDL_rotate.h"
  30. /* SDL surface based renderer implementation */
  31. static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
  32. static void SW_WindowEvent(SDL_Renderer * renderer,
  33. const SDL_WindowEvent *event);
  34. static int SW_GetOutputSize(SDL_Renderer * renderer, int *w, int *h);
  35. static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  36. static int SW_SetTextureColorMod(SDL_Renderer * renderer,
  37. SDL_Texture * texture);
  38. static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
  39. SDL_Texture * texture);
  40. static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
  41. SDL_Texture * texture);
  42. static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  43. const SDL_Rect * rect, const void *pixels,
  44. int pitch);
  45. static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  46. const SDL_Rect * rect, void **pixels, int *pitch);
  47. static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  48. static int SW_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
  49. static int SW_UpdateViewport(SDL_Renderer * renderer);
  50. static int SW_UpdateClipRect(SDL_Renderer * renderer);
  51. static int SW_RenderClear(SDL_Renderer * renderer);
  52. static int SW_RenderDrawPoints(SDL_Renderer * renderer,
  53. const SDL_FPoint * points, int count);
  54. static int SW_RenderDrawLines(SDL_Renderer * renderer,
  55. const SDL_FPoint * points, int count);
  56. static int SW_RenderFillRects(SDL_Renderer * renderer,
  57. const SDL_FRect * rects, int count);
  58. static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  59. const SDL_Rect * srcrect, const SDL_FRect * dstrect);
  60. static int SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  61. const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  62. const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
  63. static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  64. Uint32 format, void * pixels, int pitch);
  65. static void SW_RenderPresent(SDL_Renderer * renderer);
  66. static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
  67. static void SW_DestroyRenderer(SDL_Renderer * renderer);
  68. SDL_RenderDriver SW_RenderDriver = {
  69. SW_CreateRenderer,
  70. {
  71. "software",
  72. SDL_RENDERER_SOFTWARE | SDL_RENDERER_TARGETTEXTURE,
  73. 8,
  74. {
  75. SDL_PIXELFORMAT_RGB555,
  76. SDL_PIXELFORMAT_RGB565,
  77. SDL_PIXELFORMAT_RGB888,
  78. SDL_PIXELFORMAT_BGR888,
  79. SDL_PIXELFORMAT_ARGB8888,
  80. SDL_PIXELFORMAT_RGBA8888,
  81. SDL_PIXELFORMAT_ABGR8888,
  82. SDL_PIXELFORMAT_BGRA8888
  83. },
  84. 0,
  85. 0}
  86. };
  87. typedef struct
  88. {
  89. SDL_Surface *surface;
  90. SDL_Surface *window;
  91. } SW_RenderData;
  92. static SDL_Surface *
  93. SW_ActivateRenderer(SDL_Renderer * renderer)
  94. {
  95. SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  96. if (!data->surface) {
  97. data->surface = data->window;
  98. }
  99. if (!data->surface) {
  100. SDL_Surface *surface = SDL_GetWindowSurface(renderer->window);
  101. if (surface) {
  102. data->surface = data->window = surface;
  103. SW_UpdateViewport(renderer);
  104. SW_UpdateClipRect(renderer);
  105. }
  106. }
  107. return data->surface;
  108. }
  109. SDL_Renderer *
  110. SW_CreateRendererForSurface(SDL_Surface * surface)
  111. {
  112. SDL_Renderer *renderer;
  113. SW_RenderData *data;
  114. if (!surface) {
  115. SDL_SetError("Can't create renderer for NULL surface");
  116. return NULL;
  117. }
  118. renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
  119. if (!renderer) {
  120. SDL_OutOfMemory();
  121. return NULL;
  122. }
  123. data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
  124. if (!data) {
  125. SW_DestroyRenderer(renderer);
  126. SDL_OutOfMemory();
  127. return NULL;
  128. }
  129. data->surface = surface;
  130. renderer->WindowEvent = SW_WindowEvent;
  131. renderer->GetOutputSize = SW_GetOutputSize;
  132. renderer->CreateTexture = SW_CreateTexture;
  133. renderer->SetTextureColorMod = SW_SetTextureColorMod;
  134. renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
  135. renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
  136. renderer->UpdateTexture = SW_UpdateTexture;
  137. renderer->LockTexture = SW_LockTexture;
  138. renderer->UnlockTexture = SW_UnlockTexture;
  139. renderer->SetRenderTarget = SW_SetRenderTarget;
  140. renderer->UpdateViewport = SW_UpdateViewport;
  141. renderer->UpdateClipRect = SW_UpdateClipRect;
  142. renderer->RenderClear = SW_RenderClear;
  143. renderer->RenderDrawPoints = SW_RenderDrawPoints;
  144. renderer->RenderDrawLines = SW_RenderDrawLines;
  145. renderer->RenderFillRects = SW_RenderFillRects;
  146. renderer->RenderCopy = SW_RenderCopy;
  147. renderer->RenderCopyEx = SW_RenderCopyEx;
  148. renderer->RenderReadPixels = SW_RenderReadPixels;
  149. renderer->RenderPresent = SW_RenderPresent;
  150. renderer->DestroyTexture = SW_DestroyTexture;
  151. renderer->DestroyRenderer = SW_DestroyRenderer;
  152. renderer->info = SW_RenderDriver.info;
  153. renderer->driverdata = data;
  154. SW_ActivateRenderer(renderer);
  155. return renderer;
  156. }
  157. SDL_Renderer *
  158. SW_CreateRenderer(SDL_Window * window, Uint32 flags)
  159. {
  160. SDL_Surface *surface;
  161. surface = SDL_GetWindowSurface(window);
  162. if (!surface) {
  163. return NULL;
  164. }
  165. return SW_CreateRendererForSurface(surface);
  166. }
  167. static void
  168. SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
  169. {
  170. SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  171. if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
  172. data->surface = NULL;
  173. data->window = NULL;
  174. }
  175. }
  176. static int
  177. SW_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
  178. {
  179. SDL_Surface *surface = SW_ActivateRenderer(renderer);
  180. if (surface) {
  181. if (w) {
  182. *w = surface->w;
  183. }
  184. if (h) {
  185. *h = surface->h;
  186. }
  187. return 0;
  188. } else {
  189. SDL_SetError("Software renderer doesn't have an output surface");
  190. return -1;
  191. }
  192. }
  193. static int
  194. SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  195. {
  196. int bpp;
  197. Uint32 Rmask, Gmask, Bmask, Amask;
  198. if (!SDL_PixelFormatEnumToMasks
  199. (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  200. return SDL_SetError("Unknown texture format");
  201. }
  202. texture->driverdata =
  203. SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
  204. Bmask, Amask);
  205. SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
  206. texture->b);
  207. SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
  208. SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
  209. if (texture->access == SDL_TEXTUREACCESS_STATIC) {
  210. SDL_SetSurfaceRLE(texture->driverdata, 1);
  211. }
  212. if (!texture->driverdata) {
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. static int
  218. SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
  219. {
  220. SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  221. return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
  222. texture->b);
  223. }
  224. static int
  225. SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
  226. {
  227. SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  228. return SDL_SetSurfaceAlphaMod(surface, texture->a);
  229. }
  230. static int
  231. SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
  232. {
  233. SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  234. return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
  235. }
  236. static int
  237. SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  238. const SDL_Rect * rect, const void *pixels, int pitch)
  239. {
  240. SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  241. Uint8 *src, *dst;
  242. int row;
  243. size_t length;
  244. if(SDL_MUSTLOCK(surface))
  245. SDL_LockSurface(surface);
  246. src = (Uint8 *) pixels;
  247. dst = (Uint8 *) surface->pixels +
  248. rect->y * surface->pitch +
  249. rect->x * surface->format->BytesPerPixel;
  250. length = rect->w * surface->format->BytesPerPixel;
  251. for (row = 0; row < rect->h; ++row) {
  252. SDL_memcpy(dst, src, length);
  253. src += pitch;
  254. dst += surface->pitch;
  255. }
  256. if(SDL_MUSTLOCK(surface))
  257. SDL_UnlockSurface(surface);
  258. return 0;
  259. }
  260. static int
  261. SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  262. const SDL_Rect * rect, void **pixels, int *pitch)
  263. {
  264. SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  265. *pixels =
  266. (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
  267. rect->x * surface->format->BytesPerPixel);
  268. *pitch = surface->pitch;
  269. return 0;
  270. }
  271. static void
  272. SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  273. {
  274. }
  275. static int
  276. SW_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
  277. {
  278. SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  279. if (texture ) {
  280. data->surface = (SDL_Surface *) texture->driverdata;
  281. } else {
  282. data->surface = data->window;
  283. }
  284. return 0;
  285. }
  286. static int
  287. SW_UpdateViewport(SDL_Renderer * renderer)
  288. {
  289. SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  290. SDL_Surface *surface = data->surface;
  291. if (!surface) {
  292. /* We'll update the viewport after we recreate the surface */
  293. return 0;
  294. }
  295. SDL_SetClipRect(data->surface, &renderer->viewport);
  296. return 0;
  297. }
  298. static int
  299. SW_UpdateClipRect(SDL_Renderer * renderer)
  300. {
  301. SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  302. SDL_Surface *surface = data->surface;
  303. if (surface) {
  304. if (renderer->clipping_enabled) {
  305. SDL_SetClipRect(surface, &renderer->clip_rect);
  306. } else {
  307. SDL_SetClipRect(surface, NULL);
  308. }
  309. }
  310. return 0;
  311. }
  312. static int
  313. SW_RenderClear(SDL_Renderer * renderer)
  314. {
  315. SDL_Surface *surface = SW_ActivateRenderer(renderer);
  316. Uint32 color;
  317. SDL_Rect clip_rect;
  318. if (!surface) {
  319. return -1;
  320. }
  321. color = SDL_MapRGBA(surface->format,
  322. renderer->r, renderer->g, renderer->b, renderer->a);
  323. /* By definition the clear ignores the clip rect */
  324. clip_rect = surface->clip_rect;
  325. SDL_SetClipRect(surface, NULL);
  326. SDL_FillRect(surface, NULL, color);
  327. SDL_SetClipRect(surface, &clip_rect);
  328. return 0;
  329. }
  330. static int
  331. SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
  332. int count)
  333. {
  334. SDL_Surface *surface = SW_ActivateRenderer(renderer);
  335. SDL_Point *final_points;
  336. int i, status;
  337. if (!surface) {
  338. return -1;
  339. }
  340. final_points = SDL_stack_alloc(SDL_Point, count);
  341. if (!final_points) {
  342. return SDL_OutOfMemory();
  343. }
  344. if (renderer->viewport.x || renderer->viewport.y) {
  345. int x = renderer->viewport.x;
  346. int y = renderer->viewport.y;
  347. for (i = 0; i < count; ++i) {
  348. final_points[i].x = (int)(x + points[i].x);
  349. final_points[i].y = (int)(y + points[i].y);
  350. }
  351. } else {
  352. for (i = 0; i < count; ++i) {
  353. final_points[i].x = (int)points[i].x;
  354. final_points[i].y = (int)points[i].y;
  355. }
  356. }
  357. /* Draw the points! */
  358. if (renderer->blendMode == SDL_BLENDMODE_NONE) {
  359. Uint32 color = SDL_MapRGBA(surface->format,
  360. renderer->r, renderer->g, renderer->b,
  361. renderer->a);
  362. status = SDL_DrawPoints(surface, final_points, count, color);
  363. } else {
  364. status = SDL_BlendPoints(surface, final_points, count,
  365. renderer->blendMode,
  366. renderer->r, renderer->g, renderer->b,
  367. renderer->a);
  368. }
  369. SDL_stack_free(final_points);
  370. return status;
  371. }
  372. static int
  373. SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
  374. int count)
  375. {
  376. SDL_Surface *surface = SW_ActivateRenderer(renderer);
  377. SDL_Point *final_points;
  378. int i, status;
  379. if (!surface) {
  380. return -1;
  381. }
  382. final_points = SDL_stack_alloc(SDL_Point, count);
  383. if (!final_points) {
  384. return SDL_OutOfMemory();
  385. }
  386. if (renderer->viewport.x || renderer->viewport.y) {
  387. int x = renderer->viewport.x;
  388. int y = renderer->viewport.y;
  389. for (i = 0; i < count; ++i) {
  390. final_points[i].x = (int)(x + points[i].x);
  391. final_points[i].y = (int)(y + points[i].y);
  392. }
  393. } else {
  394. for (i = 0; i < count; ++i) {
  395. final_points[i].x = (int)points[i].x;
  396. final_points[i].y = (int)points[i].y;
  397. }
  398. }
  399. /* Draw the lines! */
  400. if (renderer->blendMode == SDL_BLENDMODE_NONE) {
  401. Uint32 color = SDL_MapRGBA(surface->format,
  402. renderer->r, renderer->g, renderer->b,
  403. renderer->a);
  404. status = SDL_DrawLines(surface, final_points, count, color);
  405. } else {
  406. status = SDL_BlendLines(surface, final_points, count,
  407. renderer->blendMode,
  408. renderer->r, renderer->g, renderer->b,
  409. renderer->a);
  410. }
  411. SDL_stack_free(final_points);
  412. return status;
  413. }
  414. static int
  415. SW_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
  416. {
  417. SDL_Surface *surface = SW_ActivateRenderer(renderer);
  418. SDL_Rect *final_rects;
  419. int i, status;
  420. if (!surface) {
  421. return -1;
  422. }
  423. final_rects = SDL_stack_alloc(SDL_Rect, count);
  424. if (!final_rects) {
  425. return SDL_OutOfMemory();
  426. }
  427. if (renderer->viewport.x || renderer->viewport.y) {
  428. int x = renderer->viewport.x;
  429. int y = renderer->viewport.y;
  430. for (i = 0; i < count; ++i) {
  431. final_rects[i].x = (int)(x + rects[i].x);
  432. final_rects[i].y = (int)(y + rects[i].y);
  433. final_rects[i].w = SDL_max((int)rects[i].w, 1);
  434. final_rects[i].h = SDL_max((int)rects[i].h, 1);
  435. }
  436. } else {
  437. for (i = 0; i < count; ++i) {
  438. final_rects[i].x = (int)rects[i].x;
  439. final_rects[i].y = (int)rects[i].y;
  440. final_rects[i].w = SDL_max((int)rects[i].w, 1);
  441. final_rects[i].h = SDL_max((int)rects[i].h, 1);
  442. }
  443. }
  444. if (renderer->blendMode == SDL_BLENDMODE_NONE) {
  445. Uint32 color = SDL_MapRGBA(surface->format,
  446. renderer->r, renderer->g, renderer->b,
  447. renderer->a);
  448. status = SDL_FillRects(surface, final_rects, count, color);
  449. } else {
  450. status = SDL_BlendFillRects(surface, final_rects, count,
  451. renderer->blendMode,
  452. renderer->r, renderer->g, renderer->b,
  453. renderer->a);
  454. }
  455. SDL_stack_free(final_rects);
  456. return status;
  457. }
  458. static int
  459. SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  460. const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  461. {
  462. SDL_Surface *surface = SW_ActivateRenderer(renderer);
  463. SDL_Surface *src = (SDL_Surface *) texture->driverdata;
  464. SDL_Rect final_rect;
  465. if (!surface) {
  466. return -1;
  467. }
  468. if (renderer->viewport.x || renderer->viewport.y) {
  469. final_rect.x = (int)(renderer->viewport.x + dstrect->x);
  470. final_rect.y = (int)(renderer->viewport.y + dstrect->y);
  471. } else {
  472. final_rect.x = (int)dstrect->x;
  473. final_rect.y = (int)dstrect->y;
  474. }
  475. final_rect.w = (int)dstrect->w;
  476. final_rect.h = (int)dstrect->h;
  477. if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
  478. return SDL_BlitSurface(src, srcrect, surface, &final_rect);
  479. } else {
  480. return SDL_BlitScaled(src, srcrect, surface, &final_rect);
  481. }
  482. }
  483. static int
  484. GetScaleQuality(void)
  485. {
  486. const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
  487. if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
  488. return 0;
  489. } else {
  490. return 1;
  491. }
  492. }
  493. static int
  494. SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  495. const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  496. const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
  497. {
  498. SDL_Surface *surface = SW_ActivateRenderer(renderer);
  499. SDL_Surface *src = (SDL_Surface *) texture->driverdata;
  500. SDL_Rect final_rect, tmp_rect;
  501. SDL_Surface *surface_rotated, *surface_scaled;
  502. Uint32 colorkey;
  503. int retval, dstwidth, dstheight, abscenterx, abscentery;
  504. double cangle, sangle, px, py, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y;
  505. if (!surface) {
  506. return -1;
  507. }
  508. if (renderer->viewport.x || renderer->viewport.y) {
  509. final_rect.x = (int)(renderer->viewport.x + dstrect->x);
  510. final_rect.y = (int)(renderer->viewport.y + dstrect->y);
  511. } else {
  512. final_rect.x = (int)dstrect->x;
  513. final_rect.y = (int)dstrect->y;
  514. }
  515. final_rect.w = (int)dstrect->w;
  516. final_rect.h = (int)dstrect->h;
  517. surface_scaled = SDL_CreateRGBSurface(SDL_SWSURFACE, final_rect.w, final_rect.h, src->format->BitsPerPixel,
  518. src->format->Rmask, src->format->Gmask,
  519. src->format->Bmask, src->format->Amask );
  520. if (surface_scaled) {
  521. SDL_GetColorKey(src, &colorkey);
  522. SDL_SetColorKey(surface_scaled, SDL_TRUE, colorkey);
  523. tmp_rect = final_rect;
  524. tmp_rect.x = 0;
  525. tmp_rect.y = 0;
  526. retval = SDL_BlitScaled(src, srcrect, surface_scaled, &tmp_rect);
  527. if (!retval) {
  528. SDLgfx_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, -angle, &dstwidth, &dstheight, &cangle, &sangle);
  529. surface_rotated = SDLgfx_rotateSurface(surface_scaled, -angle, dstwidth/2, dstheight/2, GetScaleQuality(), flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
  530. if(surface_rotated) {
  531. /* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
  532. abscenterx = final_rect.x + (int)center->x;
  533. abscentery = final_rect.y + (int)center->y;
  534. /* Compensate the angle inversion to match the behaviour of the other backends */
  535. sangle = -sangle;
  536. /* Top Left */
  537. px = final_rect.x - abscenterx;
  538. py = final_rect.y - abscentery;
  539. p1x = px * cangle - py * sangle + abscenterx;
  540. p1y = px * sangle + py * cangle + abscentery;
  541. /* Top Right */
  542. px = final_rect.x + final_rect.w - abscenterx;
  543. py = final_rect.y - abscentery;
  544. p2x = px * cangle - py * sangle + abscenterx;
  545. p2y = px * sangle + py * cangle + abscentery;
  546. /* Bottom Left */
  547. px = final_rect.x - abscenterx;
  548. py = final_rect.y + final_rect.h - abscentery;
  549. p3x = px * cangle - py * sangle + abscenterx;
  550. p3y = px * sangle + py * cangle + abscentery;
  551. /* Bottom Right */
  552. px = final_rect.x + final_rect.w - abscenterx;
  553. py = final_rect.y + final_rect.h - abscentery;
  554. p4x = px * cangle - py * sangle + abscenterx;
  555. p4y = px * sangle + py * cangle + abscentery;
  556. tmp_rect.x = (int)MIN(MIN(p1x, p2x), MIN(p3x, p4x));
  557. tmp_rect.y = (int)MIN(MIN(p1y, p2y), MIN(p3y, p4y));
  558. tmp_rect.w = dstwidth;
  559. tmp_rect.h = dstheight;
  560. retval = SDL_BlitSurface(surface_rotated, NULL, surface, &tmp_rect);
  561. SDL_FreeSurface(surface_scaled);
  562. SDL_FreeSurface(surface_rotated);
  563. return retval;
  564. }
  565. }
  566. return retval;
  567. }
  568. return -1;
  569. }
  570. static int
  571. SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  572. Uint32 format, void * pixels, int pitch)
  573. {
  574. SDL_Surface *surface = SW_ActivateRenderer(renderer);
  575. Uint32 src_format;
  576. void *src_pixels;
  577. SDL_Rect final_rect;
  578. if (!surface) {
  579. return -1;
  580. }
  581. if (renderer->viewport.x || renderer->viewport.y) {
  582. final_rect.x = renderer->viewport.x + rect->x;
  583. final_rect.y = renderer->viewport.y + rect->y;
  584. final_rect.w = rect->w;
  585. final_rect.h = rect->h;
  586. rect = &final_rect;
  587. }
  588. if (rect->x < 0 || rect->x+rect->w > surface->w ||
  589. rect->y < 0 || rect->y+rect->h > surface->h) {
  590. return SDL_SetError("Tried to read outside of surface bounds");
  591. }
  592. src_format = surface->format->format;
  593. src_pixels = (void*)((Uint8 *) surface->pixels +
  594. rect->y * surface->pitch +
  595. rect->x * surface->format->BytesPerPixel);
  596. return SDL_ConvertPixels(rect->w, rect->h,
  597. src_format, src_pixels, surface->pitch,
  598. format, pixels, pitch);
  599. }
  600. static void
  601. SW_RenderPresent(SDL_Renderer * renderer)
  602. {
  603. SDL_Window *window = renderer->window;
  604. if (window) {
  605. SDL_UpdateWindowSurface(window);
  606. }
  607. }
  608. static void
  609. SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  610. {
  611. SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  612. SDL_FreeSurface(surface);
  613. }
  614. static void
  615. SW_DestroyRenderer(SDL_Renderer * renderer)
  616. {
  617. SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  618. SDL_free(data);
  619. SDL_free(renderer);
  620. }
  621. #endif /* !SDL_RENDER_DISABLED */
  622. /* vi: set ts=4 sw=4 expandtab: */