2011-02-02 14:34:54 -08:00
|
|
|
/*
|
2011-04-08 13:03:26 -07:00
|
|
|
Simple DirectMedia Layer
|
2011-12-31 09:28:07 -05:00
|
|
|
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
2011-04-08 13:03:26 -07: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.
|
2011-02-02 14:34:54 -08:00
|
|
|
*/
|
|
|
|
#include "SDL_config.h"
|
|
|
|
|
|
|
|
/* The SDL 2D rendering system */
|
|
|
|
|
2011-02-05 10:35:36 -08:00
|
|
|
#include "SDL_hints.h"
|
2011-02-07 16:45:40 -08:00
|
|
|
#include "SDL_log.h"
|
2011-02-02 14:34:54 -08:00
|
|
|
#include "SDL_render.h"
|
|
|
|
#include "SDL_sysrender.h"
|
2011-02-05 12:01:11 -08:00
|
|
|
#include "software/SDL_render_sw_c.h"
|
2011-02-02 14:34:54 -08:00
|
|
|
|
|
|
|
|
2011-04-04 09:29:13 -07:00
|
|
|
#define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
#define CHECK_RENDERER_MAGIC(renderer, retval) \
|
|
|
|
if (!renderer || renderer->magic != &renderer_magic) { \
|
|
|
|
SDL_SetError("Invalid renderer"); \
|
|
|
|
return retval; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_TEXTURE_MAGIC(texture, retval) \
|
|
|
|
if (!texture || texture->magic != &texture_magic) { \
|
|
|
|
SDL_SetError("Invalid texture"); \
|
|
|
|
return retval; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const SDL_RenderDriver *render_drivers[] = {
|
2011-02-08 10:04:09 -08:00
|
|
|
#if !SDL_RENDER_DISABLED
|
2011-02-02 14:34:54 -08:00
|
|
|
#if SDL_VIDEO_RENDER_D3D
|
|
|
|
&D3D_RenderDriver,
|
|
|
|
#endif
|
|
|
|
#if SDL_VIDEO_RENDER_OGL
|
|
|
|
&GL_RenderDriver,
|
|
|
|
#endif
|
2011-02-06 00:00:13 -08:00
|
|
|
#if SDL_VIDEO_RENDER_OGL_ES2
|
|
|
|
&GLES2_RenderDriver,
|
2011-02-05 16:07:10 -08:00
|
|
|
#endif
|
2011-02-06 10:22:25 -08:00
|
|
|
#if SDL_VIDEO_RENDER_OGL_ES
|
|
|
|
&GLES_RenderDriver,
|
|
|
|
#endif
|
2011-02-05 16:07:10 -08:00
|
|
|
#if SDL_VIDEO_RENDER_DIRECTFB
|
|
|
|
&DirectFB_RenderDriver,
|
a Nintendo ds update
Frank Zago to SDL
For those interested, here's a snapshot of the current port. I did away with
most of the previous attempt which was based of the sprite engine, because the
support is limited to 128 64x64 sprites. Instead I'm using the gl engine.
The drawback is that either the frame buffer or the gl engine can be used
because there's not that much video memory on a DS.
With minimal changes to their code, it can now run the following tests: ,
testspriteminimal, testscale and testsprite2. The last 2 only run under the
emulator for some reason. The tests are not included in this patch for size
reason.
In 16 bits mode, the 16th bit indicated transparency/opacity. If 0, the color
is not displayed. So I had to patch a few core file to set that bit to 1. See
patch for src/video/SDL_RLEaccel.c and src/video/SDL_blit.h. Is that ok, or is
there a better way ?
The nds also doesn't support windowed mode, so I force the fullscreen in
src/video/SDL_video.c. Is that ok, or is there a better way ?
To get a smaller library, I also tried to not compile the software renderer
when the hardware renderer is compiled in, and define SDL_NO_COMPAT; however
the compilation eventually fails in SDL_surface.c because SDL_SRCCOLORKEY is
defined in SDL_compat.h. Is SDL_NO_COMPAT only for application and not SDL
itself ?
2011-03-06 21:12:19 -08:00
|
|
|
#endif
|
|
|
|
#if SDL_VIDEO_RENDER_NDS
|
|
|
|
&NDS_RenderDriver,
|
2011-02-02 14:34:54 -08:00
|
|
|
#endif
|
|
|
|
&SW_RenderDriver
|
2011-02-08 10:04:09 -08:00
|
|
|
#endif /* !SDL_RENDER_DISABLED */
|
2011-02-02 14:34:54 -08:00
|
|
|
};
|
|
|
|
static char renderer_magic;
|
|
|
|
static char texture_magic;
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetNumRenderDrivers(void)
|
|
|
|
{
|
|
|
|
return SDL_arraysize(render_drivers);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
|
|
|
|
SDL_SetError("index must be in the range of 0 - %d",
|
|
|
|
SDL_GetNumRenderDrivers() - 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*info = render_drivers[index]->info;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
SDL_RendererEventWatch(void *userdata, SDL_Event *event)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer = (SDL_Renderer *)userdata;
|
|
|
|
|
2011-02-15 13:59:59 -08:00
|
|
|
if (event->type == SDL_WINDOWEVENT) {
|
2011-02-02 14:34:54 -08:00
|
|
|
SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
|
|
|
|
if (window == renderer->window) {
|
2011-02-15 13:59:59 -08:00
|
|
|
if (renderer->WindowEvent) {
|
|
|
|
renderer->WindowEvent(renderer, &event->window);
|
|
|
|
}
|
|
|
|
|
2012-01-22 21:46:06 -05:00
|
|
|
if (event->window.event == SDL_WINDOWEVENT_RESIZED) {
|
2011-02-15 13:59:59 -08:00
|
|
|
/* Try to keep the previous viewport centered */
|
|
|
|
int w, h;
|
|
|
|
|
|
|
|
SDL_GetWindowSize(window, &w, &h);
|
2012-01-21 22:22:30 -05:00
|
|
|
if (renderer->target) {
|
|
|
|
renderer->viewport_backup.x = (w - renderer->viewport_backup.w) / 2;
|
|
|
|
renderer->viewport_backup.y = (h - renderer->viewport_backup.h) / 2;
|
|
|
|
} else {
|
2012-10-01 20:59:33 -07:00
|
|
|
renderer->viewport.x = (w - renderer->viewport.w) / 2;
|
|
|
|
renderer->viewport.y = (h - renderer->viewport.h) / 2;
|
|
|
|
renderer->UpdateViewport(renderer);
|
2012-01-21 22:22:30 -05:00
|
|
|
}
|
2012-01-22 21:46:06 -05:00
|
|
|
renderer->resized = SDL_TRUE;
|
|
|
|
} else if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
|
|
|
|
if (!renderer->resized) {
|
|
|
|
/* Window was programmatically resized, reset viewport */
|
2012-10-01 20:59:33 -07:00
|
|
|
int w, h;
|
|
|
|
|
2012-01-22 21:46:06 -05:00
|
|
|
SDL_GetWindowSize(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 {
|
2012-10-01 20:59:33 -07:00
|
|
|
renderer->viewport.x = 0;
|
|
|
|
renderer->viewport.y = 0;
|
|
|
|
renderer->viewport.w = w;
|
|
|
|
renderer->viewport.h = h;
|
|
|
|
renderer->UpdateViewport(renderer);
|
2012-01-22 21:46:06 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 20:59:33 -07:00
|
|
|
renderer->resized = SDL_FALSE;
|
2012-01-22 21:46:06 -05:00
|
|
|
} 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;
|
|
|
|
}
|
2011-11-07 23:07:00 -05:00
|
|
|
} else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
|
2012-01-22 21:46:06 -05:00
|
|
|
renderer->hidden = SDL_TRUE;
|
2011-11-07 23:07:00 -05:00
|
|
|
} else if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
|
2012-01-22 21:46:06 -05:00
|
|
|
if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
|
|
|
|
renderer->hidden = SDL_FALSE;
|
|
|
|
}
|
2011-02-15 13:59:59 -08:00
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-22 19:22:53 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
SDL_Renderer *
|
|
|
|
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer = NULL;
|
|
|
|
int n = SDL_GetNumRenderDrivers();
|
2011-02-05 10:35:36 -08:00
|
|
|
const char *hint;
|
2011-02-02 14:34:54 -08:00
|
|
|
|
2011-04-04 09:29:13 -07:00
|
|
|
if (!window) {
|
|
|
|
SDL_SetError("Invalid window");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_GetRenderer(window)) {
|
|
|
|
SDL_SetError("Renderer already associated with window");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-05 10:35:36 -08:00
|
|
|
hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
|
|
|
|
if (hint) {
|
|
|
|
if (*hint == '0') {
|
|
|
|
flags &= ~SDL_RENDERER_PRESENTVSYNC;
|
|
|
|
} else {
|
|
|
|
flags |= SDL_RENDERER_PRESENTVSYNC;
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
|
2011-02-05 10:35:36 -08:00
|
|
|
if (index < 0) {
|
|
|
|
hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
|
|
|
|
if (hint) {
|
2011-02-02 14:34:54 -08:00
|
|
|
for (index = 0; index < n; ++index) {
|
|
|
|
const SDL_RenderDriver *driver = render_drivers[index];
|
|
|
|
|
2011-02-05 10:35:36 -08:00
|
|
|
if (SDL_strcasecmp(hint, driver->info.name) == 0) {
|
2011-02-02 14:34:54 -08:00
|
|
|
/* Create a new renderer instance */
|
|
|
|
renderer = driver->CreateRenderer(window, flags);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-02-05 10:35:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!renderer) {
|
2011-02-02 14:34:54 -08:00
|
|
|
for (index = 0; index < n; ++index) {
|
|
|
|
const SDL_RenderDriver *driver = render_drivers[index];
|
|
|
|
|
|
|
|
if ((driver->info.flags & flags) == flags) {
|
|
|
|
/* Create a new renderer instance */
|
|
|
|
renderer = driver->CreateRenderer(window, flags);
|
|
|
|
if (renderer) {
|
|
|
|
/* Yay, we got one! */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index == n) {
|
|
|
|
SDL_SetError("Couldn't find matching render driver");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (index >= SDL_GetNumRenderDrivers()) {
|
|
|
|
SDL_SetError("index must be -1 or in the range of 0 - %d",
|
|
|
|
SDL_GetNumRenderDrivers() - 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Create a new renderer instance */
|
|
|
|
renderer = render_drivers[index]->CreateRenderer(window, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (renderer) {
|
|
|
|
renderer->magic = &renderer_magic;
|
2011-02-03 15:49:37 -08:00
|
|
|
renderer->window = window;
|
2012-10-01 20:59:33 -07:00
|
|
|
renderer->scale.x = 1.0f;
|
|
|
|
renderer->scale.y = 1.0f;
|
2011-02-02 14:34:54 -08:00
|
|
|
|
2012-01-22 21:46:06 -05:00
|
|
|
if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
|
|
|
|
renderer->hidden = SDL_TRUE;
|
2011-11-07 23:07:00 -05:00
|
|
|
} else {
|
2012-01-22 21:46:06 -05:00
|
|
|
renderer->hidden = SDL_FALSE;
|
2011-11-07 23:07:00 -05:00
|
|
|
}
|
|
|
|
|
2011-04-04 09:29:13 -07:00
|
|
|
SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
|
|
|
|
|
2011-02-15 13:59:59 -08:00
|
|
|
SDL_RenderSetViewport(renderer, NULL);
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
|
2011-02-07 16:45:40 -08:00
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
|
|
|
|
"Created renderer: %s", renderer->info.name);
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
return renderer;
|
|
|
|
}
|
|
|
|
|
2011-02-03 15:49:37 -08:00
|
|
|
SDL_Renderer *
|
|
|
|
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
|
|
|
|
{
|
2011-02-08 10:04:09 -08:00
|
|
|
#if !SDL_RENDER_DISABLED
|
2011-02-15 13:59:59 -08:00
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
renderer = SW_CreateRendererForSurface(surface);
|
|
|
|
|
|
|
|
if (renderer) {
|
|
|
|
renderer->magic = &renderer_magic;
|
|
|
|
|
|
|
|
SDL_RenderSetViewport(renderer, NULL);
|
|
|
|
}
|
|
|
|
return renderer;
|
2011-02-08 10:04:09 -08:00
|
|
|
#else
|
|
|
|
SDL_SetError("SDL not built with rendering support");
|
|
|
|
return NULL;
|
|
|
|
#endif /* !SDL_RENDER_DISABLED */
|
2011-02-03 15:49:37 -08:00
|
|
|
}
|
|
|
|
|
2011-04-04 09:29:13 -07:00
|
|
|
SDL_Renderer *
|
|
|
|
SDL_GetRenderer(SDL_Window * window)
|
|
|
|
{
|
|
|
|
return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
int
|
|
|
|
SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
*info = renderer->info;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-03 00:19:40 -08: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;
|
|
|
|
|
2011-02-12 08:17:58 -08:00
|
|
|
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) {
|
2011-02-15 15:50:21 -08:00
|
|
|
if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
|
|
|
|
SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
|
2011-02-12 08:17:58 -08:00
|
|
|
return renderer->info.texture_formats[i];
|
|
|
|
}
|
2011-02-03 00:19:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return renderer->info.texture_formats[0];
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
SDL_Texture *
|
|
|
|
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
|
|
|
|
{
|
|
|
|
SDL_Texture *texture;
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, NULL);
|
|
|
|
|
2011-02-28 14:47:39 -08:00
|
|
|
if (!format) {
|
|
|
|
format = renderer->info.texture_formats[0];
|
|
|
|
}
|
2011-02-03 00:19:40 -08:00
|
|
|
if (SDL_ISPIXELFORMAT_INDEXED(format)) {
|
|
|
|
SDL_SetError("Palettized textures are not supported");
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
if (w <= 0 || h <= 0) {
|
|
|
|
SDL_SetError("Texture dimensions can't be 0");
|
2011-02-03 00:19:40 -08:00
|
|
|
return NULL;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
|
|
|
|
if (!texture) {
|
|
|
|
SDL_OutOfMemory();
|
2011-02-03 00:19:40 -08:00
|
|
|
return NULL;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
texture->magic = &texture_magic;
|
|
|
|
texture->format = format;
|
|
|
|
texture->access = access;
|
|
|
|
texture->w = w;
|
|
|
|
texture->h = h;
|
|
|
|
texture->r = 255;
|
|
|
|
texture->g = 255;
|
|
|
|
texture->b = 255;
|
|
|
|
texture->a = 255;
|
|
|
|
texture->renderer = renderer;
|
|
|
|
texture->next = renderer->textures;
|
|
|
|
if (renderer->textures) {
|
|
|
|
renderer->textures->prev = texture;
|
|
|
|
}
|
|
|
|
renderer->textures = texture;
|
|
|
|
|
2011-02-03 00:19:40 -08:00
|
|
|
if (IsSupportedFormat(renderer, format)) {
|
|
|
|
if (renderer->CreateTexture(renderer, texture) < 0) {
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
texture->native = SDL_CreateTexture(renderer,
|
|
|
|
GetClosestSupportedFormat(renderer, format),
|
|
|
|
access, w, h);
|
|
|
|
if (!texture->native) {
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-28 04:09:06 -07:00
|
|
|
/* Swap textures to have texture before texture->native in the list */
|
|
|
|
texture->native->next = texture->next;
|
|
|
|
texture->prev = texture->native->prev;
|
|
|
|
texture->native->prev = texture;
|
|
|
|
texture->next = texture->native;
|
|
|
|
renderer->textures = texture;
|
|
|
|
|
2011-02-03 00:19:40 -08:00
|
|
|
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
|
|
|
|
texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
|
|
|
|
if (!texture->yuv) {
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else if (access == SDL_TEXTUREACCESS_STREAMING) {
|
|
|
|
/* The pitch is 4 byte aligned */
|
|
|
|
texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
|
2011-02-17 18:58:43 -08:00
|
|
|
texture->pixels = SDL_calloc(1, texture->pitch * h);
|
2011-02-03 00:19:40 -08:00
|
|
|
if (!texture->pixels) {
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Texture *
|
2011-02-03 00:54:29 -08:00
|
|
|
SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
|
2011-02-02 14:34:54 -08:00
|
|
|
{
|
2011-02-03 00:54:29 -08:00
|
|
|
const SDL_PixelFormat *fmt;
|
|
|
|
SDL_bool needAlpha;
|
|
|
|
Uint32 i;
|
|
|
|
Uint32 format;
|
|
|
|
SDL_Texture *texture;
|
2011-02-02 14:34:54 -08:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, NULL);
|
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-03 00:54:29 -08:00
|
|
|
/* See what the best texture format is */
|
|
|
|
fmt = surface->format;
|
|
|
|
if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
|
|
|
|
needAlpha = SDL_TRUE;
|
2011-02-02 14:34:54 -08:00
|
|
|
} else {
|
2011-02-03 00:54:29 -08:00
|
|
|
needAlpha = SDL_FALSE;
|
|
|
|
}
|
|
|
|
format = renderer->info.texture_formats[0];
|
|
|
|
for (i = 0; i < renderer->info.num_texture_formats; ++i) {
|
2011-02-12 08:17:58 -08:00
|
|
|
if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
|
|
|
|
SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
|
2011-02-03 00:54:29 -08:00
|
|
|
format = renderer->info.texture_formats[i];
|
|
|
|
break;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 00:54:29 -08:00
|
|
|
texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
|
|
|
|
surface->w, surface->h);
|
2011-02-02 14:34:54 -08:00
|
|
|
if (!texture) {
|
2011-02-03 00:54:29 -08:00
|
|
|
return NULL;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
2011-02-03 00:54:29 -08:00
|
|
|
|
2011-02-13 13:46:10 -08:00
|
|
|
if (format == surface->format->format) {
|
2011-02-02 14:34:54 -08:00
|
|
|
if (SDL_MUSTLOCK(surface)) {
|
|
|
|
SDL_LockSurface(surface);
|
2011-02-03 00:54:29 -08:00
|
|
|
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
|
2011-02-02 14:34:54 -08:00
|
|
|
SDL_UnlockSurface(surface);
|
|
|
|
} else {
|
2011-02-03 00:54:29 -08:00
|
|
|
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
} else {
|
2011-02-15 13:59:59 -08:00
|
|
|
SDL_PixelFormat *dst_fmt;
|
2011-02-03 00:54:29 -08:00
|
|
|
SDL_Surface *temp = NULL;
|
2011-02-02 14:34:54 -08:00
|
|
|
|
|
|
|
/* Set up a destination surface for the texture update */
|
2011-02-15 13:59:59 -08:00
|
|
|
dst_fmt = SDL_AllocFormat(format);
|
|
|
|
temp = SDL_ConvertSurface(surface, dst_fmt, 0);
|
|
|
|
SDL_FreeFormat(dst_fmt);
|
2011-02-03 00:54:29 -08:00
|
|
|
if (temp) {
|
|
|
|
SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
|
|
|
|
SDL_FreeSurface(temp);
|
|
|
|
} else {
|
2011-02-02 14:34:54 -08:00
|
|
|
SDL_DestroyTexture(texture);
|
2011-02-03 00:54:29 -08:00
|
|
|
return NULL;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Uint8 r, g, b, a;
|
|
|
|
SDL_BlendMode blendMode;
|
|
|
|
|
|
|
|
SDL_GetSurfaceColorMod(surface, &r, &g, &b);
|
|
|
|
SDL_SetTextureColorMod(texture, r, g, b);
|
|
|
|
|
|
|
|
SDL_GetSurfaceAlphaMod(surface, &a);
|
|
|
|
SDL_SetTextureAlphaMod(texture, a);
|
|
|
|
|
|
|
|
if (SDL_GetColorKey(surface, NULL) == 0) {
|
|
|
|
/* We converted to a texture with alpha format */
|
|
|
|
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
|
|
|
|
} else {
|
|
|
|
SDL_GetSurfaceBlendMode(surface, &blendMode);
|
|
|
|
SDL_SetTextureBlendMode(texture, blendMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
|
|
|
|
int *w, int *h)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (format) {
|
|
|
|
*format = texture->format;
|
|
|
|
}
|
|
|
|
if (access) {
|
|
|
|
*access = texture->access;
|
|
|
|
}
|
|
|
|
if (w) {
|
|
|
|
*w = texture->w;
|
|
|
|
}
|
|
|
|
if (h) {
|
|
|
|
*h = texture->h;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
renderer = texture->renderer;
|
|
|
|
if (r < 255 || g < 255 || b < 255) {
|
|
|
|
texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
|
|
|
|
} else {
|
|
|
|
texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
|
|
|
|
}
|
|
|
|
texture->r = r;
|
|
|
|
texture->g = g;
|
|
|
|
texture->b = b;
|
2011-02-03 00:19:40 -08:00
|
|
|
if (texture->native) {
|
|
|
|
return SDL_SetTextureColorMod(texture->native, r, g, b);
|
|
|
|
} else if (renderer->SetTextureColorMod) {
|
2011-02-02 14:34:54 -08:00
|
|
|
return renderer->SetTextureColorMod(renderer, texture);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
|
|
|
|
Uint8 * b)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
*r = texture->r;
|
|
|
|
}
|
|
|
|
if (g) {
|
|
|
|
*g = texture->g;
|
|
|
|
}
|
|
|
|
if (b) {
|
|
|
|
*b = texture->b;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
renderer = texture->renderer;
|
|
|
|
if (alpha < 255) {
|
|
|
|
texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
|
|
|
|
} else {
|
|
|
|
texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
|
|
|
|
}
|
|
|
|
texture->a = alpha;
|
2011-02-03 00:19:40 -08:00
|
|
|
if (texture->native) {
|
|
|
|
return SDL_SetTextureAlphaMod(texture->native, alpha);
|
|
|
|
} else if (renderer->SetTextureAlphaMod) {
|
2011-02-02 14:34:54 -08:00
|
|
|
return renderer->SetTextureAlphaMod(renderer, texture);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (alpha) {
|
|
|
|
*alpha = texture->a;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
renderer = texture->renderer;
|
|
|
|
texture->blendMode = blendMode;
|
2011-02-03 00:19:40 -08:00
|
|
|
if (texture->native) {
|
2011-02-05 00:32:04 -08:00
|
|
|
return SDL_SetTextureBlendMode(texture->native, blendMode);
|
2011-02-03 00:19:40 -08:00
|
|
|
} else if (renderer->SetTextureBlendMode) {
|
2011-02-02 14:34:54 -08:00
|
|
|
return renderer->SetTextureBlendMode(renderer, texture);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
|
|
|
|
{
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (blendMode) {
|
|
|
|
*blendMode = texture->blendMode;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-03 00:19:40 -08: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 */
|
|
|
|
void *native_pixels;
|
|
|
|
int native_pitch;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
void *temp_pixels;
|
|
|
|
int temp_pitch;
|
|
|
|
|
|
|
|
temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
|
|
|
|
temp_pixels = SDL_malloc(rect->h * temp_pitch);
|
|
|
|
if (!temp_pixels) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
|
|
|
|
rect->w, rect->h, temp_pixels, temp_pitch);
|
|
|
|
SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
|
|
|
|
SDL_free(temp_pixels);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
const void *pixels, int pitch)
|
|
|
|
{
|
|
|
|
SDL_Texture *native = texture->native;
|
|
|
|
|
|
|
|
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
|
|
|
|
/* We can lock the texture and copy to it */
|
|
|
|
void *native_pixels;
|
|
|
|
int native_pitch;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
void *temp_pixels;
|
|
|
|
int temp_pitch;
|
|
|
|
|
|
|
|
temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
|
|
|
|
temp_pixels = SDL_malloc(rect->h * temp_pitch);
|
|
|
|
if (!temp_pixels) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SDL_ConvertPixels(rect->w, rect->h,
|
|
|
|
texture->format, pixels, pitch,
|
|
|
|
native->format, temp_pixels, temp_pitch);
|
|
|
|
SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
|
|
|
|
SDL_free(temp_pixels);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
int
|
|
|
|
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
const void *pixels, int pitch)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
SDL_Rect full_rect;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (!rect) {
|
|
|
|
full_rect.x = 0;
|
|
|
|
full_rect.y = 0;
|
|
|
|
full_rect.w = texture->w;
|
|
|
|
full_rect.h = texture->h;
|
|
|
|
rect = &full_rect;
|
|
|
|
}
|
2011-02-03 00:19:40 -08:00
|
|
|
|
|
|
|
if (texture->yuv) {
|
|
|
|
return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
|
|
|
|
} else if (texture->native) {
|
|
|
|
return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
|
|
|
|
} else {
|
|
|
|
renderer = texture->renderer;
|
|
|
|
return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
void **pixels, int *pitch)
|
|
|
|
{
|
|
|
|
return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
|
|
|
|
void **pixels, int *pitch)
|
|
|
|
{
|
|
|
|
texture->locked_rect = *rect;
|
|
|
|
*pixels = (void *) ((Uint8 *) texture->pixels +
|
|
|
|
rect->y * texture->pitch +
|
|
|
|
rect->x * SDL_BYTESPERPIXEL(texture->format));
|
|
|
|
*pitch = texture->pitch;
|
|
|
|
return 0;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-02-03 00:19:40 -08:00
|
|
|
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
|
2011-02-02 14:34:54 -08:00
|
|
|
void **pixels, int *pitch)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
SDL_Rect full_rect;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
|
|
|
|
SDL_SetError("SDL_LockTexture(): texture must be streaming");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-03 00:19:40 -08:00
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
if (!rect) {
|
|
|
|
full_rect.x = 0;
|
|
|
|
full_rect.y = 0;
|
|
|
|
full_rect.w = texture->w;
|
|
|
|
full_rect.h = texture->h;
|
|
|
|
rect = &full_rect;
|
|
|
|
}
|
2011-02-03 00:19:40 -08:00
|
|
|
|
|
|
|
if (texture->yuv) {
|
|
|
|
return SDL_LockTextureYUV(texture, rect, pixels, pitch);
|
|
|
|
} else if (texture->native) {
|
|
|
|
return SDL_LockTextureNative(texture, rect, pixels, pitch);
|
|
|
|
} else {
|
|
|
|
renderer = texture->renderer;
|
|
|
|
return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
|
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
2011-02-03 00:19:40 -08:00
|
|
|
static void
|
|
|
|
SDL_UnlockTextureYUV(SDL_Texture * texture)
|
2011-02-02 14:34:54 -08:00
|
|
|
{
|
2011-02-03 00:19:40 -08:00
|
|
|
SDL_Texture *native = texture->native;
|
|
|
|
void *native_pixels;
|
|
|
|
int native_pitch;
|
|
|
|
SDL_Rect rect;
|
2011-02-02 14:34:54 -08:00
|
|
|
|
2011-02-03 00:19:40 -08:00
|
|
|
rect.x = 0;
|
|
|
|
rect.y = 0;
|
|
|
|
rect.w = texture->w;
|
|
|
|
rect.h = texture->h;
|
2011-02-02 14:34:54 -08:00
|
|
|
|
2011-02-03 00:19:40 -08:00
|
|
|
if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
|
2011-02-02 14:34:54 -08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-03 00:19:40 -08:00
|
|
|
SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
|
|
|
|
rect.w, rect.h, native_pixels, native_pitch);
|
|
|
|
SDL_UnlockTexture(native);
|
|
|
|
}
|
|
|
|
|
2011-10-31 05:56:58 -04:00
|
|
|
static void
|
2011-02-03 00:19:40 -08:00
|
|
|
SDL_UnlockTextureNative(SDL_Texture * texture)
|
|
|
|
{
|
|
|
|
SDL_Texture *native = texture->native;
|
|
|
|
void *native_pixels;
|
|
|
|
int native_pitch;
|
|
|
|
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) {
|
2011-02-02 14:34:54 -08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-03 00:19:40 -08:00
|
|
|
SDL_ConvertPixels(rect->w, rect->h,
|
|
|
|
texture->format, pixels, pitch,
|
|
|
|
native->format, native_pixels, native_pitch);
|
|
|
|
SDL_UnlockTexture(native);
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-02-03 00:19:40 -08:00
|
|
|
SDL_UnlockTexture(SDL_Texture * texture)
|
2011-02-02 14:34:54 -08:00
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, );
|
|
|
|
|
|
|
|
if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-03 00:19:40 -08:00
|
|
|
if (texture->yuv) {
|
|
|
|
SDL_UnlockTextureYUV(texture);
|
|
|
|
} else if (texture->native) {
|
|
|
|
SDL_UnlockTextureNative(texture);
|
|
|
|
} else {
|
|
|
|
renderer = texture->renderer;
|
|
|
|
renderer->UnlockTexture(renderer, texture);
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-21 22:22:30 -05:00
|
|
|
SDL_bool
|
|
|
|
SDL_RenderTargetSupported(SDL_Renderer *renderer)
|
|
|
|
{
|
2012-01-22 01:26:28 -05:00
|
|
|
if (!renderer || !renderer->SetRenderTarget) {
|
2012-01-21 22:22:30 -05:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-01-22 01:26:28 -05:00
|
|
|
SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
|
2012-01-21 22:22:30 -05:00
|
|
|
{
|
|
|
|
if (!SDL_RenderTargetSupported(renderer)) {
|
|
|
|
SDL_Unsupported();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (texture == renderer->target) {
|
|
|
|
/* Nothing to do! */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* texture == NULL is valid and means reset the target to the window */
|
|
|
|
if (texture) {
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
if (renderer != texture->renderer) {
|
|
|
|
SDL_SetError("Texture was not created with this renderer");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-06-21 11:16:14 -03:00
|
|
|
if (texture->access != SDL_TEXTUREACCESS_TARGET) {
|
2012-01-21 22:22:30 -05:00
|
|
|
SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (texture->native) {
|
|
|
|
/* Always render to the native texture */
|
|
|
|
texture = texture->native;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture && !renderer->target) {
|
|
|
|
/* Make a backup of the viewport */
|
|
|
|
renderer->viewport_backup = renderer->viewport;
|
2012-10-01 20:59:33 -07:00
|
|
|
renderer->scale_backup = renderer->scale;
|
2012-01-21 22:22:30 -05:00
|
|
|
}
|
|
|
|
renderer->target = texture;
|
|
|
|
|
2012-01-22 01:26:28 -05:00
|
|
|
if (renderer->SetRenderTarget(renderer, texture) < 0) {
|
2012-01-21 22:22:30 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture) {
|
2012-10-01 20:59:33 -07:00
|
|
|
renderer->viewport.x = 0;
|
|
|
|
renderer->viewport.y = 0;
|
|
|
|
renderer->viewport.w = texture->w;
|
|
|
|
renderer->viewport.h = texture->h;
|
|
|
|
renderer->scale.x = 1.0f;
|
|
|
|
renderer->scale.y = 1.0f;
|
2012-01-21 22:22:30 -05:00
|
|
|
} else {
|
2012-10-01 20:59:33 -07:00
|
|
|
renderer->viewport = renderer->viewport_backup;
|
|
|
|
renderer->scale = renderer->scale_backup;
|
2012-01-21 22:22:30 -05:00
|
|
|
}
|
2012-10-01 20:59:33 -07:00
|
|
|
if (renderer->UpdateViewport(renderer) < 0) {
|
2012-01-21 22:22:30 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All set! */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-15 13:59:59 -08:00
|
|
|
int
|
|
|
|
SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (rect) {
|
2012-10-01 20:59:33 -07:00
|
|
|
renderer->viewport.x = (int)SDL_floor(rect->x * renderer->scale.x);
|
|
|
|
renderer->viewport.y = (int)SDL_floor(rect->y * renderer->scale.y);
|
|
|
|
renderer->viewport.w = (int)SDL_ceil(rect->w * renderer->scale.x);
|
|
|
|
renderer->viewport.h = (int)SDL_ceil(rect->h * renderer->scale.y);
|
2011-02-15 13:59:59 -08:00
|
|
|
} else {
|
|
|
|
renderer->viewport.x = 0;
|
|
|
|
renderer->viewport.y = 0;
|
|
|
|
if (renderer->window) {
|
|
|
|
SDL_GetWindowSize(renderer->window,
|
|
|
|
&renderer->viewport.w, &renderer->viewport.h);
|
|
|
|
} else {
|
|
|
|
/* This will be filled in by UpdateViewport() */
|
|
|
|
renderer->viewport.w = 0;
|
|
|
|
renderer->viewport.h = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return renderer->UpdateViewport(renderer);
|
|
|
|
}
|
|
|
|
|
2011-02-07 20:06:26 -08:00
|
|
|
void
|
2011-02-15 13:59:59 -08:00
|
|
|
SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
|
2011-02-07 20:06:26 -08:00
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, );
|
|
|
|
|
2012-10-01 20:59:33 -07:00
|
|
|
if (rect) {
|
|
|
|
rect->x = (int)(renderer->viewport.x / renderer->scale.x);
|
|
|
|
rect->y = (int)(renderer->viewport.y / renderer->scale.y);
|
|
|
|
rect->w = (int)(renderer->viewport.w / renderer->scale.x);
|
|
|
|
rect->h = (int)(renderer->viewport.h / renderer->scale.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_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;
|
|
|
|
}
|
2011-02-07 20:06:26 -08:00
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
int
|
|
|
|
SDL_SetRenderDrawColor(SDL_Renderer * renderer,
|
|
|
|
Uint8 r, Uint8 g, Uint8 b, Uint8 a)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
renderer->r = r;
|
|
|
|
renderer->g = g;
|
|
|
|
renderer->b = b;
|
|
|
|
renderer->a = a;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetRenderDrawColor(SDL_Renderer * renderer,
|
|
|
|
Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
*r = renderer->r;
|
|
|
|
}
|
|
|
|
if (g) {
|
|
|
|
*g = renderer->g;
|
|
|
|
}
|
|
|
|
if (b) {
|
|
|
|
*b = renderer->b;
|
|
|
|
}
|
|
|
|
if (a) {
|
|
|
|
*a = renderer->a;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
renderer->blendMode = blendMode;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
*blendMode = renderer->blendMode;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderClear(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
2012-01-22 21:46:06 -05:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
2011-11-07 23:07:00 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
return renderer->RenderClear(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
|
|
|
|
{
|
|
|
|
SDL_Point point;
|
|
|
|
|
|
|
|
point.x = x;
|
|
|
|
point.y = y;
|
|
|
|
return SDL_RenderDrawPoints(renderer, &point, 1);
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:59:33 -07:00
|
|
|
static int
|
|
|
|
RenderDrawPointsWithRects(SDL_Renderer * renderer,
|
|
|
|
const SDL_Point * points, int count)
|
|
|
|
{
|
|
|
|
SDL_FRect *frects;
|
|
|
|
int i;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
frects = SDL_stack_alloc(SDL_FRect, count);
|
|
|
|
if (!frects) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
frects[i].x = points[i].x * renderer->scale.x;
|
|
|
|
frects[i].y = points[i].y * renderer->scale.y;
|
|
|
|
frects[i].w = renderer->scale.x;
|
|
|
|
frects[i].h = renderer->scale.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = renderer->RenderFillRects(renderer, frects, count);
|
|
|
|
|
|
|
|
SDL_stack_free(frects);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
int
|
|
|
|
SDL_RenderDrawPoints(SDL_Renderer * renderer,
|
|
|
|
const SDL_Point * points, int count)
|
|
|
|
{
|
2012-10-01 20:59:33 -07:00
|
|
|
SDL_FPoint *fpoints;
|
|
|
|
int i;
|
|
|
|
int status;
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!points) {
|
|
|
|
SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (count < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-22 21:46:06 -05:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
2011-11-07 23:07:00 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-01 20:59:33 -07:00
|
|
|
|
|
|
|
if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
|
|
|
|
return RenderDrawPointsWithRects(renderer, points, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
fpoints = SDL_stack_alloc(SDL_FPoint, count);
|
|
|
|
if (!fpoints) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
fpoints[i].x = points[i].x * renderer->scale.x;
|
|
|
|
fpoints[i].y = points[i].y * renderer->scale.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = renderer->RenderDrawPoints(renderer, fpoints, count);
|
|
|
|
|
|
|
|
SDL_stack_free(fpoints);
|
|
|
|
|
|
|
|
return status;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
|
|
|
|
{
|
|
|
|
SDL_Point points[2];
|
|
|
|
|
|
|
|
points[0].x = x1;
|
|
|
|
points[0].y = y1;
|
|
|
|
points[1].x = x2;
|
|
|
|
points[1].y = y2;
|
|
|
|
return SDL_RenderDrawLines(renderer, points, 2);
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:59:33 -07:00
|
|
|
static int
|
|
|
|
RenderDrawLinesWithRects(SDL_Renderer * renderer,
|
|
|
|
const SDL_Point * points, int count)
|
|
|
|
{
|
|
|
|
SDL_FRect *frect;
|
|
|
|
SDL_FRect *frects;
|
|
|
|
SDL_FPoint fpoints[2];
|
|
|
|
int i, nrects;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
frects = SDL_stack_alloc(SDL_FRect, count-1);
|
|
|
|
if (!frects) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
nrects = 0;
|
|
|
|
for (i = 0; i < count-1; ++i) {
|
|
|
|
if (points[i].x == points[i+1].x) {
|
|
|
|
int minY = SDL_min(points[i].y, points[i+1].y);
|
|
|
|
int maxY = SDL_max(points[i].y, points[i+1].y);
|
|
|
|
|
|
|
|
frect = &frects[nrects++];
|
|
|
|
frect->x = points[i].x * renderer->scale.x;
|
|
|
|
frect->y = minY * renderer->scale.y;
|
|
|
|
frect->w = renderer->scale.x;
|
|
|
|
frect->h = (maxY - minY + 1) * renderer->scale.y;
|
|
|
|
} else if (points[i].y == points[i+1].y) {
|
|
|
|
int minX = SDL_min(points[i].x, points[i+1].x);
|
|
|
|
int maxX = SDL_max(points[i].x, points[i+1].x);
|
|
|
|
|
|
|
|
frect = &frects[nrects++];
|
|
|
|
frect->x = minX * renderer->scale.x;
|
|
|
|
frect->y = points[i].y * renderer->scale.y;
|
|
|
|
frect->w = (maxX - minX + 1) * renderer->scale.x;
|
|
|
|
frect->h = renderer->scale.y;
|
|
|
|
} else {
|
|
|
|
/* FIXME: We can't use a rect for this line... */
|
|
|
|
frects[0].x = points[i].x * renderer->scale.x;
|
|
|
|
frects[0].y = points[i].y * renderer->scale.y;
|
|
|
|
frects[1].x = points[i+1].x * renderer->scale.x;
|
|
|
|
frects[1].y = points[i+1].y * renderer->scale.y;
|
|
|
|
status += renderer->RenderDrawLines(renderer, fpoints, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status += renderer->RenderFillRects(renderer, frects, nrects);
|
|
|
|
|
|
|
|
SDL_stack_free(frects);
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
status = -1;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
int
|
|
|
|
SDL_RenderDrawLines(SDL_Renderer * renderer,
|
|
|
|
const SDL_Point * points, int count)
|
|
|
|
{
|
2012-10-01 20:59:33 -07:00
|
|
|
SDL_FPoint *fpoints;
|
|
|
|
int i;
|
|
|
|
int status;
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!points) {
|
|
|
|
SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (count < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-22 21:46:06 -05:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
2011-11-07 23:07:00 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-01 20:59:33 -07:00
|
|
|
|
|
|
|
if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
|
|
|
|
return RenderDrawLinesWithRects(renderer, points, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
fpoints = SDL_stack_alloc(SDL_FPoint, count);
|
|
|
|
if (!fpoints) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
fpoints[i].x = points[i].x * renderer->scale.x;
|
|
|
|
fpoints[i].y = points[i].y * renderer->scale.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = renderer->RenderDrawLines(renderer, fpoints, count);
|
|
|
|
|
|
|
|
SDL_stack_free(fpoints);
|
|
|
|
|
|
|
|
return status;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
|
|
|
|
{
|
|
|
|
SDL_Rect full_rect;
|
|
|
|
SDL_Point points[5];
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
/* If 'rect' == NULL, then outline the whole surface */
|
|
|
|
if (!rect) {
|
2012-10-01 20:59:33 -07:00
|
|
|
SDL_RenderGetViewport(renderer, &full_rect);
|
2011-02-02 14:34:54 -08:00
|
|
|
full_rect.x = 0;
|
|
|
|
full_rect.y = 0;
|
|
|
|
rect = &full_rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
points[0].x = rect->x;
|
|
|
|
points[0].y = rect->y;
|
|
|
|
points[1].x = rect->x+rect->w-1;
|
|
|
|
points[1].y = rect->y;
|
|
|
|
points[2].x = rect->x+rect->w-1;
|
|
|
|
points[2].y = rect->y+rect->h-1;
|
|
|
|
points[3].x = rect->x;
|
|
|
|
points[3].y = rect->y+rect->h-1;
|
|
|
|
points[4].x = rect->x;
|
|
|
|
points[4].y = rect->y;
|
|
|
|
return SDL_RenderDrawLines(renderer, points, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderDrawRects(SDL_Renderer * renderer,
|
2011-02-15 13:59:59 -08:00
|
|
|
const SDL_Rect * rects, int count)
|
2011-02-02 14:34:54 -08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!rects) {
|
|
|
|
SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (count < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-22 21:46:06 -05:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
2011-11-07 23:07:00 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
for (i = 0; i < count; ++i) {
|
2011-02-15 13:59:59 -08:00
|
|
|
if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
|
2011-02-02 14:34:54 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
|
|
|
|
{
|
2011-02-17 02:17:38 -08:00
|
|
|
SDL_Rect full_rect;
|
2012-01-18 22:45:49 -05:00
|
|
|
|
2011-02-17 02:17:38 -08:00
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
2012-01-18 22:45:49 -05:00
|
|
|
|
2011-02-17 02:17:38 -08:00
|
|
|
/* If 'rect' == NULL, then outline the whole surface */
|
|
|
|
if (!rect) {
|
2012-10-01 20:59:33 -07:00
|
|
|
SDL_RenderGetViewport(renderer, &full_rect);
|
2011-02-17 02:17:38 -08:00
|
|
|
full_rect.x = 0;
|
|
|
|
full_rect.y = 0;
|
|
|
|
rect = &full_rect;
|
|
|
|
}
|
2011-02-15 13:59:59 -08:00
|
|
|
return SDL_RenderFillRects(renderer, rect, 1);
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderFillRects(SDL_Renderer * renderer,
|
2011-02-15 13:59:59 -08:00
|
|
|
const SDL_Rect * rects, int count)
|
2011-02-02 14:34:54 -08:00
|
|
|
{
|
2012-10-01 20:59:33 -07:00
|
|
|
SDL_FRect *frects;
|
|
|
|
int i;
|
|
|
|
int status;
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
|
|
|
|
if (!rects) {
|
|
|
|
SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (count < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-22 21:46:06 -05:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
2011-11-07 23:07:00 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-01 20:59:33 -07:00
|
|
|
|
|
|
|
frects = SDL_stack_alloc(SDL_FRect, count);
|
|
|
|
if (!frects) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
frects[i].x = rects[i].x * renderer->scale.x;
|
|
|
|
frects[i].y = rects[i].y * renderer->scale.y;
|
|
|
|
frects[i].w = rects[i].w * renderer->scale.x;
|
|
|
|
frects[i].h = rects[i].h * renderer->scale.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = renderer->RenderFillRects(renderer, frects, count);
|
|
|
|
|
|
|
|
SDL_stack_free(frects);
|
|
|
|
|
|
|
|
return status;
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
|
|
|
|
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
|
|
|
|
{
|
|
|
|
SDL_Rect real_srcrect;
|
|
|
|
SDL_Rect real_dstrect;
|
2012-10-01 20:59:33 -07:00
|
|
|
SDL_FRect frect;
|
2011-02-02 14:34:54 -08:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (renderer != texture->renderer) {
|
|
|
|
SDL_SetError("Texture was not created with this renderer");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:59:33 -07:00
|
|
|
SDL_RenderGetViewport(renderer, &real_dstrect);
|
2011-02-02 14:34:54 -08:00
|
|
|
real_dstrect.x = 0;
|
|
|
|
real_dstrect.y = 0;
|
2011-02-20 23:51:31 -08:00
|
|
|
if (dstrect) {
|
|
|
|
if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Clip srcrect by the same amount as dstrect was clipped */
|
|
|
|
if (dstrect->w != real_dstrect.w) {
|
|
|
|
int deltax = (real_dstrect.x - dstrect->x);
|
|
|
|
int deltaw = (real_dstrect.w - dstrect->w);
|
|
|
|
real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
|
|
|
|
real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
|
|
|
|
}
|
|
|
|
if (dstrect->h != real_dstrect.h) {
|
|
|
|
int deltay = (real_dstrect.y - dstrect->y);
|
|
|
|
int deltah = (real_dstrect.h - dstrect->h);
|
|
|
|
real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
|
|
|
|
real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
|
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
2011-02-03 00:19:40 -08:00
|
|
|
if (texture->native) {
|
|
|
|
texture = texture->native;
|
|
|
|
}
|
|
|
|
|
2012-01-22 21:46:06 -05:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
2011-11-07 23:07:00 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-01 20:59:33 -07:00
|
|
|
|
|
|
|
frect.x = real_dstrect.x * renderer->scale.x;
|
|
|
|
frect.y = real_dstrect.y * renderer->scale.y;
|
|
|
|
frect.w = real_dstrect.w * renderer->scale.x;
|
|
|
|
frect.h = real_dstrect.h * renderer->scale.y;
|
|
|
|
|
|
|
|
return renderer->RenderCopy(renderer, texture, &real_srcrect, &frect);
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
2012-06-01 19:51:08 -03: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)
|
|
|
|
{
|
|
|
|
SDL_Rect real_srcrect, real_dstrect;
|
|
|
|
SDL_Point real_center;
|
2012-10-01 20:59:33 -07:00
|
|
|
SDL_FRect frect;
|
|
|
|
SDL_FPoint fcenter;
|
2012-06-01 19:51:08 -03:00
|
|
|
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, -1);
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
|
|
|
|
|
|
|
if (renderer != texture->renderer) {
|
|
|
|
SDL_SetError("Texture was not created with this renderer");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!renderer->RenderCopyEx) {
|
|
|
|
SDL_SetError("Renderer does not support RenderCopyEx");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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? */
|
2012-10-01 20:59:33 -07:00
|
|
|
if (dstrect) {
|
|
|
|
real_dstrect = *dstrect;
|
|
|
|
} else {
|
|
|
|
SDL_RenderGetViewport(renderer, &real_dstrect);
|
2012-08-24 11:56:21 -03:00
|
|
|
real_dstrect.x = 0;
|
|
|
|
real_dstrect.y = 0;
|
2012-06-01 19:51:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (texture->native) {
|
|
|
|
texture = texture->native;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(center) real_center = *center;
|
|
|
|
else {
|
|
|
|
real_center.x = real_dstrect.w/2;
|
|
|
|
real_center.y = real_dstrect.h/2;
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:59:33 -07:00
|
|
|
frect.x = real_dstrect.x * renderer->scale.x;
|
|
|
|
frect.y = real_dstrect.y * renderer->scale.y;
|
|
|
|
frect.w = real_dstrect.w * renderer->scale.x;
|
|
|
|
frect.h = real_dstrect.h * renderer->scale.y;
|
|
|
|
|
|
|
|
fcenter.x = real_center.x * renderer->scale.x;
|
|
|
|
fcenter.y = real_center.y * renderer->scale.y;
|
|
|
|
|
|
|
|
return renderer->RenderCopyEx(renderer, texture, &real_srcrect, &frect, angle, &fcenter, flip);
|
2012-06-01 19:51:08 -03:00
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08: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) {
|
|
|
|
SDL_Unsupported();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!format) {
|
2012-08-09 14:14:41 -04:00
|
|
|
format = SDL_GetWindowPixelFormat(renderer->window);
|
2011-02-02 14:34:54 -08:00
|
|
|
}
|
|
|
|
|
2011-03-10 01:00:43 -08:00
|
|
|
real_rect.x = renderer->viewport.x;
|
|
|
|
real_rect.y = renderer->viewport.y;
|
2011-02-15 13:59:59 -08:00
|
|
|
real_rect.w = renderer->viewport.w;
|
|
|
|
real_rect.h = renderer->viewport.h;
|
2011-02-02 14:34:54 -08:00
|
|
|
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) {
|
2011-03-10 01:00:43 -08:00
|
|
|
int bpp = SDL_BYTESPERPIXEL(format);
|
2011-02-02 14:34:54 -08:00
|
|
|
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, );
|
|
|
|
|
2012-01-22 21:46:06 -05:00
|
|
|
/* Don't draw while we're hidden */
|
|
|
|
if (renderer->hidden) {
|
2011-11-07 23:07:00 -05:00
|
|
|
return;
|
|
|
|
}
|
2011-02-02 14:34:54 -08:00
|
|
|
renderer->RenderPresent(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SDL_DestroyTexture(SDL_Texture * texture)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
CHECK_TEXTURE_MAGIC(texture, );
|
|
|
|
texture->magic = NULL;
|
|
|
|
|
|
|
|
renderer = texture->renderer;
|
|
|
|
if (texture->next) {
|
|
|
|
texture->next->prev = texture->prev;
|
|
|
|
}
|
|
|
|
if (texture->prev) {
|
|
|
|
texture->prev->next = texture->next;
|
|
|
|
} else {
|
|
|
|
renderer->textures = texture->next;
|
|
|
|
}
|
|
|
|
|
2011-02-03 00:19:40 -08:00
|
|
|
if (texture->native) {
|
|
|
|
SDL_DestroyTexture(texture->native);
|
|
|
|
}
|
|
|
|
if (texture->yuv) {
|
|
|
|
SDL_SW_DestroyYUVTexture(texture->yuv);
|
|
|
|
}
|
|
|
|
if (texture->pixels) {
|
|
|
|
SDL_free(texture->pixels);
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
renderer->DestroyTexture(renderer, texture);
|
|
|
|
SDL_free(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SDL_DestroyRenderer(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
CHECK_RENDERER_MAGIC(renderer, );
|
|
|
|
|
|
|
|
SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
|
|
|
|
|
|
|
|
/* Free existing textures for this renderer */
|
|
|
|
while (renderer->textures) {
|
|
|
|
SDL_DestroyTexture(renderer->textures);
|
|
|
|
}
|
|
|
|
|
2012-09-06 21:34:52 -07:00
|
|
|
if (renderer->window) {
|
|
|
|
SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
|
|
|
|
}
|
2011-04-04 09:29:13 -07:00
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
/* It's no longer magical... */
|
|
|
|
renderer->magic = NULL;
|
|
|
|
|
|
|
|
/* Free the renderer instance */
|
|
|
|
renderer->DestroyRenderer(renderer);
|
|
|
|
}
|
|
|
|
|
2012-09-03 11:16:12 -03:00
|
|
|
int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
2012-09-03 11:54:48 -03:00
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
2012-09-03 11:16:12 -03:00
|
|
|
renderer = texture->renderer;
|
|
|
|
if (renderer && renderer->GL_BindTexture) {
|
|
|
|
return renderer->GL_BindTexture(renderer, texture, texw, texh);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Unsupported();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SDL_GL_UnbindTexture(SDL_Texture *texture)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
2012-09-03 11:54:48 -03:00
|
|
|
CHECK_TEXTURE_MAGIC(texture, -1);
|
2012-09-03 11:16:12 -03:00
|
|
|
renderer = texture->renderer;
|
|
|
|
if (renderer && renderer->GL_UnbindTexture) {
|
|
|
|
return renderer->GL_UnbindTexture(renderer, texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Unsupported();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|