2015-06-21 15:33:46 +00:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
2021-01-02 18:25:38 +00:00
|
|
|
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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_hints.h"
|
|
|
|
#include "SDL_render.h"
|
|
|
|
#include "SDL_sysrender.h"
|
|
|
|
#include "software/SDL_render_sw_c.h"
|
2020-03-17 14:47:30 +00:00
|
|
|
#include "../video/SDL_pixels_c.h"
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2019-06-28 14:38:42 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
# include "../core/android/SDL_android.h"
|
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
/* as a courtesy to iOS apps, we don't try to draw when in the background, as
|
|
|
|
that will crash the app. However, these apps _should_ have used
|
|
|
|
SDL_AddEventWatch to catch SDL_APP_WILLENTERBACKGROUND events and stopped
|
|
|
|
drawing themselves. Other platforms still draw, as the compositor can use it,
|
|
|
|
and more importantly: drawing to render targets isn't lost. But I still think
|
|
|
|
this should probably be removed at some point in the future. --ryan. */
|
|
|
|
#if defined(__IPHONEOS__) || defined(__TVOS__) || defined(__ANDROID__)
|
|
|
|
#define DONT_DRAW_WHILE_HIDDEN 1
|
|
|
|
#else
|
|
|
|
#define DONT_DRAW_WHILE_HIDDEN 0
|
|
|
|
#endif
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
#define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
|
|
|
|
|
|
|
|
#define CHECK_RENDERER_MAGIC(renderer, retval) \
|
2017-08-14 23:34:54 +00:00
|
|
|
SDL_assert(renderer && renderer->magic == &renderer_magic); \
|
2015-06-21 15:33:46 +00:00
|
|
|
if (!renderer || renderer->magic != &renderer_magic) { \
|
|
|
|
SDL_SetError("Invalid renderer"); \
|
|
|
|
return retval; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_TEXTURE_MAGIC(texture, retval) \
|
2017-08-14 23:34:54 +00:00
|
|
|
SDL_assert(texture && texture->magic == &texture_magic); \
|
2015-06-21 15:33:46 +00:00
|
|
|
if (!texture || texture->magic != &texture_magic) { \
|
|
|
|
SDL_SetError("Invalid texture"); \
|
|
|
|
return retval; \
|
|
|
|
}
|
|
|
|
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
/* 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)
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2020-01-16 16:52:59 +00:00
|
|
|
#define SDL_BLENDMODE_MUL_FULL \
|
|
|
|
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_DST_COLOR, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
|
|
|
|
SDL_BLENDFACTOR_DST_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
#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
|
2018-12-19 23:10:02 +00:00
|
|
|
#if SDL_VIDEO_RENDER_METAL
|
|
|
|
&METAL_RenderDriver,
|
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
#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_PSP
|
|
|
|
&PSP_RenderDriver,
|
|
|
|
#endif
|
2020-11-16 17:40:22 +00:00
|
|
|
#if SDL_VIDEO_RENDER_VITA_GXM
|
|
|
|
&VITA_GXM_RenderDriver,
|
|
|
|
#endif
|
2020-01-21 20:33:40 +00:00
|
|
|
#if SDL_VIDEO_RENDER_SW
|
2015-06-21 15:33:46 +00:00
|
|
|
&SW_RenderDriver
|
2020-01-21 20:33:40 +00:00
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
};
|
|
|
|
#endif /* !SDL_RENDER_DISABLED */
|
|
|
|
|
|
|
|
static char renderer_magic;
|
|
|
|
static char texture_magic;
|
|
|
|
|
2018-09-24 06:07:35 +00:00
|
|
|
static SDL_INLINE void
|
|
|
|
DebugLogRenderCommands(const SDL_RenderCommand *cmd)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
unsigned int i = 1;
|
|
|
|
SDL_Log("Render commands to flush:");
|
|
|
|
while (cmd) {
|
|
|
|
switch (cmd->command) {
|
|
|
|
case SDL_RENDERCMD_NO_OP:
|
|
|
|
SDL_Log(" %u. no-op", i++);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_SETVIEWPORT:
|
|
|
|
SDL_Log(" %u. set viewport (first=%u, rect={(%d, %d), %dx%d})", i++,
|
|
|
|
(unsigned int) cmd->data.viewport.first,
|
|
|
|
cmd->data.viewport.rect.x, cmd->data.viewport.rect.y,
|
|
|
|
cmd->data.viewport.rect.w, cmd->data.viewport.rect.h);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_SETCLIPRECT:
|
|
|
|
SDL_Log(" %u. set cliprect (enabled=%s, rect={(%d, %d), %dx%d})", i++,
|
|
|
|
cmd->data.cliprect.enabled ? "true" : "false",
|
|
|
|
cmd->data.cliprect.rect.x, cmd->data.cliprect.rect.y,
|
|
|
|
cmd->data.cliprect.rect.w, cmd->data.cliprect.rect.h);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_SETDRAWCOLOR:
|
|
|
|
SDL_Log(" %u. set draw color (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
|
|
|
|
(unsigned int) cmd->data.color.first,
|
|
|
|
(int) cmd->data.color.r, (int) cmd->data.color.g,
|
|
|
|
(int) cmd->data.color.b, (int) cmd->data.color.a);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_CLEAR:
|
|
|
|
SDL_Log(" %u. clear (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
|
|
|
|
(unsigned int) cmd->data.color.first,
|
|
|
|
(int) cmd->data.color.r, (int) cmd->data.color.g,
|
|
|
|
(int) cmd->data.color.b, (int) cmd->data.color.a);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_DRAW_POINTS:
|
|
|
|
SDL_Log(" %u. draw points (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
|
|
|
|
(unsigned int) cmd->data.draw.first,
|
|
|
|
(unsigned int) cmd->data.draw.count,
|
|
|
|
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
|
|
|
|
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
|
|
|
|
(int) cmd->data.draw.blend);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_DRAW_LINES:
|
|
|
|
SDL_Log(" %u. draw lines (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
|
|
|
|
(unsigned int) cmd->data.draw.first,
|
|
|
|
(unsigned int) cmd->data.draw.count,
|
|
|
|
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
|
|
|
|
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
|
|
|
|
(int) cmd->data.draw.blend);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_FILL_RECTS:
|
|
|
|
SDL_Log(" %u. fill rects (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
|
|
|
|
(unsigned int) cmd->data.draw.first,
|
|
|
|
(unsigned int) cmd->data.draw.count,
|
|
|
|
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
|
|
|
|
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
|
|
|
|
(int) cmd->data.draw.blend);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_COPY:
|
|
|
|
SDL_Log(" %u. copy (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
|
|
|
|
(unsigned int) cmd->data.draw.first,
|
|
|
|
(unsigned int) cmd->data.draw.count,
|
|
|
|
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
|
|
|
|
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
|
|
|
|
(int) cmd->data.draw.blend, cmd->data.draw.texture);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case SDL_RENDERCMD_COPY_EX:
|
|
|
|
SDL_Log(" %u. copyex (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
|
|
|
|
(unsigned int) cmd->data.draw.first,
|
|
|
|
(unsigned int) cmd->data.draw.count,
|
|
|
|
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
|
|
|
|
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
|
|
|
|
(int) cmd->data.draw.blend, cmd->data.draw.texture);
|
|
|
|
break;
|
2021-04-01 18:18:05 +00:00
|
|
|
|
|
|
|
case SDL_RENDERCMD_GEOMETRY:
|
|
|
|
SDL_Log(" %u. geometry (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
|
|
|
|
(unsigned int) cmd->data.draw.first,
|
|
|
|
(unsigned int) cmd->data.draw.count,
|
|
|
|
(int) cmd->data.draw.r, (int) cmd->data.draw.g,
|
|
|
|
(int) cmd->data.draw.b, (int) cmd->data.draw.a,
|
|
|
|
(int) cmd->data.draw.blend, cmd->data.draw.texture);
|
|
|
|
break;
|
|
|
|
|
2018-09-24 06:07:35 +00:00
|
|
|
}
|
|
|
|
cmd = cmd->next;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
FlushRenderCommands(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
int retval;
|
2018-09-24 03:20:40 +00:00
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
|
|
|
|
|
|
|
|
if (renderer->render_commands == NULL) { /* nothing to do! */
|
|
|
|
SDL_assert(renderer->vertex_data_used == 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-24 06:07:35 +00:00
|
|
|
DebugLogRenderCommands(renderer->render_commands);
|
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
retval = renderer->RunCommandQueue(renderer, renderer->render_commands, renderer->vertex_data, renderer->vertex_data_used);
|
|
|
|
|
|
|
|
/* Move the whole render command queue to the unused pool so we can reuse them next time. */
|
|
|
|
if (renderer->render_commands_tail != NULL) {
|
|
|
|
renderer->render_commands_tail->next = renderer->render_commands_pool;
|
|
|
|
renderer->render_commands_pool = renderer->render_commands;
|
|
|
|
renderer->render_commands_tail = NULL;
|
|
|
|
renderer->render_commands = NULL;
|
|
|
|
}
|
|
|
|
renderer->vertex_data_used = 0;
|
|
|
|
renderer->render_command_generation++;
|
2018-09-24 03:20:40 +00:00
|
|
|
renderer->color_queued = SDL_FALSE;
|
|
|
|
renderer->viewport_queued = SDL_FALSE;
|
|
|
|
renderer->cliprect_queued = SDL_FALSE;
|
2018-09-20 19:46:02 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
FlushRenderCommandsIfTextureNeeded(SDL_Texture *texture)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer = texture->renderer;
|
|
|
|
if (texture->last_command_generation == renderer->render_command_generation) {
|
|
|
|
/* the current command queue depends on this texture, flush the queue now before it changes */
|
|
|
|
return FlushRenderCommands(renderer);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_INLINE int
|
|
|
|
FlushRenderCommandsIfNotBatching(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
return renderer->batching ? 0 : FlushRenderCommands(renderer);
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:34:44 +00:00
|
|
|
int
|
|
|
|
SDL_RenderFlush(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
return FlushRenderCommands(renderer);
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
void *
|
2018-09-24 03:20:40 +00:00
|
|
|
SDL_AllocateRenderVertices(SDL_Renderer *renderer, const size_t numbytes, const size_t alignment, size_t *offset)
|
2018-09-20 19:46:02 +00:00
|
|
|
{
|
2018-09-24 03:20:40 +00:00
|
|
|
const size_t needed = renderer->vertex_data_used + numbytes + alignment;
|
2021-09-19 04:05:21 +00:00
|
|
|
const size_t current_offset = renderer->vertex_data_used;
|
2018-09-24 03:20:40 +00:00
|
|
|
|
2021-09-19 04:05:21 +00:00
|
|
|
const size_t aligner = (alignment && ((current_offset & (alignment - 1)) != 0)) ? (alignment - (current_offset & (alignment - 1))) : 0;
|
|
|
|
const size_t aligned = current_offset + aligner;
|
2018-09-24 03:20:40 +00:00
|
|
|
|
2019-08-17 03:43:44 +00:00
|
|
|
if (renderer->vertex_data_allocation < needed) {
|
2018-09-24 03:20:40 +00:00
|
|
|
const size_t current_allocation = renderer->vertex_data ? renderer->vertex_data_allocation : 1024;
|
2019-08-17 03:43:44 +00:00
|
|
|
size_t newsize = current_allocation * 2;
|
|
|
|
void *ptr;
|
|
|
|
while (newsize < needed) {
|
|
|
|
newsize *= 2;
|
|
|
|
}
|
2020-11-14 20:37:26 +00:00
|
|
|
|
2019-08-17 03:43:44 +00:00
|
|
|
ptr = SDL_realloc(renderer->vertex_data, newsize);
|
2020-11-14 20:37:26 +00:00
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
if (ptr == NULL) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
renderer->vertex_data = ptr;
|
|
|
|
renderer->vertex_data_allocation = newsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset) {
|
2018-09-24 03:20:40 +00:00
|
|
|
*offset = aligned;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 03:20:40 +00:00
|
|
|
renderer->vertex_data_used += aligner + numbytes;
|
|
|
|
|
2019-08-17 03:43:44 +00:00
|
|
|
return ((Uint8 *) renderer->vertex_data) + aligned;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_RenderCommand *
|
|
|
|
AllocateRenderCommand(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
SDL_RenderCommand *retval = NULL;
|
|
|
|
|
|
|
|
/* !!! FIXME: are there threading limitations in SDL's render API? If not, we need to mutex this. */
|
|
|
|
retval = renderer->render_commands_pool;
|
|
|
|
if (retval != NULL) {
|
|
|
|
renderer->render_commands_pool = retval->next;
|
|
|
|
retval->next = NULL;
|
|
|
|
} else {
|
|
|
|
retval = SDL_calloc(1, sizeof (*retval));
|
|
|
|
if (!retval) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
|
|
|
|
if (renderer->render_commands_tail != NULL) {
|
|
|
|
renderer->render_commands_tail->next = retval;
|
|
|
|
} else {
|
|
|
|
renderer->render_commands = retval;
|
|
|
|
}
|
|
|
|
renderer->render_commands_tail = retval;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-09-24 03:20:40 +00:00
|
|
|
QueueCmdSetViewport(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
if (!renderer->viewport_queued || (SDL_memcmp(&renderer->viewport, &renderer->last_queued_viewport, sizeof (SDL_Rect)) != 0)) {
|
|
|
|
SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
|
|
|
|
retval = -1;
|
|
|
|
if (cmd != NULL) {
|
|
|
|
cmd->command = SDL_RENDERCMD_SETVIEWPORT;
|
|
|
|
cmd->data.viewport.first = 0; /* render backend will fill this in. */
|
|
|
|
SDL_memcpy(&cmd->data.viewport.rect, &renderer->viewport, sizeof (renderer->viewport));
|
|
|
|
retval = renderer->QueueSetViewport(renderer, cmd);
|
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
} else {
|
|
|
|
SDL_memcpy(&renderer->last_queued_viewport, &renderer->viewport, sizeof (SDL_Rect));
|
|
|
|
renderer->viewport_queued = SDL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-09-24 03:20:40 +00:00
|
|
|
QueueCmdSetClipRect(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
if ((!renderer->cliprect_queued) ||
|
|
|
|
(renderer->clipping_enabled != renderer->last_queued_cliprect_enabled) ||
|
|
|
|
(SDL_memcmp(&renderer->clip_rect, &renderer->last_queued_cliprect, sizeof (SDL_Rect)) != 0)) {
|
|
|
|
SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
|
|
|
|
if (cmd == NULL) {
|
|
|
|
retval = -1;
|
|
|
|
} else {
|
|
|
|
cmd->command = SDL_RENDERCMD_SETCLIPRECT;
|
|
|
|
cmd->data.cliprect.enabled = renderer->clipping_enabled;
|
|
|
|
SDL_memcpy(&cmd->data.cliprect.rect, &renderer->clip_rect, sizeof (cmd->data.cliprect.rect));
|
|
|
|
SDL_memcpy(&renderer->last_queued_cliprect, &renderer->clip_rect, sizeof (SDL_Rect));
|
|
|
|
renderer->last_queued_cliprect_enabled = renderer->clipping_enabled;
|
|
|
|
renderer->cliprect_queued = SDL_TRUE;
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval;
|
2018-09-24 03:20:40 +00:00
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
|
2018-09-24 03:20:40 +00:00
|
|
|
static int
|
2021-10-24 15:16:49 +00:00
|
|
|
QueueCmdSetDrawColor(SDL_Renderer *renderer, SDL_Color *col)
|
2018-09-24 03:20:40 +00:00
|
|
|
{
|
2021-10-24 15:16:49 +00:00
|
|
|
const Uint32 color = ((col->a << 24) | (col->r << 16) | (col->g << 8) | col->b);
|
2018-09-24 03:20:40 +00:00
|
|
|
int retval = 0;
|
2021-01-02 16:29:34 +00:00
|
|
|
|
2018-09-24 03:20:40 +00:00
|
|
|
if (!renderer->color_queued || (color != renderer->last_queued_color)) {
|
|
|
|
SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
|
|
|
|
retval = -1;
|
|
|
|
|
|
|
|
if (cmd != NULL) {
|
|
|
|
cmd->command = SDL_RENDERCMD_SETDRAWCOLOR;
|
|
|
|
cmd->data.color.first = 0; /* render backend will fill this in. */
|
2021-10-24 15:16:49 +00:00
|
|
|
cmd->data.color.r = col->r;
|
|
|
|
cmd->data.color.g = col->g;
|
|
|
|
cmd->data.color.b = col->b;
|
|
|
|
cmd->data.color.a = col->a;
|
2018-09-24 03:20:40 +00:00
|
|
|
retval = renderer->QueueSetDrawColor(renderer, cmd);
|
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
} else {
|
|
|
|
renderer->last_queued_color = color;
|
|
|
|
renderer->color_queued = SDL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
QueueCmdClear(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
|
|
|
|
if (cmd == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd->command = SDL_RENDERCMD_CLEAR;
|
2018-09-24 03:20:40 +00:00
|
|
|
cmd->data.color.first = 0;
|
2021-10-24 15:16:49 +00:00
|
|
|
cmd->data.color.r = renderer->color.r;
|
|
|
|
cmd->data.color.g = renderer->color.g;
|
|
|
|
cmd->data.color.b = renderer->color.b;
|
|
|
|
cmd->data.color.a = renderer->color.a;
|
2018-09-25 21:04:47 +00:00
|
|
|
return 0;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 15:16:49 +00:00
|
|
|
static SDL_RenderCommand *
|
|
|
|
PrepQueueCmdDraw(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtype, SDL_Texture *texture)
|
2018-09-24 03:20:40 +00:00
|
|
|
{
|
2021-10-24 15:16:49 +00:00
|
|
|
SDL_RenderCommand *cmd = NULL;
|
|
|
|
int retval = 0;
|
|
|
|
SDL_Color *color;
|
|
|
|
SDL_BlendMode blendMode;
|
|
|
|
|
|
|
|
if (texture) {
|
|
|
|
color = &texture->color;
|
|
|
|
blendMode = texture->blendMode;
|
|
|
|
} else {
|
|
|
|
color = &renderer->color;
|
|
|
|
blendMode = renderer->blendMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmdtype != SDL_RENDERCMD_GEOMETRY) {
|
|
|
|
/* !!! FIXME: drop this draw if viewport w or h is zero. */
|
|
|
|
retval = QueueCmdSetDrawColor(renderer, color);
|
|
|
|
}
|
2019-08-18 12:35:11 +00:00
|
|
|
|
|
|
|
/* Set the viewport and clip rect directly before draws, so the backends
|
|
|
|
* don't have to worry about that state not being valid at draw time. */
|
2019-08-18 01:26:33 +00:00
|
|
|
if (retval == 0 && !renderer->viewport_queued) {
|
2018-09-24 03:20:40 +00:00
|
|
|
retval = QueueCmdSetViewport(renderer);
|
|
|
|
}
|
2019-08-18 01:26:33 +00:00
|
|
|
if (retval == 0 && !renderer->cliprect_queued) {
|
2018-09-24 03:20:40 +00:00
|
|
|
retval = QueueCmdSetClipRect(renderer);
|
|
|
|
}
|
|
|
|
|
2021-10-24 15:16:49 +00:00
|
|
|
if (retval == 0) {
|
2018-09-24 03:20:40 +00:00
|
|
|
cmd = AllocateRenderCommand(renderer);
|
|
|
|
if (cmd != NULL) {
|
|
|
|
cmd->command = cmdtype;
|
|
|
|
cmd->data.draw.first = 0; /* render backend will fill this in. */
|
|
|
|
cmd->data.draw.count = 0; /* render backend will fill this in. */
|
2021-10-24 15:16:49 +00:00
|
|
|
cmd->data.draw.r = color->r;
|
|
|
|
cmd->data.draw.g = color->g;
|
|
|
|
cmd->data.draw.b = color->b;
|
|
|
|
cmd->data.draw.a = color->a;
|
|
|
|
cmd->data.draw.blend = blendMode;
|
|
|
|
cmd->data.draw.texture = texture;
|
2018-09-24 03:20:40 +00:00
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
QueueCmdDrawPoints(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
|
|
|
|
{
|
2021-10-24 15:16:49 +00:00
|
|
|
SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_POINTS, NULL);
|
2018-09-20 19:46:02 +00:00
|
|
|
int retval = -1;
|
|
|
|
if (cmd != NULL) {
|
|
|
|
retval = renderer->QueueDrawPoints(renderer, cmd, points, count);
|
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
QueueCmdDrawLines(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
|
|
|
|
{
|
2021-10-24 15:16:49 +00:00
|
|
|
SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_LINES, NULL);
|
2018-09-20 19:46:02 +00:00
|
|
|
int retval = -1;
|
|
|
|
if (cmd != NULL) {
|
|
|
|
retval = renderer->QueueDrawLines(renderer, cmd, points, count);
|
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int count)
|
|
|
|
{
|
2021-09-23 20:45:45 +00:00
|
|
|
SDL_RenderCommand *cmd;
|
2018-09-20 19:46:02 +00:00
|
|
|
int retval = -1;
|
2021-09-23 20:45:45 +00:00
|
|
|
int use_geometry = renderer->QueueGeometry && ! (renderer->info.flags & SDL_RENDERER_SOFTWARE);
|
|
|
|
|
2021-10-24 15:16:49 +00:00
|
|
|
cmd = PrepQueueCmdDraw(renderer, (use_geometry ? SDL_RENDERCMD_GEOMETRY : SDL_RENDERCMD_FILL_RECTS), NULL);
|
2021-09-23 20:45:45 +00:00
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
if (cmd != NULL) {
|
2021-09-23 20:45:45 +00:00
|
|
|
if (use_geometry) {
|
|
|
|
SDL_bool isstack1;
|
|
|
|
SDL_bool isstack2;
|
|
|
|
float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
|
|
|
|
int *indices = SDL_small_alloc(int, 6 * count, &isstack2);
|
|
|
|
|
|
|
|
if (xy && indices) {
|
|
|
|
int i;
|
|
|
|
float *ptr_xy = xy;
|
|
|
|
int *ptr_indices = indices;
|
|
|
|
const int xy_stride = 2 * sizeof (float);
|
|
|
|
const int num_vertices = 4 * count;
|
|
|
|
const int num_indices = 6 * count;
|
|
|
|
const int size_indices = 4;
|
2021-09-24 20:39:49 +00:00
|
|
|
int cur_indice = 0;
|
2021-09-23 20:45:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
float minx, miny, maxx, maxy;
|
|
|
|
|
|
|
|
minx = rects[i].x;
|
|
|
|
miny = rects[i].y;
|
|
|
|
maxx = rects[i].x + rects[i].w;
|
|
|
|
maxy = rects[i].y + rects[i].h;
|
|
|
|
|
|
|
|
*ptr_xy++ = minx;
|
|
|
|
*ptr_xy++ = miny;
|
|
|
|
*ptr_xy++ = maxx;
|
|
|
|
*ptr_xy++ = miny;
|
|
|
|
*ptr_xy++ = maxx;
|
|
|
|
*ptr_xy++ = maxy;
|
|
|
|
*ptr_xy++ = minx;
|
|
|
|
*ptr_xy++ = maxy;
|
|
|
|
|
2021-09-24 20:39:49 +00:00
|
|
|
*ptr_indices++ = cur_indice + 0;
|
|
|
|
*ptr_indices++ = cur_indice + 1;
|
|
|
|
*ptr_indices++ = cur_indice + 2;
|
|
|
|
*ptr_indices++ = cur_indice + 0;
|
|
|
|
*ptr_indices++ = cur_indice + 2;
|
|
|
|
*ptr_indices++ = cur_indice + 3;
|
|
|
|
cur_indice += 4;
|
2021-09-23 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retval = renderer->QueueGeometry(renderer, cmd, NULL,
|
2021-10-24 15:16:49 +00:00
|
|
|
xy, xy_stride, (int *) &renderer->color, 0 /* color_stride */, NULL, 0,
|
2021-09-23 20:45:45 +00:00
|
|
|
num_vertices, indices, num_indices, size_indices,
|
|
|
|
1.0f, 1.0f);
|
|
|
|
|
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_small_free(xy, isstack1);
|
|
|
|
SDL_small_free(indices, isstack2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
retval = renderer->QueueFillRects(renderer, cmd, rects, count);
|
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_FRect * dstrect)
|
|
|
|
{
|
2021-10-24 15:16:49 +00:00
|
|
|
SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY, texture);
|
2018-09-20 19:46:02 +00:00
|
|
|
int retval = -1;
|
|
|
|
if (cmd != NULL) {
|
|
|
|
retval = renderer->QueueCopy(renderer, cmd, texture, srcrect, dstrect);
|
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture * texture,
|
|
|
|
const SDL_Rect * srcquad, const SDL_FRect * dstrect,
|
|
|
|
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
|
|
|
|
{
|
2021-10-24 15:16:49 +00:00
|
|
|
SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY_EX, texture);
|
2018-09-20 19:46:02 +00:00
|
|
|
int retval = -1;
|
2018-09-29 04:00:38 +00:00
|
|
|
SDL_assert(renderer->QueueCopyEx != NULL); /* should have caught at higher level. */
|
2018-09-20 19:46:02 +00:00
|
|
|
if (cmd != NULL) {
|
|
|
|
retval = renderer->QueueCopyEx(renderer, cmd, texture, srcquad, dstrect, angle, center, flip);
|
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval;
|
2018-09-20 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 14:09:34 +00:00
|
|
|
static int
|
|
|
|
QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
|
2021-04-01 07:49:16 +00:00
|
|
|
const float *xy, int xy_stride,
|
|
|
|
const int *color, int color_stride,
|
|
|
|
const float *uv, int uv_stride,
|
|
|
|
int num_vertices,
|
2021-04-23 10:00:14 +00:00
|
|
|
const void *indices, int num_indices, int size_indices,
|
2021-04-01 07:49:16 +00:00
|
|
|
float scale_x, float scale_y)
|
2021-03-16 14:09:34 +00:00
|
|
|
{
|
|
|
|
SDL_RenderCommand *cmd;
|
|
|
|
int retval = -1;
|
2021-10-24 15:16:49 +00:00
|
|
|
cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_GEOMETRY, texture);
|
2021-03-16 14:09:34 +00:00
|
|
|
if (cmd != NULL) {
|
2021-04-01 07:49:16 +00:00
|
|
|
retval = renderer->QueueGeometry(renderer, cmd, texture,
|
|
|
|
xy, xy_stride, color, color_stride, uv, uv_stride,
|
2021-04-23 10:00:14 +00:00
|
|
|
num_vertices, indices, num_indices, size_indices,
|
2021-04-01 07:49:16 +00:00
|
|
|
scale_x, scale_y);
|
2021-03-16 14:09:34 +00:00
|
|
|
if (retval < 0) {
|
|
|
|
cmd->command = SDL_RENDERCMD_NO_OP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:13:56 +00:00
|
|
|
static void GetWindowViewportValues(SDL_Renderer *renderer, int *logical_w, int *logical_h, SDL_Rect *viewport, SDL_FPoint *scale)
|
|
|
|
{
|
|
|
|
SDL_LockMutex(renderer->target_mutex);
|
|
|
|
*logical_w = renderer->target ? renderer->logical_w_backup : renderer->logical_w;
|
|
|
|
*logical_h = renderer->target ? renderer->logical_h_backup : renderer->logical_h;
|
|
|
|
*viewport = renderer->target ? renderer->viewport_backup : renderer->viewport;
|
|
|
|
*scale = renderer->target ? renderer->scale_backup : renderer->scale;
|
|
|
|
SDL_UnlockMutex(renderer->target_mutex);
|
|
|
|
}
|
|
|
|
|
2017-08-14 04:06:52 +00:00
|
|
|
static int SDLCALL
|
2015-06-21 15:33:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-08-03 12:55:45 +00:00
|
|
|
/* Update the DPI scale if the window has been resized. */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
2018-09-24 03:20:40 +00:00
|
|
|
QueueCmdSetViewport(renderer);
|
2018-09-25 21:04:47 +00:00
|
|
|
FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-01-02 16:29:34 +00:00
|
|
|
} else if (event->window.event == SDL_WINDOWEVENT_RESTORED ||
|
2015-12-27 22:55:45 +00:00
|
|
|
event->window.event == SDL_WINDOWEVENT_MAXIMIZED) {
|
2015-06-21 15:33:46 +00:00
|
|
|
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);
|
2018-06-18 20:13:56 +00:00
|
|
|
if (window == renderer->window) {
|
|
|
|
int logical_w, logical_h;
|
|
|
|
SDL_Rect viewport;
|
|
|
|
SDL_FPoint scale;
|
|
|
|
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
|
|
|
|
if (logical_w) {
|
|
|
|
event->motion.x -= (int)(viewport.x * renderer->dpi_scale.x);
|
|
|
|
event->motion.y -= (int)(viewport.y * renderer->dpi_scale.y);
|
|
|
|
event->motion.x = (int)(event->motion.x / (scale.x * renderer->dpi_scale.x));
|
|
|
|
event->motion.y = (int)(event->motion.y / (scale.y * renderer->dpi_scale.y));
|
2019-09-30 19:54:16 +00:00
|
|
|
if (event->motion.xrel != 0 && renderer->relative_scaling) {
|
render: Scale relative mouse motion better for logical sizing
From hmk:
"When scaling is enabled (e.g. via SDL_RenderSetLogicalSize, size not equal
to window size), mouse motion events are also scaled. Small motions are
rounded up (SDL_max() when the value after scaling is less than 1), while
larger motions are truncated by the floating point -> integer conversion.
https://hg.libsdl.org/SDL/file/b18197f9bf9d/src/render/SDL_render.c#l658
The end result feels something like mouse reverse mouse acceleration + angle
snapping at low speeds, but less consistent (amount of truncation & rounding
depends on how fast the mouse is moved) and potentially much worse if the
scaling factor is large. This pretty much makes it useless for anything
where you need precise mouse aiming (think of games). I suspect this is why
aiming gets so terrible in some games that let you use scaling to reduce the
render resolution (e.g. Ion Fury).
With 4x4 scaling, I can reproduce a situation where it takes three fast flicks
of the mouse across the pad to undo one slow sweep across the pad. In other
words, extreme reverse acceleration. This does not happen when scaling is
disabled.
Furthermore, any game that uses relative mouse motion events for 3D camera
rotation probably wants the raw mouse deltas and not a value that depends on
scaling and resolution and rounding and truncation. Ideal camera rotation
just takes mouse input, multiplies it by sensitivity, and adds it to the
angle-in-radians or whatever measure is used for yaw & pitch. Pixels and
screen resolution or window dimensions should not be a part of the equation
at all, even if it could be implemented without rounding errors.
[...]
This [patch] completely eliminates angle snapping for me, and makes
sensitivity consistent. In other words, it's completely usable for, say,
aiming in a first person shooter."
Partially fixes Bugzilla #4811.
2020-04-10 16:23:08 +00:00
|
|
|
float rel = renderer->xrel + event->motion.xrel / (scale.x * renderer->dpi_scale.x);
|
|
|
|
float trunc = SDL_truncf(rel);
|
|
|
|
renderer->xrel = rel - trunc;
|
2020-04-14 02:05:36 +00:00
|
|
|
event->motion.xrel = (Sint32) trunc;
|
2018-06-18 20:13:56 +00:00
|
|
|
}
|
2019-09-30 19:54:16 +00:00
|
|
|
if (event->motion.yrel != 0 && renderer->relative_scaling) {
|
render: Scale relative mouse motion better for logical sizing
From hmk:
"When scaling is enabled (e.g. via SDL_RenderSetLogicalSize, size not equal
to window size), mouse motion events are also scaled. Small motions are
rounded up (SDL_max() when the value after scaling is less than 1), while
larger motions are truncated by the floating point -> integer conversion.
https://hg.libsdl.org/SDL/file/b18197f9bf9d/src/render/SDL_render.c#l658
The end result feels something like mouse reverse mouse acceleration + angle
snapping at low speeds, but less consistent (amount of truncation & rounding
depends on how fast the mouse is moved) and potentially much worse if the
scaling factor is large. This pretty much makes it useless for anything
where you need precise mouse aiming (think of games). I suspect this is why
aiming gets so terrible in some games that let you use scaling to reduce the
render resolution (e.g. Ion Fury).
With 4x4 scaling, I can reproduce a situation where it takes three fast flicks
of the mouse across the pad to undo one slow sweep across the pad. In other
words, extreme reverse acceleration. This does not happen when scaling is
disabled.
Furthermore, any game that uses relative mouse motion events for 3D camera
rotation probably wants the raw mouse deltas and not a value that depends on
scaling and resolution and rounding and truncation. Ideal camera rotation
just takes mouse input, multiplies it by sensitivity, and adds it to the
angle-in-radians or whatever measure is used for yaw & pitch. Pixels and
screen resolution or window dimensions should not be a part of the equation
at all, even if it could be implemented without rounding errors.
[...]
This [patch] completely eliminates angle snapping for me, and makes
sensitivity consistent. In other words, it's completely usable for, say,
aiming in a first person shooter."
Partially fixes Bugzilla #4811.
2020-04-10 16:23:08 +00:00
|
|
|
float rel = renderer->yrel + event->motion.yrel / (scale.y * renderer->dpi_scale.y);
|
|
|
|
float trunc = SDL_truncf(rel);
|
|
|
|
renderer->yrel = rel - trunc;
|
2020-04-14 02:05:36 +00:00
|
|
|
event->motion.yrel = (Sint32) trunc;
|
2018-06-18 20:13:56 +00:00
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (event->type == SDL_MOUSEBUTTONDOWN ||
|
|
|
|
event->type == SDL_MOUSEBUTTONUP) {
|
|
|
|
SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
|
2018-06-18 20:13:56 +00:00
|
|
|
if (window == renderer->window) {
|
|
|
|
int logical_w, logical_h;
|
|
|
|
SDL_Rect viewport;
|
|
|
|
SDL_FPoint scale;
|
|
|
|
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
|
|
|
|
if (logical_w) {
|
|
|
|
event->button.x -= (int)(viewport.x * renderer->dpi_scale.x);
|
|
|
|
event->button.y -= (int)(viewport.y * renderer->dpi_scale.y);
|
|
|
|
event->button.x = (int)(event->button.x / (scale.x * renderer->dpi_scale.x));
|
|
|
|
event->button.y = (int)(event->button.y / (scale.y * renderer->dpi_scale.y));
|
|
|
|
}
|
2021-01-02 16:29:34 +00:00
|
|
|
}
|
2017-10-12 15:37:55 +00:00
|
|
|
} else if (event->type == SDL_FINGERDOWN ||
|
|
|
|
event->type == SDL_FINGERUP ||
|
|
|
|
event->type == SDL_FINGERMOTION) {
|
2018-06-18 20:13:56 +00:00
|
|
|
int logical_w, logical_h;
|
2020-08-09 04:55:39 +00:00
|
|
|
float physical_w, physical_h;
|
2018-06-18 20:13:56 +00:00
|
|
|
SDL_Rect viewport;
|
|
|
|
SDL_FPoint scale;
|
|
|
|
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
|
2019-10-14 14:40:46 +00:00
|
|
|
|
2020-08-09 04:55:39 +00:00
|
|
|
/* !!! FIXME: we probably should drop events that are outside of the
|
|
|
|
!!! FIXME: viewport, but we can't do that from an event watcher,
|
|
|
|
!!! FIXME: and we would have to track if a touch happened outside
|
|
|
|
!!! FIXME: the viewport and then slid into it to insert extra
|
|
|
|
!!! FIXME: events, which is a mess, so for now we just clamp these
|
|
|
|
!!! FIXME: events to the edge. */
|
2017-10-12 15:37:55 +00:00
|
|
|
|
2020-08-09 04:55:39 +00:00
|
|
|
if (renderer->GetOutputSize) {
|
|
|
|
int w, h;
|
|
|
|
renderer->GetOutputSize(renderer, &w, &h);
|
|
|
|
physical_w = (float) w;
|
|
|
|
physical_h = (float) h;
|
|
|
|
} else {
|
|
|
|
int w, h;
|
|
|
|
SDL_GetWindowSize(renderer->window, &w, &h);
|
|
|
|
physical_w = ((float) w) * renderer->dpi_scale.x;
|
|
|
|
physical_h = ((float) h) * renderer->dpi_scale.y;
|
|
|
|
}
|
2017-10-12 15:37:55 +00:00
|
|
|
|
2020-08-09 04:55:39 +00:00
|
|
|
if (physical_w == 0.0f) { /* nowhere for the touch to go, avoid division by zero and put it dead center. */
|
|
|
|
event->tfinger.x = 0.5f;
|
|
|
|
} else {
|
|
|
|
const float normalized_viewport_x = ((float) viewport.x) / physical_w;
|
|
|
|
const float normalized_viewport_w = ((float) viewport.w) / physical_w;
|
|
|
|
if (event->tfinger.x <= normalized_viewport_x) {
|
|
|
|
event->tfinger.x = 0.0f; /* to the left of the viewport, clamp to the edge. */
|
|
|
|
} else if (event->tfinger.x >= (normalized_viewport_x + normalized_viewport_w)) {
|
|
|
|
event->tfinger.x = 1.0f; /* to the right of the viewport, clamp to the edge. */
|
2017-10-12 15:37:55 +00:00
|
|
|
} else {
|
2020-08-09 04:55:39 +00:00
|
|
|
event->tfinger.x = (event->tfinger.x - normalized_viewport_x) / normalized_viewport_w;
|
2017-10-12 15:37:55 +00:00
|
|
|
}
|
2020-08-09 04:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (physical_h == 0.0f) { /* nowhere for the touch to go, avoid division by zero and put it dead center. */
|
|
|
|
event->tfinger.y = 0.5f;
|
|
|
|
} else {
|
|
|
|
const float normalized_viewport_y = ((float) viewport.y) / physical_h;
|
|
|
|
const float normalized_viewport_h = ((float) viewport.h) / physical_h;
|
|
|
|
if (event->tfinger.y <= normalized_viewport_y) {
|
|
|
|
event->tfinger.y = 0.0f; /* to the left of the viewport, clamp to the edge. */
|
|
|
|
} else if (event->tfinger.y >= (normalized_viewport_y + normalized_viewport_h)) {
|
|
|
|
event->tfinger.y = 1.0f; /* to the right of the viewport, clamp to the edge. */
|
2017-10-12 15:37:55 +00:00
|
|
|
} else {
|
2020-08-09 04:55:39 +00:00
|
|
|
event->tfinger.y = (event->tfinger.y - normalized_viewport_y) / normalized_viewport_h;
|
2017-10-12 15:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
2017-10-12 15:37:55 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
static SDL_INLINE
|
|
|
|
void VerifyDrawQueueFunctions(const SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
/* all of these functions are required to be implemented, even as no-ops, so we don't
|
|
|
|
have to check that they aren't NULL over and over. */
|
2018-09-24 03:20:40 +00:00
|
|
|
SDL_assert(renderer->QueueSetViewport != NULL);
|
|
|
|
SDL_assert(renderer->QueueSetDrawColor != NULL);
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_assert(renderer->QueueDrawPoints != NULL);
|
|
|
|
SDL_assert(renderer->QueueDrawLines != NULL);
|
2021-09-24 15:09:04 +00:00
|
|
|
SDL_assert(renderer->QueueFillRects != NULL || renderer->QueueGeometry != NULL);
|
2021-09-20 14:32:08 +00:00
|
|
|
SDL_assert(renderer->QueueCopy != NULL || renderer->QueueGeometry != NULL);
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_assert(renderer->RunCommandQueue != NULL);
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_Renderer *
|
|
|
|
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
|
|
|
|
{
|
|
|
|
#if !SDL_RENDER_DISABLED
|
|
|
|
SDL_Renderer *renderer = NULL;
|
|
|
|
int n = SDL_GetNumRenderDrivers();
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_bool batching = SDL_TRUE;
|
2015-06-21 15:33:46 +00:00
|
|
|
const char *hint;
|
|
|
|
|
2019-06-28 14:38:42 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
Android_ActivityMutex_Lock_Running();
|
|
|
|
#endif
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
if (!window) {
|
|
|
|
SDL_SetError("Invalid window");
|
2019-06-28 14:14:50 +00:00
|
|
|
goto error;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_GetRenderer(window)) {
|
|
|
|
SDL_SetError("Renderer already associated with window");
|
2019-06-28 14:14:50 +00:00
|
|
|
goto error;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 19:02:17 +00:00
|
|
|
hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
|
|
|
|
if (hint && *hint) {
|
2016-10-08 06:40:44 +00:00
|
|
|
if (SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, SDL_TRUE)) {
|
2015-06-21 15:33:46 +00:00
|
|
|
flags |= SDL_RENDERER_PRESENTVSYNC;
|
2016-10-08 06:40:44 +00:00
|
|
|
} else {
|
|
|
|
flags &= ~SDL_RENDERER_PRESENTVSYNC;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-09-20 19:46:02 +00:00
|
|
|
if (renderer) {
|
|
|
|
batching = SDL_FALSE;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
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");
|
2019-06-28 14:14:50 +00:00
|
|
|
goto error;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (index >= SDL_GetNumRenderDrivers()) {
|
|
|
|
SDL_SetError("index must be -1 or in the range of 0 - %d",
|
|
|
|
SDL_GetNumRenderDrivers() - 1);
|
2019-06-28 14:14:50 +00:00
|
|
|
goto error;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
/* Create a new renderer instance */
|
|
|
|
renderer = render_drivers[index]->CreateRenderer(window, flags);
|
2018-09-20 19:46:02 +00:00
|
|
|
batching = SDL_FALSE;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:14:50 +00:00
|
|
|
if (!renderer) {
|
|
|
|
goto error;
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
|
2019-06-28 14:14:50 +00:00
|
|
|
VerifyDrawQueueFunctions(renderer);
|
2018-09-20 19:46:02 +00:00
|
|
|
|
2019-06-28 14:14:50 +00:00
|
|
|
/* let app/user override batching decisions. */
|
|
|
|
if (renderer->always_batch) {
|
|
|
|
batching = SDL_TRUE;
|
|
|
|
} else if (SDL_GetHint(SDL_HINT_RENDER_BATCHING)) {
|
|
|
|
batching = SDL_GetHintBoolean(SDL_HINT_RENDER_BATCHING, SDL_TRUE);
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2019-06-28 14:14:50 +00:00
|
|
|
renderer->batching = batching;
|
|
|
|
renderer->magic = &renderer_magic;
|
|
|
|
renderer->window = window;
|
|
|
|
renderer->target_mutex = SDL_CreateMutex();
|
|
|
|
renderer->scale.x = 1.0f;
|
|
|
|
renderer->scale.y = 1.0f;
|
|
|
|
renderer->dpi_scale.x = 1.0f;
|
|
|
|
renderer->dpi_scale.y = 1.0f;
|
|
|
|
|
|
|
|
/* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
|
|
|
|
renderer->render_command_generation = 1;
|
|
|
|
|
|
|
|
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;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
2019-06-28 14:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 19:54:16 +00:00
|
|
|
renderer->relative_scaling = SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_SCALING, SDL_TRUE);
|
|
|
|
|
2019-06-28 14:14:50 +00:00
|
|
|
if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
|
|
|
|
renderer->hidden = SDL_TRUE;
|
|
|
|
} else {
|
|
|
|
renderer->hidden = SDL_FALSE;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2019-06-28 14:14:50 +00:00
|
|
|
SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2019-06-28 14:14:50 +00:00
|
|
|
SDL_RenderSetViewport(renderer, NULL);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2019-06-28 14:14:50 +00:00
|
|
|
SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
|
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
|
|
|
|
"Created renderer: %s", renderer->info.name);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2019-06-28 14:38:42 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
Android_ActivityMutex_Unlock();
|
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
return renderer;
|
2019-06-28 14:14:50 +00:00
|
|
|
|
|
|
|
error:
|
2019-06-28 14:38:42 +00:00
|
|
|
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
Android_ActivityMutex_Unlock();
|
|
|
|
#endif
|
2019-06-28 14:14:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
#else
|
|
|
|
SDL_SetError("SDL not built with rendering support");
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Renderer *
|
|
|
|
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
|
|
|
|
{
|
2020-01-21 20:33:40 +00:00
|
|
|
#if !SDL_RENDER_DISABLED && SDL_VIDEO_RENDER_SW
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
renderer = SW_CreateRendererForSurface(surface);
|
|
|
|
|
|
|
|
if (renderer) {
|
2018-09-20 19:46:02 +00:00
|
|
|
VerifyDrawQueueFunctions(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
renderer->magic = &renderer_magic;
|
2018-06-18 20:13:56 +00:00
|
|
|
renderer->target_mutex = SDL_CreateMutex();
|
2015-06-21 15:33:46 +00:00
|
|
|
renderer->scale.x = 1.0f;
|
|
|
|
renderer->scale.y = 1.0f;
|
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
/* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
|
|
|
|
renderer->render_command_generation = 1;
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
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:
|
2020-01-16 16:52:59 +00:00
|
|
|
case SDL_BLENDMODE_MUL:
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_TRUE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return renderer->SupportsBlendMode && renderer->SupportsBlendMode(renderer, blendMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2018-09-27 21:56:29 +00:00
|
|
|
|
|
|
|
static SDL_ScaleMode SDL_GetScaleMode(void)
|
2018-05-08 02:52:25 +00:00
|
|
|
{
|
|
|
|
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
|
|
|
|
|
|
|
|
if (!hint || SDL_strcasecmp(hint, "nearest") == 0) {
|
|
|
|
return SDL_ScaleModeNearest;
|
|
|
|
} else if (SDL_strcasecmp(hint, "linear") == 0) {
|
|
|
|
return SDL_ScaleModeLinear;
|
|
|
|
} else if (SDL_strcasecmp(hint, "best") == 0) {
|
|
|
|
return SDL_ScaleModeBest;
|
|
|
|
} else {
|
|
|
|
return (SDL_ScaleMode)SDL_atoi(hint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_Texture *
|
|
|
|
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
|
|
|
|
{
|
|
|
|
SDL_Texture *texture;
|
2021-04-13 12:42:38 +00:00
|
|
|
SDL_bool texture_is_fourcc_and_target;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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)) {
|
2021-10-01 20:30:51 +00:00
|
|
|
if (!IsSupportedFormat(renderer, format)) {
|
|
|
|
SDL_SetError("Palettized textures are not supported");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
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;
|
2021-10-24 15:16:49 +00:00
|
|
|
texture->color.r = 255;
|
|
|
|
texture->color.g = 255;
|
|
|
|
texture->color.b = 255;
|
|
|
|
texture->color.a = 255;
|
2018-05-08 02:52:25 +00:00
|
|
|
texture->scaleMode = SDL_GetScaleMode();
|
2015-06-21 15:33:46 +00:00
|
|
|
texture->renderer = renderer;
|
|
|
|
texture->next = renderer->textures;
|
|
|
|
if (renderer->textures) {
|
|
|
|
renderer->textures->prev = texture;
|
|
|
|
}
|
|
|
|
renderer->textures = texture;
|
|
|
|
|
2021-04-13 12:42:38 +00:00
|
|
|
/* FOURCC format cannot be used directly by renderer back-ends for target texture */
|
|
|
|
texture_is_fourcc_and_target = (access == SDL_TEXTUREACCESS_TARGET && SDL_ISPIXELFORMAT_FOURCC(texture->format));
|
|
|
|
|
|
|
|
if (texture_is_fourcc_and_target == SDL_FALSE && IsSupportedFormat(renderer, format)) {
|
2015-06-21 15:33:46 +00:00
|
|
|
if (renderer->CreateTexture(renderer, texture) < 0) {
|
|
|
|
SDL_DestroyTexture(texture);
|
2015-08-21 21:50:37 +00:00
|
|
|
return NULL;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-13 12:42:38 +00:00
|
|
|
int closest_format;
|
|
|
|
|
|
|
|
if (texture_is_fourcc_and_target == SDL_FALSE) {
|
|
|
|
closest_format = GetClosestSupportedFormat(renderer, format);
|
|
|
|
} else {
|
|
|
|
closest_format = renderer->info.texture_formats[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
texture->native = SDL_CreateTexture(renderer, closest_format, access, w, h);
|
2015-06-21 15:33:46 +00:00
|
|
|
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)) {
|
2020-01-21 20:33:40 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
|
2020-01-21 20:33:40 +00:00
|
|
|
#else
|
|
|
|
SDL_SetError("SDL not built with YUV support");
|
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
2019-01-19 15:47:43 +00:00
|
|
|
SDL_bool direct_update;
|
2019-03-19 23:52:09 +00:00
|
|
|
int i;
|
Fixed bug 4469 - make SDL_CreateTextureFromSurface pick a more appropriate format
Sylvain
Currently SDL_CreateTextureFromSurface picks first valid format, and do a conversion.
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;
It could try to find a better format, for instance :
if SDL_Surface has no Amask, but a colorkey :
if surface fmt is RGB888, try to pick ARGB8888 renderer fmt
if surface fmt is BGR888, try to pick ABGR8888 renderer fmt
else
try to pick the same renderer format as surface fmt
if no format has been picked, use the fallback.
I think it goes with bug 4290 fastpath BlitNtoN
when you expand a surface with pixel format of size 24 to 32, there is a fast path possible.
So with this issue:
- if you have a surface with colorkey (RGB or BGR, not palette), it takes a renderer format where the conversion is faster.
(it avoids, if possible, RGB -> ABGR which means switching RGB to BGR)
- if you have a surface ABGR format, it try to take the ABGR from the renderer.
(it avoids, if possible, ABGR -> ARGB, which means switch RGB to BGR)
2019-05-19 19:04:06 +00:00
|
|
|
Uint32 format = SDL_PIXELFORMAT_UNKNOWN;
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
2018-09-26 02:53:16 +00:00
|
|
|
if (fmt->Amask || SDL_HasColorKey(surface)) {
|
2015-06-21 15:33:46 +00:00
|
|
|
needAlpha = SDL_TRUE;
|
|
|
|
} else {
|
|
|
|
needAlpha = SDL_FALSE;
|
|
|
|
}
|
2018-12-15 13:50:12 +00:00
|
|
|
|
|
|
|
/* If Palette contains alpha values, promotes to alpha format */
|
|
|
|
if (fmt->palette) {
|
2020-03-17 08:35:42 +00:00
|
|
|
SDL_bool is_opaque, has_alpha_channel;
|
|
|
|
SDL_DetectPalette(fmt->palette, &is_opaque, &has_alpha_channel);
|
|
|
|
if (!is_opaque) {
|
|
|
|
needAlpha = SDL_TRUE;
|
2018-12-15 13:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Fixed bug 4469 - make SDL_CreateTextureFromSurface pick a more appropriate format
Sylvain
Currently SDL_CreateTextureFromSurface picks first valid format, and do a conversion.
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;
It could try to find a better format, for instance :
if SDL_Surface has no Amask, but a colorkey :
if surface fmt is RGB888, try to pick ARGB8888 renderer fmt
if surface fmt is BGR888, try to pick ABGR8888 renderer fmt
else
try to pick the same renderer format as surface fmt
if no format has been picked, use the fallback.
I think it goes with bug 4290 fastpath BlitNtoN
when you expand a surface with pixel format of size 24 to 32, there is a fast path possible.
So with this issue:
- if you have a surface with colorkey (RGB or BGR, not palette), it takes a renderer format where the conversion is faster.
(it avoids, if possible, RGB -> ABGR which means switching RGB to BGR)
- if you have a surface ABGR format, it try to take the ABGR from the renderer.
(it avoids, if possible, ABGR -> ARGB, which means switch RGB to BGR)
2019-05-19 19:04:06 +00:00
|
|
|
/* Try to have the best pixel format for the texture */
|
|
|
|
/* No alpha, but a colorkey => promote to alpha */
|
|
|
|
if (!fmt->Amask && SDL_HasColorKey(surface)) {
|
|
|
|
if (fmt->format == SDL_PIXELFORMAT_RGB888) {
|
2019-06-09 02:12:05 +00:00
|
|
|
for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
|
Fixed bug 4469 - make SDL_CreateTextureFromSurface pick a more appropriate format
Sylvain
Currently SDL_CreateTextureFromSurface picks first valid format, and do a conversion.
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;
It could try to find a better format, for instance :
if SDL_Surface has no Amask, but a colorkey :
if surface fmt is RGB888, try to pick ARGB8888 renderer fmt
if surface fmt is BGR888, try to pick ABGR8888 renderer fmt
else
try to pick the same renderer format as surface fmt
if no format has been picked, use the fallback.
I think it goes with bug 4290 fastpath BlitNtoN
when you expand a surface with pixel format of size 24 to 32, there is a fast path possible.
So with this issue:
- if you have a surface with colorkey (RGB or BGR, not palette), it takes a renderer format where the conversion is faster.
(it avoids, if possible, RGB -> ABGR which means switching RGB to BGR)
- if you have a surface ABGR format, it try to take the ABGR from the renderer.
(it avoids, if possible, ABGR -> ARGB, which means switch RGB to BGR)
2019-05-19 19:04:06 +00:00
|
|
|
if (renderer->info.texture_formats[i] == SDL_PIXELFORMAT_ARGB8888) {
|
|
|
|
format = SDL_PIXELFORMAT_ARGB8888;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (fmt->format == SDL_PIXELFORMAT_BGR888) {
|
2019-06-09 02:12:05 +00:00
|
|
|
for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
|
Fixed bug 4469 - make SDL_CreateTextureFromSurface pick a more appropriate format
Sylvain
Currently SDL_CreateTextureFromSurface picks first valid format, and do a conversion.
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;
It could try to find a better format, for instance :
if SDL_Surface has no Amask, but a colorkey :
if surface fmt is RGB888, try to pick ARGB8888 renderer fmt
if surface fmt is BGR888, try to pick ABGR8888 renderer fmt
else
try to pick the same renderer format as surface fmt
if no format has been picked, use the fallback.
I think it goes with bug 4290 fastpath BlitNtoN
when you expand a surface with pixel format of size 24 to 32, there is a fast path possible.
So with this issue:
- if you have a surface with colorkey (RGB or BGR, not palette), it takes a renderer format where the conversion is faster.
(it avoids, if possible, RGB -> ABGR which means switching RGB to BGR)
- if you have a surface ABGR format, it try to take the ABGR from the renderer.
(it avoids, if possible, ABGR -> ARGB, which means switch RGB to BGR)
2019-05-19 19:04:06 +00:00
|
|
|
if (renderer->info.texture_formats[i] == SDL_PIXELFORMAT_ABGR8888) {
|
|
|
|
format = SDL_PIXELFORMAT_ABGR8888;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Exact match would be fine */
|
2019-06-09 02:12:05 +00:00
|
|
|
for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
|
Fixed bug 4469 - make SDL_CreateTextureFromSurface pick a more appropriate format
Sylvain
Currently SDL_CreateTextureFromSurface picks first valid format, and do a conversion.
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;
It could try to find a better format, for instance :
if SDL_Surface has no Amask, but a colorkey :
if surface fmt is RGB888, try to pick ARGB8888 renderer fmt
if surface fmt is BGR888, try to pick ABGR8888 renderer fmt
else
try to pick the same renderer format as surface fmt
if no format has been picked, use the fallback.
I think it goes with bug 4290 fastpath BlitNtoN
when you expand a surface with pixel format of size 24 to 32, there is a fast path possible.
So with this issue:
- if you have a surface with colorkey (RGB or BGR, not palette), it takes a renderer format where the conversion is faster.
(it avoids, if possible, RGB -> ABGR which means switching RGB to BGR)
- if you have a surface ABGR format, it try to take the ABGR from the renderer.
(it avoids, if possible, ABGR -> ARGB, which means switch RGB to BGR)
2019-05-19 19:04:06 +00:00
|
|
|
if (renderer->info.texture_formats[i] == fmt->format) {
|
|
|
|
format = fmt->format;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fallback, choose a valid pixel format */
|
|
|
|
if (format == SDL_PIXELFORMAT_UNKNOWN) {
|
|
|
|
format = renderer->info.texture_formats[0];
|
2019-06-09 02:12:05 +00:00
|
|
|
for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
|
Fixed bug 4469 - make SDL_CreateTextureFromSurface pick a more appropriate format
Sylvain
Currently SDL_CreateTextureFromSurface picks first valid format, and do a conversion.
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;
It could try to find a better format, for instance :
if SDL_Surface has no Amask, but a colorkey :
if surface fmt is RGB888, try to pick ARGB8888 renderer fmt
if surface fmt is BGR888, try to pick ABGR8888 renderer fmt
else
try to pick the same renderer format as surface fmt
if no format has been picked, use the fallback.
I think it goes with bug 4290 fastpath BlitNtoN
when you expand a surface with pixel format of size 24 to 32, there is a fast path possible.
So with this issue:
- if you have a surface with colorkey (RGB or BGR, not palette), it takes a renderer format where the conversion is faster.
(it avoids, if possible, RGB -> ABGR which means switching RGB to BGR)
- if you have a surface ABGR format, it try to take the ABGR from the renderer.
(it avoids, if possible, ABGR -> ARGB, which means switch RGB to BGR)
2019-05-19 19:04:06 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
|
|
|
|
surface->w, surface->h);
|
|
|
|
if (!texture) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == surface->format->format) {
|
2019-01-19 15:47:43 +00:00
|
|
|
if (surface->format->Amask && SDL_HasColorKey(surface)) {
|
|
|
|
/* Surface and Renderer formats are identicals.
|
|
|
|
* Intermediate conversion is needed to convert color key to alpha (SDL_ConvertColorkeyToAlpha()). */
|
|
|
|
direct_update = SDL_FALSE;
|
|
|
|
} else {
|
|
|
|
/* Update Texture directly */
|
|
|
|
direct_update = SDL_TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Surface and Renderer formats are differents, it needs an intermediate conversion. */
|
|
|
|
direct_update = SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direct_update) {
|
2015-06-21 15:33:46 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-10-01 20:30:51 +00:00
|
|
|
|
|
|
|
#if SDL_VIDEO_RENDER_DIRECTFB
|
|
|
|
/* DirectFB allows palette format for textures.
|
|
|
|
* Copy SDL_Surface palette to the texture */
|
|
|
|
if (SDL_ISPIXELFORMAT_INDEXED(format)) {
|
|
|
|
if (SDL_strcasecmp(renderer->info.name, "directfb") == 0) {
|
|
|
|
extern void DirectFB_SetTexturePalette(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Palette *pal);
|
|
|
|
DirectFB_SetTexturePalette(renderer, texture, surface->format->palette);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
} 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);
|
|
|
|
|
2018-09-26 02:53:16 +00:00
|
|
|
if (SDL_HasColorKey(surface)) {
|
2015-06-21 15:33:46 +00:00
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (r < 255 || g < 255 || b < 255) {
|
|
|
|
texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
|
|
|
|
} else {
|
|
|
|
texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
|
|
|
|
}
|
2021-10-24 15:16:49 +00:00
|
|
|
texture->color.r = r;
|
|
|
|
texture->color.g = g;
|
|
|
|
texture->color.b = b;
|
2015-06-21 15:33:46 +00:00
|
|
|
if (texture->native) {
|
|
|
|
return SDL_SetTextureColorMod(texture->native, r, g, b);
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
return 0;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
|
|
|
|
Uint8 * b)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (r) {
|
2021-10-24 15:16:49 +00:00
|
|
|
*r = texture->color.r;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
if (g) {
|
2021-10-24 15:16:49 +00:00
|
|
|
*g = texture->color.g;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
if (b) {
|
2021-10-24 15:16:49 +00:00
|
|
|
*b = texture->color.b;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (alpha < 255) {
|
|
|
|
texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
|
|
|
|
} else {
|
|
|
|
texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
|
|
|
|
}
|
2021-10-24 15:16:49 +00:00
|
|
|
texture->color.a = alpha;
|
2015-06-21 15:33:46 +00:00
|
|
|
if (texture->native) {
|
|
|
|
return SDL_SetTextureAlphaMod(texture->native, alpha);
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
return 0;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (alpha) {
|
2021-10-24 15:16:49 +00:00
|
|
|
*alpha = texture->color.a;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
renderer = texture->renderer;
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
if (!IsSupportedBlendMode(renderer, blendMode)) {
|
|
|
|
return SDL_Unsupported();
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
texture->blendMode = blendMode;
|
|
|
|
if (texture->native) {
|
|
|
|
return SDL_SetTextureBlendMode(texture->native, blendMode);
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
return 0;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (blendMode) {
|
|
|
|
*blendMode = texture->blendMode;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-22 21:39:44 +00:00
|
|
|
int
|
|
|
|
SDL_SetTextureScaleMode(SDL_Texture * texture, SDL_ScaleMode scaleMode)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
renderer = texture->renderer;
|
|
|
|
renderer->SetTextureScaleMode(renderer, texture, scaleMode);
|
|
|
|
texture->scaleMode = scaleMode;
|
|
|
|
if (texture->native) {
|
|
|
|
return SDL_SetTextureScaleMode(texture->native, scaleMode);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetTextureScaleMode(SDL_Texture * texture, SDL_ScaleMode *scaleMode)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (scaleMode) {
|
|
|
|
*scaleMode = texture->scaleMode;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-10 22:17:59 +00:00
|
|
|
int
|
|
|
|
SDL_SetTextureUserData(SDL_Texture * texture, void *userdata)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
texture->userdata = userdata;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
SDL_GetTextureUserData(SDL_Texture * texture)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, NULL);
|
|
|
|
|
|
|
|
return texture->userdata;
|
|
|
|
}
|
|
|
|
|
2020-01-23 09:00:52 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
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 */
|
2016-11-25 02:41:09 +00:00
|
|
|
void *native_pixels = NULL;
|
|
|
|
int native_pitch = 0;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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 */
|
2016-11-25 02:41:09 +00:00
|
|
|
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);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-01-23 09:00:52 +00:00
|
|
|
#endif /* SDL_HAVE_YUV */
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
const void *pixels, int pitch)
|
|
|
|
{
|
|
|
|
SDL_Texture *native = texture->native;
|
|
|
|
|
2016-11-25 02:41:09 +00:00
|
|
|
if (!rect->w || !rect->h) {
|
|
|
|
return 0; /* nothing to do. */
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
|
|
|
|
/* We can lock the texture and copy to it */
|
2016-11-25 02:41:09 +00:00
|
|
|
void *native_pixels = NULL;
|
|
|
|
int native_pitch = 0;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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 */
|
2016-11-25 02:41:09 +00:00
|
|
|
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);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
const void *pixels, int pitch)
|
|
|
|
{
|
2021-01-27 09:20:13 +00:00
|
|
|
SDL_Rect real_rect;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (!pixels) {
|
|
|
|
return SDL_InvalidParamError("pixels");
|
|
|
|
}
|
|
|
|
if (!pitch) {
|
|
|
|
return SDL_InvalidParamError("pitch");
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:20:13 +00:00
|
|
|
real_rect.x = 0;
|
|
|
|
real_rect.y = 0;
|
|
|
|
real_rect.w = texture->w;
|
|
|
|
real_rect.h = texture->h;
|
|
|
|
if (rect) {
|
|
|
|
if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 09:20:13 +00:00
|
|
|
if (real_rect.w == 0 || real_rect.h == 0) {
|
2015-06-21 15:33:46 +00:00
|
|
|
return 0; /* nothing to do. */
|
2020-01-21 20:33:40 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
} else if (texture->yuv) {
|
2021-01-27 09:20:13 +00:00
|
|
|
return SDL_UpdateTextureYUV(texture, &real_rect, pixels, pitch);
|
2020-01-21 20:33:40 +00:00
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
} else if (texture->native) {
|
2021-01-27 09:20:13 +00:00
|
|
|
return SDL_UpdateTextureNative(texture, &real_rect, pixels, pitch);
|
2015-06-21 15:33:46 +00:00
|
|
|
} else {
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_Renderer *renderer = texture->renderer;
|
|
|
|
if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-01-27 09:20:13 +00:00
|
|
|
return renderer->UpdateTexture(renderer, texture, &real_rect, pixels, pitch);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 09:00:52 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
|
|
|
|
2016-11-25 02:41:09 +00:00
|
|
|
if (!rect->w || !rect->h) {
|
|
|
|
return 0; /* nothing to do. */
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
|
|
|
|
/* We can lock the texture and copy to it */
|
2016-11-25 02:41:09 +00:00
|
|
|
void *native_pixels = NULL;
|
|
|
|
int native_pitch = 0;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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 */
|
2016-11-25 02:41:09 +00:00
|
|
|
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);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-05 10:56:22 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
SDL_UpdateTextureNVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
const Uint8 *Yplane, int Ypitch,
|
|
|
|
const Uint8 *UVplane, int UVpitch)
|
|
|
|
{
|
|
|
|
SDL_Texture *native = texture->native;
|
|
|
|
SDL_Rect full_rect;
|
|
|
|
|
|
|
|
if (SDL_SW_UpdateNVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, UVplane, UVpitch) < 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-23 09:00:52 +00:00
|
|
|
#endif /* SDL_HAVE_YUV */
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-01-21 20:33:40 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_Renderer *renderer;
|
2021-01-27 09:54:49 +00:00
|
|
|
SDL_Rect real_rect;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:54:49 +00:00
|
|
|
real_rect.x = 0;
|
|
|
|
real_rect.y = 0;
|
|
|
|
real_rect.w = texture->w;
|
|
|
|
real_rect.h = texture->h;
|
|
|
|
if (rect) {
|
|
|
|
SDL_IntersectRect(rect, &real_rect, &real_rect);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 09:54:49 +00:00
|
|
|
if (real_rect.w == 0 || real_rect.h == 0) {
|
2016-11-25 02:41:09 +00:00
|
|
|
return 0; /* nothing to do. */
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
if (texture->yuv) {
|
2021-01-27 09:54:49 +00:00
|
|
|
return SDL_UpdateTextureYUVPlanar(texture, &real_rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
|
2015-06-21 15:33:46 +00:00
|
|
|
} else {
|
|
|
|
SDL_assert(!texture->native);
|
|
|
|
renderer = texture->renderer;
|
|
|
|
SDL_assert(renderer->UpdateTextureYUV);
|
|
|
|
if (renderer->UpdateTextureYUV) {
|
2018-09-20 19:46:02 +00:00
|
|
|
if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-01-27 09:54:49 +00:00
|
|
|
return renderer->UpdateTextureYUV(renderer, texture, &real_rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
|
2015-06-21 15:33:46 +00:00
|
|
|
} else {
|
|
|
|
return SDL_Unsupported();
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 20:33:40 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 10:56:22 +00:00
|
|
|
int SDL_UpdateNVTexture(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
const Uint8 *Yplane, int Ypitch,
|
|
|
|
const Uint8 *UVplane, int UVpitch)
|
|
|
|
{
|
|
|
|
#if SDL_HAVE_YUV
|
|
|
|
SDL_Renderer *renderer;
|
2021-01-27 09:54:49 +00:00
|
|
|
SDL_Rect real_rect;
|
2021-01-05 10:56:22 +00:00
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (!Yplane) {
|
|
|
|
return SDL_InvalidParamError("Yplane");
|
|
|
|
}
|
|
|
|
if (!Ypitch) {
|
|
|
|
return SDL_InvalidParamError("Ypitch");
|
|
|
|
}
|
|
|
|
if (!UVplane) {
|
|
|
|
return SDL_InvalidParamError("UVplane");
|
|
|
|
}
|
|
|
|
if (!UVpitch) {
|
|
|
|
return SDL_InvalidParamError("UVpitch");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture->format != SDL_PIXELFORMAT_NV12 &&
|
|
|
|
texture->format != SDL_PIXELFORMAT_NV21) {
|
|
|
|
return SDL_SetError("Texture format must by NV12 or NV21");
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:54:49 +00:00
|
|
|
real_rect.x = 0;
|
|
|
|
real_rect.y = 0;
|
|
|
|
real_rect.w = texture->w;
|
|
|
|
real_rect.h = texture->h;
|
|
|
|
if (rect) {
|
|
|
|
SDL_IntersectRect(rect, &real_rect, &real_rect);
|
2021-01-05 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 09:54:49 +00:00
|
|
|
if (real_rect.w == 0 || real_rect.h == 0) {
|
2021-01-05 10:56:22 +00:00
|
|
|
return 0; /* nothing to do. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture->yuv) {
|
2021-01-27 09:54:49 +00:00
|
|
|
return SDL_UpdateTextureNVPlanar(texture, &real_rect, Yplane, Ypitch, UVplane, UVpitch);
|
2021-01-05 10:56:22 +00:00
|
|
|
} else {
|
|
|
|
SDL_assert(!texture->native);
|
|
|
|
renderer = texture->renderer;
|
|
|
|
SDL_assert(renderer->UpdateTextureNV);
|
|
|
|
if (renderer->UpdateTextureNV) {
|
|
|
|
if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-01-27 09:54:49 +00:00
|
|
|
return renderer->UpdateTextureNV(renderer, texture, &real_rect, Yplane, Ypitch, UVplane, UVpitch);
|
2021-01-05 10:56:22 +00:00
|
|
|
} else {
|
|
|
|
return SDL_Unsupported();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-23 09:00:52 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
static int
|
|
|
|
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
void **pixels, int *pitch)
|
|
|
|
{
|
|
|
|
return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
|
|
|
|
}
|
2020-01-23 09:00:52 +00:00
|
|
|
#endif /* SDL_HAVE_YUV */
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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_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;
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:33:40 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
if (texture->yuv) {
|
2018-09-20 19:46:02 +00:00
|
|
|
if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
return SDL_LockTextureYUV(texture, rect, pixels, pitch);
|
2020-01-21 20:33:40 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (texture->native) {
|
2018-09-20 19:46:02 +00:00
|
|
|
/* Calls a real SDL_LockTexture/SDL_UnlockTexture on unlock, flushing then. */
|
2015-06-21 15:33:46 +00:00
|
|
|
return SDL_LockTextureNative(texture, rect, pixels, pitch);
|
|
|
|
} else {
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_Renderer *renderer = texture->renderer;
|
|
|
|
if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 18:58:44 +00:00
|
|
|
int
|
|
|
|
SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect,
|
|
|
|
SDL_Surface **surface)
|
|
|
|
{
|
2019-10-01 07:26:30 +00:00
|
|
|
SDL_Rect real_rect;
|
2019-09-30 18:58:44 +00:00
|
|
|
void *pixels = NULL;
|
2020-05-15 19:33:47 +00:00
|
|
|
int pitch = 0; /* fix static analysis */
|
|
|
|
int ret;
|
2019-09-30 18:58:44 +00:00
|
|
|
|
|
|
|
if (texture == NULL || surface == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-01 07:26:30 +00:00
|
|
|
real_rect.x = 0;
|
|
|
|
real_rect.y = 0;
|
|
|
|
real_rect.w = texture->w;
|
|
|
|
real_rect.h = texture->h;
|
|
|
|
if (rect) {
|
|
|
|
SDL_IntersectRect(rect, &real_rect, &real_rect);
|
2019-09-30 18:58:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 07:26:30 +00:00
|
|
|
ret = SDL_LockTexture(texture, &real_rect, &pixels, &pitch);
|
2019-09-30 18:58:44 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-01 07:26:30 +00:00
|
|
|
texture->locked_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, real_rect.w, real_rect.h, 0, pitch, texture->format);
|
2019-09-30 18:58:44 +00:00
|
|
|
if (texture->locked_surface == NULL) {
|
|
|
|
SDL_UnlockTexture(texture);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*surface = texture->locked_surface;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-23 09:00:52 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-01-23 09:00:52 +00:00
|
|
|
#endif /* SDL_HAVE_YUV */
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, );
|
|
|
|
|
|
|
|
if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-21 20:33:40 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2015-06-21 15:33:46 +00:00
|
|
|
if (texture->yuv) {
|
|
|
|
SDL_UnlockTextureYUV(texture);
|
2020-01-21 20:33:40 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (texture->native) {
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_UnlockTextureNative(texture);
|
|
|
|
} else {
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_Renderer *renderer = texture->renderer;
|
2015-06-21 15:33:46 +00:00
|
|
|
renderer->UnlockTexture(renderer, texture);
|
|
|
|
}
|
2019-09-30 18:58:44 +00:00
|
|
|
|
|
|
|
SDL_FreeSurface(texture->locked_surface);
|
|
|
|
texture->locked_surface = NULL;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2018-09-20 19:46:02 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:42:31 +00:00
|
|
|
if (texture == renderer->target) {
|
|
|
|
/* Nothing to do! */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FlushRenderCommands(renderer); /* time to send everything to the GPU! */
|
|
|
|
|
2018-06-18 20:13:56 +00:00
|
|
|
SDL_LockMutex(renderer->target_mutex);
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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) {
|
2018-06-18 20:13:56 +00:00
|
|
|
SDL_UnlockMutex(renderer->target_mutex);
|
2015-06-21 15:33:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture) {
|
|
|
|
renderer->viewport.x = 0;
|
|
|
|
renderer->viewport.y = 0;
|
|
|
|
renderer->viewport.w = texture->w;
|
|
|
|
renderer->viewport.h = texture->h;
|
2016-10-01 20:29:30 +00:00
|
|
|
SDL_zero(renderer->clip_rect);
|
|
|
|
renderer->clipping_enabled = SDL_FALSE;
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-06-18 20:13:56 +00:00
|
|
|
|
|
|
|
SDL_UnlockMutex(renderer->target_mutex);
|
|
|
|
|
2018-09-24 03:20:40 +00:00
|
|
|
if (QueueCmdSetViewport(renderer) < 0) {
|
2015-06-21 15:33:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-09-24 03:20:40 +00:00
|
|
|
if (QueueCmdSetClipRect(renderer) < 0) {
|
2015-06-21 15:33:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All set! */
|
2018-09-25 21:04:47 +00:00
|
|
|
return FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
Adds support to control the scaling policy/mode of SDL_RenderSetLogicalSize for both letterbox (current behavior) and a new overscan mode (expand to fill the entire screen, even if some parts draw off the screen).
The expected use case is for games that are designed with multiple aspect ratios already in mind and leave optional margins on the edges of the game which won't hurt if they are cut off.
An example use case is a game is designed for wide-screen/16:9, but then wants to deploy on an iPad which is 4:3. Normally, SDL will letterbox, which will shrink things and result in wasted space. But the designer already thought about 4:3 and designed the edges of the game so they could be cut off without any functional loss. So rather than wasting space with letterboxing, "overscan" mode will zoom the rendering to fill up the entire screen. Parts on the edges will be drawn offscreen, but since the game was already designed with this in mind, it is fine. The end result is the iPad (4:3) experience is much better since it feels like a game designed for that screen aspect ratio.
This patch introduces a new SDL_hint: SDL_HINT_RENDER_LOGICAL_SIZE_MODE.
Valid values are "letterbox" or "0" for letterboxing and "overscan" or "1" for overscan.
The default mode is letterbox to preserve existing behavior.
// Example usage:
SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "overscan");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-03 12:41:26 +00:00
|
|
|
/* 0 is for letterbox, 1 is for overscan */
|
|
|
|
int scale_policy = 0;
|
2017-12-13 00:34:16 +00:00
|
|
|
const char *hint;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2016-10-14 07:51:57 +00:00
|
|
|
if (!renderer->logical_w || !renderer->logical_h) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:34:16 +00:00
|
|
|
hint = SDL_GetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE);
|
|
|
|
if (hint && (*hint == '1' || SDL_strcasecmp(hint, "overscan") == 0)) {
|
2018-05-10 05:25:23 +00:00
|
|
|
#if SDL_VIDEO_RENDER_D3D
|
2017-12-13 00:34:16 +00:00
|
|
|
SDL_bool overscan_supported = SDL_TRUE;
|
|
|
|
/* Unfortunately, Direct3D 9 doesn't support negative viewport numbers
|
|
|
|
which the overscan implementation relies on.
|
2016-04-05 02:25:24 +00:00
|
|
|
*/
|
2017-12-13 00:34:16 +00:00
|
|
|
if (SDL_strcasecmp(SDL_GetCurrentVideoDriver(), "direct3d") == 0) {
|
|
|
|
overscan_supported = SDL_FALSE;
|
|
|
|
}
|
|
|
|
if (overscan_supported) {
|
2016-04-05 02:25:24 +00:00
|
|
|
scale_policy = 1;
|
|
|
|
}
|
2018-05-10 05:25:23 +00:00
|
|
|
#else
|
|
|
|
scale_policy = 1;
|
|
|
|
#endif
|
Adds support to control the scaling policy/mode of SDL_RenderSetLogicalSize for both letterbox (current behavior) and a new overscan mode (expand to fill the entire screen, even if some parts draw off the screen).
The expected use case is for games that are designed with multiple aspect ratios already in mind and leave optional margins on the edges of the game which won't hurt if they are cut off.
An example use case is a game is designed for wide-screen/16:9, but then wants to deploy on an iPad which is 4:3. Normally, SDL will letterbox, which will shrink things and result in wasted space. But the designer already thought about 4:3 and designed the edges of the game so they could be cut off without any functional loss. So rather than wasting space with letterboxing, "overscan" mode will zoom the rendering to fill up the entire screen. Parts on the edges will be drawn offscreen, but since the game was already designed with this in mind, it is fine. The end result is the iPad (4:3) experience is much better since it feels like a game designed for that screen aspect ratio.
This patch introduces a new SDL_hint: SDL_HINT_RENDER_LOGICAL_SIZE_MODE.
Valid values are "letterbox" or "0" for letterboxing and "overscan" or "1" for overscan.
The default mode is letterbox to preserve existing behavior.
// Example usage:
SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "overscan");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-03 12:41:26 +00:00
|
|
|
}
|
2017-12-13 00:34:16 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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);
|
|
|
|
|
2016-01-05 21:39:18 +00:00
|
|
|
if (renderer->integer_scale) {
|
|
|
|
if (want_aspect > real_aspect) {
|
2016-01-16 20:25:10 +00:00
|
|
|
scale = (float)(w / renderer->logical_w);
|
2016-01-05 21:39:18 +00:00
|
|
|
} else {
|
2016-01-16 20:25:10 +00:00
|
|
|
scale = (float)(h / renderer->logical_h);
|
2016-01-05 21:39:18 +00:00
|
|
|
}
|
2020-12-30 15:12:14 +00:00
|
|
|
viewport.w = (int)SDL_floor(renderer->logical_w * scale);
|
2016-01-05 21:39:18 +00:00
|
|
|
viewport.x = (w - viewport.w) / 2;
|
2020-12-30 15:12:14 +00:00
|
|
|
viewport.h = (int)SDL_floor(renderer->logical_h * scale);
|
2016-01-05 21:39:18 +00:00
|
|
|
viewport.y = (h - viewport.h) / 2;
|
|
|
|
|
|
|
|
SDL_RenderSetViewport(renderer, &viewport);
|
|
|
|
} else if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
|
2015-06-21 15:33:46 +00:00
|
|
|
/* 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) {
|
Adds support to control the scaling policy/mode of SDL_RenderSetLogicalSize for both letterbox (current behavior) and a new overscan mode (expand to fill the entire screen, even if some parts draw off the screen).
The expected use case is for games that are designed with multiple aspect ratios already in mind and leave optional margins on the edges of the game which won't hurt if they are cut off.
An example use case is a game is designed for wide-screen/16:9, but then wants to deploy on an iPad which is 4:3. Normally, SDL will letterbox, which will shrink things and result in wasted space. But the designer already thought about 4:3 and designed the edges of the game so they could be cut off without any functional loss. So rather than wasting space with letterboxing, "overscan" mode will zoom the rendering to fill up the entire screen. Parts on the edges will be drawn offscreen, but since the game was already designed with this in mind, it is fine. The end result is the iPad (4:3) experience is much better since it feels like a game designed for that screen aspect ratio.
This patch introduces a new SDL_hint: SDL_HINT_RENDER_LOGICAL_SIZE_MODE.
Valid values are "letterbox" or "0" for letterboxing and "overscan" or "1" for overscan.
The default mode is letterbox to preserve existing behavior.
// Example usage:
SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "overscan");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-03 12:41:26 +00:00
|
|
|
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;
|
2020-12-30 15:12:14 +00:00
|
|
|
viewport.w = (int)SDL_floor(renderer->logical_w * scale);
|
Adds support to control the scaling policy/mode of SDL_RenderSetLogicalSize for both letterbox (current behavior) and a new overscan mode (expand to fill the entire screen, even if some parts draw off the screen).
The expected use case is for games that are designed with multiple aspect ratios already in mind and leave optional margins on the edges of the game which won't hurt if they are cut off.
An example use case is a game is designed for wide-screen/16:9, but then wants to deploy on an iPad which is 4:3. Normally, SDL will letterbox, which will shrink things and result in wasted space. But the designer already thought about 4:3 and designed the edges of the game so they could be cut off without any functional loss. So rather than wasting space with letterboxing, "overscan" mode will zoom the rendering to fill up the entire screen. Parts on the edges will be drawn offscreen, but since the game was already designed with this in mind, it is fine. The end result is the iPad (4:3) experience is much better since it feels like a game designed for that screen aspect ratio.
This patch introduces a new SDL_hint: SDL_HINT_RENDER_LOGICAL_SIZE_MODE.
Valid values are "letterbox" or "0" for letterboxing and "overscan" or "1" for overscan.
The default mode is letterbox to preserve existing behavior.
// Example usage:
SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "overscan");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-03 12:41:26 +00:00
|
|
|
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;
|
2020-12-30 15:12:14 +00:00
|
|
|
viewport.h = (int)SDL_floor(renderer->logical_h * scale);
|
Adds support to control the scaling policy/mode of SDL_RenderSetLogicalSize for both letterbox (current behavior) and a new overscan mode (expand to fill the entire screen, even if some parts draw off the screen).
The expected use case is for games that are designed with multiple aspect ratios already in mind and leave optional margins on the edges of the game which won't hurt if they are cut off.
An example use case is a game is designed for wide-screen/16:9, but then wants to deploy on an iPad which is 4:3. Normally, SDL will letterbox, which will shrink things and result in wasted space. But the designer already thought about 4:3 and designed the edges of the game so they could be cut off without any functional loss. So rather than wasting space with letterboxing, "overscan" mode will zoom the rendering to fill up the entire screen. Parts on the edges will be drawn offscreen, but since the game was already designed with this in mind, it is fine. The end result is the iPad (4:3) experience is much better since it feels like a game designed for that screen aspect ratio.
This patch introduces a new SDL_hint: SDL_HINT_RENDER_LOGICAL_SIZE_MODE.
Valid values are "letterbox" or "0" for letterboxing and "overscan" or "1" for overscan.
The default mode is letterbox to preserve existing behavior.
// Example usage:
SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "overscan");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-03 12:41:26 +00:00
|
|
|
viewport.y = (h - viewport.h) / 2;
|
|
|
|
SDL_RenderSetViewport(renderer, &viewport);
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
} else {
|
Adds support to control the scaling policy/mode of SDL_RenderSetLogicalSize for both letterbox (current behavior) and a new overscan mode (expand to fill the entire screen, even if some parts draw off the screen).
The expected use case is for games that are designed with multiple aspect ratios already in mind and leave optional margins on the edges of the game which won't hurt if they are cut off.
An example use case is a game is designed for wide-screen/16:9, but then wants to deploy on an iPad which is 4:3. Normally, SDL will letterbox, which will shrink things and result in wasted space. But the designer already thought about 4:3 and designed the edges of the game so they could be cut off without any functional loss. So rather than wasting space with letterboxing, "overscan" mode will zoom the rendering to fill up the entire screen. Parts on the edges will be drawn offscreen, but since the game was already designed with this in mind, it is fine. The end result is the iPad (4:3) experience is much better since it feels like a game designed for that screen aspect ratio.
This patch introduces a new SDL_hint: SDL_HINT_RENDER_LOGICAL_SIZE_MODE.
Valid values are "letterbox" or "0" for letterboxing and "overscan" or "1" for overscan.
The default mode is letterbox to preserve existing behavior.
// Example usage:
SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "overscan");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-03 12:41:26 +00:00
|
|
|
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;
|
2020-12-30 15:12:14 +00:00
|
|
|
viewport.h = (int)SDL_floor(renderer->logical_h * scale);
|
Adds support to control the scaling policy/mode of SDL_RenderSetLogicalSize for both letterbox (current behavior) and a new overscan mode (expand to fill the entire screen, even if some parts draw off the screen).
The expected use case is for games that are designed with multiple aspect ratios already in mind and leave optional margins on the edges of the game which won't hurt if they are cut off.
An example use case is a game is designed for wide-screen/16:9, but then wants to deploy on an iPad which is 4:3. Normally, SDL will letterbox, which will shrink things and result in wasted space. But the designer already thought about 4:3 and designed the edges of the game so they could be cut off without any functional loss. So rather than wasting space with letterboxing, "overscan" mode will zoom the rendering to fill up the entire screen. Parts on the edges will be drawn offscreen, but since the game was already designed with this in mind, it is fine. The end result is the iPad (4:3) experience is much better since it feels like a game designed for that screen aspect ratio.
This patch introduces a new SDL_hint: SDL_HINT_RENDER_LOGICAL_SIZE_MODE.
Valid values are "letterbox" or "0" for letterboxing and "overscan" or "1" for overscan.
The default mode is letterbox to preserve existing behavior.
// Example usage:
SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "overscan");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-03 12:41:26 +00:00
|
|
|
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;
|
2020-12-30 15:12:14 +00:00
|
|
|
viewport.w = (int)SDL_floor(renderer->logical_w * scale);
|
Adds support to control the scaling policy/mode of SDL_RenderSetLogicalSize for both letterbox (current behavior) and a new overscan mode (expand to fill the entire screen, even if some parts draw off the screen).
The expected use case is for games that are designed with multiple aspect ratios already in mind and leave optional margins on the edges of the game which won't hurt if they are cut off.
An example use case is a game is designed for wide-screen/16:9, but then wants to deploy on an iPad which is 4:3. Normally, SDL will letterbox, which will shrink things and result in wasted space. But the designer already thought about 4:3 and designed the edges of the game so they could be cut off without any functional loss. So rather than wasting space with letterboxing, "overscan" mode will zoom the rendering to fill up the entire screen. Parts on the edges will be drawn offscreen, but since the game was already designed with this in mind, it is fine. The end result is the iPad (4:3) experience is much better since it feels like a game designed for that screen aspect ratio.
This patch introduces a new SDL_hint: SDL_HINT_RENDER_LOGICAL_SIZE_MODE.
Valid values are "letterbox" or "0" for letterboxing and "overscan" or "1" for overscan.
The default mode is letterbox to preserve existing behavior.
// Example usage:
SDL_SetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE, "overscan");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-03 12:41:26 +00:00
|
|
|
viewport.x = (w - viewport.w) / 2;
|
|
|
|
SDL_RenderSetViewport(renderer, &viewport);
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 21:39:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
int
|
|
|
|
SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
|
|
|
|
{
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval;
|
2015-06-21 15:33:46 +00:00
|
|
|
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);
|
2020-12-30 15:12:14 +00:00
|
|
|
renderer->viewport.w = (int)SDL_floor(rect->w * renderer->scale.x);
|
|
|
|
renderer->viewport.h = (int)SDL_floor(rect->h * renderer->scale.y);
|
2015-06-21 15:33:46 +00:00
|
|
|
} else {
|
|
|
|
renderer->viewport.x = 0;
|
|
|
|
renderer->viewport.y = 0;
|
|
|
|
if (SDL_GetRendererOutputSize(renderer, &renderer->viewport.w, &renderer->viewport.h) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
retval = QueueCmdSetViewport(renderer);
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:12:30 +00:00
|
|
|
static void
|
2021-01-02 16:18:13 +00:00
|
|
|
RenderGetViewportSize(SDL_Renderer * renderer, SDL_FRect * rect)
|
2021-01-02 15:12:30 +00:00
|
|
|
{
|
2021-01-02 16:29:34 +00:00
|
|
|
rect->x = 0.0f;
|
|
|
|
rect->y = 0.0f;
|
|
|
|
rect->w = renderer->viewport.w / renderer->scale.x;
|
|
|
|
rect->h = renderer->viewport.h / renderer->scale.y;
|
2021-01-02 15:12:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
int
|
|
|
|
SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
|
|
|
|
{
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval;
|
2015-06-21 15:33:46 +00:00
|
|
|
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);
|
2020-12-30 15:12:14 +00:00
|
|
|
renderer->clip_rect.w = (int)SDL_floor(rect->w * renderer->scale.x);
|
|
|
|
renderer->clip_rect.h = (int)SDL_floor(rect->h * renderer->scale.y);
|
2015-06-21 15:33:46 +00:00
|
|
|
} else {
|
|
|
|
renderer->clipping_enabled = SDL_FALSE;
|
|
|
|
SDL_zero(renderer->clip_rect);
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
|
|
|
|
retval = QueueCmdSetClipRect(renderer);
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-10-24 15:16:49 +00:00
|
|
|
renderer->color.r = r;
|
|
|
|
renderer->color.g = g;
|
|
|
|
renderer->color.b = b;
|
|
|
|
renderer->color.a = a;
|
2015-06-21 15:33:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetRenderDrawColor(SDL_Renderer * renderer,
|
|
|
|
Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (r) {
|
2021-10-24 15:16:49 +00:00
|
|
|
*r = renderer->color.r;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
if (g) {
|
2021-10-24 15:16:49 +00:00
|
|
|
*g = renderer->color.g;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
if (b) {
|
2021-10-24 15:16:49 +00:00
|
|
|
*b = renderer->color.b;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
if (a) {
|
2021-10-24 15:16:49 +00:00
|
|
|
*a = renderer->color.a;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
if (!IsSupportedBlendMode(renderer, blendMode)) {
|
|
|
|
return SDL_Unsupported();
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
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)
|
|
|
|
{
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval;
|
2015-06-21 15:33:46 +00:00
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
2018-09-25 21:04:47 +00:00
|
|
|
retval = QueueCmdClear(renderer);
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 05:34:03 +00:00
|
|
|
|
|
|
|
/* !!! FIXME: delete all the duplicate code for the integer versions in 2.1,
|
|
|
|
!!! FIXME: making the floating point versions the only available APIs. */
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
int
|
|
|
|
SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
|
|
|
|
{
|
2018-11-01 17:04:24 +00:00
|
|
|
SDL_FPoint fpoint;
|
|
|
|
fpoint.x = (float) x;
|
|
|
|
fpoint.y = (float) y;
|
2018-10-23 05:34:03 +00:00
|
|
|
return SDL_RenderDrawPointsF(renderer, &fpoint, 1);
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 05:34:03 +00:00
|
|
|
int
|
|
|
|
SDL_RenderDrawPointF(SDL_Renderer * renderer, float x, float y)
|
|
|
|
{
|
2018-11-01 17:04:24 +00:00
|
|
|
SDL_FPoint fpoint;
|
|
|
|
fpoint.x = x;
|
|
|
|
fpoint.y = y;
|
2018-10-23 05:34:03 +00:00
|
|
|
return SDL_RenderDrawPointsF(renderer, &fpoint, 1);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
RenderDrawPointsWithRects(SDL_Renderer * renderer,
|
2018-10-23 05:34:03 +00:00
|
|
|
const SDL_Point * points, const int count)
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval = -1;
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_bool isstack;
|
2018-10-23 05:34:03 +00:00
|
|
|
SDL_FRect *frects = SDL_small_alloc(SDL_FRect, count, &isstack);
|
|
|
|
int i;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
if (!frects) {
|
|
|
|
return SDL_OutOfMemory();
|
|
|
|
}
|
2018-10-23 05:34:03 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
retval = QueueCmdFillRects(renderer, frects, count);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_small_free(frects, isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderDrawPoints(SDL_Renderer * renderer,
|
|
|
|
const SDL_Point * points, int count)
|
|
|
|
{
|
|
|
|
SDL_FPoint *fpoints;
|
|
|
|
int i;
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval;
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_bool isstack;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!points) {
|
|
|
|
return SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
|
|
|
|
}
|
|
|
|
if (count < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-01 21:31:00 +00:00
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2015-06-21 15:33:46 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
|
|
|
|
return RenderDrawPointsWithRects(renderer, points, count);
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
retval = QueueCmdDrawPoints(renderer, fpoints, count);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_small_free(fpoints, isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 05:34:03 +00:00
|
|
|
static int
|
|
|
|
RenderDrawPointsWithRectsF(SDL_Renderer * renderer,
|
|
|
|
const SDL_FPoint * fpoints, const int count)
|
|
|
|
{
|
|
|
|
int retval = -1;
|
|
|
|
SDL_bool isstack;
|
|
|
|
SDL_FRect *frects = SDL_small_alloc(SDL_FRect, count, &isstack);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!frects) {
|
|
|
|
return SDL_OutOfMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
frects[i].x = fpoints[i].x * renderer->scale.x;
|
|
|
|
frects[i].y = fpoints[i].y * renderer->scale.y;
|
|
|
|
frects[i].w = renderer->scale.x;
|
|
|
|
frects[i].h = renderer->scale.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = QueueCmdFillRects(renderer, frects, count);
|
|
|
|
|
|
|
|
SDL_small_free(frects, isstack);
|
|
|
|
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderDrawPointsF(SDL_Renderer * renderer,
|
|
|
|
const SDL_FPoint * points, int count)
|
|
|
|
{
|
|
|
|
SDL_FPoint *fpoints;
|
|
|
|
int i;
|
|
|
|
int retval;
|
|
|
|
SDL_bool isstack;
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!points) {
|
|
|
|
return SDL_SetError("SDL_RenderDrawFPoints(): Passed NULL points");
|
|
|
|
}
|
|
|
|
if (count < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2018-10-23 05:34:03 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2018-10-23 05:34:03 +00:00
|
|
|
|
|
|
|
if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
|
|
|
|
return RenderDrawPointsWithRectsF(renderer, points, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = QueueCmdDrawPoints(renderer, fpoints, count);
|
|
|
|
|
|
|
|
SDL_small_free(fpoints, isstack);
|
|
|
|
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
int
|
|
|
|
SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
|
|
|
|
{
|
2018-11-01 17:04:24 +00:00
|
|
|
SDL_FPoint points[2];
|
|
|
|
points[0].x = (float) x1;
|
|
|
|
points[0].y = (float) y1;
|
|
|
|
points[1].x = (float) x2;
|
|
|
|
points[1].y = (float) y2;
|
2018-10-23 05:34:03 +00:00
|
|
|
return SDL_RenderDrawLinesF(renderer, points, 2);
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 05:34:03 +00:00
|
|
|
int
|
|
|
|
SDL_RenderDrawLineF(SDL_Renderer * renderer, float x1, float y1, float x2, float y2)
|
|
|
|
{
|
2018-11-01 17:04:24 +00:00
|
|
|
SDL_FPoint points[2];
|
|
|
|
points[0].x = x1;
|
|
|
|
points[0].y = y1;
|
|
|
|
points[1].x = x2;
|
|
|
|
points[1].y = y2;
|
2018-10-23 05:34:03 +00:00
|
|
|
return SDL_RenderDrawLinesF(renderer, points, 2);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
RenderDrawLinesWithRects(SDL_Renderer * renderer,
|
2018-10-23 05:34:03 +00:00
|
|
|
const SDL_Point * points, const int count)
|
|
|
|
{
|
|
|
|
SDL_FRect *frect;
|
|
|
|
SDL_FRect *frects;
|
|
|
|
SDL_FPoint fpoints[2];
|
|
|
|
int i, nrects = 0;
|
|
|
|
int retval = 0;
|
|
|
|
SDL_bool isstack;
|
|
|
|
|
|
|
|
frects = SDL_small_alloc(SDL_FRect, count-1, &isstack);
|
|
|
|
if (!frects) {
|
|
|
|
return SDL_OutOfMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count-1; ++i) {
|
|
|
|
if (points[i].x == points[i+1].x) {
|
|
|
|
const int minY = SDL_min(points[i].y, points[i+1].y);
|
|
|
|
const 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) {
|
|
|
|
const int minX = SDL_min(points[i].x, points[i+1].x);
|
|
|
|
const 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;
|
|
|
|
retval += QueueCmdDrawLines(renderer, fpoints, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
render: Fix -Wmaybe-uninitialized warning in RenderDrawLinesWithRects{,F}
The RenderDrawLinesWithRects and RenderDrawLinesWithRectsF functions can
sometimes call QueueCmdFillRects() with the data pointed to by frects
uninitialised. This can occur if none of the lines can be replaced with
rects, in which case the frects array is empty, and nrects is 0.
gcc 10.3.0 will detect this possibility, and print a warning like:
/home/david/Development/SDL/src/render/SDL_render.c: In function 'RenderDrawLinesWithRectsF':
/home/david/Development/SDL/src/render/SDL_render.c:2725:15: warning: '<unknown>' may be used uninitialized [-Wmaybe-uninitialized]
2725 | retval += QueueCmdFillRects(renderer, frects, nrects);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/david/Development/SDL/src/render/SDL_render.c:499:1: note: by argument 2 of type 'const SDL_FRect *' to 'QueueCmdFillRects' declared here
499 | QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int count)
| ^~~~~~~~~~~~~~~~~
This is harmless, because when this is uninitialised, nrects is always
0, so QueueCmdFillRects() does nothing anyway. We therefore can work
around this by only calling QueueCmdFillRects() when nrects is nonzero.
Somewhat impressively, gcc recognises that this is now safe.
2021-07-30 14:31:17 +00:00
|
|
|
if (nrects) {
|
|
|
|
retval += QueueCmdFillRects(renderer, frects, nrects);
|
|
|
|
}
|
2018-10-23 05:34:03 +00:00
|
|
|
|
|
|
|
SDL_small_free(frects, isstack);
|
|
|
|
|
|
|
|
if (retval < 0) {
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
RenderDrawLinesWithRectsF(SDL_Renderer * renderer,
|
|
|
|
const SDL_FPoint * points, const int count)
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
|
|
|
SDL_FRect *frect;
|
|
|
|
SDL_FRect *frects;
|
|
|
|
SDL_FPoint fpoints[2];
|
2018-09-20 19:46:02 +00:00
|
|
|
int i, nrects = 0;
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval = 0;
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_bool isstack;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
frects = SDL_small_alloc(SDL_FRect, count-1, &isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
if (!frects) {
|
|
|
|
return SDL_OutOfMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count-1; ++i) {
|
|
|
|
if (points[i].x == points[i+1].x) {
|
2018-12-06 08:22:00 +00:00
|
|
|
const int minY = (int)SDL_min(points[i].y, points[i+1].y);
|
|
|
|
const int maxY = (int)SDL_max(points[i].y, points[i+1].y);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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) {
|
2018-12-06 08:22:00 +00:00
|
|
|
const int minX = (int)SDL_min(points[i].x, points[i+1].x);
|
|
|
|
const int maxX = (int)SDL_max(points[i].x, points[i+1].x);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
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;
|
2018-09-25 21:04:47 +00:00
|
|
|
retval += QueueCmdDrawLines(renderer, fpoints, 2);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
render: Fix -Wmaybe-uninitialized warning in RenderDrawLinesWithRects{,F}
The RenderDrawLinesWithRects and RenderDrawLinesWithRectsF functions can
sometimes call QueueCmdFillRects() with the data pointed to by frects
uninitialised. This can occur if none of the lines can be replaced with
rects, in which case the frects array is empty, and nrects is 0.
gcc 10.3.0 will detect this possibility, and print a warning like:
/home/david/Development/SDL/src/render/SDL_render.c: In function 'RenderDrawLinesWithRectsF':
/home/david/Development/SDL/src/render/SDL_render.c:2725:15: warning: '<unknown>' may be used uninitialized [-Wmaybe-uninitialized]
2725 | retval += QueueCmdFillRects(renderer, frects, nrects);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/david/Development/SDL/src/render/SDL_render.c:499:1: note: by argument 2 of type 'const SDL_FRect *' to 'QueueCmdFillRects' declared here
499 | QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int count)
| ^~~~~~~~~~~~~~~~~
This is harmless, because when this is uninitialised, nrects is always
0, so QueueCmdFillRects() does nothing anyway. We therefore can work
around this by only calling QueueCmdFillRects() when nrects is nonzero.
Somewhat impressively, gcc recognises that this is now safe.
2021-07-30 14:31:17 +00:00
|
|
|
if (nrects) {
|
|
|
|
retval += QueueCmdFillRects(renderer, frects, nrects);
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_small_free(frects, isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
if (retval < 0) {
|
|
|
|
retval = -1;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderDrawLines(SDL_Renderer * renderer,
|
|
|
|
const SDL_Point * points, int count)
|
|
|
|
{
|
|
|
|
SDL_FPoint *fpoints;
|
|
|
|
int i;
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval;
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_bool isstack;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!points) {
|
|
|
|
return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
|
|
|
|
}
|
|
|
|
if (count < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-01 21:31:00 +00:00
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2015-06-21 15:33:46 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
|
|
|
|
return RenderDrawLinesWithRects(renderer, points, count);
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
retval = QueueCmdDrawLines(renderer, fpoints, count);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_small_free(fpoints, isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 05:34:03 +00:00
|
|
|
int
|
|
|
|
SDL_RenderDrawLinesF(SDL_Renderer * renderer,
|
|
|
|
const SDL_FPoint * points, int count)
|
|
|
|
{
|
|
|
|
SDL_FPoint *fpoints;
|
|
|
|
int i;
|
|
|
|
int retval;
|
|
|
|
SDL_bool isstack;
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!points) {
|
|
|
|
return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
|
|
|
|
}
|
|
|
|
if (count < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2018-10-23 05:34:03 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2018-10-23 05:34:03 +00:00
|
|
|
|
|
|
|
if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
|
|
|
|
return RenderDrawLinesWithRectsF(renderer, points, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = QueueCmdDrawLines(renderer, fpoints, count);
|
|
|
|
|
|
|
|
SDL_small_free(fpoints, isstack);
|
|
|
|
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
int
|
|
|
|
SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
|
|
|
|
{
|
2018-10-23 05:34:03 +00:00
|
|
|
SDL_FRect frect;
|
|
|
|
SDL_FRect *prect = NULL;
|
|
|
|
|
|
|
|
if (rect) {
|
|
|
|
frect.x = (float) rect->x;
|
|
|
|
frect.y = (float) rect->y;
|
|
|
|
frect.w = (float) rect->w;
|
|
|
|
frect.h = (float) rect->h;
|
|
|
|
prect = &frect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_RenderDrawRectF(renderer, prect);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderDrawRectF(SDL_Renderer * renderer, const SDL_FRect * rect)
|
|
|
|
{
|
|
|
|
SDL_FRect frect;
|
|
|
|
SDL_FPoint points[5];
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
/* If 'rect' == NULL, then outline the whole surface */
|
|
|
|
if (!rect) {
|
2021-01-02 16:18:13 +00:00
|
|
|
RenderGetViewportSize(renderer, &frect);
|
2018-10-23 05:34:03 +00:00
|
|
|
rect = &frect;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-10-23 05:34:03 +00:00
|
|
|
return SDL_RenderDrawLinesF(renderer, points, 5);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2015-06-21 15:33:46 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2016-10-01 21:31:00 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-23 05:34:03 +00:00
|
|
|
int
|
|
|
|
SDL_RenderDrawRectsF(SDL_Renderer * renderer,
|
|
|
|
const SDL_FRect * 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;
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2018-10-23 05:34:03 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2018-10-23 05:34:03 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
if (SDL_RenderDrawRectF(renderer, &rects[i]) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
int
|
|
|
|
SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
|
|
|
|
{
|
2018-10-23 05:34:03 +00:00
|
|
|
SDL_FRect frect;
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
/* If 'rect' == NULL, then outline the whole surface */
|
|
|
|
if (rect) {
|
|
|
|
frect.x = (float) rect->x;
|
|
|
|
frect.y = (float) rect->y;
|
|
|
|
frect.w = (float) rect->w;
|
|
|
|
frect.h = (float) rect->h;
|
|
|
|
} else {
|
2021-01-02 16:18:13 +00:00
|
|
|
RenderGetViewportSize(renderer, &frect);
|
2018-10-23 05:34:03 +00:00
|
|
|
}
|
|
|
|
return SDL_RenderFillRectsF(renderer, &frect, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderFillRectF(SDL_Renderer * renderer, const SDL_FRect * rect)
|
|
|
|
{
|
|
|
|
SDL_FRect frect;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
/* If 'rect' == NULL, then outline the whole surface */
|
|
|
|
if (!rect) {
|
2021-01-02 16:18:13 +00:00
|
|
|
RenderGetViewportSize(renderer, &frect);
|
2018-10-23 05:34:03 +00:00
|
|
|
rect = &frect;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
2018-10-23 05:34:03 +00:00
|
|
|
return SDL_RenderFillRectsF(renderer, rect, 1);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderFillRects(SDL_Renderer * renderer,
|
|
|
|
const SDL_Rect * rects, int count)
|
|
|
|
{
|
|
|
|
SDL_FRect *frects;
|
|
|
|
int i;
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval;
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_bool isstack;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!rects) {
|
|
|
|
return SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
|
|
|
|
}
|
|
|
|
if (count < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-01 21:31:00 +00:00
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2015-06-21 15:33:46 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
frects = SDL_small_alloc(SDL_FRect, count, &isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
retval = QueueCmdFillRects(renderer, frects, count);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-10-23 00:50:32 +00:00
|
|
|
SDL_small_free(frects, isstack);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 05:34:03 +00:00
|
|
|
int
|
|
|
|
SDL_RenderFillRectsF(SDL_Renderer * renderer,
|
|
|
|
const SDL_FRect * rects, int count)
|
|
|
|
{
|
|
|
|
SDL_FRect *frects;
|
|
|
|
int i;
|
|
|
|
int retval;
|
|
|
|
SDL_bool isstack;
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!rects) {
|
|
|
|
return SDL_SetError("SDL_RenderFillFRects(): Passed NULL rects");
|
|
|
|
}
|
|
|
|
if (count < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2018-10-23 05:34:03 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2018-10-23 05:34:03 +00:00
|
|
|
|
|
|
|
frects = SDL_small_alloc(SDL_FRect, count, &isstack);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = QueueCmdFillRects(renderer, frects, count);
|
|
|
|
|
|
|
|
SDL_small_free(frects, isstack);
|
|
|
|
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* !!! FIXME: move this to a public API if we want to do float versions of all of these later */
|
|
|
|
SDL_FORCE_INLINE SDL_bool SDL_FRectEmpty(const SDL_FRect *r)
|
|
|
|
{
|
|
|
|
return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* !!! FIXME: move this to a public API if we want to do float versions of all of these later */
|
|
|
|
static SDL_bool
|
|
|
|
SDL_HasIntersectionF(const SDL_FRect * A, const SDL_FRect * B)
|
|
|
|
{
|
|
|
|
float Amin, Amax, Bmin, Bmax;
|
|
|
|
|
|
|
|
if (!A) {
|
|
|
|
SDL_InvalidParamError("A");
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!B) {
|
|
|
|
SDL_InvalidParamError("B");
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Special cases for empty rects */
|
|
|
|
if (SDL_FRectEmpty(A) || SDL_FRectEmpty(B)) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Horizontal intersection */
|
|
|
|
Amin = A->x;
|
|
|
|
Amax = Amin + A->w;
|
|
|
|
Bmin = B->x;
|
|
|
|
Bmax = Bmin + B->w;
|
|
|
|
if (Bmin > Amin)
|
|
|
|
Amin = Bmin;
|
|
|
|
if (Bmax < Amax)
|
|
|
|
Amax = Bmax;
|
|
|
|
if (Amax <= Amin)
|
|
|
|
return SDL_FALSE;
|
|
|
|
|
|
|
|
/* Vertical intersection */
|
|
|
|
Amin = A->y;
|
|
|
|
Amax = Amin + A->h;
|
|
|
|
Bmin = B->y;
|
|
|
|
Bmax = Bmin + B->h;
|
|
|
|
if (Bmin > Amin)
|
|
|
|
Amin = Bmin;
|
|
|
|
if (Bmax < Amax)
|
|
|
|
Amax = Bmax;
|
|
|
|
if (Amax <= Amin)
|
|
|
|
return SDL_FALSE;
|
|
|
|
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
int
|
|
|
|
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
|
|
|
|
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
|
|
|
|
{
|
2018-10-23 05:34:03 +00:00
|
|
|
SDL_FRect dstfrect;
|
|
|
|
SDL_FRect *pdstfrect = NULL;
|
|
|
|
if (dstrect) {
|
|
|
|
dstfrect.x = (float) dstrect->x;
|
|
|
|
dstfrect.y = (float) dstrect->y;
|
|
|
|
dstfrect.w = (float) dstrect->w;
|
|
|
|
dstfrect.h = (float) dstrect->h;
|
|
|
|
pdstfrect = &dstfrect;
|
|
|
|
}
|
|
|
|
return SDL_RenderCopyF(renderer, texture, srcrect, pdstfrect);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture,
|
|
|
|
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
|
|
|
|
{
|
|
|
|
SDL_Rect real_srcrect;
|
|
|
|
SDL_FRect real_dstrect;
|
2018-09-25 21:04:47 +00:00
|
|
|
int retval;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (renderer != texture->renderer) {
|
|
|
|
return SDL_SetError("Texture was not created with this renderer");
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2016-10-01 21:31:00 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2016-10-01 21:31:00 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 16:18:13 +00:00
|
|
|
RenderGetViewportSize(renderer, &real_dstrect);
|
2015-06-21 15:33:46 +00:00
|
|
|
if (dstrect) {
|
2020-12-28 17:17:25 +00:00
|
|
|
if (!SDL_HasIntersectionF(dstrect, &real_dstrect)) {
|
2015-06-21 15:33:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-12-28 17:17:25 +00:00
|
|
|
real_dstrect = *dstrect;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (texture->native) {
|
|
|
|
texture = texture->native;
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
texture->last_command_generation = renderer->render_command_generation;
|
|
|
|
|
2021-09-20 14:32:08 +00:00
|
|
|
if (renderer->QueueGeometry && renderer->QueueCopy == NULL) {
|
|
|
|
float xy[8];
|
|
|
|
const int xy_stride = 2 * sizeof (float);
|
|
|
|
float uv[8];
|
|
|
|
const int uv_stride = 2 * sizeof (float);
|
|
|
|
const int num_vertices = 4;
|
|
|
|
const int indices[6] = {0, 1, 2, 0, 2, 3};
|
|
|
|
const int num_indices = 6;
|
|
|
|
const int size_indices = 4;
|
|
|
|
float minu, minv, maxu, maxv;
|
|
|
|
float minx, miny, maxx, maxy;
|
|
|
|
|
|
|
|
minu = (float) (real_srcrect.x) / (float) texture->w;
|
|
|
|
minv = (float) (real_srcrect.y) / (float) texture->h;
|
|
|
|
maxu = (float) (real_srcrect.x + real_srcrect.w) / (float) texture->w;
|
|
|
|
maxv = (float) (real_srcrect.y + real_srcrect.h) / (float) texture->h;
|
|
|
|
|
|
|
|
minx = real_dstrect.x;
|
|
|
|
miny = real_dstrect.y;
|
|
|
|
maxx = real_dstrect.x + real_dstrect.w;
|
|
|
|
maxy = real_dstrect.y + real_dstrect.h;
|
|
|
|
|
|
|
|
uv[0] = minu;
|
|
|
|
uv[1] = minv;
|
|
|
|
uv[2] = maxu;
|
|
|
|
uv[3] = minv;
|
|
|
|
uv[4] = maxu;
|
|
|
|
uv[5] = maxv;
|
|
|
|
uv[6] = minu;
|
|
|
|
uv[7] = maxv;
|
|
|
|
|
|
|
|
xy[0] = minx;
|
|
|
|
xy[1] = miny;
|
|
|
|
xy[2] = maxx;
|
|
|
|
xy[3] = miny;
|
|
|
|
xy[4] = maxx;
|
|
|
|
xy[5] = maxy;
|
|
|
|
xy[6] = minx;
|
|
|
|
xy[7] = maxy;
|
|
|
|
|
|
|
|
retval = QueueCmdGeometry(renderer, texture,
|
2021-10-24 15:16:49 +00:00
|
|
|
xy, xy_stride, (int *)&texture->color, 0 /* color_stride */, uv, uv_stride,
|
2021-09-20 14:32:08 +00:00
|
|
|
num_vertices,
|
|
|
|
indices, num_indices, size_indices,
|
|
|
|
renderer->scale.x, renderer->scale.y);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
real_dstrect.x *= renderer->scale.x;
|
|
|
|
real_dstrect.y *= renderer->scale.y;
|
|
|
|
real_dstrect.w *= renderer->scale.x;
|
|
|
|
real_dstrect.h *= renderer->scale.y;
|
|
|
|
|
|
|
|
retval = QueueCmdCopy(renderer, texture, &real_srcrect, &real_dstrect);
|
|
|
|
}
|
2018-09-25 21:04:47 +00:00
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2018-10-23 05:34:03 +00:00
|
|
|
SDL_FRect dstfrect;
|
|
|
|
SDL_FRect *pdstfrect = NULL;
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_FPoint fcenter;
|
2018-10-23 05:34:03 +00:00
|
|
|
SDL_FPoint *pfcenter = NULL;
|
|
|
|
|
|
|
|
if (dstrect) {
|
|
|
|
dstfrect.x = (float) dstrect->x;
|
|
|
|
dstfrect.y = (float) dstrect->y;
|
|
|
|
dstfrect.w = (float) dstrect->w;
|
|
|
|
dstfrect.h = (float) dstrect->h;
|
|
|
|
pdstfrect = &dstfrect;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (center) {
|
|
|
|
fcenter.x = (float) center->x;
|
|
|
|
fcenter.y = (float) center->y;
|
|
|
|
pfcenter = &fcenter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_RenderCopyExF(renderer, texture, srcrect, pdstfrect, angle, pfcenter, flip);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
|
|
|
|
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
|
|
|
|
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
|
|
|
|
{
|
|
|
|
SDL_Rect real_srcrect;
|
|
|
|
SDL_FRect real_dstrect;
|
|
|
|
SDL_FPoint real_center;
|
|
|
|
int retval;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2016-10-08 01:00:30 +00:00
|
|
|
if (flip == SDL_FLIP_NONE && (int)(angle/360) == angle/360) { /* fast path when we don't need rotation or flipping */
|
2018-10-23 05:34:03 +00:00
|
|
|
return SDL_RenderCopyF(renderer, texture, srcrect, dstrect);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (renderer != texture->renderer) {
|
|
|
|
return SDL_SetError("Texture was not created with this renderer");
|
|
|
|
}
|
2021-09-20 14:32:08 +00:00
|
|
|
if (!renderer->QueueCopyEx && !renderer->QueueGeometry) {
|
2015-06-21 15:33:46 +00:00
|
|
|
return SDL_SetError("Renderer does not support RenderCopyEx");
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2016-10-01 21:31:00 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
2016-10-01 21:31:00 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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 {
|
2021-01-02 16:18:13 +00:00
|
|
|
RenderGetViewportSize(renderer, &real_dstrect);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (texture->native) {
|
|
|
|
texture = texture->native;
|
|
|
|
}
|
|
|
|
|
2016-10-01 21:31:00 +00:00
|
|
|
if (center) {
|
|
|
|
real_center = *center;
|
|
|
|
} else {
|
2018-10-23 05:34:03 +00:00
|
|
|
real_center.x = real_dstrect.w / 2.0f;
|
|
|
|
real_center.y = real_dstrect.h / 2.0f;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 14:32:08 +00:00
|
|
|
texture->last_command_generation = renderer->render_command_generation;
|
|
|
|
|
|
|
|
if (renderer->QueueGeometry && renderer->QueueCopyEx == NULL) {
|
|
|
|
float xy[8];
|
|
|
|
const int xy_stride = 2 * sizeof (float);
|
|
|
|
float uv[8];
|
|
|
|
const int uv_stride = 2 * sizeof (float);
|
|
|
|
const int num_vertices = 4;
|
|
|
|
const int indices[6] = {0, 1, 2, 0, 2, 3};
|
|
|
|
const int num_indices = 6;
|
|
|
|
const int size_indices = 4;
|
|
|
|
float minu, minv, maxu, maxv;
|
|
|
|
float minx, miny, maxx, maxy;
|
|
|
|
float centerx, centery;
|
|
|
|
|
|
|
|
float s_minx, s_miny, s_maxx, s_maxy;
|
|
|
|
float c_minx, c_miny, c_maxx, c_maxy;
|
|
|
|
|
|
|
|
const float radian_angle = (float)((M_PI * angle) / 180.0);
|
|
|
|
const float s = SDL_sin(radian_angle);
|
|
|
|
const float c = SDL_cos(radian_angle);
|
|
|
|
|
|
|
|
minu = (float) (real_srcrect.x) / (float) texture->w;
|
|
|
|
minv = (float) (real_srcrect.y) / (float) texture->h;
|
|
|
|
maxu = (float) (real_srcrect.x + real_srcrect.w) / (float) texture->w;
|
|
|
|
maxv = (float) (real_srcrect.y + real_srcrect.h) / (float) texture->h;
|
|
|
|
|
|
|
|
centerx = real_center.x + real_dstrect.x;
|
|
|
|
centery = real_center.y + real_dstrect.y;
|
|
|
|
|
|
|
|
if (flip & SDL_FLIP_HORIZONTAL) {
|
|
|
|
minx = real_dstrect.x + real_dstrect.w;
|
|
|
|
maxx = real_dstrect.x;
|
|
|
|
} else {
|
|
|
|
minx = real_dstrect.x;
|
|
|
|
maxx = real_dstrect.x + real_dstrect.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flip & SDL_FLIP_VERTICAL) {
|
|
|
|
miny = real_dstrect.y + real_dstrect.h;
|
|
|
|
maxy = real_dstrect.y;
|
|
|
|
} else {
|
|
|
|
miny = real_dstrect.y;
|
|
|
|
maxy = real_dstrect.y + real_dstrect.h;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv[0] = minu;
|
|
|
|
uv[1] = minv;
|
|
|
|
uv[2] = maxu;
|
|
|
|
uv[3] = minv;
|
|
|
|
uv[4] = maxu;
|
|
|
|
uv[5] = maxv;
|
|
|
|
uv[6] = minu;
|
|
|
|
uv[7] = maxv;
|
|
|
|
|
|
|
|
/* apply rotation with 2x2 matrix ( c -s )
|
|
|
|
* ( s c ) */
|
|
|
|
s_minx = s * (minx - centerx);
|
|
|
|
s_miny = s * (miny - centery);
|
|
|
|
s_maxx = s * (maxx - centerx);
|
|
|
|
s_maxy = s * (maxy - centery);
|
|
|
|
c_minx = c * (minx - centerx);
|
|
|
|
c_miny = c * (miny - centery);
|
|
|
|
c_maxx = c * (maxx - centerx);
|
|
|
|
c_maxy = c * (maxy - centery);
|
|
|
|
|
|
|
|
/* (minx, miny) */
|
|
|
|
xy[0] = (c_minx - s_miny) + centerx;
|
|
|
|
xy[1] = (s_minx + c_miny) + centery;
|
|
|
|
/* (maxx, miny) */
|
|
|
|
xy[2] = (c_maxx - s_miny) + centerx;
|
|
|
|
xy[3] = (s_maxx + c_miny) + centery;
|
|
|
|
/* (maxx, maxy) */
|
|
|
|
xy[4] = (c_maxx - s_maxy) + centerx;
|
|
|
|
xy[5] = (s_maxx + c_maxy) + centery;
|
|
|
|
/* (minx, maxy) */
|
|
|
|
xy[6] = (c_minx - s_maxy) + centerx;
|
|
|
|
xy[7] = (s_minx + c_maxy) + centery;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2021-09-20 14:32:08 +00:00
|
|
|
retval = QueueCmdGeometry(renderer, texture,
|
2021-10-24 15:16:49 +00:00
|
|
|
xy, xy_stride, (int *)&texture->color, 0 /* color_stride */, uv, uv_stride,
|
2021-09-20 14:32:08 +00:00
|
|
|
num_vertices,
|
|
|
|
indices, num_indices, size_indices,
|
|
|
|
renderer->scale.x, renderer->scale.y);
|
|
|
|
} else {
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2021-09-20 14:32:08 +00:00
|
|
|
real_dstrect.x *= renderer->scale.x;
|
|
|
|
real_dstrect.y *= renderer->scale.y;
|
|
|
|
real_dstrect.w *= renderer->scale.x;
|
|
|
|
real_dstrect.h *= renderer->scale.y;
|
2018-09-20 19:46:02 +00:00
|
|
|
|
2021-09-20 14:32:08 +00:00
|
|
|
real_center.x *= renderer->scale.x;
|
|
|
|
real_center.y *= renderer->scale.y;
|
|
|
|
|
|
|
|
retval = QueueCmdCopyEx(renderer, texture, &real_srcrect, &real_dstrect, angle, &real_center, flip);
|
|
|
|
}
|
2018-10-23 05:34:03 +00:00
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 07:55:00 +00:00
|
|
|
|
2021-08-19 09:11:10 +00:00
|
|
|
#define SDL_OFFSETOF(_TYPE,_MEMBER) ((intptr_t)&(((_TYPE*)0)->_MEMBER))
|
2021-04-01 07:55:00 +00:00
|
|
|
int
|
|
|
|
SDL_RenderGeometry(SDL_Renderer *renderer,
|
|
|
|
SDL_Texture *texture,
|
|
|
|
const SDL_Vertex *vertices, int num_vertices,
|
|
|
|
const int *indices, int num_indices)
|
|
|
|
{
|
|
|
|
const float *xy = (const float *)((const Uint8 *)vertices + SDL_OFFSETOF(SDL_Vertex, position));
|
|
|
|
int xy_stride = sizeof (SDL_Vertex);
|
|
|
|
const int *color = (const int *) ((const Uint8 *)vertices + SDL_OFFSETOF(SDL_Vertex, color));
|
|
|
|
int color_stride = sizeof (SDL_Vertex);
|
|
|
|
const float *uv = (const float *)((const Uint8 *)vertices + SDL_OFFSETOF(SDL_Vertex, tex_coord));
|
|
|
|
int uv_stride = sizeof (SDL_Vertex);
|
2021-04-23 10:00:14 +00:00
|
|
|
int size_indices = 4;
|
2021-04-01 07:55:00 +00:00
|
|
|
|
2021-04-23 10:00:14 +00:00
|
|
|
return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices, indices, num_indices, size_indices);
|
2021-04-01 07:55:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 08:16:27 +00:00
|
|
|
static int
|
|
|
|
remap_one_indice(
|
|
|
|
int prev,
|
|
|
|
int k,
|
|
|
|
SDL_Texture *texture,
|
|
|
|
const float *xy, int xy_stride,
|
|
|
|
const int *color, int color_stride,
|
2021-04-01 09:47:45 +00:00
|
|
|
const float *uv, int uv_stride)
|
2021-04-01 08:16:27 +00:00
|
|
|
{
|
|
|
|
const float *xy0_, *xy1_, *uv0_, *uv1_;
|
|
|
|
int col0_, col1_;
|
|
|
|
xy0_ = (const float *)((const char*)xy + prev * xy_stride);
|
|
|
|
xy1_ = (const float *)((const char*)xy + k * xy_stride);
|
|
|
|
if (xy0_[0] != xy1_[0]) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
if (xy0_[1] != xy1_[1]) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
if (texture) {
|
|
|
|
uv0_ = (const float *)((const char*)uv + prev * uv_stride);
|
|
|
|
uv1_ = (const float *)((const char*)uv + k * uv_stride);
|
|
|
|
if (uv0_[0] != uv1_[0]) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
if (uv0_[1] != uv1_[1]) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
col0_ = *(const int *)((const char*)color + prev * color_stride);
|
|
|
|
col1_ = *(const int *)((const char*)color + k * color_stride);
|
|
|
|
|
|
|
|
if (col0_ != col1_) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
remap_indices(
|
|
|
|
int prev[3],
|
|
|
|
int k,
|
|
|
|
SDL_Texture *texture,
|
|
|
|
const float *xy, int xy_stride,
|
|
|
|
const int *color, int color_stride,
|
|
|
|
const float *uv, int uv_stride)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (prev[0] == -1) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
int new_k = remap_one_indice(prev[i], k, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
|
|
|
|
if (new_k != k) {
|
|
|
|
return new_k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2021-08-27 05:16:40 +00:00
|
|
|
#define DEBUG_SW_RENDER_GEOMETRY 0
|
2021-04-01 08:16:27 +00:00
|
|
|
/* For the software renderer, try to reinterpret triangles as SDL_Rect */
|
|
|
|
static int SDLCALL
|
|
|
|
SDL_SW_RenderGeometryRaw(SDL_Renderer *renderer,
|
|
|
|
SDL_Texture *texture,
|
|
|
|
const float *xy, int xy_stride,
|
|
|
|
const int *color, int color_stride,
|
|
|
|
const float *uv, int uv_stride,
|
|
|
|
int num_vertices,
|
2021-04-23 10:00:14 +00:00
|
|
|
const void *indices, int num_indices, int size_indices)
|
2021-04-01 08:16:27 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int retval = 0;
|
|
|
|
int count = indices ? num_indices : num_vertices;
|
|
|
|
int prev[3]; /* Previous triangle vertex indices */
|
|
|
|
int texw = 0, texh = 0;
|
2021-04-01 18:18:05 +00:00
|
|
|
SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
|
|
|
|
Uint8 r = 0, g = 0, b = 0, a = 0;
|
2021-04-01 10:43:39 +00:00
|
|
|
|
|
|
|
/* Save */
|
|
|
|
SDL_GetRenderDrawBlendMode(renderer, &blendMode);
|
|
|
|
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
|
2021-04-01 08:16:27 +00:00
|
|
|
|
|
|
|
if (texture) {
|
2021-04-01 09:47:45 +00:00
|
|
|
SDL_QueryTexture(texture, NULL, NULL, &texw, &texh);
|
2021-04-01 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prev[0] = -1; prev[1] = -1; prev[2] = -1;
|
2021-08-27 05:47:28 +00:00
|
|
|
size_indices = indices ? size_indices : 0;
|
2021-04-01 08:16:27 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count; i += 3) {
|
|
|
|
int k0, k1, k2; /* Current triangle indices */
|
|
|
|
int is_quad = 1;
|
2021-08-27 05:16:40 +00:00
|
|
|
#if DEBUG_SW_RENDER_GEOMETRY
|
2021-04-01 08:16:27 +00:00
|
|
|
int is_uniform = 1;
|
|
|
|
int is_rectangle = 1;
|
2021-08-27 05:16:40 +00:00
|
|
|
#endif
|
2021-04-01 08:16:27 +00:00
|
|
|
int A = -1; /* Top left vertex */
|
|
|
|
int B = -1; /* Bottom right vertex */
|
|
|
|
int C = -1; /* Third vertex of current triangle */
|
|
|
|
int C2 = -1; /* Last, vertex of previous triangle */
|
|
|
|
|
2021-04-23 10:00:14 +00:00
|
|
|
if (size_indices == 4) {
|
2021-04-01 08:16:27 +00:00
|
|
|
k0 = ((const Uint32 *)indices)[i];
|
|
|
|
k1 = ((const Uint32 *)indices)[i + 1];
|
|
|
|
k2 = ((const Uint32 *)indices)[i + 2];
|
2021-04-23 10:00:14 +00:00
|
|
|
} else if (size_indices == 2) {
|
2021-04-01 08:16:27 +00:00
|
|
|
k0 = ((const Uint16 *)indices)[i];
|
|
|
|
k1 = ((const Uint16 *)indices)[i + 1];
|
|
|
|
k2 = ((const Uint16 *)indices)[i + 2];
|
2021-04-23 10:00:14 +00:00
|
|
|
} else if (size_indices == 1) {
|
2021-04-01 08:16:27 +00:00
|
|
|
k0 = ((const Uint8 *)indices)[i];
|
|
|
|
k1 = ((const Uint8 *)indices)[i + 1];
|
|
|
|
k2 = ((const Uint8 *)indices)[i + 2];
|
|
|
|
} else {
|
|
|
|
/* Vertices were not provided by indices. Maybe some are duplicated.
|
|
|
|
* We try to indentificate the duplicates by comparing with the previous three vertices */
|
|
|
|
k0 = remap_indices(prev, i, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
|
|
|
|
k1 = remap_indices(prev, i + 1, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
|
|
|
|
k2 = remap_indices(prev, i + 2, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prev[0] == -1) {
|
|
|
|
prev[0] = k0;
|
|
|
|
prev[1] = k1;
|
|
|
|
prev[2] = k2;
|
2021-04-01 09:47:45 +00:00
|
|
|
continue;
|
2021-04-01 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Two triangles forming a quadialateral,
|
|
|
|
* prev and current triangles must have exactly 2 common vertices */
|
|
|
|
{
|
|
|
|
int cnt = 0, j = 3;
|
|
|
|
while (j--) {
|
|
|
|
int p = prev[j];
|
|
|
|
if (p == k0 || p == k1 || p == k2) {
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is_quad = (cnt == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Identify vertices */
|
|
|
|
if (is_quad) {
|
|
|
|
const float *xy0_, *xy1_, *xy2_;
|
|
|
|
float x0, x1, x2;
|
|
|
|
float y0, y1, y2;
|
|
|
|
xy0_ = (const float *)((const char*)xy + k0 * xy_stride);
|
|
|
|
xy1_ = (const float *)((const char*)xy + k1 * xy_stride);
|
|
|
|
xy2_ = (const float *)((const char*)xy + k2 * xy_stride);
|
|
|
|
x0 = xy0_[0]; y0 = xy0_[1];
|
|
|
|
x1 = xy1_[0]; y1 = xy1_[1];
|
|
|
|
x2 = xy2_[0]; y2 = xy2_[1];
|
|
|
|
|
|
|
|
/* Find top-left */
|
|
|
|
if (x0 <= x1 && y0 <= y1) {
|
|
|
|
if (x0 <= x2 && y0 <= y2) {
|
|
|
|
A = k0;
|
|
|
|
} else {
|
|
|
|
A = k2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (x1 <= x2 && y1 <= y2) {
|
|
|
|
A = k1;
|
|
|
|
} else {
|
|
|
|
A = k2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find bottom-right */
|
|
|
|
if (x0 >= x1 && y0 >= y1) {
|
|
|
|
if (x0 >= x2 && y0 >= y2) {
|
|
|
|
B = k0;
|
|
|
|
} else {
|
|
|
|
B = k2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (x1 >= x2 && y1 >= y2) {
|
|
|
|
B = k1;
|
|
|
|
} else {
|
|
|
|
B = k2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find C */
|
|
|
|
if (k0 != A && k0 != B) {
|
|
|
|
C = k0;
|
|
|
|
} else if (k1 != A && k1 != B) {
|
|
|
|
C = k1;
|
|
|
|
} else {
|
|
|
|
C = k2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find C2 */
|
|
|
|
if (prev[0] != A && prev[0] != B) {
|
|
|
|
C2 = prev[0];
|
|
|
|
} else if (prev[1] != A && prev[1] != B) {
|
|
|
|
C2 = prev[1];
|
|
|
|
} else {
|
|
|
|
C2 = prev[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
xy0_ = (const float *)((const char*)xy + A * xy_stride);
|
|
|
|
xy1_ = (const float *)((const char*)xy + B * xy_stride);
|
|
|
|
xy2_ = (const float *)((const char*)xy + C * xy_stride);
|
|
|
|
x0 = xy0_[0]; y0 = xy0_[1];
|
|
|
|
x1 = xy1_[0]; y1 = xy1_[1];
|
|
|
|
x2 = xy2_[0]; y2 = xy2_[1];
|
|
|
|
|
2021-04-01 09:47:45 +00:00
|
|
|
/* Check if triangle A B C is rectangle */
|
2021-04-01 08:16:27 +00:00
|
|
|
if ((x0 == x2 && y1 == y2) || (y0 == y2 && x1 == x2)){
|
|
|
|
/* ok */
|
|
|
|
} else {
|
|
|
|
is_quad = 0;
|
2021-08-27 05:16:40 +00:00
|
|
|
#if DEBUG_SW_RENDER_GEOMETRY
|
2021-04-01 08:16:27 +00:00
|
|
|
is_rectangle = 0;
|
2021-08-27 05:16:40 +00:00
|
|
|
#endif
|
2021-04-01 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
xy2_ = (const float *)((const char*)xy + C2 * xy_stride);
|
|
|
|
x2 = xy2_[0]; y2 = xy2_[1];
|
|
|
|
|
2021-04-01 09:47:45 +00:00
|
|
|
/* Check if triangle A B C2 is rectangle */
|
2021-04-01 08:16:27 +00:00
|
|
|
if ((x0 == x2 && y1 == y2) || (y0 == y2 && x1 == x2)){
|
|
|
|
/* ok */
|
|
|
|
} else {
|
|
|
|
is_quad = 0;
|
2021-08-27 05:16:40 +00:00
|
|
|
#if DEBUG_SW_RENDER_GEOMETRY
|
2021-04-01 08:16:27 +00:00
|
|
|
is_rectangle = 0;
|
2021-08-27 05:16:40 +00:00
|
|
|
#endif
|
2021-04-01 08:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if uniformly colored */
|
|
|
|
if (is_quad) {
|
|
|
|
const int col0_ = *(const int *)((const char*)color + A * color_stride);
|
|
|
|
const int col1_ = *(const int *)((const char*)color + B * color_stride);
|
|
|
|
const int col2_ = *(const int *)((const char*)color + C * color_stride);
|
|
|
|
const int col3_ = *(const int *)((const char*)color + C2 * color_stride);
|
|
|
|
if (col0_ == col1_ && col0_ == col2_ && col0_ == col3_) {
|
|
|
|
/* ok */
|
|
|
|
} else {
|
|
|
|
is_quad = 0;
|
2021-08-27 05:16:40 +00:00
|
|
|
#if DEBUG_SW_RENDER_GEOMETRY
|
2021-04-01 08:16:27 +00:00
|
|
|
is_uniform = 0;
|
2021-08-27 05:16:40 +00:00
|
|
|
#endif
|
2021-04-01 08:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start rendering rect */
|
|
|
|
if (is_quad) {
|
|
|
|
SDL_Rect s;
|
|
|
|
SDL_FRect d;
|
|
|
|
const float *xy0_, *xy1_, *uv0_, *uv1_;
|
|
|
|
SDL_Color col0_ = *(const SDL_Color *)((const char*)color + k0 * color_stride);
|
|
|
|
|
|
|
|
xy0_ = (const float *)((const char*)xy + A * xy_stride);
|
|
|
|
xy1_ = (const float *)((const char*)xy + B * xy_stride);
|
|
|
|
|
|
|
|
if (texture) {
|
|
|
|
uv0_ = (const float *)((const char*)uv + A * uv_stride);
|
|
|
|
uv1_ = (const float *)((const char*)uv + B * uv_stride);
|
2021-04-01 18:18:05 +00:00
|
|
|
s.x = (int) (uv0_[0] * texw);
|
|
|
|
s.y = (int) (uv0_[1] * texh);
|
|
|
|
s.w = (int) (uv1_[0] * texw - s.x);
|
|
|
|
s.h = (int) (uv1_[1] * texh - s.y);
|
2021-04-01 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.x = xy0_[0];
|
|
|
|
d.y = xy0_[1];
|
|
|
|
d.w = xy1_[0] - d.x;
|
|
|
|
d.h = xy1_[1] - d.y;
|
|
|
|
|
|
|
|
/* Rect + texture */
|
|
|
|
if (texture && s.w != 0 && s.h != 0) {
|
|
|
|
SDL_SetTextureAlphaMod(texture, col0_.a);
|
|
|
|
SDL_SetTextureColorMod(texture, col0_.r, col0_.g, col0_.b);
|
|
|
|
SDL_RenderCopyF(renderer, texture, &s, &d);
|
2021-08-27 05:16:40 +00:00
|
|
|
#if DEBUG_SW_RENDER_GEOMETRY
|
|
|
|
SDL_Log("Rect-COPY: RGB %d %d %d - Alpha:%d - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
|
|
|
|
(void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
|
|
|
|
#endif
|
2021-04-01 08:16:27 +00:00
|
|
|
} else if (d.w != 0.0f && d.h != 0.0f) { /* Rect, no texture */
|
|
|
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
|
|
|
SDL_SetRenderDrawColor(renderer, col0_.r, col0_.g, col0_.b, col0_.a);
|
|
|
|
SDL_RenderFillRectF(renderer, &d);
|
2021-08-27 05:16:40 +00:00
|
|
|
#if DEBUG_SW_RENDER_GEOMETRY
|
|
|
|
SDL_Log("Rect-FILL: RGB %d %d %d - Alpha:%d - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
|
|
|
|
(void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
|
2021-04-01 08:16:27 +00:00
|
|
|
} else {
|
2021-08-27 05:16:40 +00:00
|
|
|
SDL_Log("Rect-DISMISS: RGB %d %d %d - Alpha:%d - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
|
|
|
|
(void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
|
|
|
|
#endif
|
2021-04-01 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prev[0] = -1;
|
|
|
|
} else {
|
|
|
|
/* Render triangles */
|
|
|
|
if (prev[0] != -1) {
|
2021-08-27 05:16:40 +00:00
|
|
|
#if DEBUG_SW_RENDER_GEOMETRY
|
|
|
|
SDL_Log("Triangle %d %d %d - is_uniform:%d is_rectangle:%d", prev[0], prev[1], prev[2], is_uniform, is_rectangle);
|
|
|
|
#endif
|
2021-04-01 08:16:27 +00:00
|
|
|
retval = QueueCmdGeometry(renderer, texture,
|
|
|
|
xy, xy_stride, color, color_stride, uv, uv_stride,
|
|
|
|
num_vertices, prev, 3, 4, renderer->scale.x, renderer->scale.y);
|
|
|
|
if (retval < 0) {
|
2021-04-01 10:43:39 +00:00
|
|
|
goto end;
|
2021-04-01 08:16:27 +00:00
|
|
|
} else {
|
|
|
|
FlushRenderCommandsIfNotBatching(renderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prev[0] = k0;
|
|
|
|
prev[1] = k1;
|
|
|
|
prev[2] = k2;
|
|
|
|
}
|
|
|
|
} /* End for(), next triangle */
|
|
|
|
|
|
|
|
if (prev[0] != -1) {
|
|
|
|
/* flush the last triangle */
|
2021-08-27 05:16:40 +00:00
|
|
|
#if DEBUG_SW_RENDER_GEOMETRY
|
|
|
|
SDL_Log("Last triangle %d %d %d", prev[0], prev[1], prev[2]);
|
|
|
|
#endif
|
2021-04-01 08:16:27 +00:00
|
|
|
retval = QueueCmdGeometry(renderer, texture,
|
|
|
|
xy, xy_stride, color, color_stride, uv, uv_stride,
|
|
|
|
num_vertices, prev, 3, 4, renderer->scale.x, renderer->scale.y);
|
|
|
|
if (retval < 0) {
|
2021-04-01 10:43:39 +00:00
|
|
|
goto end;
|
2021-04-01 08:16:27 +00:00
|
|
|
} else {
|
|
|
|
FlushRenderCommandsIfNotBatching(renderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:43:39 +00:00
|
|
|
end:
|
|
|
|
/* Restore */
|
|
|
|
SDL_SetRenderDrawBlendMode(renderer, blendMode);
|
|
|
|
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
|
|
|
|
2021-04-01 08:16:27 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:09:34 +00:00
|
|
|
int
|
2021-04-01 07:49:16 +00:00
|
|
|
SDL_RenderGeometryRaw(SDL_Renderer *renderer,
|
|
|
|
SDL_Texture *texture,
|
|
|
|
const float *xy, int xy_stride,
|
|
|
|
const int *color, int color_stride,
|
|
|
|
const float *uv, int uv_stride,
|
|
|
|
int num_vertices,
|
2021-04-23 10:00:14 +00:00
|
|
|
const void *indices, int num_indices, int size_indices)
|
2021-03-16 14:09:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
2021-04-01 07:49:16 +00:00
|
|
|
int retval = 0;
|
2021-03-16 14:09:34 +00:00
|
|
|
int count = indices ? num_indices : num_vertices;
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!renderer->QueueGeometry) {
|
|
|
|
return SDL_Unsupported();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture) {
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (renderer != texture->renderer) {
|
|
|
|
return SDL_SetError("Texture was not created with this renderer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 07:49:16 +00:00
|
|
|
if (!xy) {
|
|
|
|
return SDL_InvalidParamError("xy");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!color) {
|
|
|
|
return SDL_InvalidParamError("color");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture && !uv) {
|
|
|
|
return SDL_InvalidParamError("uv");
|
2021-03-16 14:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count % 3 != 0) {
|
|
|
|
return SDL_InvalidParamError(indices ? "num_indices" : "num_vertices");
|
|
|
|
}
|
|
|
|
|
2021-04-01 07:49:16 +00:00
|
|
|
if (indices) {
|
2021-04-23 10:27:35 +00:00
|
|
|
if (size_indices != 1 && size_indices != 2 && size_indices != 4) {
|
2021-04-23 10:00:14 +00:00
|
|
|
return SDL_InvalidParamError("size_indices");
|
2021-04-01 07:49:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-23 10:00:14 +00:00
|
|
|
size_indices = 0;
|
2021-04-01 07:49:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:44:17 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2021-03-16 14:09:34 +00:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-08 15:44:17 +00:00
|
|
|
#endif
|
2021-03-16 14:09:34 +00:00
|
|
|
|
|
|
|
if (num_vertices < 3) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture && texture->native) {
|
|
|
|
texture = texture->native;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture) {
|
|
|
|
for (i = 0; i < num_vertices; ++i) {
|
2021-04-01 07:49:16 +00:00
|
|
|
const float *uv_ = (const float *)((const char*)uv + i * uv_stride);
|
|
|
|
float u = uv_[0];
|
|
|
|
float v = uv_[1];
|
|
|
|
if (u < 0.0f || v < 0.0f || u > 1.0f || v > 1.0f) {
|
|
|
|
return SDL_SetError("Values of 'uv' out of bounds %f %f at %d/%d", u, v, i, num_vertices);
|
2021-03-16 14:09:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (indices) {
|
|
|
|
for (i = 0; i < num_indices; ++i) {
|
2021-04-01 07:49:16 +00:00
|
|
|
int j;
|
2021-04-23 10:00:14 +00:00
|
|
|
if (size_indices == 4) {
|
2021-04-01 07:49:16 +00:00
|
|
|
j = ((const Uint32 *)indices)[i];
|
2021-04-23 10:00:14 +00:00
|
|
|
} else if (size_indices == 2) {
|
2021-04-01 07:49:16 +00:00
|
|
|
j = ((const Uint16 *)indices)[i];
|
|
|
|
} else {
|
|
|
|
j = ((const Uint8 *)indices)[i];
|
|
|
|
}
|
|
|
|
if (j < 0 || j >= num_vertices) {
|
2021-03-16 14:09:34 +00:00
|
|
|
return SDL_SetError("Values of 'indices' out of bounds");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture) {
|
|
|
|
texture->last_command_generation = renderer->render_command_generation;
|
|
|
|
}
|
|
|
|
|
2021-04-01 08:16:27 +00:00
|
|
|
/* For the software renderer, try to reinterpret triangles as SDL_Rect */
|
|
|
|
if (renderer->info.flags & SDL_RENDERER_SOFTWARE) {
|
|
|
|
return SDL_SW_RenderGeometryRaw(renderer, texture,
|
|
|
|
xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices,
|
2021-04-23 10:00:14 +00:00
|
|
|
indices, num_indices, size_indices);
|
2021-04-01 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 07:49:16 +00:00
|
|
|
retval = QueueCmdGeometry(renderer, texture,
|
|
|
|
xy, xy_stride, color, color_stride, uv, uv_stride,
|
|
|
|
num_vertices,
|
2021-04-23 10:00:14 +00:00
|
|
|
indices, num_indices, size_indices,
|
2021-04-01 07:49:16 +00:00
|
|
|
renderer->scale.x, renderer->scale.y);
|
2021-03-16 14:09:34 +00:00
|
|
|
|
|
|
|
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
|
|
|
|
}
|
|
|
|
|
2021-04-06 19:32:02 +00:00
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
FlushRenderCommands(renderer); /* we need to render before we read the results. */
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
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, );
|
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
FlushRenderCommands(renderer); /* time to send everything to the GPU! */
|
|
|
|
|
2021-04-02 18:01:41 +00:00
|
|
|
#if DONT_DRAW_WHILE_HIDDEN
|
2018-09-20 19:46:02 +00:00
|
|
|
/* Don't present while we're hidden */
|
2015-06-21 15:33:46 +00:00
|
|
|
if (renderer->hidden) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-02 18:01:41 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
renderer->RenderPresent(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SDL_DestroyTexture(SDL_Texture * texture)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, );
|
|
|
|
|
|
|
|
renderer = texture->renderer;
|
|
|
|
if (texture == renderer->target) {
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_SetRenderTarget(renderer, NULL); /* implies command queue flush */
|
|
|
|
} else {
|
|
|
|
FlushRenderCommandsIfTextureNeeded(texture);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 08:40:22 +00:00
|
|
|
texture->magic = NULL;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
if (texture->next) {
|
|
|
|
texture->next->prev = texture->prev;
|
|
|
|
}
|
|
|
|
if (texture->prev) {
|
|
|
|
texture->prev->next = texture->next;
|
|
|
|
} else {
|
|
|
|
renderer->textures = texture->next;
|
|
|
|
}
|
|
|
|
|
2017-01-06 08:40:22 +00:00
|
|
|
if (texture->native) {
|
|
|
|
SDL_DestroyTexture(texture->native);
|
|
|
|
}
|
2020-01-21 20:33:40 +00:00
|
|
|
#if SDL_HAVE_YUV
|
2017-01-06 08:40:22 +00:00
|
|
|
if (texture->yuv) {
|
|
|
|
SDL_SW_DestroyYUVTexture(texture->yuv);
|
|
|
|
}
|
2020-01-21 20:33:40 +00:00
|
|
|
#endif
|
2017-01-06 08:40:22 +00:00
|
|
|
SDL_free(texture->pixels);
|
|
|
|
|
|
|
|
renderer->DestroyTexture(renderer, texture);
|
2019-09-30 18:58:44 +00:00
|
|
|
|
|
|
|
SDL_FreeSurface(texture->locked_surface);
|
|
|
|
texture->locked_surface = NULL;
|
|
|
|
|
2017-01-06 08:40:22 +00:00
|
|
|
SDL_free(texture);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SDL_DestroyRenderer(SDL_Renderer * renderer)
|
|
|
|
{
|
2018-09-20 19:46:02 +00:00
|
|
|
SDL_RenderCommand *cmd;
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
CHECK_RENDERER_MAGIC(renderer, );
|
|
|
|
|
|
|
|
SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
|
|
|
|
|
2018-09-20 19:46:02 +00:00
|
|
|
if (renderer->render_commands_tail != NULL) {
|
|
|
|
renderer->render_commands_tail->next = renderer->render_commands_pool;
|
|
|
|
cmd = renderer->render_commands;
|
|
|
|
} else {
|
|
|
|
cmd = renderer->render_commands_pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer->render_commands_pool = NULL;
|
|
|
|
renderer->render_commands_tail = NULL;
|
|
|
|
renderer->render_commands = NULL;
|
|
|
|
|
|
|
|
while (cmd != NULL) {
|
|
|
|
SDL_RenderCommand *next = cmd->next;
|
|
|
|
SDL_free(cmd);
|
|
|
|
cmd = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_free(renderer->vertex_data);
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
/* Free existing textures for this renderer */
|
2017-01-06 08:32:06 +00:00
|
|
|
while (renderer->textures) {
|
2017-03-03 21:38:17 +00:00
|
|
|
SDL_Texture *tex = renderer->textures; (void) tex;
|
2017-01-06 08:32:06 +00:00
|
|
|
SDL_DestroyTexture(renderer->textures);
|
2017-01-07 02:17:33 +00:00
|
|
|
SDL_assert(tex != renderer->textures); /* satisfy static analysis. */
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (renderer->window) {
|
|
|
|
SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's no longer magical... */
|
|
|
|
renderer->magic = NULL;
|
|
|
|
|
2018-06-18 20:13:56 +00:00
|
|
|
/* Free the target mutex */
|
|
|
|
SDL_DestroyMutex(renderer->target_mutex);
|
|
|
|
renderer->target_mutex = NULL;
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
/* 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) {
|
2018-09-20 19:46:02 +00:00
|
|
|
FlushRenderCommandsIfTextureNeeded(texture); /* in case the app is going to mess with it. */
|
2015-06-21 15:33:46 +00:00
|
|
|
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) {
|
2018-09-20 19:46:02 +00:00
|
|
|
FlushRenderCommandsIfTextureNeeded(texture); /* in case the app messed with it. */
|
2015-06-21 15:33:46 +00:00
|
|
|
return renderer->GL_UnbindTexture(renderer, texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_Unsupported();
|
|
|
|
}
|
|
|
|
|
2017-12-08 22:30:10 +00:00
|
|
|
void *
|
|
|
|
SDL_RenderGetMetalLayer(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, NULL);
|
|
|
|
|
|
|
|
if (renderer->GetMetalLayer) {
|
2018-09-20 19:46:02 +00:00
|
|
|
FlushRenderCommands(renderer); /* in case the app is going to mess with it. */
|
2017-12-08 22:30:10 +00:00
|
|
|
return renderer->GetMetalLayer(renderer);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
SDL_RenderGetMetalCommandEncoder(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, NULL);
|
|
|
|
|
|
|
|
if (renderer->GetMetalCommandEncoder) {
|
2018-09-20 19:46:02 +00:00
|
|
|
FlushRenderCommands(renderer); /* in case the app is going to mess with it. */
|
2017-12-08 22:30:10 +00:00
|
|
|
return renderer->GetMetalCommandEncoder(renderer);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
static SDL_BlendMode
|
|
|
|
SDL_GetShortBlendMode(SDL_BlendMode blendMode)
|
|
|
|
{
|
2017-08-15 03:07:30 +00:00
|
|
|
if (blendMode == SDL_BLENDMODE_NONE_FULL) {
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_BLENDMODE_NONE;
|
2017-08-15 03:07:30 +00:00
|
|
|
}
|
|
|
|
if (blendMode == SDL_BLENDMODE_BLEND_FULL) {
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_BLENDMODE_BLEND;
|
2017-08-15 03:07:30 +00:00
|
|
|
}
|
|
|
|
if (blendMode == SDL_BLENDMODE_ADD_FULL) {
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_BLENDMODE_ADD;
|
2017-08-15 03:07:30 +00:00
|
|
|
}
|
|
|
|
if (blendMode == SDL_BLENDMODE_MOD_FULL) {
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_BLENDMODE_MOD;
|
|
|
|
}
|
2020-01-16 16:52:59 +00:00
|
|
|
if (blendMode == SDL_BLENDMODE_MUL_FULL) {
|
|
|
|
return SDL_BLENDMODE_MUL;
|
|
|
|
}
|
2017-08-15 03:07:30 +00:00
|
|
|
return blendMode;
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_BlendMode
|
|
|
|
SDL_GetLongBlendMode(SDL_BlendMode blendMode)
|
|
|
|
{
|
2017-08-15 03:07:30 +00:00
|
|
|
if (blendMode == SDL_BLENDMODE_NONE) {
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_BLENDMODE_NONE_FULL;
|
2017-08-15 03:07:30 +00:00
|
|
|
}
|
|
|
|
if (blendMode == SDL_BLENDMODE_BLEND) {
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_BLENDMODE_BLEND_FULL;
|
2017-08-15 03:07:30 +00:00
|
|
|
}
|
|
|
|
if (blendMode == SDL_BLENDMODE_ADD) {
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_BLENDMODE_ADD_FULL;
|
2017-08-15 03:07:30 +00:00
|
|
|
}
|
|
|
|
if (blendMode == SDL_BLENDMODE_MOD) {
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
return SDL_BLENDMODE_MOD_FULL;
|
|
|
|
}
|
2020-01-16 16:52:59 +00:00
|
|
|
if (blendMode == SDL_BLENDMODE_MUL) {
|
|
|
|
return SDL_BLENDMODE_MUL_FULL;
|
|
|
|
}
|
2017-08-15 03:07:30 +00:00
|
|
|
return blendMode;
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-08-15 03:37:07 +00:00
|
|
|
return (SDL_BlendOperation)(((Uint32)blendMode >> 16) & 0xF);
|
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
This fixes bug 2828 - Subtractive Blending
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_SUBTRACT);
This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode
blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD);
2017-08-14 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 23:20:45 +00:00
|
|
|
int
|
|
|
|
SDL_RenderSetVSync(SDL_Renderer * renderer, int vsync)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
2021-09-14 22:51:17 +00:00
|
|
|
if (vsync != 0 && vsync != 1) {
|
|
|
|
return SDL_Unsupported();
|
|
|
|
}
|
|
|
|
|
2021-03-07 23:20:45 +00:00
|
|
|
if (renderer->SetVSync) {
|
|
|
|
return renderer->SetVSync(renderer, vsync);
|
|
|
|
}
|
|
|
|
return SDL_Unsupported();
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|