SDL_render_sw.c 24 KB

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