src/render/SDL_render.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 Jul 2013 12:54:27 -0700
changeset 7496 13dfa871f0af
parent 7419 a22566f42281
child 7508 1f64c6874619
permissions -rw-r--r--
Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize driedfruit A trivial issue, the xrel and yrel values of MouseMotion event struct are not adjusted to renderer logical size.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6581
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
     7
  arising from the use of this software.
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    11
  freely, subject to the following restrictions:
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5528
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
/* The SDL 2D rendering system */
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
5192
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
    25
#include "SDL_hints.h"
5221
2ee8112bfc6b Added a simple log message API
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
    26
#include "SDL_log.h"
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "SDL_render.h"
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "SDL_sysrender.h"
5195
bb45ecd958d8 Renamed files for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5192
diff changeset
    29
#include "software/SDL_render_sw_c.h"
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
5528
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
    32
#define SDL_WINDOWRENDERDATA    "_SDL_WindowRenderData"
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
    33
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
#define CHECK_RENDERER_MAGIC(renderer, retval) \
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
    if (!renderer || renderer->magic != &renderer_magic) { \
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
        SDL_SetError("Invalid renderer"); \
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
        return retval; \
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
#define CHECK_TEXTURE_MAGIC(texture, retval) \
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
    if (!texture || texture->magic != &texture_magic) { \
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
        SDL_SetError("Invalid texture"); \
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
        return retval; \
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    47
#if !SDL_RENDER_DISABLED
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
static const SDL_RenderDriver *render_drivers[] = {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
#if SDL_VIDEO_RENDER_D3D
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    &D3D_RenderDriver,
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
#endif
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
#if SDL_VIDEO_RENDER_OGL
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
    &GL_RenderDriver,
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
#endif
5209
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5201
diff changeset
    55
#if SDL_VIDEO_RENDER_OGL_ES2
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5201
diff changeset
    56
    &GLES2_RenderDriver,
115fff0641ee Prefer the OpenGL ES 2.0 context when it's available, make it possible to create an OpenGL 2.0 context on iPhoneOS
Sam Lantinga <slouken@libsdl.org>
parents: 5201
diff changeset
    57
#endif
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
#if SDL_VIDEO_RENDER_OGL_ES
5201
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents: 5199
diff changeset
    59
    &GLES_RenderDriver,
523409574510 Added an OpenGL ES 2.0 renderer, contributed by itsnotabigtruck
Sam Lantinga <slouken@libsdl.org>
parents: 5199
diff changeset
    60
#endif
5199
164f20ba08eb Updated the DirectFB support, from Couriersud
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
    61
#if SDL_VIDEO_RENDER_DIRECTFB
164f20ba08eb Updated the DirectFB support, from Couriersud
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
    62
    &DirectFB_RenderDriver,
164f20ba08eb Updated the DirectFB support, from Couriersud
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
    63
#endif
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    64
#if SDL_VIDEO_RENDER_PSP
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    65
    &PSP_RenderDriver,
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    66
#endif
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
    &SW_RenderDriver
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    68
};
5226
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
    69
#endif /* !SDL_RENDER_DISABLED */
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    70
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
static char renderer_magic;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
static char texture_magic;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
6530
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
    74
static int UpdateLogicalSize(SDL_Renderer *renderer);
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
    75
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
SDL_GetNumRenderDrivers(void)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
{
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    79
#if !SDL_RENDER_DISABLED
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    return SDL_arraysize(render_drivers);
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    81
#else
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    82
    return 0;
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    83
#endif
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
{
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    89
#if !SDL_RENDER_DISABLED
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
    if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
    91
        return SDL_SetError("index must be in the range of 0 - %d",
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
    92
                            SDL_GetNumRenderDrivers() - 1);
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
    *info = render_drivers[index]->info;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
    return 0;
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    96
#else
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    97
    return SDL_SetError("SDL not built with rendering support");
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
    98
#endif
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
static int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
SDL_RendererEventWatch(void *userdata, SDL_Event *event)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
    SDL_Renderer *renderer = (SDL_Renderer *)userdata;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   106
    if (event->type == SDL_WINDOWEVENT) {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
        SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
        if (window == renderer->window) {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   109
            if (renderer->WindowEvent) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   110
                renderer->WindowEvent(renderer, &event->window);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   111
            }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   112
7240
39eb4958950a When the window is resized, the viewport is automatically reset.
Sam Lantinga <slouken@libsdl.org>
parents: 7239
diff changeset
   113
            if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
6530
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   114
                if (renderer->logical_w) {
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   115
                    UpdateLogicalSize(renderer);
7240
39eb4958950a When the window is resized, the viewport is automatically reset.
Sam Lantinga <slouken@libsdl.org>
parents: 7239
diff changeset
   116
                } else {
39eb4958950a When the window is resized, the viewport is automatically reset.
Sam Lantinga <slouken@libsdl.org>
parents: 7239
diff changeset
   117
                    /* Window was resized, reset viewport */
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   118
                    int w, h;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   119
6260
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   120
                    SDL_GetWindowSize(window, &w, &h);
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   121
                    if (renderer->target) {
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   122
                        renderer->viewport_backup.x = 0;
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   123
                        renderer->viewport_backup.y = 0;
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   124
                        renderer->viewport_backup.w = w;
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   125
                        renderer->viewport_backup.h = h;
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   126
                    } else {
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   127
                        renderer->viewport.x = 0;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   128
                        renderer->viewport.y = 0;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   129
                        renderer->viewport.w = w;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   130
                        renderer->viewport.h = h;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   131
                        renderer->UpdateViewport(renderer);
6260
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   132
                    }
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   133
                }
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   134
            } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   135
                renderer->hidden = SDL_TRUE;
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   136
            } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   137
                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   138
                    renderer->hidden = SDL_FALSE;
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   139
                }
6060
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   140
            } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
6260
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   141
                renderer->hidden = SDL_TRUE;
6060
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   142
            } else if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
6260
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   143
                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   144
                    renderer->hidden = SDL_FALSE;
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   145
                }
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   146
            }
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
        }
6530
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   148
    } else if (event->type == SDL_MOUSEMOTION) {
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   149
        if (renderer->logical_w) {
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   150
            event->motion.x -= renderer->viewport.x;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   151
            event->motion.y -= renderer->viewport.y;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   152
            event->motion.x = (int)(event->motion.x / renderer->scale.x);
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   153
            event->motion.y = (int)(event->motion.y / renderer->scale.y);
7496
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   154
            if (event->motion.xrel > 0) {
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   155
                event->motion.xrel = SDL_max(1, (int)(event->motion.xrel / renderer->scale.x));
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   156
            } else if (event->motion.xrel < 0) {
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   157
                event->motion.xrel = SDL_min(-1, (int)(event->motion.xrel / renderer->scale.x));
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   158
            }
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   159
            if (event->motion.yrel > 0) {
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   160
                event->motion.yrel = SDL_max(1, (int)(event->motion.yrel / renderer->scale.y));
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   161
            } else if (event->motion.yrel < 0) {
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   162
                event->motion.yrel = SDL_min(-1, (int)(event->motion.yrel / renderer->scale.y));
13dfa871f0af Fixed bug 1813 - MouseMotion relative values do not respect renderer LogicalSize
Sam Lantinga <slouken@libsdl.org>
parents: 7419
diff changeset
   163
            }
6530
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   164
        }
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   165
    } else if (event->type == SDL_MOUSEBUTTONDOWN ||
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   166
               event->type == SDL_MOUSEBUTTONUP) {
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   167
        if (renderer->logical_w) {
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   168
            event->button.x -= renderer->viewport.x;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   169
            event->button.y -= renderer->viewport.y;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   170
            event->button.x = (int)(event->button.x / renderer->scale.x);
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   171
            event->button.y = (int)(event->button.y / renderer->scale.y);
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   172
        }
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
    return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
6258
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   177
int
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   178
SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   179
                            SDL_Window **window, SDL_Renderer **renderer)
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   180
{
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   181
    *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   182
                                     SDL_WINDOWPOS_UNDEFINED,
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   183
                                     width, height, window_flags);
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   184
    if (!*window) {
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   185
        *renderer = NULL;
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   186
        return -1;
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   187
    }
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   188
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   189
    *renderer = SDL_CreateRenderer(*window, -1, 0);
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   190
    if (!*renderer) {
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   191
        return -1;
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   192
    }
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   193
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   194
    return 0;
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   195
}
fce7ce72677b Added a convenience function SDL_CreateWindowAndRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   196
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
SDL_Renderer *
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
{
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
   200
#if !SDL_RENDER_DISABLED
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
    SDL_Renderer *renderer = NULL;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
    int n = SDL_GetNumRenderDrivers();
5192
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   203
    const char *hint;
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   204
5528
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   205
    if (!window) {
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   206
        SDL_SetError("Invalid window");
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   207
        return NULL;
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   208
    }
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   209
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   210
    if (SDL_GetRenderer(window)) {
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   211
        SDL_SetError("Renderer already associated with window");
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   212
        return NULL;
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   213
    }
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   214
5192
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   215
    hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   216
    if (hint) {
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   217
        if (*hint == '0') {
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   218
            flags &= ~SDL_RENDERER_PRESENTVSYNC;
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   219
        } else {
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   220
            flags |= SDL_RENDERER_PRESENTVSYNC;
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   221
        }
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   222
    }
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
    if (index < 0) {
5192
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   225
        hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   226
        if (hint) {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
            for (index = 0; index < n; ++index) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
                const SDL_RenderDriver *driver = render_drivers[index];
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
5192
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   230
                if (SDL_strcasecmp(hint, driver->info.name) == 0) {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   231
                    /* Create a new renderer instance */
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   232
                    renderer = driver->CreateRenderer(window, flags);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
                    break;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
                }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
            }
5192
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   236
        }
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   237
ef5a61ea3202 Added the SDL_HINT_RENDER_DRIVER and SDL_HINT_RENDER_VSYNC hints.
Sam Lantinga <slouken@libsdl.org>
parents: 5180
diff changeset
   238
        if (!renderer) {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
            for (index = 0; index < n; ++index) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
                const SDL_RenderDriver *driver = render_drivers[index];
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
                if ((driver->info.flags & flags) == flags) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
                    /* Create a new renderer instance */
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   244
                    renderer = driver->CreateRenderer(window, flags);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
                    if (renderer) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   246
                        /* Yay, we got one! */
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   247
                        break;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
                    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
                }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
            }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
        }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
        if (index == n) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
            SDL_SetError("Couldn't find matching render driver");
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
            return NULL;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
        }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
    } else {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
        if (index >= SDL_GetNumRenderDrivers()) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
            SDL_SetError("index must be -1 or in the range of 0 - %d",
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
                         SDL_GetNumRenderDrivers() - 1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
            return NULL;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
        }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
        /* Create a new renderer instance */
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
        renderer = render_drivers[index]->CreateRenderer(window, flags);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
    if (renderer) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
        renderer->magic = &renderer_magic;
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
   268
        renderer->window = window;
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   269
        renderer->scale.x = 1.0f;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   270
        renderer->scale.y = 1.0f;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   271
6260
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   272
        if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   273
            renderer->hidden = SDL_TRUE;
6060
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   274
        } else {
6260
fd494c5f305b Fixed loading textures when the window starts hidden.
Sam Lantinga <slouken@libsdl.org>
parents: 6258
diff changeset
   275
            renderer->hidden = SDL_FALSE;
6060
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   276
        }
59d398ac031e Fixed SDL applications being killed immediately after being backgrounded, because they were trying to draw while minimized.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   277
5528
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   278
        SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   279
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   280
        SDL_RenderSetViewport(renderer, NULL);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   281
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
        SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
5221
2ee8112bfc6b Added a simple log message API
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   283
2ee8112bfc6b Added a simple log message API
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   284
        SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
2ee8112bfc6b Added a simple log message API
Sam Lantinga <slouken@libsdl.org>
parents: 5209
diff changeset
   285
                    "Created renderer: %s", renderer->info.name);
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
    return renderer;
7109
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
   288
#else
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
   289
    SDL_SetError("SDL not built with rendering support");
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
   290
    return NULL;
003d40e446f5 Fixed bug 1583 - Fix build for disabled SDL render subsystem
Sam Lantinga <slouken@libsdl.org>
parents: 7073
diff changeset
   291
#endif
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   293
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
   294
SDL_Renderer *
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
   295
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
   296
{
5226
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
   297
#if !SDL_RENDER_DISABLED
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   298
    SDL_Renderer *renderer;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   299
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   300
    renderer = SW_CreateRendererForSurface(surface);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   301
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   302
    if (renderer) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   303
        renderer->magic = &renderer_magic;
6532
58f726a2fc82 Initialized default scale for software renderer (thanks Marcus von Appen!)
Sam Lantinga <slouken@libsdl.org>
parents: 6531
diff changeset
   304
        renderer->scale.x = 1.0f;
58f726a2fc82 Initialized default scale for software renderer (thanks Marcus von Appen!)
Sam Lantinga <slouken@libsdl.org>
parents: 6531
diff changeset
   305
        renderer->scale.y = 1.0f;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   306
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   307
        SDL_RenderSetViewport(renderer, NULL);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   308
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   309
    return renderer;
5226
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
   310
#else
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
   311
    SDL_SetError("SDL not built with rendering support");
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
   312
    return NULL;
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
   313
#endif /* !SDL_RENDER_DISABLED */
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
   314
}
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
   315
5528
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   316
SDL_Renderer *
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   317
SDL_GetRenderer(SDL_Window * window)
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   318
{
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   319
    return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   320
}
15c9c03a80cc Added SDL_GetRenderer()
Sam Lantinga <slouken@libsdl.org>
parents: 5464
diff changeset
   321
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   323
SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   324
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   325
    CHECK_RENDERER_MAGIC(renderer, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   326
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
    *info = renderer->info;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   328
    return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   329
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   330
7239
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   331
int
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   332
SDL_GetRendererOutputSize(SDL_Renderer * renderer, int *w, int *h)
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   333
{
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   334
    CHECK_RENDERER_MAGIC(renderer, -1);
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   335
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   336
    if (renderer->target) {
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   337
        return SDL_QueryTexture(renderer->target, NULL, NULL, w, h);
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   338
    } else if (renderer->window) {
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   339
        SDL_GetWindowSize(renderer->window, w, h);
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   340
        return 0;
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   341
    } else if (renderer->GetOutputSize) {
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   342
        return renderer->GetOutputSize(renderer, w, h);
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   343
    } else {
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   344
        /* This should never happen */
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   345
        SDL_SetError("Renderer doesn't support querying output size");
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   346
        return -1;
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   347
    }
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   348
}
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   349
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   350
static SDL_bool
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   351
IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   352
{
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   353
    Uint32 i;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   354
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   355
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   356
        if (renderer->info.texture_formats[i] == format) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   357
            return SDL_TRUE;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   358
        }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   359
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   360
    return SDL_FALSE;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   361
}
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   362
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   363
static Uint32
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   364
GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   365
{
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   366
    Uint32 i;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   367
5268
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   368
    if (SDL_ISPIXELFORMAT_FOURCC(format)) {
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   369
        /* Look for an exact match */
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   370
        for (i = 0; i < renderer->info.num_texture_formats; ++i) {
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   371
            if (renderer->info.texture_formats[i] == format) {
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   372
                return renderer->info.texture_formats[i];
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   373
            }
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   374
        }
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   375
    } else {
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   376
        SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   377
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   378
        /* We just want to match the first format that has the same channels */
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   379
        for (i = 0; i < renderer->info.num_texture_formats; ++i) {
5300
3f825465a14a Don't accidentally pick a YV12 format as the closest format.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
   380
            if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
3f825465a14a Don't accidentally pick a YV12 format as the closest format.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
   381
                SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
5268
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   382
                return renderer->info.texture_formats[i];
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   383
            }
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   384
        }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   385
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   386
    return renderer->info.texture_formats[0];
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   387
}
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   388
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   389
SDL_Texture *
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   390
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   391
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   392
    SDL_Texture *texture;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   394
    CHECK_RENDERER_MAGIC(renderer, NULL);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
5413
64c0e474c459 If you pass in texture format 0, it'll use the first supported texture format.
Sam Lantinga <slouken@libsdl.org>
parents: 5369
diff changeset
   396
    if (!format) {
64c0e474c459 If you pass in texture format 0, it'll use the first supported texture format.
Sam Lantinga <slouken@libsdl.org>
parents: 5369
diff changeset
   397
        format = renderer->info.texture_formats[0];
64c0e474c459 If you pass in texture format 0, it'll use the first supported texture format.
Sam Lantinga <slouken@libsdl.org>
parents: 5369
diff changeset
   398
    }
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   399
    if (SDL_ISPIXELFORMAT_INDEXED(format)) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   400
        SDL_SetError("Palettized textures are not supported");
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   401
        return NULL;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   402
    }
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   403
    if (w <= 0 || h <= 0) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   404
        SDL_SetError("Texture dimensions can't be 0");
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   405
        return NULL;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   406
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   407
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   408
    if (!texture) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   409
        SDL_OutOfMemory();
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   410
        return NULL;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   411
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   412
    texture->magic = &texture_magic;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
    texture->format = format;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   414
    texture->access = access;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   415
    texture->w = w;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   416
    texture->h = h;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   417
    texture->r = 255;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   418
    texture->g = 255;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   419
    texture->b = 255;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   420
    texture->a = 255;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   421
    texture->renderer = renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   422
    texture->next = renderer->textures;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   423
    if (renderer->textures) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   424
        renderer->textures->prev = texture;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
    renderer->textures = texture;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   428
    if (IsSupportedFormat(renderer, format)) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   429
        if (renderer->CreateTexture(renderer, texture) < 0) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   430
            SDL_DestroyTexture(texture);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   431
            return 0;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   432
        }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   433
    } else {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   434
        texture->native = SDL_CreateTexture(renderer,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   435
                                GetClosestSupportedFormat(renderer, format),
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   436
                                access, w, h);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   437
        if (!texture->native) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   438
            SDL_DestroyTexture(texture);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   439
            return NULL;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   440
        }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   441
6497
e844e2632149 Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction
Sam Lantinga <slouken@libsdl.org>
parents: 6417
diff changeset
   442
        /* Swap textures to have texture before texture->native in the list */
e844e2632149 Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction
Sam Lantinga <slouken@libsdl.org>
parents: 6417
diff changeset
   443
        texture->native->next = texture->next;
6533
c784245304dd Fixed texture list when swapping textures (thanks Drake Wilson!)
Sam Lantinga <slouken@libsdl.org>
parents: 6532
diff changeset
   444
        if (texture->native->next) {
c784245304dd Fixed texture list when swapping textures (thanks Drake Wilson!)
Sam Lantinga <slouken@libsdl.org>
parents: 6532
diff changeset
   445
            texture->native->next->prev = texture->native;
c784245304dd Fixed texture list when swapping textures (thanks Drake Wilson!)
Sam Lantinga <slouken@libsdl.org>
parents: 6532
diff changeset
   446
        }
6497
e844e2632149 Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction
Sam Lantinga <slouken@libsdl.org>
parents: 6417
diff changeset
   447
        texture->prev = texture->native->prev;
6533
c784245304dd Fixed texture list when swapping textures (thanks Drake Wilson!)
Sam Lantinga <slouken@libsdl.org>
parents: 6532
diff changeset
   448
        if (texture->prev) {
c784245304dd Fixed texture list when swapping textures (thanks Drake Wilson!)
Sam Lantinga <slouken@libsdl.org>
parents: 6532
diff changeset
   449
            texture->prev->next = texture;
c784245304dd Fixed texture list when swapping textures (thanks Drake Wilson!)
Sam Lantinga <slouken@libsdl.org>
parents: 6532
diff changeset
   450
        }
6497
e844e2632149 Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction
Sam Lantinga <slouken@libsdl.org>
parents: 6417
diff changeset
   451
        texture->native->prev = texture;
e844e2632149 Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction
Sam Lantinga <slouken@libsdl.org>
parents: 6417
diff changeset
   452
        texture->next = texture->native;
e844e2632149 Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction
Sam Lantinga <slouken@libsdl.org>
parents: 6417
diff changeset
   453
        renderer->textures = texture;
e844e2632149 Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction
Sam Lantinga <slouken@libsdl.org>
parents: 6417
diff changeset
   454
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   455
        if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   456
            texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   457
            if (!texture->yuv) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   458
                SDL_DestroyTexture(texture);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   459
                return NULL;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   460
            }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   461
        } else if (access == SDL_TEXTUREACCESS_STREAMING) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   462
            /* The pitch is 4 byte aligned */
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   463
            texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
5342
d4b691de3b8f Zero streaming textures to reduce confusion.
Sam Lantinga <slouken@libsdl.org>
parents: 5333
diff changeset
   464
            texture->pixels = SDL_calloc(1, texture->pitch * h);
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   465
            if (!texture->pixels) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   466
                SDL_DestroyTexture(texture);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   467
                return NULL;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   468
            }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   469
        }
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   470
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   471
    return texture;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   472
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   473
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   474
SDL_Texture *
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   475
SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   476
{
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   477
    const SDL_PixelFormat *fmt;
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   478
    SDL_bool needAlpha;
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   479
    Uint32 i;
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   480
    Uint32 format;
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   481
    SDL_Texture *texture;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   482
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   483
    CHECK_RENDERER_MAGIC(renderer, NULL);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   484
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   485
    if (!surface) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   486
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   487
        return NULL;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   488
    }
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   489
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   490
    /* See what the best texture format is */
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   491
    fmt = surface->format;
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   492
    if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   493
        needAlpha = SDL_TRUE;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   494
    } else {
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   495
        needAlpha = SDL_FALSE;
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   496
    }
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   497
    format = renderer->info.texture_formats[0];
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   498
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
5268
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   499
        if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
c5a04f3c3908 Fixed a crash caused by the 1.2 code path getting a YV12 texture. :)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   500
            SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   501
            format = renderer->info.texture_formats[i];
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   502
            break;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   503
        }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   504
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   505
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   506
    texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   507
                                surface->w, surface->h);
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   508
    if (!texture) {
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   509
        return NULL;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   510
    }
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   511
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5270
diff changeset
   512
    if (format == surface->format->format) {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   513
        if (SDL_MUSTLOCK(surface)) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   514
            SDL_LockSurface(surface);
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   515
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   516
            SDL_UnlockSurface(surface);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   517
        } else {
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   518
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   519
        }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   520
    } else {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   521
        SDL_PixelFormat *dst_fmt;
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   522
        SDL_Surface *temp = NULL;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   523
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   524
        /* Set up a destination surface for the texture update */
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   525
        dst_fmt = SDL_AllocFormat(format);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   526
        temp = SDL_ConvertSurface(surface, dst_fmt, 0);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5289
diff changeset
   527
        SDL_FreeFormat(dst_fmt);
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   528
        if (temp) {
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   529
            SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   530
            SDL_FreeSurface(temp);
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   531
        } else {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   532
            SDL_DestroyTexture(texture);
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
   533
            return NULL;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   534
        }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   535
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   536
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   537
    {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   538
        Uint8 r, g, b, a;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   539
        SDL_BlendMode blendMode;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   540
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   541
        SDL_GetSurfaceColorMod(surface, &r, &g, &b);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   542
        SDL_SetTextureColorMod(texture, r, g, b);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   543
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   544
        SDL_GetSurfaceAlphaMod(surface, &a);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   545
        SDL_SetTextureAlphaMod(texture, a);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   546
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   547
        if (SDL_GetColorKey(surface, NULL) == 0) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   548
            /* We converted to a texture with alpha format */
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   549
            SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   550
        } else {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   551
            SDL_GetSurfaceBlendMode(surface, &blendMode);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   552
            SDL_SetTextureBlendMode(texture, blendMode);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   553
        }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   554
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   555
    return texture;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   556
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   557
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   558
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   559
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   560
                 int *w, int *h)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   561
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   562
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   563
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   564
    if (format) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   565
        *format = texture->format;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   566
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   567
    if (access) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   568
        *access = texture->access;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   569
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   570
    if (w) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   571
        *w = texture->w;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   572
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   573
    if (h) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   574
        *h = texture->h;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   575
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   576
    return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   577
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   578
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   579
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   580
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   581
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   582
    SDL_Renderer *renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   583
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   584
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   585
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   586
    renderer = texture->renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   587
    if (r < 255 || g < 255 || b < 255) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   588
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   589
    } else {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   590
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   591
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   592
    texture->r = r;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   593
    texture->g = g;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   594
    texture->b = b;
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   595
    if (texture->native) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   596
        return SDL_SetTextureColorMod(texture->native, r, g, b);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   597
    } else if (renderer->SetTextureColorMod) {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   598
        return renderer->SetTextureColorMod(renderer, texture);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   599
    } else {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   600
        return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   601
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   602
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   603
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   604
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   605
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   606
                       Uint8 * b)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   607
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   608
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   609
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   610
    if (r) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   611
        *r = texture->r;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   612
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   613
    if (g) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   614
        *g = texture->g;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   615
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   616
    if (b) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   617
        *b = texture->b;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   618
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   619
    return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   620
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   621
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   622
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   623
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   624
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   625
    SDL_Renderer *renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   626
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   627
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   628
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   629
    renderer = texture->renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   630
    if (alpha < 255) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   631
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   632
    } else {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   633
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   634
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   635
    texture->a = alpha;
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   636
    if (texture->native) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   637
        return SDL_SetTextureAlphaMod(texture->native, alpha);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   638
    } else if (renderer->SetTextureAlphaMod) {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   639
        return renderer->SetTextureAlphaMod(renderer, texture);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   640
    } else {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   641
        return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   642
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   643
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   644
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   645
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   646
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   647
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   648
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   649
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   650
    if (alpha) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   651
        *alpha = texture->a;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   652
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   653
    return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   654
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   655
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   656
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   657
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   658
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   659
    SDL_Renderer *renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   660
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   661
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   662
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   663
    renderer = texture->renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   664
    texture->blendMode = blendMode;
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   665
    if (texture->native) {
5180
4d098d286794 SDL_SetTextureBlendMode broken, caught by Kai Sterker - Thanks!
Sam Lantinga <slouken@libsdl.org>
parents: 5166
diff changeset
   666
        return SDL_SetTextureBlendMode(texture->native, blendMode);
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   667
    } else if (renderer->SetTextureBlendMode) {
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   668
        return renderer->SetTextureBlendMode(renderer, texture);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   669
    } else {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   670
        return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   671
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   672
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   673
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   674
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   675
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   676
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   677
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   678
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   679
    if (blendMode) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   680
        *blendMode = texture->blendMode;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   681
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   682
    return 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   683
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   684
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   685
static int
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   686
SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   687
                     const void *pixels, int pitch)
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   688
{
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   689
    SDL_Texture *native = texture->native;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   690
    SDL_Rect full_rect;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   691
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   692
    if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   693
        return -1;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   694
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   695
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   696
    full_rect.x = 0;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   697
    full_rect.y = 0;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   698
    full_rect.w = texture->w;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   699
    full_rect.h = texture->h;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   700
    rect = &full_rect;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   701
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   702
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   703
        /* We can lock the texture and copy to it */
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   704
        void *native_pixels;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   705
        int native_pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   706
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   707
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   708
            return -1;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   709
        }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   710
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   711
                            rect->w, rect->h, native_pixels, native_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   712
        SDL_UnlockTexture(native);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   713
    } else {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   714
        /* Use a temporary buffer for updating */
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   715
        void *temp_pixels;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   716
        int temp_pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   717
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   718
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   719
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   720
        if (!temp_pixels) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   721
            return SDL_OutOfMemory();
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   722
        }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   723
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   724
                            rect->w, rect->h, temp_pixels, temp_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   725
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   726
        SDL_free(temp_pixels);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   727
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   728
    return 0;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   729
}
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   730
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   731
static int
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   732
SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   733
                        const void *pixels, int pitch)
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   734
{
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   735
    SDL_Texture *native = texture->native;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   736
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   737
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   738
        /* We can lock the texture and copy to it */
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   739
        void *native_pixels;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   740
        int native_pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   741
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   742
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   743
            return -1;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   744
        }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   745
        SDL_ConvertPixels(rect->w, rect->h,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   746
                          texture->format, pixels, pitch,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   747
                          native->format, native_pixels, native_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   748
        SDL_UnlockTexture(native);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   749
    } else {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   750
        /* Use a temporary buffer for updating */
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   751
        void *temp_pixels;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   752
        int temp_pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   753
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   754
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   755
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   756
        if (!temp_pixels) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   757
            return SDL_OutOfMemory();
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   758
        }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   759
        SDL_ConvertPixels(rect->w, rect->h,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   760
                          texture->format, pixels, pitch,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   761
                          native->format, temp_pixels, temp_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   762
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   763
        SDL_free(temp_pixels);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   764
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   765
    return 0;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   766
}
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   767
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   768
int
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   769
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   770
                  const void *pixels, int pitch)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   771
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   772
    SDL_Renderer *renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   773
    SDL_Rect full_rect;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   774
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   775
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   776
7409
8b5ae6ff6a49 Check the parameters to SDL_UpdateTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7339
diff changeset
   777
    if (!pixels) {
8b5ae6ff6a49 Check the parameters to SDL_UpdateTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7339
diff changeset
   778
        return SDL_InvalidParamError("pixels");
8b5ae6ff6a49 Check the parameters to SDL_UpdateTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7339
diff changeset
   779
    }
8b5ae6ff6a49 Check the parameters to SDL_UpdateTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7339
diff changeset
   780
    if (!pitch) {
8b5ae6ff6a49 Check the parameters to SDL_UpdateTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7339
diff changeset
   781
        return SDL_InvalidParamError("pitch");
8b5ae6ff6a49 Check the parameters to SDL_UpdateTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7339
diff changeset
   782
    }
8b5ae6ff6a49 Check the parameters to SDL_UpdateTexture()
Sam Lantinga <slouken@libsdl.org>
parents: 7339
diff changeset
   783
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   784
    if (!rect) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   785
        full_rect.x = 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   786
        full_rect.y = 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   787
        full_rect.w = texture->w;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   788
        full_rect.h = texture->h;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   789
        rect = &full_rect;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   790
    }
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   791
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   792
    if (texture->yuv) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   793
        return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   794
    } else if (texture->native) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   795
        return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   796
    } else {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   797
        renderer = texture->renderer;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   798
        return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   799
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   800
}
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   801
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   802
static int
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   803
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   804
                   void **pixels, int *pitch)
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   805
{
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   806
    return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   807
}
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   808
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   809
static int
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   810
SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   811
                      void **pixels, int *pitch)
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   812
{
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   813
    texture->locked_rect = *rect;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   814
    *pixels = (void *) ((Uint8 *) texture->pixels +
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   815
                        rect->y * texture->pitch +
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   816
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   817
    *pitch = texture->pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   818
    return 0;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   819
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   820
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   821
int
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   822
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   823
                void **pixels, int *pitch)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   824
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   825
    SDL_Renderer *renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   826
    SDL_Rect full_rect;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   827
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   828
    CHECK_TEXTURE_MAGIC(texture, -1);
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   829
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   830
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   831
        return SDL_SetError("SDL_LockTexture(): texture must be streaming");
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   832
    }
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   833
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   834
    if (!rect) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   835
        full_rect.x = 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   836
        full_rect.y = 0;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   837
        full_rect.w = texture->w;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   838
        full_rect.h = texture->h;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   839
        rect = &full_rect;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   840
    }
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   841
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   842
    if (texture->yuv) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   843
        return SDL_LockTextureYUV(texture, rect, pixels, pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   844
    } else if (texture->native) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   845
        return SDL_LockTextureNative(texture, rect, pixels, pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   846
    } else {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   847
        renderer = texture->renderer;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   848
        return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   849
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   850
}
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   851
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   852
static void
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   853
SDL_UnlockTextureYUV(SDL_Texture * texture)
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   854
{
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   855
    SDL_Texture *native = texture->native;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   856
    void *native_pixels;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   857
    int native_pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   858
    SDL_Rect rect;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   859
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   860
    rect.x = 0;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   861
    rect.y = 0;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   862
    rect.w = texture->w;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   863
    rect.h = texture->h;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   864
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   865
    if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   866
        return;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   867
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   868
    SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   869
                        rect.w, rect.h, native_pixels, native_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   870
    SDL_UnlockTexture(native);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   871
}
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   872
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   873
static void
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   874
SDL_UnlockTextureNative(SDL_Texture * texture)
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   875
{
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   876
    SDL_Texture *native = texture->native;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   877
    void *native_pixels;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   878
    int native_pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   879
    const SDL_Rect *rect = &texture->locked_rect;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   880
    const void* pixels = (void *) ((Uint8 *) texture->pixels +
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   881
                        rect->y * texture->pitch +
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   882
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   883
    int pitch = texture->pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   884
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   885
    if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   886
        return;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   887
    }
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   888
    SDL_ConvertPixels(rect->w, rect->h,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   889
                      texture->format, pixels, pitch,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   890
                      native->format, native_pixels, native_pitch);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   891
    SDL_UnlockTexture(native);
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   892
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   893
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   894
void
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   895
SDL_UnlockTexture(SDL_Texture * texture)
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   896
{
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   897
    SDL_Renderer *renderer;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   898
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   899
    CHECK_TEXTURE_MAGIC(texture, );
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   900
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   901
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   902
        return;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   903
    }
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   904
    if (texture->yuv) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   905
        SDL_UnlockTextureYUV(texture);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   906
    } else if (texture->native) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   907
        SDL_UnlockTextureNative(texture);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   908
    } else {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   909
        renderer = texture->renderer;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   910
        renderer->UnlockTexture(renderer, texture);
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   911
    }
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   912
}
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   913
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   914
SDL_bool
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   915
SDL_RenderTargetSupported(SDL_Renderer *renderer)
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   916
{
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6246
diff changeset
   917
    if (!renderer || !renderer->SetRenderTarget) {
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   918
        return SDL_FALSE;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   919
    }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   920
    return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   921
}
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   922
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   923
int
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6246
diff changeset
   924
SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   925
{
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   926
    if (!SDL_RenderTargetSupported(renderer)) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   927
        return SDL_Unsupported();
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   928
    }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   929
    if (texture == renderer->target) {
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   930
        /* Nothing to do! */
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   931
        return 0;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   932
    }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   933
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   934
    /* texture == NULL is valid and means reset the target to the window */
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   935
    if (texture) {
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   936
        CHECK_TEXTURE_MAGIC(texture, -1);
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   937
        if (renderer != texture->renderer) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   938
            return SDL_SetError("Texture was not created with this renderer");
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   939
        }
6337
3c91a153f398 Fixes #1523 by removing inconsistent use of texture->access
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6320
diff changeset
   940
        if (texture->access != SDL_TEXTUREACCESS_TARGET) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   941
            return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   942
        }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   943
        if (texture->native) {
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   944
            /* Always render to the native texture */
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   945
            texture = texture->native;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   946
        }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   947
    }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   948
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   949
    if (texture && !renderer->target) {
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   950
        /* Make a backup of the viewport */
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   951
        renderer->viewport_backup = renderer->viewport;
7141
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7109
diff changeset
   952
        renderer->clip_rect_backup = renderer->clip_rect;
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   953
        renderer->scale_backup = renderer->scale;
6581
008057dcc079 The logical size set for a render target is temporary and shouldn't conflict with the logical size set for a window.
Sam Lantinga <slouken@libsdl.org>
parents: 6579
diff changeset
   954
        renderer->logical_w_backup = renderer->logical_w;
008057dcc079 The logical size set for a render target is temporary and shouldn't conflict with the logical size set for a window.
Sam Lantinga <slouken@libsdl.org>
parents: 6579
diff changeset
   955
        renderer->logical_h_backup = renderer->logical_h;
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   956
    }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   957
    renderer->target = texture;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   958
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6246
diff changeset
   959
    if (renderer->SetRenderTarget(renderer, texture) < 0) {
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   960
        return -1;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   961
    }
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   962
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   963
    if (texture) {
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   964
        renderer->viewport.x = 0;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   965
        renderer->viewport.y = 0;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   966
        renderer->viewport.w = texture->w;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   967
        renderer->viewport.h = texture->h;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   968
        renderer->scale.x = 1.0f;
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   969
        renderer->scale.y = 1.0f;
7339
2d2fd56db9b7 Fixed the logical size for rendering to texture, thanks to Mason Wheeler.
Sam Lantinga <slouken@libsdl.org>
parents: 7318
diff changeset
   970
        renderer->logical_w = texture->w;
2d2fd56db9b7 Fixed the logical size for rendering to texture, thanks to Mason Wheeler.
Sam Lantinga <slouken@libsdl.org>
parents: 7318
diff changeset
   971
        renderer->logical_h = texture->h;
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   972
    } else {
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   973
        renderer->viewport = renderer->viewport_backup;
7141
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7109
diff changeset
   974
        renderer->clip_rect = renderer->clip_rect_backup;
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   975
        renderer->scale = renderer->scale_backup;
6581
008057dcc079 The logical size set for a render target is temporary and shouldn't conflict with the logical size set for a window.
Sam Lantinga <slouken@libsdl.org>
parents: 6579
diff changeset
   976
        renderer->logical_w = renderer->logical_w_backup;
008057dcc079 The logical size set for a render target is temporary and shouldn't conflict with the logical size set for a window.
Sam Lantinga <slouken@libsdl.org>
parents: 6579
diff changeset
   977
        renderer->logical_h = renderer->logical_h_backup;
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   978
    }
6528
e978048ced60 Added SDL_RenderSetScale() and SDL_RenderGetScale()
Sam Lantinga <slouken@libsdl.org>
parents: 6497
diff changeset
   979
    if (renderer->UpdateViewport(renderer) < 0) {
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   980
        return -1;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   981
    }
7141
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7109
diff changeset
   982
    if (renderer->UpdateClipRect(renderer) < 0) {
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7109
diff changeset
   983
        return -1;
e276777b4247 First pass on SDL render clip rect functionality
Sam Lantinga <slouken@libsdl.org>
parents: 7109
diff changeset
   984
    }
6246
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   985
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   986
    /* All set! */
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   987
    return 0;
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   988
}
c70ec935a4bb Reorganized the render target code, moving the viewport handling to the general code and adding software implementation.
Sam Lantinga <slouken@libsdl.org>
parents: 6237
diff changeset
   989
6578
77d70ec131bc Added SDL_GetRenderTarget() API function
Sam Lantinga <slouken@libsdl.org>
parents: 6546
diff changeset
   990
SDL_Texture *
77d70ec131bc Added SDL_GetRenderTarget() API function
Sam Lantinga <slouken@libsdl.org>
parents: 6546
diff changeset
   991
SDL_GetRenderTarget(SDL_Renderer *renderer)
77d70ec131bc Added SDL_GetRenderTarget() API function
Sam Lantinga <slouken@libsdl.org>
parents: 6546
diff changeset
   992
{
77d70ec131bc Added SDL_GetRenderTarget() API function
Sam Lantinga <slouken@libsdl.org>
parents: 6546
diff changeset
   993
    return renderer->target;
77d70ec131bc Added SDL_GetRenderTarget() API function
Sam Lantinga <slouken@libsdl.org>
parents: 6546
diff changeset
   994
}
77d70ec131bc Added SDL_GetRenderTarget() API function
Sam Lantinga <slouken@libsdl.org>
parents: 6546
diff changeset
   995
6530
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   996
static int
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   997
UpdateLogicalSize(SDL_Renderer *renderer)
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   998
{
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
   999
    int w, h;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1000
    float want_aspect;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1001
    float real_aspect;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1002
    float scale;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1003
    SDL_Rect viewport;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1004
7239
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
  1005
    if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
04dda95ba22c Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
  1006
        return -1;
6530
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1007
    }
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1008
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1009
    want_aspect = (float)renderer->logical_w / renderer->logical_h;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1010
    real_aspect = (float)w / h;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1011
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1012
    /* Clear the scale because we're setting viewport in output coordinates */
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1013
    SDL_RenderSetScale(renderer, 1.0f, 1.0f);
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1014
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1015
    if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1016
        /* The aspect ratios are the same, just scale appropriately */
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1017
        scale = (float)w / renderer->logical_w;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1018
        SDL_RenderSetViewport(renderer, NULL);
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1019
    } else if (want_aspect > real_aspect) {
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1020
        /* We want a wider aspect ratio than is available - letterbox it */
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1021
        scale = (float)w / renderer->logical_w;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1022
        viewport.x = 0;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1023
        viewport.w = w;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1024
        viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset
  1025
        viewport.y = (h - viewport.h) / 2;
ba5de88eab60 Added SDL_RenderSetLogicalSize() and SDL_RenderGetLogicalSize()
Sam Lantinga <slouken@libsdl.org>
parents: 6528
diff changeset