| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234 |
- /*
- Simple DirectMedia Layer
- Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- */
- #include "../SDL_internal.h"
- /* The SDL 2D rendering system */
- #include "SDL_assert.h"
- #include "SDL_hints.h"
- #include "SDL_log.h"
- #include "SDL_render.h"
- #include "SDL_sysrender.h"
- #include "software/SDL_render_sw_c.h"
- #define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
- #define CHECK_RENDERER_MAGIC(renderer, retval) \
- SDL_assert(renderer && renderer->magic == &renderer_magic); \
- if (!renderer || renderer->magic != &renderer_magic) { \
- SDL_SetError("Invalid renderer"); \
- return retval; \
- }
- #define CHECK_TEXTURE_MAGIC(texture, retval) \
- SDL_assert(texture && texture->magic == &texture_magic); \
- if (!texture || texture->magic != &texture_magic) { \
- SDL_SetError("Invalid texture"); \
- return retval; \
- }
- /* Predefined blend modes */
- #define SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation, \
- srcAlphaFactor, dstAlphaFactor, alphaOperation) \
- (SDL_BlendMode)(((Uint32)colorOperation << 0) | \
- ((Uint32)srcColorFactor << 4) | \
- ((Uint32)dstColorFactor << 8) | \
- ((Uint32)alphaOperation << 16) | \
- ((Uint32)srcAlphaFactor << 20) | \
- ((Uint32)dstAlphaFactor << 24))
- #define SDL_BLENDMODE_NONE_FULL \
- SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD, \
- SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD)
- #define SDL_BLENDMODE_BLEND_FULL \
- SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
- SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
- #define SDL_BLENDMODE_ADD_FULL \
- SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, \
- SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
- #define SDL_BLENDMODE_MOD_FULL \
- SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_COLOR, SDL_BLENDOPERATION_ADD, \
- SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
- #if !SDL_RENDER_DISABLED
- static const SDL_RenderDriver *render_drivers[] = {
- #if SDL_VIDEO_RENDER_D3D
- &D3D_RenderDriver,
- #endif
- #if SDL_VIDEO_RENDER_D3D11
- &D3D11_RenderDriver,
- #endif
- #if SDL_VIDEO_RENDER_OGL
- &GL_RenderDriver,
- #endif
- #if SDL_VIDEO_RENDER_OGL_ES2
- &GLES2_RenderDriver,
- #endif
- #if SDL_VIDEO_RENDER_OGL_ES
- &GLES_RenderDriver,
- #endif
- #if SDL_VIDEO_RENDER_DIRECTFB
- &DirectFB_RenderDriver,
- #endif
- #if SDL_VIDEO_RENDER_METAL
- &METAL_RenderDriver,
- #endif
- #if SDL_VIDEO_RENDER_PSP
- &PSP_RenderDriver,
- #endif
- &SW_RenderDriver
- };
- #endif /* !SDL_RENDER_DISABLED */
- static char renderer_magic;
- static char texture_magic;
- static int UpdateLogicalSize(SDL_Renderer *renderer);
- int
- SDL_GetNumRenderDrivers(void)
- {
- #if !SDL_RENDER_DISABLED
- return SDL_arraysize(render_drivers);
- #else
- return 0;
- #endif
- }
- int
- SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
- {
- #if !SDL_RENDER_DISABLED
- if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
- return SDL_SetError("index must be in the range of 0 - %d",
- SDL_GetNumRenderDrivers() - 1);
- }
- *info = render_drivers[index]->info;
- return 0;
- #else
- return SDL_SetError("SDL not built with rendering support");
- #endif
- }
- static int SDLCALL
- SDL_RendererEventWatch(void *userdata, SDL_Event *event)
- {
- SDL_Renderer *renderer = (SDL_Renderer *)userdata;
- if (event->type == SDL_WINDOWEVENT) {
- SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
- if (window == renderer->window) {
- if (renderer->WindowEvent) {
- renderer->WindowEvent(renderer, &event->window);
- }
- if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
- /* Make sure we're operating on the default render target */
- SDL_Texture *saved_target = SDL_GetRenderTarget(renderer);
- if (saved_target) {
- SDL_SetRenderTarget(renderer, NULL);
- }
- if (renderer->logical_w) {
- UpdateLogicalSize(renderer);
- } else {
- /* Window was resized, reset viewport */
- int w, h;
- if (renderer->GetOutputSize) {
- renderer->GetOutputSize(renderer, &w, &h);
- } else {
- SDL_GetWindowSize(renderer->window, &w, &h);
- }
- if (renderer->target) {
- renderer->viewport_backup.x = 0;
- renderer->viewport_backup.y = 0;
- renderer->viewport_backup.w = w;
- renderer->viewport_backup.h = h;
- } else {
- renderer->viewport.x = 0;
- renderer->viewport.y = 0;
- renderer->viewport.w = w;
- renderer->viewport.h = h;
- renderer->UpdateViewport(renderer);
- }
- }
- if (saved_target) {
- SDL_SetRenderTarget(renderer, saved_target);
- }
- } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
- renderer->hidden = SDL_TRUE;
- } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
- if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
- renderer->hidden = SDL_FALSE;
- }
- } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
- renderer->hidden = SDL_TRUE;
- } else if (event->window.event == SDL_WINDOWEVENT_RESTORED ||
- event->window.event == SDL_WINDOWEVENT_MAXIMIZED) {
- if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
- renderer->hidden = SDL_FALSE;
- }
- }
- }
- } else if (event->type == SDL_MOUSEMOTION) {
- SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
- if (renderer->logical_w && window == renderer->window) {
- event->motion.x -= (int)(renderer->viewport.x * renderer->dpi_scale.x);
- event->motion.y -= (int)(renderer->viewport.y * renderer->dpi_scale.y);
- event->motion.x = (int)(event->motion.x / (renderer->scale.x * renderer->dpi_scale.x));
- event->motion.y = (int)(event->motion.y / (renderer->scale.y * renderer->dpi_scale.y));
- if (event->motion.xrel > 0) {
- event->motion.xrel = SDL_max(1, (int)(event->motion.xrel / (renderer->scale.x * renderer->dpi_scale.x)));
- } else if (event->motion.xrel < 0) {
- event->motion.xrel = SDL_min(-1, (int)(event->motion.xrel / (renderer->scale.x * renderer->dpi_scale.x)));
- }
- if (event->motion.yrel > 0) {
- event->motion.yrel = SDL_max(1, (int)(event->motion.yrel / (renderer->scale.y * renderer->dpi_scale.y)));
- } else if (event->motion.yrel < 0) {
- event->motion.yrel = SDL_min(-1, (int)(event->motion.yrel / (renderer->scale.y * renderer->dpi_scale.y)));
- }
- }
- } else if (event->type == SDL_MOUSEBUTTONDOWN ||
- event->type == SDL_MOUSEBUTTONUP) {
- SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
- if (renderer->logical_w && window == renderer->window) {
- event->button.x -= (int)(renderer->viewport.x * renderer->dpi_scale.x);
- event->button.y -= (int)(renderer->viewport.y * renderer->dpi_scale.y);
- event->button.x = (int)(event->button.x / (renderer->scale.x * renderer->dpi_scale.x));
- event->button.y = (int)(event->button.y / (renderer->scale.y * renderer->dpi_scale.y));
- }
- } else if (event->type == SDL_FINGERDOWN ||
- event->type == SDL_FINGERUP ||
- event->type == SDL_FINGERMOTION) {
- if (renderer->logical_w) {
- int w = 1;
- int h = 1;
- SDL_GetRendererOutputSize(renderer, &w, &h);
- event->tfinger.x *= (w - 1);
- event->tfinger.y *= (h - 1);
- event->tfinger.x -= (renderer->viewport.x * renderer->dpi_scale.x);
- event->tfinger.y -= (renderer->viewport.y * renderer->dpi_scale.y);
- event->tfinger.x = (event->tfinger.x / (renderer->scale.x * renderer->dpi_scale.x));
- event->tfinger.y = (event->tfinger.y / (renderer->scale.y * renderer->dpi_scale.y));
- if (renderer->logical_w > 1) {
- event->tfinger.x = event->tfinger.x / (renderer->logical_w - 1);
- } else {
- event->tfinger.x = 0.5f;
- }
- if (renderer->logical_h > 1) {
- event->tfinger.y = event->tfinger.y / (renderer->logical_h - 1);
- } else {
- event->tfinger.y = 0.5f;
- }
- }
- }
- return 0;
- }
- int
- SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
- SDL_Window **window, SDL_Renderer **renderer)
- {
- *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- width, height, window_flags);
- if (!*window) {
- *renderer = NULL;
- return -1;
- }
- *renderer = SDL_CreateRenderer(*window, -1, 0);
- if (!*renderer) {
- return -1;
- }
- return 0;
- }
- SDL_Renderer *
- SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
- {
- #if !SDL_RENDER_DISABLED
- SDL_Renderer *renderer = NULL;
- int n = SDL_GetNumRenderDrivers();
- const char *hint;
- if (!window) {
- SDL_SetError("Invalid window");
- return NULL;
- }
- if (SDL_GetRenderer(window)) {
- SDL_SetError("Renderer already associated with window");
- return NULL;
- }
- if (SDL_GetHint(SDL_HINT_RENDER_VSYNC)) {
- if (SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, SDL_TRUE)) {
- flags |= SDL_RENDERER_PRESENTVSYNC;
- } else {
- flags &= ~SDL_RENDERER_PRESENTVSYNC;
- }
- }
- if (index < 0) {
- hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
- if (hint) {
- for (index = 0; index < n; ++index) {
- const SDL_RenderDriver *driver = render_drivers[index];
- if (SDL_strcasecmp(hint, driver->info.name) == 0) {
- /* Create a new renderer instance */
- renderer = driver->CreateRenderer(window, flags);
- break;
- }
- }
- }
- if (!renderer) {
- for (index = 0; index < n; ++index) {
- const SDL_RenderDriver *driver = render_drivers[index];
- if ((driver->info.flags & flags) == flags) {
- /* Create a new renderer instance */
- renderer = driver->CreateRenderer(window, flags);
- if (renderer) {
- /* Yay, we got one! */
- break;
- }
- }
- }
- }
- if (index == n) {
- SDL_SetError("Couldn't find matching render driver");
- return NULL;
- }
- } else {
- if (index >= SDL_GetNumRenderDrivers()) {
- SDL_SetError("index must be -1 or in the range of 0 - %d",
- SDL_GetNumRenderDrivers() - 1);
- return NULL;
- }
- /* Create a new renderer instance */
- renderer = render_drivers[index]->CreateRenderer(window, flags);
- }
- if (renderer) {
- renderer->magic = &renderer_magic;
- renderer->window = window;
- renderer->scale.x = 1.0f;
- renderer->scale.y = 1.0f;
- renderer->dpi_scale.x = 1.0f;
- renderer->dpi_scale.y = 1.0f;
- if (window && renderer->GetOutputSize) {
- int window_w, window_h;
- int output_w, output_h;
- if (renderer->GetOutputSize(renderer, &output_w, &output_h) == 0) {
- SDL_GetWindowSize(renderer->window, &window_w, &window_h);
- renderer->dpi_scale.x = (float)window_w / output_w;
- renderer->dpi_scale.y = (float)window_h / output_h;
- }
- }
- if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
- renderer->hidden = SDL_TRUE;
- } else {
- renderer->hidden = SDL_FALSE;
- }
- SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
- SDL_RenderSetViewport(renderer, NULL);
- SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
- SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
- "Created renderer: %s", renderer->info.name);
- }
- return renderer;
- #else
- SDL_SetError("SDL not built with rendering support");
- return NULL;
- #endif
- }
- SDL_Renderer *
- SDL_CreateSoftwareRenderer(SDL_Surface * surface)
- {
- #if !SDL_RENDER_DISABLED
- SDL_Renderer *renderer;
- renderer = SW_CreateRendererForSurface(surface);
- if (renderer) {
- renderer->magic = &renderer_magic;
- renderer->scale.x = 1.0f;
- renderer->scale.y = 1.0f;
- SDL_RenderSetViewport(renderer, NULL);
- }
- return renderer;
- #else
- SDL_SetError("SDL not built with rendering support");
- return NULL;
- #endif /* !SDL_RENDER_DISABLED */
- }
- SDL_Renderer *
- SDL_GetRenderer(SDL_Window * window)
- {
- return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
- }
- int
- SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- *info = renderer->info;
- return 0;
- }
- int
- SDL_GetRendererOutputSize(SDL_Renderer * renderer, int *w, int *h)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (renderer->target) {
- return SDL_QueryTexture(renderer->target, NULL, NULL, w, h);
- } else if (renderer->GetOutputSize) {
- return renderer->GetOutputSize(renderer, w, h);
- } else if (renderer->window) {
- SDL_GetWindowSize(renderer->window, w, h);
- return 0;
- } else {
- SDL_assert(0 && "This should never happen");
- return SDL_SetError("Renderer doesn't support querying output size");
- }
- }
- static SDL_bool
- IsSupportedBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
- {
- switch (blendMode)
- {
- /* These are required to be supported by all renderers */
- case SDL_BLENDMODE_NONE:
- case SDL_BLENDMODE_BLEND:
- case SDL_BLENDMODE_ADD:
- case SDL_BLENDMODE_MOD:
- return SDL_TRUE;
- default:
- return renderer->SupportsBlendMode && renderer->SupportsBlendMode(renderer, blendMode);
- }
- }
- static SDL_bool
- IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
- {
- Uint32 i;
- for (i = 0; i < renderer->info.num_texture_formats; ++i) {
- if (renderer->info.texture_formats[i] == format) {
- return SDL_TRUE;
- }
- }
- return SDL_FALSE;
- }
- static Uint32
- GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
- {
- Uint32 i;
- if (SDL_ISPIXELFORMAT_FOURCC(format)) {
- /* Look for an exact match */
- for (i = 0; i < renderer->info.num_texture_formats; ++i) {
- if (renderer->info.texture_formats[i] == format) {
- return renderer->info.texture_formats[i];
- }
- }
- } else {
- SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
- /* We just want to match the first format that has the same channels */
- for (i = 0; i < renderer->info.num_texture_formats; ++i) {
- if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
- SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
- return renderer->info.texture_formats[i];
- }
- }
- }
- return renderer->info.texture_formats[0];
- }
- SDL_Texture *
- SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
- {
- SDL_Texture *texture;
- CHECK_RENDERER_MAGIC(renderer, NULL);
- if (!format) {
- format = renderer->info.texture_formats[0];
- }
- if (SDL_BYTESPERPIXEL(format) == 0) {
- SDL_SetError("Invalid texture format");
- return NULL;
- }
- if (SDL_ISPIXELFORMAT_INDEXED(format)) {
- SDL_SetError("Palettized textures are not supported");
- return NULL;
- }
- if (w <= 0 || h <= 0) {
- SDL_SetError("Texture dimensions can't be 0");
- return NULL;
- }
- if ((renderer->info.max_texture_width && w > renderer->info.max_texture_width) ||
- (renderer->info.max_texture_height && h > renderer->info.max_texture_height)) {
- SDL_SetError("Texture dimensions are limited to %dx%d", renderer->info.max_texture_width, renderer->info.max_texture_height);
- return NULL;
- }
- texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
- if (!texture) {
- SDL_OutOfMemory();
- return NULL;
- }
- texture->magic = &texture_magic;
- texture->format = format;
- texture->access = access;
- texture->w = w;
- texture->h = h;
- texture->r = 255;
- texture->g = 255;
- texture->b = 255;
- texture->a = 255;
- texture->renderer = renderer;
- texture->next = renderer->textures;
- if (renderer->textures) {
- renderer->textures->prev = texture;
- }
- renderer->textures = texture;
- if (IsSupportedFormat(renderer, format)) {
- if (renderer->CreateTexture(renderer, texture) < 0) {
- SDL_DestroyTexture(texture);
- return NULL;
- }
- } else {
- texture->native = SDL_CreateTexture(renderer,
- GetClosestSupportedFormat(renderer, format),
- access, w, h);
- if (!texture->native) {
- SDL_DestroyTexture(texture);
- return NULL;
- }
- /* Swap textures to have texture before texture->native in the list */
- texture->native->next = texture->next;
- if (texture->native->next) {
- texture->native->next->prev = texture->native;
- }
- texture->prev = texture->native->prev;
- if (texture->prev) {
- texture->prev->next = texture;
- }
- texture->native->prev = texture;
- texture->next = texture->native;
- renderer->textures = texture;
- if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
- texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
- if (!texture->yuv) {
- SDL_DestroyTexture(texture);
- return NULL;
- }
- } else if (access == SDL_TEXTUREACCESS_STREAMING) {
- /* The pitch is 4 byte aligned */
- texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
- texture->pixels = SDL_calloc(1, texture->pitch * h);
- if (!texture->pixels) {
- SDL_DestroyTexture(texture);
- return NULL;
- }
- }
- }
- return texture;
- }
- SDL_Texture *
- SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
- {
- const SDL_PixelFormat *fmt;
- SDL_bool needAlpha;
- Uint32 i;
- Uint32 format;
- SDL_Texture *texture;
- CHECK_RENDERER_MAGIC(renderer, NULL);
- if (!surface) {
- SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
- return NULL;
- }
- /* See what the best texture format is */
- fmt = surface->format;
- if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
- needAlpha = SDL_TRUE;
- } else {
- needAlpha = SDL_FALSE;
- }
- format = renderer->info.texture_formats[0];
- for (i = 0; i < renderer->info.num_texture_formats; ++i) {
- if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
- SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
- format = renderer->info.texture_formats[i];
- break;
- }
- }
- texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
- surface->w, surface->h);
- if (!texture) {
- return NULL;
- }
- if (format == surface->format->format) {
- if (SDL_MUSTLOCK(surface)) {
- SDL_LockSurface(surface);
- SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
- SDL_UnlockSurface(surface);
- } else {
- SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
- }
- } else {
- SDL_PixelFormat *dst_fmt;
- SDL_Surface *temp = NULL;
- /* Set up a destination surface for the texture update */
- dst_fmt = SDL_AllocFormat(format);
- if (!dst_fmt) {
- SDL_DestroyTexture(texture);
- return NULL;
- }
- temp = SDL_ConvertSurface(surface, dst_fmt, 0);
- SDL_FreeFormat(dst_fmt);
- if (temp) {
- SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
- SDL_FreeSurface(temp);
- } else {
- SDL_DestroyTexture(texture);
- return NULL;
- }
- }
- {
- Uint8 r, g, b, a;
- SDL_BlendMode blendMode;
- SDL_GetSurfaceColorMod(surface, &r, &g, &b);
- SDL_SetTextureColorMod(texture, r, g, b);
- SDL_GetSurfaceAlphaMod(surface, &a);
- SDL_SetTextureAlphaMod(texture, a);
- if (SDL_GetColorKey(surface, NULL) == 0) {
- /* We converted to a texture with alpha format */
- SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
- } else {
- SDL_GetSurfaceBlendMode(surface, &blendMode);
- SDL_SetTextureBlendMode(texture, blendMode);
- }
- }
- return texture;
- }
- int
- SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
- int *w, int *h)
- {
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (format) {
- *format = texture->format;
- }
- if (access) {
- *access = texture->access;
- }
- if (w) {
- *w = texture->w;
- }
- if (h) {
- *h = texture->h;
- }
- return 0;
- }
- int
- SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
- {
- SDL_Renderer *renderer;
- CHECK_TEXTURE_MAGIC(texture, -1);
- renderer = texture->renderer;
- if (r < 255 || g < 255 || b < 255) {
- texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
- } else {
- texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
- }
- texture->r = r;
- texture->g = g;
- texture->b = b;
- if (texture->native) {
- return SDL_SetTextureColorMod(texture->native, r, g, b);
- } else if (renderer->SetTextureColorMod) {
- return renderer->SetTextureColorMod(renderer, texture);
- } else {
- return 0;
- }
- }
- int
- SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
- Uint8 * b)
- {
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (r) {
- *r = texture->r;
- }
- if (g) {
- *g = texture->g;
- }
- if (b) {
- *b = texture->b;
- }
- return 0;
- }
- int
- SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
- {
- SDL_Renderer *renderer;
- CHECK_TEXTURE_MAGIC(texture, -1);
- renderer = texture->renderer;
- if (alpha < 255) {
- texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
- } else {
- texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
- }
- texture->a = alpha;
- if (texture->native) {
- return SDL_SetTextureAlphaMod(texture->native, alpha);
- } else if (renderer->SetTextureAlphaMod) {
- return renderer->SetTextureAlphaMod(renderer, texture);
- } else {
- return 0;
- }
- }
- int
- SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
- {
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (alpha) {
- *alpha = texture->a;
- }
- return 0;
- }
- int
- SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
- {
- SDL_Renderer *renderer;
- CHECK_TEXTURE_MAGIC(texture, -1);
- renderer = texture->renderer;
- if (!IsSupportedBlendMode(renderer, blendMode)) {
- return SDL_Unsupported();
- }
- texture->blendMode = blendMode;
- if (texture->native) {
- return SDL_SetTextureBlendMode(texture->native, blendMode);
- } else if (renderer->SetTextureBlendMode) {
- return renderer->SetTextureBlendMode(renderer, texture);
- } else {
- return 0;
- }
- }
- int
- SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
- {
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (blendMode) {
- *blendMode = texture->blendMode;
- }
- return 0;
- }
- static int
- SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
- const void *pixels, int pitch)
- {
- SDL_Texture *native = texture->native;
- SDL_Rect full_rect;
- if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
- return -1;
- }
- full_rect.x = 0;
- full_rect.y = 0;
- full_rect.w = texture->w;
- full_rect.h = texture->h;
- rect = &full_rect;
- if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
- /* We can lock the texture and copy to it */
- void *native_pixels = NULL;
- int native_pitch = 0;
- if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
- return -1;
- }
- SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
- rect->w, rect->h, native_pixels, native_pitch);
- SDL_UnlockTexture(native);
- } else {
- /* Use a temporary buffer for updating */
- const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
- const size_t alloclen = rect->h * temp_pitch;
- if (alloclen > 0) {
- void *temp_pixels = SDL_malloc(alloclen);
- if (!temp_pixels) {
- return SDL_OutOfMemory();
- }
- SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
- rect->w, rect->h, temp_pixels, temp_pitch);
- SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
- SDL_free(temp_pixels);
- }
- }
- return 0;
- }
- static int
- SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
- const void *pixels, int pitch)
- {
- SDL_Texture *native = texture->native;
- if (!rect->w || !rect->h) {
- return 0; /* nothing to do. */
- }
- if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
- /* We can lock the texture and copy to it */
- void *native_pixels = NULL;
- int native_pitch = 0;
- if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
- return -1;
- }
- SDL_ConvertPixels(rect->w, rect->h,
- texture->format, pixels, pitch,
- native->format, native_pixels, native_pitch);
- SDL_UnlockTexture(native);
- } else {
- /* Use a temporary buffer for updating */
- const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
- const size_t alloclen = rect->h * temp_pitch;
- if (alloclen > 0) {
- void *temp_pixels = SDL_malloc(alloclen);
- if (!temp_pixels) {
- return SDL_OutOfMemory();
- }
- SDL_ConvertPixels(rect->w, rect->h,
- texture->format, pixels, pitch,
- native->format, temp_pixels, temp_pitch);
- SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
- SDL_free(temp_pixels);
- }
- }
- return 0;
- }
- int
- SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
- const void *pixels, int pitch)
- {
- SDL_Renderer *renderer;
- SDL_Rect full_rect;
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (!pixels) {
- return SDL_InvalidParamError("pixels");
- }
- if (!pitch) {
- return SDL_InvalidParamError("pitch");
- }
- if (!rect) {
- full_rect.x = 0;
- full_rect.y = 0;
- full_rect.w = texture->w;
- full_rect.h = texture->h;
- rect = &full_rect;
- }
- if ((rect->w == 0) || (rect->h == 0)) {
- return 0; /* nothing to do. */
- } else if (texture->yuv) {
- return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
- } else if (texture->native) {
- return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
- } else {
- renderer = texture->renderer;
- return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
- }
- }
- static int
- SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
- const Uint8 *Yplane, int Ypitch,
- const Uint8 *Uplane, int Upitch,
- const Uint8 *Vplane, int Vpitch)
- {
- SDL_Texture *native = texture->native;
- SDL_Rect full_rect;
- if (SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch) < 0) {
- return -1;
- }
- full_rect.x = 0;
- full_rect.y = 0;
- full_rect.w = texture->w;
- full_rect.h = texture->h;
- rect = &full_rect;
- if (!rect->w || !rect->h) {
- return 0; /* nothing to do. */
- }
- if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
- /* We can lock the texture and copy to it */
- void *native_pixels = NULL;
- int native_pitch = 0;
- if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
- return -1;
- }
- SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
- rect->w, rect->h, native_pixels, native_pitch);
- SDL_UnlockTexture(native);
- } else {
- /* Use a temporary buffer for updating */
- const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
- const size_t alloclen = rect->h * temp_pitch;
- if (alloclen > 0) {
- void *temp_pixels = SDL_malloc(alloclen);
- if (!temp_pixels) {
- return SDL_OutOfMemory();
- }
- SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
- rect->w, rect->h, temp_pixels, temp_pitch);
- SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
- SDL_free(temp_pixels);
- }
- }
- return 0;
- }
- int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
- const Uint8 *Yplane, int Ypitch,
- const Uint8 *Uplane, int Upitch,
- const Uint8 *Vplane, int Vpitch)
- {
- SDL_Renderer *renderer;
- SDL_Rect full_rect;
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (!Yplane) {
- return SDL_InvalidParamError("Yplane");
- }
- if (!Ypitch) {
- return SDL_InvalidParamError("Ypitch");
- }
- if (!Uplane) {
- return SDL_InvalidParamError("Uplane");
- }
- if (!Upitch) {
- return SDL_InvalidParamError("Upitch");
- }
- if (!Vplane) {
- return SDL_InvalidParamError("Vplane");
- }
- if (!Vpitch) {
- return SDL_InvalidParamError("Vpitch");
- }
- if (texture->format != SDL_PIXELFORMAT_YV12 &&
- texture->format != SDL_PIXELFORMAT_IYUV) {
- return SDL_SetError("Texture format must by YV12 or IYUV");
- }
- if (!rect) {
- full_rect.x = 0;
- full_rect.y = 0;
- full_rect.w = texture->w;
- full_rect.h = texture->h;
- rect = &full_rect;
- }
- if (!rect->w || !rect->h) {
- return 0; /* nothing to do. */
- }
- if (texture->yuv) {
- return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
- } else {
- SDL_assert(!texture->native);
- renderer = texture->renderer;
- SDL_assert(renderer->UpdateTextureYUV);
- if (renderer->UpdateTextureYUV) {
- return renderer->UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
- } else {
- return SDL_Unsupported();
- }
- }
- }
- static int
- SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
- void **pixels, int *pitch)
- {
- return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
- }
- static int
- SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
- void **pixels, int *pitch)
- {
- texture->locked_rect = *rect;
- *pixels = (void *) ((Uint8 *) texture->pixels +
- rect->y * texture->pitch +
- rect->x * SDL_BYTESPERPIXEL(texture->format));
- *pitch = texture->pitch;
- return 0;
- }
- int
- SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
- void **pixels, int *pitch)
- {
- SDL_Renderer *renderer;
- SDL_Rect full_rect;
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
- return SDL_SetError("SDL_LockTexture(): texture must be streaming");
- }
- if (!rect) {
- full_rect.x = 0;
- full_rect.y = 0;
- full_rect.w = texture->w;
- full_rect.h = texture->h;
- rect = &full_rect;
- }
- if (texture->yuv) {
- return SDL_LockTextureYUV(texture, rect, pixels, pitch);
- } else if (texture->native) {
- return SDL_LockTextureNative(texture, rect, pixels, pitch);
- } else {
- renderer = texture->renderer;
- return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
- }
- }
- static void
- SDL_UnlockTextureYUV(SDL_Texture * texture)
- {
- SDL_Texture *native = texture->native;
- void *native_pixels = NULL;
- int native_pitch = 0;
- SDL_Rect rect;
- rect.x = 0;
- rect.y = 0;
- rect.w = texture->w;
- rect.h = texture->h;
- if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
- return;
- }
- SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
- rect.w, rect.h, native_pixels, native_pitch);
- SDL_UnlockTexture(native);
- }
- static void
- SDL_UnlockTextureNative(SDL_Texture * texture)
- {
- SDL_Texture *native = texture->native;
- void *native_pixels = NULL;
- int native_pitch = 0;
- const SDL_Rect *rect = &texture->locked_rect;
- const void* pixels = (void *) ((Uint8 *) texture->pixels +
- rect->y * texture->pitch +
- rect->x * SDL_BYTESPERPIXEL(texture->format));
- int pitch = texture->pitch;
- if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
- return;
- }
- SDL_ConvertPixels(rect->w, rect->h,
- texture->format, pixels, pitch,
- native->format, native_pixels, native_pitch);
- SDL_UnlockTexture(native);
- }
- void
- SDL_UnlockTexture(SDL_Texture * texture)
- {
- SDL_Renderer *renderer;
- CHECK_TEXTURE_MAGIC(texture, );
- if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
- return;
- }
- if (texture->yuv) {
- SDL_UnlockTextureYUV(texture);
- } else if (texture->native) {
- SDL_UnlockTextureNative(texture);
- } else {
- renderer = texture->renderer;
- renderer->UnlockTexture(renderer, texture);
- }
- }
- SDL_bool
- SDL_RenderTargetSupported(SDL_Renderer *renderer)
- {
- if (!renderer || !renderer->SetRenderTarget) {
- return SDL_FALSE;
- }
- return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
- }
- int
- SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
- {
- if (!SDL_RenderTargetSupported(renderer)) {
- return SDL_Unsupported();
- }
- if (texture == renderer->target) {
- /* Nothing to do! */
- return 0;
- }
- /* texture == NULL is valid and means reset the target to the window */
- if (texture) {
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (renderer != texture->renderer) {
- return SDL_SetError("Texture was not created with this renderer");
- }
- if (texture->access != SDL_TEXTUREACCESS_TARGET) {
- return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
- }
- if (texture->native) {
- /* Always render to the native texture */
- texture = texture->native;
- }
- }
- if (texture && !renderer->target) {
- /* Make a backup of the viewport */
- renderer->viewport_backup = renderer->viewport;
- renderer->clip_rect_backup = renderer->clip_rect;
- renderer->clipping_enabled_backup = renderer->clipping_enabled;
- renderer->scale_backup = renderer->scale;
- renderer->logical_w_backup = renderer->logical_w;
- renderer->logical_h_backup = renderer->logical_h;
- }
- renderer->target = texture;
- if (renderer->SetRenderTarget(renderer, texture) < 0) {
- return -1;
- }
- if (texture) {
- renderer->viewport.x = 0;
- renderer->viewport.y = 0;
- renderer->viewport.w = texture->w;
- renderer->viewport.h = texture->h;
- SDL_zero(renderer->clip_rect);
- renderer->clipping_enabled = SDL_FALSE;
- renderer->scale.x = 1.0f;
- renderer->scale.y = 1.0f;
- renderer->logical_w = texture->w;
- renderer->logical_h = texture->h;
- } else {
- renderer->viewport = renderer->viewport_backup;
- renderer->clip_rect = renderer->clip_rect_backup;
- renderer->clipping_enabled = renderer->clipping_enabled_backup;
- renderer->scale = renderer->scale_backup;
- renderer->logical_w = renderer->logical_w_backup;
- renderer->logical_h = renderer->logical_h_backup;
- }
- if (renderer->UpdateViewport(renderer) < 0) {
- return -1;
- }
- if (renderer->UpdateClipRect(renderer) < 0) {
- return -1;
- }
- /* All set! */
- return 0;
- }
- SDL_Texture *
- SDL_GetRenderTarget(SDL_Renderer *renderer)
- {
- return renderer->target;
- }
- static int
- UpdateLogicalSize(SDL_Renderer *renderer)
- {
- int w = 1, h = 1;
- float want_aspect;
- float real_aspect;
- float scale;
- SDL_Rect viewport;
- /* 0 is for letterbox, 1 is for overscan */
- int scale_policy = 0;
- const char *hint;
- if (!renderer->logical_w || !renderer->logical_h) {
- return 0;
- }
- if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
- return -1;
- }
- hint = SDL_GetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE);
- if (hint && (*hint == '1' || SDL_strcasecmp(hint, "overscan") == 0)) {
- SDL_bool overscan_supported = SDL_TRUE;
- /* Unfortunately, Direct3D 9 doesn't support negative viewport numbers
- which the overscan implementation relies on.
- */
- if (SDL_strcasecmp(SDL_GetCurrentVideoDriver(), "direct3d") == 0) {
- overscan_supported = SDL_FALSE;
- }
- if (overscan_supported) {
- scale_policy = 1;
- }
- }
- want_aspect = (float)renderer->logical_w / renderer->logical_h;
- real_aspect = (float)w / h;
- /* Clear the scale because we're setting viewport in output coordinates */
- SDL_RenderSetScale(renderer, 1.0f, 1.0f);
- if (renderer->integer_scale) {
- if (want_aspect > real_aspect) {
- scale = (float)(w / renderer->logical_w);
- } else {
- scale = (float)(h / renderer->logical_h);
- }
- viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
- viewport.x = (w - viewport.w) / 2;
- viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
- viewport.y = (h - viewport.h) / 2;
- SDL_RenderSetViewport(renderer, &viewport);
- } else if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
- /* The aspect ratios are the same, just scale appropriately */
- scale = (float)w / renderer->logical_w;
- SDL_RenderSetViewport(renderer, NULL);
- } else if (want_aspect > real_aspect) {
- if (scale_policy == 1) {
- /* We want a wider aspect ratio than is available -
- zoom so logical height matches the real height
- and the width will grow off the screen
- */
- scale = (float)h / renderer->logical_h;
- viewport.y = 0;
- viewport.h = h;
- viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
- viewport.x = (w - viewport.w) / 2;
- SDL_RenderSetViewport(renderer, &viewport);
- } else {
- /* We want a wider aspect ratio than is available - letterbox it */
- scale = (float)w / renderer->logical_w;
- viewport.x = 0;
- viewport.w = w;
- viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
- viewport.y = (h - viewport.h) / 2;
- SDL_RenderSetViewport(renderer, &viewport);
- }
- } else {
- if (scale_policy == 1) {
- /* We want a narrower aspect ratio than is available -
- zoom so logical width matches the real width
- and the height will grow off the screen
- */
- scale = (float)w / renderer->logical_w;
- viewport.x = 0;
- viewport.w = w;
- viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
- viewport.y = (h - viewport.h) / 2;
- SDL_RenderSetViewport(renderer, &viewport);
- } else {
- /* We want a narrower aspect ratio than is available - use side-bars */
- scale = (float)h / renderer->logical_h;
- viewport.y = 0;
- viewport.h = h;
- viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
- viewport.x = (w - viewport.w) / 2;
- SDL_RenderSetViewport(renderer, &viewport);
- }
- }
- /* Set the new scale */
- SDL_RenderSetScale(renderer, scale, scale);
- return 0;
- }
- int
- SDL_RenderSetLogicalSize(SDL_Renderer * renderer, int w, int h)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (!w || !h) {
- /* Clear any previous logical resolution */
- renderer->logical_w = 0;
- renderer->logical_h = 0;
- SDL_RenderSetViewport(renderer, NULL);
- SDL_RenderSetScale(renderer, 1.0f, 1.0f);
- return 0;
- }
- renderer->logical_w = w;
- renderer->logical_h = h;
- return UpdateLogicalSize(renderer);
- }
- void
- SDL_RenderGetLogicalSize(SDL_Renderer * renderer, int *w, int *h)
- {
- CHECK_RENDERER_MAGIC(renderer, );
- if (w) {
- *w = renderer->logical_w;
- }
- if (h) {
- *h = renderer->logical_h;
- }
- }
- int
- SDL_RenderSetIntegerScale(SDL_Renderer * renderer, SDL_bool enable)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- renderer->integer_scale = enable;
- return UpdateLogicalSize(renderer);
- }
- SDL_bool
- SDLCALL SDL_RenderGetIntegerScale(SDL_Renderer * renderer)
- {
- CHECK_RENDERER_MAGIC(renderer, SDL_FALSE);
- return renderer->integer_scale;
- }
- int
- SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (rect) {
- renderer->viewport.x = (int)SDL_floor(rect->x * renderer->scale.x);
- renderer->viewport.y = (int)SDL_floor(rect->y * renderer->scale.y);
- renderer->viewport.w = (int)SDL_ceil(rect->w * renderer->scale.x);
- renderer->viewport.h = (int)SDL_ceil(rect->h * renderer->scale.y);
- } else {
- renderer->viewport.x = 0;
- renderer->viewport.y = 0;
- if (SDL_GetRendererOutputSize(renderer, &renderer->viewport.w, &renderer->viewport.h) < 0) {
- return -1;
- }
- }
- return renderer->UpdateViewport(renderer);
- }
- void
- SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
- {
- CHECK_RENDERER_MAGIC(renderer, );
- if (rect) {
- rect->x = (int)(renderer->viewport.x / renderer->scale.x);
- rect->y = (int)(renderer->viewport.y / renderer->scale.y);
- rect->w = (int)(renderer->viewport.w / renderer->scale.x);
- rect->h = (int)(renderer->viewport.h / renderer->scale.y);
- }
- }
- int
- SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
- {
- CHECK_RENDERER_MAGIC(renderer, -1)
- if (rect) {
- renderer->clipping_enabled = SDL_TRUE;
- renderer->clip_rect.x = (int)SDL_floor(rect->x * renderer->scale.x);
- renderer->clip_rect.y = (int)SDL_floor(rect->y * renderer->scale.y);
- renderer->clip_rect.w = (int)SDL_ceil(rect->w * renderer->scale.x);
- renderer->clip_rect.h = (int)SDL_ceil(rect->h * renderer->scale.y);
- } else {
- renderer->clipping_enabled = SDL_FALSE;
- SDL_zero(renderer->clip_rect);
- }
- return renderer->UpdateClipRect(renderer);
- }
- void
- SDL_RenderGetClipRect(SDL_Renderer * renderer, SDL_Rect * rect)
- {
- CHECK_RENDERER_MAGIC(renderer, )
- if (rect) {
- rect->x = (int)(renderer->clip_rect.x / renderer->scale.x);
- rect->y = (int)(renderer->clip_rect.y / renderer->scale.y);
- rect->w = (int)(renderer->clip_rect.w / renderer->scale.x);
- rect->h = (int)(renderer->clip_rect.h / renderer->scale.y);
- }
- }
- SDL_bool
- SDL_RenderIsClipEnabled(SDL_Renderer * renderer)
- {
- CHECK_RENDERER_MAGIC(renderer, SDL_FALSE)
- return renderer->clipping_enabled;
- }
- int
- SDL_RenderSetScale(SDL_Renderer * renderer, float scaleX, float scaleY)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- renderer->scale.x = scaleX;
- renderer->scale.y = scaleY;
- return 0;
- }
- void
- SDL_RenderGetScale(SDL_Renderer * renderer, float *scaleX, float *scaleY)
- {
- CHECK_RENDERER_MAGIC(renderer, );
- if (scaleX) {
- *scaleX = renderer->scale.x;
- }
- if (scaleY) {
- *scaleY = renderer->scale.y;
- }
- }
- int
- SDL_SetRenderDrawColor(SDL_Renderer * renderer,
- Uint8 r, Uint8 g, Uint8 b, Uint8 a)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- renderer->r = r;
- renderer->g = g;
- renderer->b = b;
- renderer->a = a;
- return 0;
- }
- int
- SDL_GetRenderDrawColor(SDL_Renderer * renderer,
- Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (r) {
- *r = renderer->r;
- }
- if (g) {
- *g = renderer->g;
- }
- if (b) {
- *b = renderer->b;
- }
- if (a) {
- *a = renderer->a;
- }
- return 0;
- }
- int
- SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (!IsSupportedBlendMode(renderer, blendMode)) {
- return SDL_Unsupported();
- }
- renderer->blendMode = blendMode;
- return 0;
- }
- int
- SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- *blendMode = renderer->blendMode;
- return 0;
- }
- int
- SDL_RenderClear(SDL_Renderer * renderer)
- {
- CHECK_RENDERER_MAGIC(renderer, -1);
- /* Don't draw while we're hidden */
- if (renderer->hidden) {
- return 0;
- }
- return renderer->RenderClear(renderer);
- }
- int
- SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
- {
- SDL_Point point;
- point.x = x;
- point.y = y;
- return SDL_RenderDrawPoints(renderer, &point, 1);
- }
- static int
- RenderDrawPointsWithRects(SDL_Renderer * renderer,
- const SDL_Point * points, int count)
- {
- SDL_FRect *frects;
- int i;
- int status;
- frects = SDL_stack_alloc(SDL_FRect, count);
- if (!frects) {
- return SDL_OutOfMemory();
- }
- for (i = 0; i < count; ++i) {
- frects[i].x = points[i].x * renderer->scale.x;
- frects[i].y = points[i].y * renderer->scale.y;
- frects[i].w = renderer->scale.x;
- frects[i].h = renderer->scale.y;
- }
- status = renderer->RenderFillRects(renderer, frects, count);
- SDL_stack_free(frects);
- return status;
- }
- int
- SDL_RenderDrawPoints(SDL_Renderer * renderer,
- const SDL_Point * points, int count)
- {
- SDL_FPoint *fpoints;
- int i;
- int status;
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (!points) {
- return SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
- }
- if (count < 1) {
- return 0;
- }
- /* Don't draw while we're hidden */
- if (renderer->hidden) {
- return 0;
- }
- if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
- return RenderDrawPointsWithRects(renderer, points, count);
- }
- fpoints = SDL_stack_alloc(SDL_FPoint, count);
- if (!fpoints) {
- return SDL_OutOfMemory();
- }
- for (i = 0; i < count; ++i) {
- fpoints[i].x = points[i].x * renderer->scale.x;
- fpoints[i].y = points[i].y * renderer->scale.y;
- }
- status = renderer->RenderDrawPoints(renderer, fpoints, count);
- SDL_stack_free(fpoints);
- return status;
- }
- int
- SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
- {
- SDL_Point points[2];
- points[0].x = x1;
- points[0].y = y1;
- points[1].x = x2;
- points[1].y = y2;
- return SDL_RenderDrawLines(renderer, points, 2);
- }
- static int
- RenderDrawLinesWithRects(SDL_Renderer * renderer,
- const SDL_Point * points, int count)
- {
- SDL_FRect *frect;
- SDL_FRect *frects;
- SDL_FPoint fpoints[2];
- int i, nrects;
- int status;
- frects = SDL_stack_alloc(SDL_FRect, count-1);
- if (!frects) {
- return SDL_OutOfMemory();
- }
- status = 0;
- nrects = 0;
- for (i = 0; i < count-1; ++i) {
- if (points[i].x == points[i+1].x) {
- int minY = SDL_min(points[i].y, points[i+1].y);
- int maxY = SDL_max(points[i].y, points[i+1].y);
- frect = &frects[nrects++];
- frect->x = points[i].x * renderer->scale.x;
- frect->y = minY * renderer->scale.y;
- frect->w = renderer->scale.x;
- frect->h = (maxY - minY + 1) * renderer->scale.y;
- } else if (points[i].y == points[i+1].y) {
- int minX = SDL_min(points[i].x, points[i+1].x);
- int maxX = SDL_max(points[i].x, points[i+1].x);
- frect = &frects[nrects++];
- frect->x = minX * renderer->scale.x;
- frect->y = points[i].y * renderer->scale.y;
- frect->w = (maxX - minX + 1) * renderer->scale.x;
- frect->h = renderer->scale.y;
- } else {
- /* FIXME: We can't use a rect for this line... */
- fpoints[0].x = points[i].x * renderer->scale.x;
- fpoints[0].y = points[i].y * renderer->scale.y;
- fpoints[1].x = points[i+1].x * renderer->scale.x;
- fpoints[1].y = points[i+1].y * renderer->scale.y;
- status += renderer->RenderDrawLines(renderer, fpoints, 2);
- }
- }
- status += renderer->RenderFillRects(renderer, frects, nrects);
- SDL_stack_free(frects);
- if (status < 0) {
- status = -1;
- }
- return status;
- }
- int
- SDL_RenderDrawLines(SDL_Renderer * renderer,
- const SDL_Point * points, int count)
- {
- SDL_FPoint *fpoints;
- int i;
- int status;
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (!points) {
- return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
- }
- if (count < 2) {
- return 0;
- }
- /* Don't draw while we're hidden */
- if (renderer->hidden) {
- return 0;
- }
- if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
- return RenderDrawLinesWithRects(renderer, points, count);
- }
- fpoints = SDL_stack_alloc(SDL_FPoint, count);
- if (!fpoints) {
- return SDL_OutOfMemory();
- }
- for (i = 0; i < count; ++i) {
- fpoints[i].x = points[i].x * renderer->scale.x;
- fpoints[i].y = points[i].y * renderer->scale.y;
- }
- status = renderer->RenderDrawLines(renderer, fpoints, count);
- SDL_stack_free(fpoints);
- return status;
- }
- int
- SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
- {
- SDL_Rect full_rect;
- SDL_Point points[5];
- CHECK_RENDERER_MAGIC(renderer, -1);
- /* If 'rect' == NULL, then outline the whole surface */
- if (!rect) {
- SDL_RenderGetViewport(renderer, &full_rect);
- full_rect.x = 0;
- full_rect.y = 0;
- rect = &full_rect;
- }
- points[0].x = rect->x;
- points[0].y = rect->y;
- points[1].x = rect->x+rect->w-1;
- points[1].y = rect->y;
- points[2].x = rect->x+rect->w-1;
- points[2].y = rect->y+rect->h-1;
- points[3].x = rect->x;
- points[3].y = rect->y+rect->h-1;
- points[4].x = rect->x;
- points[4].y = rect->y;
- return SDL_RenderDrawLines(renderer, points, 5);
- }
- int
- SDL_RenderDrawRects(SDL_Renderer * renderer,
- const SDL_Rect * rects, int count)
- {
- int i;
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (!rects) {
- return SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
- }
- if (count < 1) {
- return 0;
- }
- /* Don't draw while we're hidden */
- if (renderer->hidden) {
- return 0;
- }
- for (i = 0; i < count; ++i) {
- if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
- return -1;
- }
- }
- return 0;
- }
- int
- SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
- {
- SDL_Rect full_rect = { 0, 0, 0, 0 };
- CHECK_RENDERER_MAGIC(renderer, -1);
- /* If 'rect' == NULL, then outline the whole surface */
- if (!rect) {
- SDL_RenderGetViewport(renderer, &full_rect);
- full_rect.x = 0;
- full_rect.y = 0;
- rect = &full_rect;
- }
- return SDL_RenderFillRects(renderer, rect, 1);
- }
- int
- SDL_RenderFillRects(SDL_Renderer * renderer,
- const SDL_Rect * rects, int count)
- {
- SDL_FRect *frects;
- int i;
- int status;
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (!rects) {
- return SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
- }
- if (count < 1) {
- return 0;
- }
- /* Don't draw while we're hidden */
- if (renderer->hidden) {
- return 0;
- }
- frects = SDL_stack_alloc(SDL_FRect, count);
- if (!frects) {
- return SDL_OutOfMemory();
- }
- for (i = 0; i < count; ++i) {
- frects[i].x = rects[i].x * renderer->scale.x;
- frects[i].y = rects[i].y * renderer->scale.y;
- frects[i].w = rects[i].w * renderer->scale.x;
- frects[i].h = rects[i].h * renderer->scale.y;
- }
- status = renderer->RenderFillRects(renderer, frects, count);
- SDL_stack_free(frects);
- return status;
- }
- int
- SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
- const SDL_Rect * srcrect, const SDL_Rect * dstrect)
- {
- SDL_Rect real_srcrect = { 0, 0, 0, 0 };
- SDL_Rect real_dstrect = { 0, 0, 0, 0 };
- SDL_FRect frect;
- CHECK_RENDERER_MAGIC(renderer, -1);
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (renderer != texture->renderer) {
- return SDL_SetError("Texture was not created with this renderer");
- }
- /* Don't draw while we're hidden */
- if (renderer->hidden) {
- return 0;
- }
- real_srcrect.x = 0;
- real_srcrect.y = 0;
- real_srcrect.w = texture->w;
- real_srcrect.h = texture->h;
- if (srcrect) {
- if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
- return 0;
- }
- }
- SDL_RenderGetViewport(renderer, &real_dstrect);
- real_dstrect.x = 0;
- real_dstrect.y = 0;
- if (dstrect) {
- if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
- return 0;
- }
- real_dstrect = *dstrect;
- }
- if (texture->native) {
- texture = texture->native;
- }
- frect.x = real_dstrect.x * renderer->scale.x;
- frect.y = real_dstrect.y * renderer->scale.y;
- frect.w = real_dstrect.w * renderer->scale.x;
- frect.h = real_dstrect.h * renderer->scale.y;
- return renderer->RenderCopy(renderer, texture, &real_srcrect, &frect);
- }
- int
- SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
- const SDL_Rect * srcrect, const SDL_Rect * dstrect,
- const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
- {
- SDL_Rect real_srcrect = { 0, 0, 0, 0 };
- SDL_Rect real_dstrect = { 0, 0, 0, 0 };
- SDL_Point real_center;
- SDL_FRect frect;
- SDL_FPoint fcenter;
- if (flip == SDL_FLIP_NONE && (int)(angle/360) == angle/360) { /* fast path when we don't need rotation or flipping */
- return SDL_RenderCopy(renderer, texture, srcrect, dstrect);
- }
- CHECK_RENDERER_MAGIC(renderer, -1);
- CHECK_TEXTURE_MAGIC(texture, -1);
- if (renderer != texture->renderer) {
- return SDL_SetError("Texture was not created with this renderer");
- }
- if (!renderer->RenderCopyEx) {
- return SDL_SetError("Renderer does not support RenderCopyEx");
- }
- /* Don't draw while we're hidden */
- if (renderer->hidden) {
- return 0;
- }
- real_srcrect.x = 0;
- real_srcrect.y = 0;
- real_srcrect.w = texture->w;
- real_srcrect.h = texture->h;
- if (srcrect) {
- if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
- return 0;
- }
- }
- /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
- if (dstrect) {
- real_dstrect = *dstrect;
- } else {
- SDL_RenderGetViewport(renderer, &real_dstrect);
- real_dstrect.x = 0;
- real_dstrect.y = 0;
- }
- if (texture->native) {
- texture = texture->native;
- }
- if (center) {
- real_center = *center;
- } else {
- real_center.x = real_dstrect.w/2;
- real_center.y = real_dstrect.h/2;
- }
- frect.x = real_dstrect.x * renderer->scale.x;
- frect.y = real_dstrect.y * renderer->scale.y;
- frect.w = real_dstrect.w * renderer->scale.x;
- frect.h = real_dstrect.h * renderer->scale.y;
- fcenter.x = real_center.x * renderer->scale.x;
- fcenter.y = real_center.y * renderer->scale.y;
- return renderer->RenderCopyEx(renderer, texture, &real_srcrect, &frect, angle, &fcenter, flip);
- }
- int
- SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
- Uint32 format, void * pixels, int pitch)
- {
- SDL_Rect real_rect;
- CHECK_RENDERER_MAGIC(renderer, -1);
- if (!renderer->RenderReadPixels) {
- return SDL_Unsupported();
- }
- if (!format) {
- format = SDL_GetWindowPixelFormat(renderer->window);
- }
- real_rect.x = renderer->viewport.x;
- real_rect.y = renderer->viewport.y;
- real_rect.w = renderer->viewport.w;
- real_rect.h = renderer->viewport.h;
- if (rect) {
- if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
- return 0;
- }
- if (real_rect.y > rect->y) {
- pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
- }
- if (real_rect.x > rect->x) {
- int bpp = SDL_BYTESPERPIXEL(format);
- pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
- }
- }
- return renderer->RenderReadPixels(renderer, &real_rect,
- format, pixels, pitch);
- }
- void
- SDL_RenderPresent(SDL_Renderer * renderer)
- {
- CHECK_RENDERER_MAGIC(renderer, );
- /* Don't draw while we're hidden */
- if (renderer->hidden) {
- return;
- }
- renderer->RenderPresent(renderer);
- }
- void
- SDL_DestroyTexture(SDL_Texture * texture)
- {
- SDL_Renderer *renderer;
- CHECK_TEXTURE_MAGIC(texture, );
- renderer = texture->renderer;
- if (texture == renderer->target) {
- SDL_SetRenderTarget(renderer, NULL);
- }
- texture->magic = NULL;
- if (texture->next) {
- texture->next->prev = texture->prev;
- }
- if (texture->prev) {
- texture->prev->next = texture->next;
- } else {
- renderer->textures = texture->next;
- }
- if (texture->native) {
- SDL_DestroyTexture(texture->native);
- }
- if (texture->yuv) {
- SDL_SW_DestroyYUVTexture(texture->yuv);
- }
- SDL_free(texture->pixels);
- renderer->DestroyTexture(renderer, texture);
- SDL_free(texture);
- }
- void
- SDL_DestroyRenderer(SDL_Renderer * renderer)
- {
- CHECK_RENDERER_MAGIC(renderer, );
- SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
- /* Free existing textures for this renderer */
- while (renderer->textures) {
- SDL_Texture *tex = renderer->textures; (void) tex;
- SDL_DestroyTexture(renderer->textures);
- SDL_assert(tex != renderer->textures); /* satisfy static analysis. */
- }
- if (renderer->window) {
- SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
- }
- /* It's no longer magical... */
- renderer->magic = NULL;
- /* Free the renderer instance */
- renderer->DestroyRenderer(renderer);
- }
- int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
- {
- SDL_Renderer *renderer;
- CHECK_TEXTURE_MAGIC(texture, -1);
- renderer = texture->renderer;
- if (texture->native) {
- return SDL_GL_BindTexture(texture->native, texw, texh);
- } else if (renderer && renderer->GL_BindTexture) {
- return renderer->GL_BindTexture(renderer, texture, texw, texh);
- } else {
- return SDL_Unsupported();
- }
- }
- int SDL_GL_UnbindTexture(SDL_Texture *texture)
- {
- SDL_Renderer *renderer;
- CHECK_TEXTURE_MAGIC(texture, -1);
- renderer = texture->renderer;
- if (texture->native) {
- return SDL_GL_UnbindTexture(texture->native);
- } else if (renderer && renderer->GL_UnbindTexture) {
- return renderer->GL_UnbindTexture(renderer, texture);
- }
- return SDL_Unsupported();
- }
- void *
- SDL_RenderGetMetalLayer(SDL_Renderer * renderer)
- {
- CHECK_RENDERER_MAGIC(renderer, NULL);
- if (renderer->GetMetalLayer) {
- return renderer->GetMetalLayer(renderer);
- }
- return NULL;
- }
- void *
- SDL_RenderGetMetalCommandEncoder(SDL_Renderer * renderer)
- {
- CHECK_RENDERER_MAGIC(renderer, NULL);
- if (renderer->GetMetalCommandEncoder) {
- return renderer->GetMetalCommandEncoder(renderer);
- }
- return NULL;
- }
- static SDL_BlendMode
- SDL_GetShortBlendMode(SDL_BlendMode blendMode)
- {
- if (blendMode == SDL_BLENDMODE_NONE_FULL) {
- return SDL_BLENDMODE_NONE;
- }
- if (blendMode == SDL_BLENDMODE_BLEND_FULL) {
- return SDL_BLENDMODE_BLEND;
- }
- if (blendMode == SDL_BLENDMODE_ADD_FULL) {
- return SDL_BLENDMODE_ADD;
- }
- if (blendMode == SDL_BLENDMODE_MOD_FULL) {
- return SDL_BLENDMODE_MOD;
- }
- return blendMode;
- }
- static SDL_BlendMode
- SDL_GetLongBlendMode(SDL_BlendMode blendMode)
- {
- if (blendMode == SDL_BLENDMODE_NONE) {
- return SDL_BLENDMODE_NONE_FULL;
- }
- if (blendMode == SDL_BLENDMODE_BLEND) {
- return SDL_BLENDMODE_BLEND_FULL;
- }
- if (blendMode == SDL_BLENDMODE_ADD) {
- return SDL_BLENDMODE_ADD_FULL;
- }
- if (blendMode == SDL_BLENDMODE_MOD) {
- return SDL_BLENDMODE_MOD_FULL;
- }
- return blendMode;
- }
- SDL_BlendMode
- SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor, SDL_BlendFactor dstColorFactor,
- SDL_BlendOperation colorOperation,
- SDL_BlendFactor srcAlphaFactor, SDL_BlendFactor dstAlphaFactor,
- SDL_BlendOperation alphaOperation)
- {
- SDL_BlendMode blendMode = SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation,
- srcAlphaFactor, dstAlphaFactor, alphaOperation);
- return SDL_GetShortBlendMode(blendMode);
- }
- SDL_BlendFactor
- SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode)
- {
- blendMode = SDL_GetLongBlendMode(blendMode);
- return (SDL_BlendFactor)(((Uint32)blendMode >> 4) & 0xF);
- }
- SDL_BlendFactor
- SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode)
- {
- blendMode = SDL_GetLongBlendMode(blendMode);
- return (SDL_BlendFactor)(((Uint32)blendMode >> 8) & 0xF);
- }
- SDL_BlendOperation
- SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode)
- {
- blendMode = SDL_GetLongBlendMode(blendMode);
- return (SDL_BlendOperation)(((Uint32)blendMode >> 0) & 0xF);
- }
- SDL_BlendFactor
- SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode)
- {
- blendMode = SDL_GetLongBlendMode(blendMode);
- return (SDL_BlendFactor)(((Uint32)blendMode >> 20) & 0xF);
- }
- SDL_BlendFactor
- SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode)
- {
- blendMode = SDL_GetLongBlendMode(blendMode);
- return (SDL_BlendFactor)(((Uint32)blendMode >> 24) & 0xF);
- }
- SDL_BlendOperation
- SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode)
- {
- blendMode = SDL_GetLongBlendMode(blendMode);
- return (SDL_BlendOperation)(((Uint32)blendMode >> 16) & 0xF);
- }
- /* vi: set ts=4 sw=4 expandtab: */
|