src/render/SDL_render.c
author Gabriel Jacobo <gabomdq@gmail.com>
Mon, 24 Feb 2014 18:57:22 -0300
changeset 8249 294f28074ecc
parent 8149 681eb46b8ac4
child 8583 fb2933ca805f
permissions -rw-r--r--
Fixes #2296 - SDL_GL_UnbindTexture segfaults (thanks Daniel Bünzli)
slouken@5154
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@5154
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5154
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5154
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5154
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@5154
    22
slouken@5154
    23
/* The SDL 2D rendering system */
slouken@5154
    24
slouken@7760
    25
#include "SDL_assert.h"
slouken@5192
    26
#include "SDL_hints.h"
slouken@5221
    27
#include "SDL_log.h"
slouken@5154
    28
#include "SDL_render.h"
slouken@5154
    29
#include "SDL_sysrender.h"
slouken@5195
    30
#include "software/SDL_render_sw_c.h"
slouken@5154
    31
slouken@5154
    32
slouken@5528
    33
#define SDL_WINDOWRENDERDATA    "_SDL_WindowRenderData"
slouken@5528
    34
slouken@5154
    35
#define CHECK_RENDERER_MAGIC(renderer, retval) \
slouken@5154
    36
    if (!renderer || renderer->magic != &renderer_magic) { \
slouken@5154
    37
        SDL_SetError("Invalid renderer"); \
slouken@5154
    38
        return retval; \
slouken@5154
    39
    }
slouken@5154
    40
slouken@5154
    41
#define CHECK_TEXTURE_MAGIC(texture, retval) \
slouken@5154
    42
    if (!texture || texture->magic != &texture_magic) { \
slouken@5154
    43
        SDL_SetError("Invalid texture"); \
slouken@5154
    44
        return retval; \
slouken@5154
    45
    }
slouken@5154
    46
slouken@5154
    47
slouken@7109
    48
#if !SDL_RENDER_DISABLED
slouken@5154
    49
static const SDL_RenderDriver *render_drivers[] = {
slouken@5154
    50
#if SDL_VIDEO_RENDER_D3D
slouken@5154
    51
    &D3D_RenderDriver,
slouken@5154
    52
#endif
slouken@5154
    53
#if SDL_VIDEO_RENDER_OGL
slouken@5154
    54
    &GL_RenderDriver,
slouken@5154
    55
#endif
slouken@5209
    56
#if SDL_VIDEO_RENDER_OGL_ES2
slouken@5209
    57
    &GLES2_RenderDriver,
slouken@5209
    58
#endif
slouken@5154
    59
#if SDL_VIDEO_RENDER_OGL_ES
slouken@5201
    60
    &GLES_RenderDriver,
slouken@5201
    61
#endif
slouken@5199
    62
#if SDL_VIDEO_RENDER_DIRECTFB
slouken@5199
    63
    &DirectFB_RenderDriver,
slouken@5199
    64
#endif
kimonline@7009
    65
#if SDL_VIDEO_RENDER_PSP
kimonline@7009
    66
    &PSP_RenderDriver,
kimonline@7009
    67
#endif
slouken@5154
    68
    &SW_RenderDriver
slouken@7109
    69
};
slouken@5226
    70
#endif /* !SDL_RENDER_DISABLED */
slouken@7109
    71
slouken@5154
    72
static char renderer_magic;
slouken@5154
    73
static char texture_magic;
slouken@5154
    74
slouken@6530
    75
static int UpdateLogicalSize(SDL_Renderer *renderer);
slouken@6530
    76
slouken@5154
    77
int
slouken@5154
    78
SDL_GetNumRenderDrivers(void)
slouken@5154
    79
{
slouken@7109
    80
#if !SDL_RENDER_DISABLED
slouken@5154
    81
    return SDL_arraysize(render_drivers);
slouken@7109
    82
#else
slouken@7109
    83
    return 0;
slouken@7109
    84
#endif
slouken@5154
    85
}
slouken@5154
    86
slouken@5154
    87
int
slouken@5154
    88
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
slouken@5154
    89
{
slouken@7109
    90
#if !SDL_RENDER_DISABLED
slouken@5154
    91
    if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
icculus@7037
    92
        return SDL_SetError("index must be in the range of 0 - %d",
icculus@7037
    93
                            SDL_GetNumRenderDrivers() - 1);
slouken@5154
    94
    }
slouken@5154
    95
    *info = render_drivers[index]->info;
slouken@5154
    96
    return 0;
slouken@7109
    97
#else
slouken@7109
    98
    return SDL_SetError("SDL not built with rendering support");
slouken@7109
    99
#endif
slouken@5154
   100
}
slouken@5154
   101
slouken@5154
   102
static int
slouken@5154
   103
SDL_RendererEventWatch(void *userdata, SDL_Event *event)
slouken@5154
   104
{
slouken@5154
   105
    SDL_Renderer *renderer = (SDL_Renderer *)userdata;
slouken@5154
   106
slouken@5297
   107
    if (event->type == SDL_WINDOWEVENT) {
slouken@5154
   108
        SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
slouken@5154
   109
        if (window == renderer->window) {
slouken@5297
   110
            if (renderer->WindowEvent) {
slouken@5297
   111
                renderer->WindowEvent(renderer, &event->window);
slouken@5297
   112
            }
slouken@5297
   113
slouken@7240
   114
            if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@6530
   115
                if (renderer->logical_w) {
slouken@6530
   116
                    UpdateLogicalSize(renderer);
slouken@7240
   117
                } else {
slouken@7240
   118
                    /* Window was resized, reset viewport */
slouken@6528
   119
                    int w, h;
slouken@6528
   120
urkle@7746
   121
                    if (renderer->GetOutputSize) {
urkle@7746
   122
                        renderer->GetOutputSize(renderer, &w, &h);
urkle@7746
   123
                    } else {
urkle@7746
   124
                        SDL_GetWindowSize(renderer->window, &w, &h);
urkle@7746
   125
                    }
urkle@7746
   126
slouken@6260
   127
                    if (renderer->target) {
slouken@6260
   128
                        renderer->viewport_backup.x = 0;
slouken@6260
   129
                        renderer->viewport_backup.y = 0;
slouken@6260
   130
                        renderer->viewport_backup.w = w;
slouken@6260
   131
                        renderer->viewport_backup.h = h;
slouken@6260
   132
                    } else {
slouken@6528
   133
                        renderer->viewport.x = 0;
slouken@6528
   134
                        renderer->viewport.y = 0;
slouken@6528
   135
                        renderer->viewport.w = w;
slouken@6528
   136
                        renderer->viewport.h = h;
slouken@6528
   137
                        renderer->UpdateViewport(renderer);
slouken@6260
   138
                    }
slouken@6260
   139
                }
slouken@6260
   140
            } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
slouken@6260
   141
                renderer->hidden = SDL_TRUE;
slouken@6260
   142
            } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
slouken@6260
   143
                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
slouken@6260
   144
                    renderer->hidden = SDL_FALSE;
slouken@6260
   145
                }
slouken@6060
   146
            } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
slouken@6260
   147
                renderer->hidden = SDL_TRUE;
slouken@6060
   148
            } else if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
slouken@6260
   149
                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
slouken@6260
   150
                    renderer->hidden = SDL_FALSE;
slouken@6260
   151
                }
slouken@5297
   152
            }
slouken@5154
   153
        }
slouken@6530
   154
    } else if (event->type == SDL_MOUSEMOTION) {
slouken@6530
   155
        if (renderer->logical_w) {
slouken@6530
   156
            event->motion.x -= renderer->viewport.x;
slouken@6530
   157
            event->motion.y -= renderer->viewport.y;
slouken@6530
   158
            event->motion.x = (int)(event->motion.x / renderer->scale.x);
slouken@6530
   159
            event->motion.y = (int)(event->motion.y / renderer->scale.y);
slouken@7496
   160
            if (event->motion.xrel > 0) {
slouken@7496
   161
                event->motion.xrel = SDL_max(1, (int)(event->motion.xrel / renderer->scale.x));
slouken@7496
   162
            } else if (event->motion.xrel < 0) {
slouken@7496
   163
                event->motion.xrel = SDL_min(-1, (int)(event->motion.xrel / renderer->scale.x));
slouken@7496
   164
            }
slouken@7496
   165
            if (event->motion.yrel > 0) {
slouken@7496
   166
                event->motion.yrel = SDL_max(1, (int)(event->motion.yrel / renderer->scale.y));
slouken@7496
   167
            } else if (event->motion.yrel < 0) {
slouken@7496
   168
                event->motion.yrel = SDL_min(-1, (int)(event->motion.yrel / renderer->scale.y));
slouken@7496
   169
            }
slouken@6530
   170
        }
slouken@6530
   171
    } else if (event->type == SDL_MOUSEBUTTONDOWN ||
slouken@6530
   172
               event->type == SDL_MOUSEBUTTONUP) {
slouken@6530
   173
        if (renderer->logical_w) {
slouken@6530
   174
            event->button.x -= renderer->viewport.x;
slouken@6530
   175
            event->button.y -= renderer->viewport.y;
slouken@6530
   176
            event->button.x = (int)(event->button.x / renderer->scale.x);
slouken@6530
   177
            event->button.y = (int)(event->button.y / renderer->scale.y);
slouken@6530
   178
        }
slouken@5154
   179
    }
slouken@5154
   180
    return 0;
slouken@5154
   181
}
slouken@5154
   182
slouken@6258
   183
int
slouken@6258
   184
SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
slouken@6258
   185
                            SDL_Window **window, SDL_Renderer **renderer)
slouken@6258
   186
{
slouken@6258
   187
    *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
slouken@6258
   188
                                     SDL_WINDOWPOS_UNDEFINED,
slouken@6258
   189
                                     width, height, window_flags);
slouken@6258
   190
    if (!*window) {
slouken@6258
   191
        *renderer = NULL;
slouken@6258
   192
        return -1;
slouken@6258
   193
    }
slouken@6258
   194
slouken@6258
   195
    *renderer = SDL_CreateRenderer(*window, -1, 0);
slouken@6258
   196
    if (!*renderer) {
slouken@6258
   197
        return -1;
slouken@6258
   198
    }
slouken@6258
   199
slouken@6258
   200
    return 0;
slouken@6258
   201
}
slouken@6258
   202
slouken@5154
   203
SDL_Renderer *
slouken@5154
   204
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
slouken@5154
   205
{
slouken@7109
   206
#if !SDL_RENDER_DISABLED
slouken@5154
   207
    SDL_Renderer *renderer = NULL;
slouken@5154
   208
    int n = SDL_GetNumRenderDrivers();
slouken@5192
   209
    const char *hint;
slouken@5192
   210
slouken@5528
   211
    if (!window) {
slouken@5528
   212
        SDL_SetError("Invalid window");
slouken@5528
   213
        return NULL;
slouken@5528
   214
    }
slouken@5528
   215
slouken@5528
   216
    if (SDL_GetRenderer(window)) {
slouken@5528
   217
        SDL_SetError("Renderer already associated with window");
slouken@5528
   218
        return NULL;
slouken@5528
   219
    }
slouken@5528
   220
slouken@5192
   221
    hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
slouken@5192
   222
    if (hint) {
slouken@5192
   223
        if (*hint == '0') {
slouken@5192
   224
            flags &= ~SDL_RENDERER_PRESENTVSYNC;
slouken@5192
   225
        } else {
slouken@5192
   226
            flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@5192
   227
        }
slouken@5192
   228
    }
slouken@5154
   229
slouken@5154
   230
    if (index < 0) {
slouken@5192
   231
        hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
slouken@5192
   232
        if (hint) {
slouken@5154
   233
            for (index = 0; index < n; ++index) {
slouken@5154
   234
                const SDL_RenderDriver *driver = render_drivers[index];
slouken@5154
   235
slouken@5192
   236
                if (SDL_strcasecmp(hint, driver->info.name) == 0) {
slouken@5154
   237
                    /* Create a new renderer instance */
slouken@5154
   238
                    renderer = driver->CreateRenderer(window, flags);
slouken@5154
   239
                    break;
slouken@5154
   240
                }
slouken@5154
   241
            }
slouken@5192
   242
        }
slouken@5192
   243
slouken@5192
   244
        if (!renderer) {
slouken@5154
   245
            for (index = 0; index < n; ++index) {
slouken@5154
   246
                const SDL_RenderDriver *driver = render_drivers[index];
slouken@5154
   247
slouken@5154
   248
                if ((driver->info.flags & flags) == flags) {
slouken@5154
   249
                    /* Create a new renderer instance */
slouken@5154
   250
                    renderer = driver->CreateRenderer(window, flags);
slouken@5154
   251
                    if (renderer) {
slouken@5154
   252
                        /* Yay, we got one! */
slouken@5154
   253
                        break;
slouken@5154
   254
                    }
slouken@5154
   255
                }
slouken@5154
   256
            }
slouken@5154
   257
        }
slouken@5154
   258
        if (index == n) {
slouken@5154
   259
            SDL_SetError("Couldn't find matching render driver");
slouken@5154
   260
            return NULL;
slouken@5154
   261
        }
slouken@5154
   262
    } else {
slouken@5154
   263
        if (index >= SDL_GetNumRenderDrivers()) {
slouken@5154
   264
            SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@5154
   265
                         SDL_GetNumRenderDrivers() - 1);
slouken@5154
   266
            return NULL;
slouken@5154
   267
        }
slouken@5154
   268
        /* Create a new renderer instance */
slouken@5154
   269
        renderer = render_drivers[index]->CreateRenderer(window, flags);
slouken@5154
   270
    }
slouken@5154
   271
slouken@5154
   272
    if (renderer) {
slouken@5154
   273
        renderer->magic = &renderer_magic;
slouken@5166
   274
        renderer->window = window;
slouken@6528
   275
        renderer->scale.x = 1.0f;
slouken@6528
   276
        renderer->scale.y = 1.0f;
slouken@5154
   277
slouken@6260
   278
        if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
slouken@6260
   279
            renderer->hidden = SDL_TRUE;
slouken@6060
   280
        } else {
slouken@6260
   281
            renderer->hidden = SDL_FALSE;
slouken@6060
   282
        }
slouken@6060
   283
slouken@5528
   284
        SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
slouken@5528
   285
slouken@5297
   286
        SDL_RenderSetViewport(renderer, NULL);
slouken@5297
   287
slouken@5154
   288
        SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
slouken@5221
   289
slouken@5221
   290
        SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
slouken@5221
   291
                    "Created renderer: %s", renderer->info.name);
slouken@5154
   292
    }
slouken@5154
   293
    return renderer;
slouken@7109
   294
#else
slouken@7109
   295
    SDL_SetError("SDL not built with rendering support");
slouken@7109
   296
    return NULL;
slouken@7109
   297
#endif
slouken@5154
   298
}
slouken@5154
   299
slouken@5166
   300
SDL_Renderer *
slouken@5166
   301
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
slouken@5166
   302
{
slouken@5226
   303
#if !SDL_RENDER_DISABLED
slouken@5297
   304
    SDL_Renderer *renderer;
slouken@5297
   305
slouken@5297
   306
    renderer = SW_CreateRendererForSurface(surface);
slouken@5297
   307
slouken@5297
   308
    if (renderer) {
slouken@5297
   309
        renderer->magic = &renderer_magic;
slouken@6532
   310
        renderer->scale.x = 1.0f;
slouken@6532
   311
        renderer->scale.y = 1.0f;
slouken@5297
   312
slouken@5297
   313
        SDL_RenderSetViewport(renderer, NULL);
slouken@5297
   314
    }
slouken@5297
   315
    return renderer;
slouken@5226
   316
#else
slouken@5226
   317
    SDL_SetError("SDL not built with rendering support");
slouken@5226
   318
    return NULL;
slouken@5226
   319
#endif /* !SDL_RENDER_DISABLED */
slouken@5166
   320
}
slouken@5166
   321
slouken@5528
   322
SDL_Renderer *
slouken@5528
   323
SDL_GetRenderer(SDL_Window * window)
slouken@5528
   324
{
slouken@5528
   325
    return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
slouken@5528
   326
}
slouken@5528
   327
slouken@5154
   328
int
slouken@5154
   329
SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
slouken@5154
   330
{
slouken@5154
   331
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   332
slouken@5154
   333
    *info = renderer->info;
slouken@5154
   334
    return 0;
slouken@5154
   335
}
slouken@5154
   336
slouken@7239
   337
int
slouken@7239
   338
SDL_GetRendererOutputSize(SDL_Renderer * renderer, int *w, int *h)
slouken@7239
   339
{
slouken@7239
   340
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@7239
   341
slouken@7239
   342
    if (renderer->target) {
slouken@7239
   343
        return SDL_QueryTexture(renderer->target, NULL, NULL, w, h);
urkle@7746
   344
    } else if (renderer->GetOutputSize) {
urkle@7746
   345
        return renderer->GetOutputSize(renderer, w, h);
slouken@7239
   346
    } else if (renderer->window) {
slouken@7239
   347
        SDL_GetWindowSize(renderer->window, w, h);
slouken@7239
   348
        return 0;
slouken@7239
   349
    } else {
slouken@7239
   350
        /* This should never happen */
philipp@7898
   351
        return SDL_SetError("Renderer doesn't support querying output size");
slouken@7239
   352
    }
slouken@7239
   353
}
slouken@7239
   354
slouken@5156
   355
static SDL_bool
slouken@5156
   356
IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
slouken@5156
   357
{
slouken@5156
   358
    Uint32 i;
slouken@5156
   359
slouken@5156
   360
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5156
   361
        if (renderer->info.texture_formats[i] == format) {
slouken@5156
   362
            return SDL_TRUE;
slouken@5156
   363
        }
slouken@5156
   364
    }
slouken@5156
   365
    return SDL_FALSE;
slouken@5156
   366
}
slouken@5156
   367
slouken@5156
   368
static Uint32
slouken@5156
   369
GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
slouken@5156
   370
{
slouken@5156
   371
    Uint32 i;
slouken@5156
   372
slouken@5268
   373
    if (SDL_ISPIXELFORMAT_FOURCC(format)) {
slouken@5268
   374
        /* Look for an exact match */
slouken@5268
   375
        for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5268
   376
            if (renderer->info.texture_formats[i] == format) {
slouken@5268
   377
                return renderer->info.texture_formats[i];
slouken@5268
   378
            }
slouken@5268
   379
        }
slouken@5268
   380
    } else {
slouken@5268
   381
        SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
slouken@5268
   382
slouken@5268
   383
        /* We just want to match the first format that has the same channels */
slouken@5268
   384
        for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5300
   385
            if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
slouken@5300
   386
                SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
slouken@5268
   387
                return renderer->info.texture_formats[i];
slouken@5268
   388
            }
slouken@5156
   389
        }
slouken@5156
   390
    }
slouken@5156
   391
    return renderer->info.texture_formats[0];
slouken@5156
   392
}
slouken@5156
   393
slouken@5154
   394
SDL_Texture *
slouken@5154
   395
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
slouken@5154
   396
{
slouken@5154
   397
    SDL_Texture *texture;
slouken@5154
   398
slouken@5154
   399
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   400
slouken@5413
   401
    if (!format) {
slouken@5413
   402
        format = renderer->info.texture_formats[0];
slouken@5413
   403
    }
slouken@5156
   404
    if (SDL_ISPIXELFORMAT_INDEXED(format)) {
slouken@5156
   405
        SDL_SetError("Palettized textures are not supported");
slouken@5156
   406
        return NULL;
slouken@5156
   407
    }
slouken@5154
   408
    if (w <= 0 || h <= 0) {
slouken@5154
   409
        SDL_SetError("Texture dimensions can't be 0");
slouken@5156
   410
        return NULL;
slouken@5154
   411
    }
slouken@7770
   412
    if ((renderer->info.max_texture_width && w > renderer->info.max_texture_width) ||
slouken@7770
   413
        (renderer->info.max_texture_height && h > renderer->info.max_texture_height)) {
slouken@7770
   414
        SDL_SetError("Texture dimensions are limited to %dx%d", renderer->info.max_texture_width, renderer->info.max_texture_height);
slouken@7770
   415
        return NULL;
slouken@7770
   416
    }
slouken@5154
   417
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@5154
   418
    if (!texture) {
slouken@5154
   419
        SDL_OutOfMemory();
slouken@5156
   420
        return NULL;
slouken@5154
   421
    }
slouken@5154
   422
    texture->magic = &texture_magic;
slouken@5154
   423
    texture->format = format;
slouken@5154
   424
    texture->access = access;
slouken@5154
   425
    texture->w = w;
slouken@5154
   426
    texture->h = h;
slouken@5154
   427
    texture->r = 255;
slouken@5154
   428
    texture->g = 255;
slouken@5154
   429
    texture->b = 255;
slouken@5154
   430
    texture->a = 255;
slouken@5154
   431
    texture->renderer = renderer;
slouken@5154
   432
    texture->next = renderer->textures;
slouken@5154
   433
    if (renderer->textures) {
slouken@5154
   434
        renderer->textures->prev = texture;
slouken@5154
   435
    }
slouken@5154
   436
    renderer->textures = texture;
slouken@5154
   437
slouken@5156
   438
    if (IsSupportedFormat(renderer, format)) {
slouken@5156
   439
        if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@5156
   440
            SDL_DestroyTexture(texture);
slouken@5156
   441
            return 0;
slouken@5156
   442
        }
slouken@5156
   443
    } else {
slouken@5156
   444
        texture->native = SDL_CreateTexture(renderer,
slouken@5156
   445
                                GetClosestSupportedFormat(renderer, format),
slouken@5156
   446
                                access, w, h);
slouken@5156
   447
        if (!texture->native) {
slouken@5156
   448
            SDL_DestroyTexture(texture);
slouken@5156
   449
            return NULL;
slouken@5156
   450
        }
slouken@5156
   451
slouken@6497
   452
        /* Swap textures to have texture before texture->native in the list */
slouken@6497
   453
        texture->native->next = texture->next;
slouken@6533
   454
        if (texture->native->next) {
slouken@6533
   455
            texture->native->next->prev = texture->native;
slouken@6533
   456
        }
slouken@6497
   457
        texture->prev = texture->native->prev;
slouken@6533
   458
        if (texture->prev) {
slouken@6533
   459
            texture->prev->next = texture;
slouken@6533
   460
        }
slouken@6497
   461
        texture->native->prev = texture;
slouken@6497
   462
        texture->next = texture->native;
slouken@6497
   463
        renderer->textures = texture;
slouken@6497
   464
slouken@5156
   465
        if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@5156
   466
            texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
slouken@5156
   467
            if (!texture->yuv) {
slouken@5156
   468
                SDL_DestroyTexture(texture);
slouken@5156
   469
                return NULL;
slouken@5156
   470
            }
slouken@5156
   471
        } else if (access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   472
            /* The pitch is 4 byte aligned */
slouken@5156
   473
            texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
slouken@5342
   474
            texture->pixels = SDL_calloc(1, texture->pitch * h);
slouken@5156
   475
            if (!texture->pixels) {
slouken@5156
   476
                SDL_DestroyTexture(texture);
slouken@5156
   477
                return NULL;
slouken@5156
   478
            }
slouken@5156
   479
        }
slouken@5154
   480
    }
slouken@5154
   481
    return texture;
slouken@5154
   482
}
slouken@5154
   483
slouken@5154
   484
SDL_Texture *
slouken@5158
   485
SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
slouken@5154
   486
{
slouken@5158
   487
    const SDL_PixelFormat *fmt;
slouken@5158
   488
    SDL_bool needAlpha;
slouken@5158
   489
    Uint32 i;
slouken@5158
   490
    Uint32 format;
slouken@5158
   491
    SDL_Texture *texture;
slouken@5154
   492
slouken@5154
   493
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   494
slouken@5154
   495
    if (!surface) {
slouken@5154
   496
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@5154
   497
        return NULL;
slouken@5154
   498
    }
slouken@5158
   499
slouken@5158
   500
    /* See what the best texture format is */
slouken@5154
   501
    fmt = surface->format;
slouken@5158
   502
    if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
slouken@5158
   503
        needAlpha = SDL_TRUE;
slouken@5154
   504
    } else {
slouken@5158
   505
        needAlpha = SDL_FALSE;
slouken@5158
   506
    }
slouken@5158
   507
    format = renderer->info.texture_formats[0];
slouken@5158
   508
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5268
   509
        if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
slouken@5268
   510
            SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
slouken@5158
   511
            format = renderer->info.texture_formats[i];
slouken@5158
   512
            break;
slouken@5154
   513
        }
slouken@5154
   514
    }
slouken@5154
   515
slouken@5158
   516
    texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
slouken@5158
   517
                                surface->w, surface->h);
slouken@5154
   518
    if (!texture) {
slouken@5158
   519
        return NULL;
slouken@5154
   520
    }
slouken@5158
   521
slouken@5288
   522
    if (format == surface->format->format) {
slouken@5154
   523
        if (SDL_MUSTLOCK(surface)) {
slouken@5154
   524
            SDL_LockSurface(surface);
slouken@5158
   525
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
slouken@5154
   526
            SDL_UnlockSurface(surface);
slouken@5154
   527
        } else {
slouken@5158
   528
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
slouken@5154
   529
        }
slouken@5154
   530
    } else {
slouken@5297
   531
        SDL_PixelFormat *dst_fmt;
slouken@5158
   532
        SDL_Surface *temp = NULL;
slouken@5154
   533
slouken@5154
   534
        /* Set up a destination surface for the texture update */
slouken@5297
   535
        dst_fmt = SDL_AllocFormat(format);
slouken@5297
   536
        temp = SDL_ConvertSurface(surface, dst_fmt, 0);
slouken@5297
   537
        SDL_FreeFormat(dst_fmt);
slouken@5158
   538
        if (temp) {
slouken@5158
   539
            SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
slouken@5158
   540
            SDL_FreeSurface(temp);
slouken@5158
   541
        } else {
slouken@5154
   542
            SDL_DestroyTexture(texture);
slouken@5158
   543
            return NULL;
slouken@5154
   544
        }
slouken@5154
   545
    }
slouken@5154
   546
slouken@5154
   547
    {
slouken@5154
   548
        Uint8 r, g, b, a;
slouken@5154
   549
        SDL_BlendMode blendMode;
slouken@5154
   550
slouken@5154
   551
        SDL_GetSurfaceColorMod(surface, &r, &g, &b);
slouken@5154
   552
        SDL_SetTextureColorMod(texture, r, g, b);
slouken@5154
   553
slouken@5154
   554
        SDL_GetSurfaceAlphaMod(surface, &a);
slouken@5154
   555
        SDL_SetTextureAlphaMod(texture, a);
slouken@5154
   556
slouken@5154
   557
        if (SDL_GetColorKey(surface, NULL) == 0) {
slouken@5154
   558
            /* We converted to a texture with alpha format */
slouken@5154
   559
            SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
slouken@5154
   560
        } else {
slouken@5154
   561
            SDL_GetSurfaceBlendMode(surface, &blendMode);
slouken@5154
   562
            SDL_SetTextureBlendMode(texture, blendMode);
slouken@5154
   563
        }
slouken@5154
   564
    }
slouken@5154
   565
    return texture;
slouken@5154
   566
}
slouken@5154
   567
slouken@5154
   568
int
slouken@5154
   569
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
slouken@5154
   570
                 int *w, int *h)
slouken@5154
   571
{
slouken@5154
   572
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   573
slouken@5154
   574
    if (format) {
slouken@5154
   575
        *format = texture->format;
slouken@5154
   576
    }
slouken@5154
   577
    if (access) {
slouken@5154
   578
        *access = texture->access;
slouken@5154
   579
    }
slouken@5154
   580
    if (w) {
slouken@5154
   581
        *w = texture->w;
slouken@5154
   582
    }
slouken@5154
   583
    if (h) {
slouken@5154
   584
        *h = texture->h;
slouken@5154
   585
    }
slouken@5154
   586
    return 0;
slouken@5154
   587
}
slouken@5154
   588
slouken@5154
   589
int
slouken@5154
   590
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
slouken@5154
   591
{
slouken@5154
   592
    SDL_Renderer *renderer;
slouken@5154
   593
slouken@5154
   594
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   595
slouken@5154
   596
    renderer = texture->renderer;
slouken@5154
   597
    if (r < 255 || g < 255 || b < 255) {
slouken@5154
   598
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   599
    } else {
slouken@5154
   600
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   601
    }
slouken@5154
   602
    texture->r = r;
slouken@5154
   603
    texture->g = g;
slouken@5154
   604
    texture->b = b;
slouken@5156
   605
    if (texture->native) {
slouken@5156
   606
        return SDL_SetTextureColorMod(texture->native, r, g, b);
slouken@5156
   607
    } else if (renderer->SetTextureColorMod) {
slouken@5154
   608
        return renderer->SetTextureColorMod(renderer, texture);
slouken@5154
   609
    } else {
slouken@5154
   610
        return 0;
slouken@5154
   611
    }
slouken@5154
   612
}
slouken@5154
   613
slouken@5154
   614
int
slouken@5154
   615
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
slouken@5154
   616
                       Uint8 * b)
slouken@5154
   617
{
slouken@5154
   618
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   619
slouken@5154
   620
    if (r) {
slouken@5154
   621
        *r = texture->r;
slouken@5154
   622
    }
slouken@5154
   623
    if (g) {
slouken@5154
   624
        *g = texture->g;
slouken@5154
   625
    }
slouken@5154
   626
    if (b) {
slouken@5154
   627
        *b = texture->b;
slouken@5154
   628
    }
slouken@5154
   629
    return 0;
slouken@5154
   630
}
slouken@5154
   631
slouken@5154
   632
int
slouken@5154
   633
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
slouken@5154
   634
{
slouken@5154
   635
    SDL_Renderer *renderer;
slouken@5154
   636
slouken@5154
   637
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   638
slouken@5154
   639
    renderer = texture->renderer;
slouken@5154
   640
    if (alpha < 255) {
slouken@5154
   641
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   642
    } else {
slouken@5154
   643
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   644
    }
slouken@5154
   645
    texture->a = alpha;
slouken@5156
   646
    if (texture->native) {
slouken@5156
   647
        return SDL_SetTextureAlphaMod(texture->native, alpha);
slouken@5156
   648
    } else if (renderer->SetTextureAlphaMod) {
slouken@5154
   649
        return renderer->SetTextureAlphaMod(renderer, texture);
slouken@5154
   650
    } else {
slouken@5154
   651
        return 0;
slouken@5154
   652
    }
slouken@5154
   653
}
slouken@5154
   654
slouken@5154
   655
int
slouken@5154
   656
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
slouken@5154
   657
{
slouken@5154
   658
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   659
slouken@5154
   660
    if (alpha) {
slouken@5154
   661
        *alpha = texture->a;
slouken@5154
   662
    }
slouken@5154
   663
    return 0;
slouken@5154
   664
}
slouken@5154
   665
slouken@5154
   666
int
slouken@5154
   667
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
slouken@5154
   668
{
slouken@5154
   669
    SDL_Renderer *renderer;
slouken@5154
   670
slouken@5154
   671
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   672
slouken@5154
   673
    renderer = texture->renderer;
slouken@5154
   674
    texture->blendMode = blendMode;
slouken@5156
   675
    if (texture->native) {
slouken@5180
   676
        return SDL_SetTextureBlendMode(texture->native, blendMode);
slouken@5156
   677
    } else if (renderer->SetTextureBlendMode) {
slouken@5154
   678
        return renderer->SetTextureBlendMode(renderer, texture);
slouken@5154
   679
    } else {
slouken@5154
   680
        return 0;
slouken@5154
   681
    }
slouken@5154
   682
}
slouken@5154
   683
slouken@5154
   684
int
slouken@5154
   685
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
slouken@5154
   686
{
slouken@5154
   687
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   688
slouken@5154
   689
    if (blendMode) {
slouken@5154
   690
        *blendMode = texture->blendMode;
slouken@5154
   691
    }
slouken@5154
   692
    return 0;
slouken@5154
   693
}
slouken@5154
   694
slouken@5156
   695
static int
slouken@5156
   696
SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   697
                     const void *pixels, int pitch)
slouken@5156
   698
{
slouken@5156
   699
    SDL_Texture *native = texture->native;
slouken@5156
   700
    SDL_Rect full_rect;
slouken@5156
   701
slouken@5156
   702
    if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
slouken@5156
   703
        return -1;
slouken@5156
   704
    }
slouken@5156
   705
slouken@5156
   706
    full_rect.x = 0;
slouken@5156
   707
    full_rect.y = 0;
slouken@5156
   708
    full_rect.w = texture->w;
slouken@5156
   709
    full_rect.h = texture->h;
slouken@5156
   710
    rect = &full_rect;
slouken@5156
   711
slouken@5156
   712
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   713
        /* We can lock the texture and copy to it */
slouken@5156
   714
        void *native_pixels;
slouken@5156
   715
        int native_pitch;
slouken@5156
   716
slouken@5156
   717
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   718
            return -1;
slouken@5156
   719
        }
slouken@5156
   720
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@5156
   721
                            rect->w, rect->h, native_pixels, native_pitch);
slouken@5156
   722
        SDL_UnlockTexture(native);
slouken@5156
   723
    } else {
slouken@5156
   724
        /* Use a temporary buffer for updating */
slouken@5156
   725
        void *temp_pixels;
slouken@5156
   726
        int temp_pitch;
slouken@5156
   727
slouken@5156
   728
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@5156
   729
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5156
   730
        if (!temp_pixels) {
icculus@7037
   731
            return SDL_OutOfMemory();
slouken@5156
   732
        }
slouken@5156
   733
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@5156
   734
                            rect->w, rect->h, temp_pixels, temp_pitch);
slouken@5156
   735
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@5156
   736
        SDL_free(temp_pixels);
slouken@5156
   737
    }
slouken@5156
   738
    return 0;
slouken@5156
   739
}
slouken@5156
   740
slouken@5156
   741
static int
slouken@5156
   742
SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   743
                        const void *pixels, int pitch)
slouken@5156
   744
{
slouken@5156
   745
    SDL_Texture *native = texture->native;
slouken@5156
   746
slouken@5156
   747
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   748
        /* We can lock the texture and copy to it */
slouken@5156
   749
        void *native_pixels;
slouken@5156
   750
        int native_pitch;
slouken@5156
   751
slouken@5156
   752
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   753
            return -1;
slouken@5156
   754
        }
slouken@5156
   755
        SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   756
                          texture->format, pixels, pitch,
slouken@5156
   757
                          native->format, native_pixels, native_pitch);
slouken@5156
   758
        SDL_UnlockTexture(native);
slouken@5156
   759
    } else {
slouken@5156
   760
        /* Use a temporary buffer for updating */
slouken@5156
   761
        void *temp_pixels;
slouken@5156
   762
        int temp_pitch;
slouken@5156
   763
slouken@5156
   764
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@5156
   765
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5156
   766
        if (!temp_pixels) {
icculus@7037
   767
            return SDL_OutOfMemory();
slouken@5156
   768
        }
slouken@5156
   769
        SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   770
                          texture->format, pixels, pitch,
slouken@5156
   771
                          native->format, temp_pixels, temp_pitch);
slouken@5156
   772
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@5156
   773
        SDL_free(temp_pixels);
slouken@5156
   774
    }
slouken@5156
   775
    return 0;
slouken@5156
   776
}
slouken@5156
   777
slouken@5154
   778
int
slouken@5154
   779
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   780
                  const void *pixels, int pitch)
slouken@5154
   781
{
slouken@5154
   782
    SDL_Renderer *renderer;
slouken@5154
   783
    SDL_Rect full_rect;
slouken@5154
   784
slouken@5154
   785
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   786
slouken@7409
   787
    if (!pixels) {
slouken@7409
   788
        return SDL_InvalidParamError("pixels");
slouken@7409
   789
    }
slouken@7409
   790
    if (!pitch) {
slouken@7409
   791
        return SDL_InvalidParamError("pitch");
slouken@7409
   792
    }
slouken@7409
   793
slouken@5154
   794
    if (!rect) {
slouken@5154
   795
        full_rect.x = 0;
slouken@5154
   796
        full_rect.y = 0;
slouken@5154
   797
        full_rect.w = texture->w;
slouken@5154
   798
        full_rect.h = texture->h;
slouken@5154
   799
        rect = &full_rect;
slouken@5154
   800
    }
slouken@5156
   801
slouken@5156
   802
    if (texture->yuv) {
slouken@5156
   803
        return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
slouken@5156
   804
    } else if (texture->native) {
slouken@5156
   805
        return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
slouken@5156
   806
    } else {
slouken@5156
   807
        renderer = texture->renderer;
slouken@5156
   808
        return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@5156
   809
    }
slouken@5156
   810
}
slouken@5156
   811
slouken@5156
   812
static int
slouken@7759
   813
SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
slouken@7759
   814
                           const Uint8 *Yplane, int Ypitch,
slouken@7759
   815
                           const Uint8 *Uplane, int Upitch,
slouken@7759
   816
                           const Uint8 *Vplane, int Vpitch)
slouken@7759
   817
{
slouken@7759
   818
    SDL_Texture *native = texture->native;
slouken@7759
   819
    SDL_Rect full_rect;
slouken@7759
   820
slouken@7759
   821
    if (SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch) < 0) {
slouken@7759
   822
        return -1;
slouken@7759
   823
    }
slouken@7759
   824
slouken@7759
   825
    full_rect.x = 0;
slouken@7759
   826
    full_rect.y = 0;
slouken@7759
   827
    full_rect.w = texture->w;
slouken@7759
   828
    full_rect.h = texture->h;
slouken@7759
   829
    rect = &full_rect;
slouken@7759
   830
slouken@7759
   831
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@7759
   832
        /* We can lock the texture and copy to it */
slouken@7759
   833
        void *native_pixels;
slouken@7759
   834
        int native_pitch;
slouken@7759
   835
slouken@7759
   836
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@7759
   837
            return -1;
slouken@7759
   838
        }
slouken@7759
   839
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@7759
   840
                            rect->w, rect->h, native_pixels, native_pitch);
slouken@7759
   841
        SDL_UnlockTexture(native);
slouken@7759
   842
    } else {
slouken@7759
   843
        /* Use a temporary buffer for updating */
slouken@7759
   844
        void *temp_pixels;
slouken@7759
   845
        int temp_pitch;
slouken@7759
   846
slouken@7759
   847
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@7759
   848
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@7759
   849
        if (!temp_pixels) {
slouken@7759
   850
            return SDL_OutOfMemory();
slouken@7759
   851
        }
slouken@7759
   852
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@7759
   853
                            rect->w, rect->h, temp_pixels, temp_pitch);
slouken@7759
   854
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@7759
   855
        SDL_free(temp_pixels);
slouken@7759
   856
    }
slouken@7759
   857
    return 0;
slouken@7759
   858
}
slouken@7759
   859
slouken@7759
   860
int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@7759
   861
                         const Uint8 *Yplane, int Ypitch,
slouken@7759
   862
                         const Uint8 *Uplane, int Upitch,
slouken@7759
   863
                         const Uint8 *Vplane, int Vpitch)
slouken@7759
   864
{
slouken@7759
   865
    SDL_Renderer *renderer;
slouken@7759
   866
    SDL_Rect full_rect;
slouken@7759
   867
slouken@7759
   868
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@7759
   869
slouken@7759
   870
    if (!Yplane) {
slouken@7759
   871
        return SDL_InvalidParamError("Yplane");
slouken@7759
   872
    }
slouken@7759
   873
    if (!Ypitch) {
slouken@7759
   874
        return SDL_InvalidParamError("Ypitch");
slouken@7759
   875
    }
slouken@7759
   876
    if (!Uplane) {
slouken@7759
   877
        return SDL_InvalidParamError("Uplane");
slouken@7759
   878
    }
slouken@7759
   879
    if (!Upitch) {
slouken@7759
   880
        return SDL_InvalidParamError("Upitch");
slouken@7759
   881
    }
slouken@7759
   882
    if (!Vplane) {
slouken@7759
   883
        return SDL_InvalidParamError("Vplane");
slouken@7759
   884
    }
slouken@7759
   885
    if (!Vpitch) {
slouken@7759
   886
        return SDL_InvalidParamError("Vpitch");
slouken@7759
   887
    }
slouken@7759
   888
slouken@7759
   889
    if (texture->format != SDL_PIXELFORMAT_YV12 &&
slouken@7759
   890
        texture->format != SDL_PIXELFORMAT_IYUV) {
slouken@7759
   891
        return SDL_SetError("Texture format must by YV12 or IYUV");
slouken@7759
   892
    }
slouken@7759
   893
slouken@7759
   894
    if (!rect) {
slouken@7759
   895
        full_rect.x = 0;
slouken@7759
   896
        full_rect.y = 0;
slouken@7759
   897
        full_rect.w = texture->w;
slouken@7759
   898
        full_rect.h = texture->h;
slouken@7759
   899
        rect = &full_rect;
slouken@7759
   900
    }
slouken@7759
   901
slouken@7759
   902
    if (texture->yuv) {
slouken@7759
   903
        return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
slouken@7759
   904
    } else {
slouken@7759
   905
        SDL_assert(!texture->native);
slouken@7759
   906
        renderer = texture->renderer;
slouken@7759
   907
        SDL_assert(renderer->UpdateTextureYUV);
slouken@7761
   908
		if (renderer->UpdateTextureYUV) {
slouken@7761
   909
			return renderer->UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
slouken@7761
   910
		} else {
slouken@7761
   911
			return SDL_Unsupported();
slouken@7761
   912
		}
slouken@7761
   913
	}
slouken@7759
   914
}
slouken@7759
   915
slouken@7759
   916
static int
slouken@5156
   917
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   918
                   void **pixels, int *pitch)
slouken@5156
   919
{
slouken@5156
   920
    return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
slouken@5156
   921
}
slouken@5156
   922
slouken@5156
   923
static int
slouken@5156
   924
SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   925
                      void **pixels, int *pitch)
slouken@5156
   926
{
slouken@5156
   927
    texture->locked_rect = *rect;
slouken@5156
   928
    *pixels = (void *) ((Uint8 *) texture->pixels +
slouken@5156
   929
                        rect->y * texture->pitch +
slouken@5156
   930
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5156
   931
    *pitch = texture->pitch;
slouken@5156
   932
    return 0;
slouken@5154
   933
}
slouken@5154
   934
slouken@5154
   935
int
slouken@5156
   936
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   937
                void **pixels, int *pitch)
slouken@5154
   938
{
slouken@5154
   939
    SDL_Renderer *renderer;
slouken@5154
   940
    SDL_Rect full_rect;
slouken@5154
   941
slouken@5154
   942
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   943
slouken@5154
   944
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
icculus@7037
   945
        return SDL_SetError("SDL_LockTexture(): texture must be streaming");
slouken@5154
   946
    }
slouken@5156
   947
slouken@5154
   948
    if (!rect) {
slouken@5154
   949
        full_rect.x = 0;
slouken@5154
   950
        full_rect.y = 0;
slouken@5154
   951
        full_rect.w = texture->w;
slouken@5154
   952
        full_rect.h = texture->h;
slouken@5154
   953
        rect = &full_rect;
slouken@5154
   954
    }
slouken@5156
   955
slouken@5156
   956
    if (texture->yuv) {
slouken@5156
   957
        return SDL_LockTextureYUV(texture, rect, pixels, pitch);
slouken@5156
   958
    } else if (texture->native) {
slouken@5156
   959
        return SDL_LockTextureNative(texture, rect, pixels, pitch);
slouken@5156
   960
    } else {
slouken@5156
   961
        renderer = texture->renderer;
slouken@5156
   962
        return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
slouken@5156
   963
    }
slouken@5156
   964
}
slouken@5156
   965
slouken@5156
   966
static void
slouken@5156
   967
SDL_UnlockTextureYUV(SDL_Texture * texture)
slouken@5156
   968
{
slouken@5156
   969
    SDL_Texture *native = texture->native;
slouken@5156
   970
    void *native_pixels;
slouken@5156
   971
    int native_pitch;
slouken@5156
   972
    SDL_Rect rect;
slouken@5156
   973
slouken@5156
   974
    rect.x = 0;
slouken@5156
   975
    rect.y = 0;
slouken@5156
   976
    rect.w = texture->w;
slouken@5156
   977
    rect.h = texture->h;
slouken@5156
   978
slouken@5156
   979
    if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   980
        return;
slouken@5156
   981
    }
slouken@5156
   982
    SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
slouken@5156
   983
                        rect.w, rect.h, native_pixels, native_pitch);
slouken@5156
   984
    SDL_UnlockTexture(native);
slouken@5156
   985
}
slouken@5156
   986
slouken@6044
   987
static void
slouken@5156
   988
SDL_UnlockTextureNative(SDL_Texture * texture)
slouken@5156
   989
{
slouken@5156
   990
    SDL_Texture *native = texture->native;
slouken@5156
   991
    void *native_pixels;
slouken@5156
   992
    int native_pitch;
slouken@5156
   993
    const SDL_Rect *rect = &texture->locked_rect;
slouken@5156
   994
    const void* pixels = (void *) ((Uint8 *) texture->pixels +
slouken@5156
   995
                        rect->y * texture->pitch +
slouken@5156
   996
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5156
   997
    int pitch = texture->pitch;
slouken@5156
   998
slouken@5156
   999
    if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
  1000
        return;
slouken@5156
  1001
    }
slouken@5156
  1002
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
  1003
                      texture->format, pixels, pitch,
slouken@5156
  1004
                      native->format, native_pixels, native_pitch);
slouken@5156
  1005
    SDL_UnlockTexture(native);
slouken@5154
  1006
}
slouken@5154
  1007
slouken@5154
  1008
void
slouken@5154
  1009
SDL_UnlockTexture(SDL_Texture * texture)
slouken@5154
  1010
{
slouken@5154
  1011
    SDL_Renderer *renderer;
slouken@5154
  1012
slouken@5154
  1013
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
  1014
slouken@5154
  1015
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
  1016
        return;
slouken@5154
  1017
    }
slouken@5156
  1018
    if (texture->yuv) {
slouken@5156
  1019
        SDL_UnlockTextureYUV(texture);
slouken@5156
  1020
    } else if (texture->native) {
slouken@5156
  1021
        SDL_UnlockTextureNative(texture);
slouken@5156
  1022
    } else {
slouken@5156
  1023
        renderer = texture->renderer;
slouken@5156
  1024
        renderer->UnlockTexture(renderer, texture);
slouken@5154
  1025
    }
slouken@5154
  1026
}
slouken@5154
  1027
slouken@6246
  1028
SDL_bool
slouken@6246
  1029
SDL_RenderTargetSupported(SDL_Renderer *renderer)
slouken@6246
  1030
{
slouken@6247
  1031
    if (!renderer || !renderer->SetRenderTarget) {
slouken@6246
  1032
        return SDL_FALSE;
slouken@6246
  1033
    }
slouken@6246
  1034
    return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
slouken@6246
  1035
}
slouken@6246
  1036
slouken@6246
  1037
int
slouken@6247
  1038
SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@6246
  1039
{
slouken@6246
  1040
    if (!SDL_RenderTargetSupported(renderer)) {
icculus@7037
  1041
        return SDL_Unsupported();
slouken@6246
  1042
    }
slouken@6246
  1043
    if (texture == renderer->target) {
slouken@6246
  1044
        /* Nothing to do! */
slouken@6246
  1045
        return 0;
slouken@6246
  1046
    }
slouken@6246
  1047
slouken@6246
  1048
    /* texture == NULL is valid and means reset the target to the window */
slouken@6246
  1049
    if (texture) {
slouken@6246
  1050
        CHECK_TEXTURE_MAGIC(texture, -1);
slouken@6246
  1051
        if (renderer != texture->renderer) {
icculus@7037
  1052
            return SDL_SetError("Texture was not created with this renderer");
slouken@6246
  1053
        }
gabomdq@6337
  1054
        if (texture->access != SDL_TEXTUREACCESS_TARGET) {
icculus@7037
  1055
            return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
slouken@6246
  1056
        }
slouken@6246
  1057
        if (texture->native) {
slouken@6246
  1058
            /* Always render to the native texture */
slouken@6246
  1059
            texture = texture->native;
slouken@6246
  1060
        }
slouken@6246
  1061
    }
slouken@6246
  1062
slouken@6246
  1063
    if (texture && !renderer->target) {
slouken@6246
  1064
        /* Make a backup of the viewport */
slouken@6246
  1065
        renderer->viewport_backup = renderer->viewport;
slouken@7141
  1066
        renderer->clip_rect_backup = renderer->clip_rect;
slouken@6528
  1067
        renderer->scale_backup = renderer->scale;
slouken@6581
  1068
        renderer->logical_w_backup = renderer->logical_w;
slouken@6581
  1069
        renderer->logical_h_backup = renderer->logical_h;
slouken@6246
  1070
    }
slouken@6246
  1071
    renderer->target = texture;
slouken@6246
  1072
slouken@6247
  1073
    if (renderer->SetRenderTarget(renderer, texture) < 0) {
slouken@6246
  1074
        return -1;
slouken@6246
  1075
    }
slouken@6246
  1076
slouken@6246
  1077
    if (texture) {
slouken@6528
  1078
        renderer->viewport.x = 0;
slouken@6528
  1079
        renderer->viewport.y = 0;
slouken@6528
  1080
        renderer->viewport.w = texture->w;
slouken@6528
  1081
        renderer->viewport.h = texture->h;
slouken@6528
  1082
        renderer->scale.x = 1.0f;
slouken@6528
  1083
        renderer->scale.y = 1.0f;
slouken@7339
  1084
        renderer->logical_w = texture->w;
slouken@7339
  1085
        renderer->logical_h = texture->h;
slouken@6246
  1086
    } else {
slouken@6528
  1087
        renderer->viewport = renderer->viewport_backup;
slouken@7141
  1088
        renderer->clip_rect = renderer->clip_rect_backup;
slouken@6528
  1089
        renderer->scale = renderer->scale_backup;
slouken@6581
  1090
        renderer->logical_w = renderer->logical_w_backup;
slouken@6581
  1091
        renderer->logical_h = renderer->logical_h_backup;
slouken@6246
  1092
    }
slouken@6528
  1093
    if (renderer->UpdateViewport(renderer) < 0) {
slouken@6246
  1094
        return -1;
slouken@6246
  1095
    }
slouken@7141
  1096
    if (renderer->UpdateClipRect(renderer) < 0) {
slouken@7141
  1097
        return -1;
slouken@7141
  1098
    }
slouken@6246
  1099
slouken@6246
  1100
    /* All set! */
slouken@6246
  1101
    return 0;
slouken@6246
  1102
}
slouken@6246
  1103
slouken@6578
  1104
SDL_Texture *
slouken@6578
  1105
SDL_GetRenderTarget(SDL_Renderer *renderer)
slouken@6578
  1106
{
slouken@6578
  1107
    return renderer->target;
slouken@6578
  1108
}
slouken@6578
  1109
slouken@6530
  1110
static int
slouken@6530
  1111
UpdateLogicalSize(SDL_Renderer *renderer)
slouken@6530
  1112
{
slouken@6530
  1113
    int w, h;
slouken@6530
  1114
    float want_aspect;
slouken@6530
  1115
    float real_aspect;
slouken@6530
  1116
    float scale;
slouken@6530
  1117
    SDL_Rect viewport;
slouken@6530
  1118
slouken@7239
  1119
    if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
slouken@7239
  1120
        return -1;
slouken@6530
  1121
    }
slouken@6530
  1122
slouken@6530
  1123
    want_aspect = (float)renderer->logical_w / renderer->logical_h;
slouken@6530
  1124
    real_aspect = (float)w / h;
slouken@6530
  1125
slouken@6530
  1126
    /* Clear the scale because we're setting viewport in output coordinates */
slouken@6530
  1127
    SDL_RenderSetScale(renderer, 1.0f, 1.0f);
slouken@6530
  1128
slouken@6530
  1129
    if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
slouken@6530
  1130
        /* The aspect ratios are the same, just scale appropriately */
slouken@6530
  1131
        scale = (float)w / renderer->logical_w;
slouken@6530
  1132
        SDL_RenderSetViewport(renderer, NULL);
slouken@6530
  1133
    } else if (want_aspect > real_aspect) {
slouken@6530
  1134
        /* We want a wider aspect ratio than is available - letterbox it */
slouken@6530
  1135
        scale = (float)w / renderer->logical_w;
slouken@6530
  1136
        viewport.x = 0;
slouken@6530
  1137
        viewport.w = w;
slouken@6530
  1138
        viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
slouken@6530
  1139
        viewport.y = (h - viewport.h) / 2;
slouken@6530
  1140
        SDL_RenderSetViewport(renderer, &viewport);
slouken@6530
  1141
    } else {
slouken@6530
  1142
        /* We want a narrower aspect ratio than is available - use side-bars */
slouken@6530
  1143
        scale = (float)h / renderer->logical_h;
slouken@6530
  1144
        viewport.y = 0;
slouken@6530
  1145
        viewport.h = h;
slouken@6530
  1146
        viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
slouken@6530
  1147
        viewport.x = (w - viewport.w) / 2;
slouken@6530
  1148
        SDL_RenderSetViewport(renderer, &viewport);
slouken@6530
  1149
    }
slouken@6530
  1150
slouken@6530
  1151
    /* Set the new scale */
slouken@6530
  1152
    SDL_RenderSetScale(renderer, scale, scale);
slouken@6531
  1153
slouken@6531
  1154
    return 0;
slouken@6530
  1155
}
slouken@6530
  1156
slouken@6530
  1157
int
slouken@6530
  1158
SDL_RenderSetLogicalSize(SDL_Renderer * renderer, int w, int h)
slouken@6530
  1159
{
slouken@6530
  1160
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@6530
  1161
slouken@6530
  1162
    if (!w || !h) {
slouken@6530
  1163
        /* Clear any previous logical resolution */
slouken@6530
  1164
        renderer->logical_w = 0;
slouken@6530
  1165
        renderer->logical_h = 0;
slouken@6530
  1166
        SDL_RenderSetViewport(renderer, NULL);
slouken@6530
  1167
        SDL_RenderSetScale(renderer, 1.0f, 1.0f);
slouken@6530
  1168
        return 0;
slouken@6530
  1169
    }
slouken@6530
  1170
slouken@6530
  1171
    renderer->logical_w = w;
slouken@6530
  1172
    renderer->logical_h = h;
slouken@6530
  1173
slouken@6530
  1174
    return UpdateLogicalSize(renderer);
slouken@6530
  1175
}
slouken@6530
  1176
slouken@6530
  1177
void
slouken@6530
  1178
SDL_RenderGetLogicalSize(SDL_Renderer * renderer, int *w, int *h)
slouken@6530
  1179
{
slouken@6530
  1180
    CHECK_RENDERER_MAGIC(renderer, );
slouken@6530
  1181
slouken@6530
  1182
    if (w) {
slouken@6530
  1183
        *w = renderer->logical_w;
slouken@6530
  1184
    }
slouken@6530
  1185
    if (h) {
slouken@6530
  1186
        *h = renderer->logical_h;
slouken@6530
  1187
    }
slouken@6530
  1188
}
slouken@6530
  1189
slouken@5297
  1190
int
slouken@5297
  1191
SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5297
  1192
{
slouken@5297
  1193
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5297
  1194
slouken@5297
  1195
    if (rect) {
slouken@6528
  1196
        renderer->viewport.x = (int)SDL_floor(rect->x * renderer->scale.x);
slouken@6528
  1197
        renderer->viewport.y = (int)SDL_floor(rect->y * renderer->scale.y);
slouken@6528
  1198
        renderer->viewport.w = (int)SDL_ceil(rect->w * renderer->scale.x);
slouken@6528
  1199
        renderer->viewport.h = (int)SDL_ceil(rect->h * renderer->scale.y);
slouken@5297
  1200
    } else {
slouken@5297
  1201
        renderer->viewport.x = 0;
slouken@5297
  1202
        renderer->viewport.y = 0;
slouken@7239
  1203
        if (SDL_GetRendererOutputSize(renderer, &renderer->viewport.w, &renderer->viewport.h) < 0) {
slouken@7239
  1204
            return -1;
slouken@5297
  1205
        }
slouken@5297
  1206
    }
slouken@5297
  1207
    return renderer->UpdateViewport(renderer);
slouken@5297
  1208
}
slouken@5297
  1209
slouken@5224
  1210
void
slouken@5297
  1211
SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
slouken@5224
  1212
{
slouken@5224
  1213
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5224
  1214
slouken@6528
  1215
    if (rect) {
slouken@6528
  1216
        rect->x = (int)(renderer->viewport.x / renderer->scale.x);
slouken@6528
  1217
        rect->y = (int)(renderer->viewport.y / renderer->scale.y);
slouken@6528
  1218
        rect->w = (int)(renderer->viewport.w / renderer->scale.x);
slouken@6528
  1219
        rect->h = (int)(renderer->viewport.h / renderer->scale.y);
slouken@6528
  1220
    }
slouken@6528
  1221
}
slouken@6528
  1222
slouken@6528
  1223
int
slouken@7141
  1224
SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@7141
  1225
{
philipp@7143
  1226
    CHECK_RENDERER_MAGIC(renderer, -1)
slouken@7141
  1227
slouken@7141
  1228
    if (rect) {
slouken@7141
  1229
        renderer->clip_rect.x = (int)SDL_floor(rect->x * renderer->scale.x);
slouken@7141
  1230
        renderer->clip_rect.y = (int)SDL_floor(rect->y * renderer->scale.y);
slouken@7141
  1231
        renderer->clip_rect.w = (int)SDL_ceil(rect->w * renderer->scale.x);
slouken@7141
  1232
        renderer->clip_rect.h = (int)SDL_ceil(rect->h * renderer->scale.y);
slouken@7141
  1233
    } else {
slouken@7141
  1234
        SDL_zero(renderer->clip_rect);
slouken@7141
  1235
    }
slouken@7141
  1236
    return renderer->UpdateClipRect(renderer);
slouken@7141
  1237
}
slouken@7141
  1238
slouken@7141
  1239
void
slouken@7141
  1240
SDL_RenderGetClipRect(SDL_Renderer * renderer, SDL_Rect * rect)
slouken@7141
  1241
{
slouken@7141
  1242
    CHECK_RENDERER_MAGIC(renderer, )
slouken@7141
  1243
slouken@7141
  1244
    if (rect) {
slouken@7141
  1245
        rect->x = (int)(renderer->clip_rect.x / renderer->scale.x);
slouken@7141
  1246
        rect->y = (int)(renderer->clip_rect.y / renderer->scale.y);
slouken@7141
  1247
        rect->w = (int)(renderer->clip_rect.w / renderer->scale.x);
slouken@7141
  1248
        rect->h = (int)(renderer->clip_rect.h / renderer->scale.y);
slouken@7141
  1249
    }
slouken@7141
  1250
}
slouken@7141
  1251
slouken@7141
  1252
int
slouken@6528
  1253
SDL_RenderSetScale(SDL_Renderer * renderer, float scaleX, float scaleY)
slouken@6528
  1254
{
slouken@6528
  1255
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@6528
  1256
slouken@6528
  1257
    renderer->scale.x = scaleX;
slouken@6528
  1258
    renderer->scale.y = scaleY;
slouken@6528
  1259
    return 0;
slouken@6528
  1260
}
slouken@6528
  1261
slouken@6528
  1262
void
slouken@6528
  1263
SDL_RenderGetScale(SDL_Renderer * renderer, float *scaleX, float *scaleY)
slouken@6528
  1264
{
slouken@6528
  1265
    CHECK_RENDERER_MAGIC(renderer, );
slouken@6528
  1266
slouken@6528
  1267
    if (scaleX) {
slouken@6528
  1268
        *scaleX = renderer->scale.x;
slouken@6528
  1269
    }
slouken@6528
  1270
    if (scaleY) {
slouken@6528
  1271
        *scaleY = renderer->scale.y;
slouken@6528
  1272
    }
slouken@5224
  1273
}
slouken@5224
  1274
slouken@5154
  1275
int
slouken@5154
  1276
SDL_SetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
  1277
                       Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5154
  1278
{
slouken@5154
  1279
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1280
slouken@5154
  1281
    renderer->r = r;
slouken@5154
  1282
    renderer->g = g;
slouken@5154
  1283
    renderer->b = b;
slouken@5154
  1284
    renderer->a = a;
slouken@5154
  1285
    return 0;
slouken@5154
  1286
}
slouken@5154
  1287
slouken@5154
  1288
int
slouken@5154
  1289
SDL_GetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
  1290
                       Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@5154
  1291
{
slouken@5154
  1292
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1293
slouken@5154
  1294
    if (r) {
slouken@5154
  1295
        *r = renderer->r;
slouken@5154
  1296
    }
slouken@5154
  1297
    if (g) {
slouken@5154
  1298
        *g = renderer->g;
slouken@5154
  1299
    }
slouken@5154
  1300
    if (b) {
slouken@5154
  1301
        *b = renderer->b;
slouken@5154
  1302
    }
slouken@5154
  1303
    if (a) {
slouken@5154
  1304
        *a = renderer->a;
slouken@5154
  1305
    }
slouken@5154
  1306
    return 0;
slouken@5154
  1307
}
slouken@5154
  1308
slouken@5154
  1309
int
slouken@5154
  1310
SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
slouken@5154
  1311
{
slouken@5154
  1312
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1313
slouken@5154
  1314
    renderer->blendMode = blendMode;
slouken@5154
  1315
    return 0;
slouken@5154
  1316
}
slouken@5154
  1317
slouken@5154
  1318
int
slouken@5154
  1319
SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
slouken@5154
  1320
{
slouken@5154
  1321
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1322
slouken@5154
  1323
    *blendMode = renderer->blendMode;
slouken@5154
  1324
    return 0;
slouken@5154
  1325
}
slouken@5154
  1326
slouken@5154
  1327
int
slouken@5154
  1328
SDL_RenderClear(SDL_Renderer * renderer)
slouken@5154
  1329
{
slouken@5154
  1330
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1331
slouken@6260
  1332
    /* Don't draw while we're hidden */
slouken@6260
  1333
    if (renderer->hidden) {
slouken@6060
  1334
        return 0;
slouken@6060
  1335
    }
slouken@5154
  1336
    return renderer->RenderClear(renderer);
slouken@5154
  1337
}
slouken@5154
  1338
slouken@5154
  1339
int
slouken@5154
  1340
SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
slouken@5154
  1341
{
slouken@5154
  1342
    SDL_Point point;
slouken@5154
  1343
slouken@5154
  1344
    point.x = x;
slouken@5154
  1345
    point.y = y;
slouken@5154
  1346
    return SDL_RenderDrawPoints(renderer, &point, 1);
slouken@5154
  1347
}
slouken@5154
  1348
slouken@6528
  1349
static int
slouken@6528
  1350
RenderDrawPointsWithRects(SDL_Renderer * renderer,
slouken@6528
  1351
                     const SDL_Point * points, int count)
slouken@6528
  1352
{
slouken@6528
  1353
    SDL_FRect *frects;
slouken@6528
  1354
    int i;
slouken@6528
  1355
    int status;
slouken@6528
  1356
slouken@6528
  1357
    frects = SDL_stack_alloc(SDL_FRect, count);
slouken@6528
  1358
    if (!frects) {
icculus@7037
  1359
        return SDL_OutOfMemory();
slouken@6528
  1360
    }
slouken@6528
  1361
    for (i = 0; i < count; ++i) {
slouken@6528
  1362
        frects[i].x = points[i].x * renderer->scale.x;
slouken@6528
  1363
        frects[i].y = points[i].y * renderer->scale.y;
slouken@6528
  1364
        frects[i].w = renderer->scale.x;
slouken@6528
  1365
        frects[i].h = renderer->scale.y;
slouken@6528
  1366
    }
slouken@6528
  1367
slouken@6528
  1368
    status = renderer->RenderFillRects(renderer, frects, count);
slouken@6528
  1369
slouken@6528
  1370
    SDL_stack_free(frects);
slouken@6528
  1371
slouken@6528
  1372
    return status;
slouken@6528
  1373
}
slouken@6528
  1374
slouken@5154
  1375
int
slouken@5154
  1376
SDL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@5154
  1377
                     const SDL_Point * points, int count)
slouken@5154
  1378
{
slouken@6528
  1379
    SDL_FPoint *fpoints;
slouken@6528
  1380
    int i;
slouken@6528
  1381
    int status;
slouken@6528
  1382
slouken@5154
  1383
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1384
slouken@5154
  1385
    if (!points) {
icculus@7037
  1386
        return SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
slouken@5154
  1387
    }
slouken@5154
  1388
    if (count < 1) {
slouken@5154
  1389
        return 0;
slouken@5154
  1390
    }
slouken@6260
  1391
    /* Don't draw while we're hidden */
slouken@6260
  1392
    if (renderer->hidden) {
slouken@6060
  1393
        return 0;
slouken@6060
  1394
    }
slouken@6528
  1395
slouken@6528
  1396
    if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
slouken@6528
  1397
        return RenderDrawPointsWithRects(renderer, points, count);
slouken@6528
  1398
    }
slouken@6528
  1399
slouken@6528
  1400
    fpoints = SDL_stack_alloc(SDL_FPoint, count);
slouken@6528
  1401
    if (!fpoints) {
icculus@7037
  1402
        return SDL_OutOfMemory();
slouken@6528
  1403
    }
slouken@6528
  1404
    for (i = 0; i < count; ++i) {
slouken@6528
  1405
        fpoints[i].x = points[i].x * renderer->scale.x;
slouken@6528
  1406
        fpoints[i].y = points[i].y * renderer->scale.y;
slouken@6528
  1407
    }
slouken@6528
  1408
slouken@6528
  1409
    status = renderer->RenderDrawPoints(renderer, fpoints, count);
slouken@6528
  1410
slouken@6528
  1411
    SDL_stack_free(fpoints);
slouken@6528
  1412
slouken@6528
  1413
    return status;
slouken@5154
  1414
}
slouken@5154
  1415
slouken@5154
  1416
int
slouken@5154
  1417
SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
slouken@5154
  1418
{
slouken@5154
  1419
    SDL_Point points[2];
slouken@5154
  1420
slouken@5154
  1421
    points[0].x = x1;
slouken@5154
  1422
    points[0].y = y1;
slouken@5154
  1423
    points[1].x = x2;
slouken@5154
  1424
    points[1].y = y2;
slouken@5154
  1425
    return SDL_RenderDrawLines(renderer, points, 2);
slouken@5154
  1426
}
slouken@5154
  1427
slouken@6528
  1428
static int
slouken@6528
  1429
RenderDrawLinesWithRects(SDL_Renderer * renderer,
slouken@6528
  1430
                     const SDL_Point * points, int count)
slouken@6528
  1431
{
slouken@6528
  1432
    SDL_FRect *frect;
slouken@6528
  1433
    SDL_FRect *frects;
slouken@6528
  1434
    SDL_FPoint fpoints[2];
slouken@6528
  1435
    int i, nrects;
slouken@6528
  1436
    int status;
slouken@6528
  1437
slouken@6528
  1438
    frects = SDL_stack_alloc(SDL_FRect, count-1);
slouken@6528
  1439
    if (!frects) {
icculus@7037
  1440
        return SDL_OutOfMemory();
slouken@6528
  1441
    }
slouken@6528
  1442
slouken@6528
  1443
    status = 0;
slouken@6528
  1444
    nrects = 0;
slouken@6528
  1445
    for (i = 0; i < count-1; ++i) {
slouken@6528
  1446
        if (points[i].x == points[i+1].x) {
slouken@6528
  1447
            int minY = SDL_min(points[i].y, points[i+1].y);
slouken@6528
  1448
            int maxY = SDL_max(points[i].y, points[i+1].y);
slouken@6528
  1449
slouken@6528
  1450
            frect = &frects[nrects++];
slouken@6528
  1451
            frect->x = points[i].x * renderer->scale.x;
slouken@6528
  1452
            frect->y = minY * renderer->scale.y;
slouken@6528
  1453
            frect->w = renderer->scale.x;
slouken@6528
  1454
            frect->h = (maxY - minY + 1) * renderer->scale.y;
slouken@6528
  1455
        } else if (points[i].y == points[i+1].y) {
slouken@6528
  1456
            int minX = SDL_min(points[i].x, points[i+1].x);
slouken@6528
  1457
            int maxX = SDL_max(points[i].x, points[i+1].x);
slouken@6528
  1458
slouken@6528
  1459
            frect = &frects[nrects++];
slouken@6528
  1460
            frect->x = minX * renderer->scale.x;
slouken@6528
  1461
            frect->y = points[i].y * renderer->scale.y;
slouken@6528
  1462
            frect->w = (maxX - minX + 1) * renderer->scale.x;
slouken@6528
  1463
            frect->h = renderer->scale.y;
slouken@6528
  1464
        } else {
slouken@6528
  1465
            /* FIXME: We can't use a rect for this line... */
aschiffler@7073
  1466
            fpoints[0].x = points[i].x * renderer->scale.x;
aschiffler@7073
  1467
            fpoints[0].y = points[i].y * renderer->scale.y;
aschiffler@7073
  1468
            fpoints[1].x = points[i+1].x * renderer->scale.x;
aschiffler@7073
  1469
            fpoints[1].y = points[i+1].y * renderer->scale.y;
slouken@6528
  1470
            status += renderer->RenderDrawLines(renderer, fpoints, 2);
slouken@6528
  1471
        }
slouken@6528
  1472
    }
slouken@6528
  1473
slouken@6528
  1474
    status += renderer->RenderFillRects(renderer, frects, nrects);
slouken@6528
  1475
slouken@6528
  1476
    SDL_stack_free(frects);
slouken@6528
  1477
slouken@6528
  1478
    if (status < 0) {
slouken@6528
  1479
        status = -1;
slouken@6528
  1480
    }
slouken@6528
  1481
    return status;
slouken@6528
  1482
}
slouken@6528
  1483
slouken@5154
  1484
int
slouken@5154
  1485
SDL_RenderDrawLines(SDL_Renderer * renderer,
slouken@5154
  1486
                    const SDL_Point * points, int count)
slouken@5154
  1487
{
slouken@6528
  1488
    SDL_FPoint *fpoints;
slouken@6528
  1489
    int i;
slouken@6528
  1490
    int status;
slouken@6528
  1491
slouken@5154
  1492
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1493
slouken@5154
  1494
    if (!points) {
icculus@7037
  1495
        return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
slouken@5154
  1496
    }
slouken@5154
  1497
    if (count < 2) {
slouken@5154
  1498
        return 0;
slouken@5154
  1499
    }
slouken@6260
  1500
    /* Don't draw while we're hidden */
slouken@6260
  1501
    if (renderer->hidden) {
slouken@6060
  1502
        return 0;
slouken@6060
  1503
    }
slouken@6528
  1504
slouken@6528
  1505
    if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
slouken@6528
  1506
        return RenderDrawLinesWithRects(renderer, points, count);
slouken@6528
  1507
    }
slouken@6528
  1508
slouken@6528
  1509
    fpoints = SDL_stack_alloc(SDL_FPoint, count);
slouken@6528
  1510
    if (!fpoints) {
icculus@7037
  1511
        return SDL_OutOfMemory();
slouken@6528
  1512
    }
slouken@6528
  1513
    for (i = 0; i < count; ++i) {
slouken@6528
  1514
        fpoints[i].x = points[i].x * renderer->scale.x;
slouken@6528
  1515
        fpoints[i].y = points[i].y * renderer->scale.y;
slouken@6528
  1516
    }
slouken@6528
  1517
slouken@6528
  1518
    status = renderer->RenderDrawLines(renderer, fpoints, count);
slouken@6528
  1519
slouken@6528
  1520
    SDL_stack_free(fpoints);
slouken@6528
  1521
slouken@6528
  1522
    return status;
slouken@5154
  1523
}
slouken@5154
  1524
slouken@5154
  1525
int
slouken@5154
  1526
SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
  1527
{
slouken@5154
  1528
    SDL_Rect full_rect;
slouken@5154
  1529
    SDL_Point points[5];
slouken@5154
  1530
slouken@5154
  1531
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1532
slouken@5154
  1533
    /* If 'rect' == NULL, then outline the whole surface */
slouken@5154
  1534
    if (!rect) {
slouken@6528
  1535
        SDL_RenderGetViewport(renderer, &full_rect);
slouken@5154
  1536
        full_rect.x = 0;
slouken@5154
  1537
        full_rect.y = 0;
slouken@5154
  1538
        rect = &full_rect;
slouken@5154
  1539
    }
slouken@5154
  1540
slouken@5154
  1541
    points[0].x = rect->x;
slouken@5154
  1542
    points[0].y = rect->y;
slouken@5154
  1543
    points[1].x = rect->x+rect->w-1;
slouken@5154
  1544
    points[1].y = rect->y;
slouken@5154
  1545
    points[2].x = rect->x+rect->w-1;
slouken@5154
  1546
    points[2].y = rect->y+rect->h-1;
slouken@5154
  1547
    points[3].x = rect->x;
slouken@5154
  1548
    points[3].y = rect->y+rect->h-1;
slouken@5154
  1549
    points[4].x = rect->x;
slouken@5154
  1550
    points[4].y = rect->y;
slouken@5154
  1551
    return SDL_RenderDrawLines(renderer, points, 5);
slouken@5154
  1552
}
slouken@5154
  1553
slouken@5154
  1554
int
slouken@5154
  1555
SDL_RenderDrawRects(SDL_Renderer * renderer,
slouken@5297
  1556
                    const SDL_Rect * rects, int count)
slouken@5154
  1557
{
slouken@5154
  1558
    int i;
slouken@5154
  1559
slouken@5154
  1560
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1561
slouken@5154
  1562
    if (!rects) {
icculus@7037
  1563
        return SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
slouken@5154
  1564
    }
slouken@5154
  1565
    if (count < 1) {
slouken@5154
  1566
        return 0;
slouken@5154
  1567
    }
slouken@5154
  1568
slouken@6260
  1569
    /* Don't draw while we're hidden */
slouken@6260
  1570
    if (renderer->hidden) {
slouken@6060
  1571
        return 0;
slouken@6060
  1572
    }
slouken@5154
  1573
    for (i = 0; i < count; ++i) {
slouken@5297
  1574
        if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
slouken@5154
  1575
            return -1;
slouken@5154
  1576
        }
slouken@5154
  1577
    }
slouken@5154
  1578
    return 0;
slouken@5154
  1579
}
slouken@5154
  1580
slouken@5154
  1581
int
slouken@5154
  1582
SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
  1583
{
slouken@7511
  1584
    SDL_Rect full_rect = { 0, 0, 0, 0 };
slouken@6232
  1585
slouken@5331
  1586
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@6232
  1587
slouken@5331
  1588
    /* If 'rect' == NULL, then outline the whole surface */
slouken@5331
  1589
    if (!rect) {
slouken@6528
  1590
        SDL_RenderGetViewport(renderer, &full_rect);
slouken@5331
  1591
        full_rect.x = 0;
slouken@5331
  1592
        full_rect.y = 0;
slouken@5331
  1593
        rect = &full_rect;
slouken@5331
  1594
    }
slouken@5297
  1595
    return SDL_RenderFillRects(renderer, rect, 1);
slouken@5154
  1596
}
slouken@5154
  1597
slouken@5154
  1598
int
slouken@5154
  1599
SDL_RenderFillRects(SDL_Renderer * renderer,
slouken@5297
  1600
                    const SDL_Rect * rects, int count)
slouken@5154
  1601
{
slouken@6528
  1602
    SDL_FRect *frects;
slouken@6528
  1603
    int i;
slouken@6528
  1604
    int status;
slouken@6528
  1605
slouken@5154
  1606
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1607
slouken@5154
  1608
    if (!rects) {
icculus@7037
  1609
        return SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
slouken@5154
  1610
    }
slouken@5154
  1611
    if (count < 1) {
slouken@5154
  1612
        return 0;
slouken@5154
  1613
    }
slouken@6260
  1614
    /* Don't draw while we're hidden */
slouken@6260
  1615
    if (renderer->hidden) {
slouken@6060
  1616
        return 0;
slouken@6060
  1617
    }
slouken@6528
  1618
slouken@6528
  1619
    frects = SDL_stack_alloc(SDL_FRect, count);
slouken@6528
  1620
    if (!frects) {
icculus@7037
  1621
        return SDL_OutOfMemory();
slouken@6528
  1622
    }
slouken@6528
  1623
    for (i = 0; i < count; ++i) {
slouken@6528
  1624
        frects[i].x = rects[i].x * renderer->scale.x;
slouken@6528
  1625
        frects[i].y = rects[i].y * renderer->scale.y;
slouken@6528
  1626
        frects[i].w = rects[i].w * renderer->scale.x;
slouken@6528
  1627
        frects[i].h = rects[i].h * renderer->scale.y;
slouken@6528
  1628
    }
slouken@6528
  1629
slouken@6528
  1630
    status = renderer->RenderFillRects(renderer, frects, count);
slouken@6528
  1631
slouken@6528
  1632
    SDL_stack_free(frects);
slouken@6528
  1633
slouken@6528
  1634
    return status;
slouken@5154
  1635
}
slouken@5154
  1636
slouken@5154
  1637
int
slouken@5154
  1638
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5154
  1639
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@5154
  1640
{
icculus@6545
  1641
    SDL_Rect real_srcrect = { 0, 0, 0, 0 };
icculus@6545
  1642
    SDL_Rect real_dstrect = { 0, 0, 0, 0 };
slouken@6528
  1643
    SDL_FRect frect;
slouken@5154
  1644
slouken@5154
  1645
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1646
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
  1647
slouken@5154
  1648
    if (renderer != texture->renderer) {
icculus@7037
  1649
        return SDL_SetError("Texture was not created with this renderer");
slouken@5154
  1650
    }
slouken@5154
  1651
slouken@5154
  1652
    real_srcrect.x = 0;
slouken@5154
  1653
    real_srcrect.y = 0;
slouken@5154
  1654
    real_srcrect.w = texture->w;
slouken@5154
  1655
    real_srcrect.h = texture->h;
slouken@5154
  1656
    if (srcrect) {
slouken@5154
  1657
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
slouken@5154
  1658
            return 0;
slouken@5154
  1659
        }
slouken@5154
  1660
    }
slouken@5154
  1661
slouken@6528
  1662
    SDL_RenderGetViewport(renderer, &real_dstrect);
slouken@5154
  1663
    real_dstrect.x = 0;
slouken@5154
  1664
    real_dstrect.y = 0;
slouken@5369
  1665
    if (dstrect) {
slouken@7563
  1666
        if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
slouken@5369
  1667
            return 0;
slouken@5369
  1668
        }
slouken@7563
  1669
        real_dstrect = *dstrect;
slouken@5154
  1670
    }
slouken@5154
  1671
slouken@5156
  1672
    if (texture->native) {
slouken@5156
  1673
        texture = texture->native;
slouken@5156
  1674
    }
slouken@5156
  1675
slouken@6260
  1676
    /* Don't draw while we're hidden */
slouken@6260
  1677
    if (renderer->hidden) {
slouken@6060
  1678
        return 0;
slouken@6060
  1679
    }
slouken@6528
  1680
slouken@6528
  1681
    frect.x = real_dstrect.x * renderer->scale.x;
slouken@6528
  1682
    frect.y = real_dstrect.y * renderer->scale.y;
slouken@6528
  1683
    frect.w = real_dstrect.w * renderer->scale.x;
slouken@6528
  1684
    frect.h = real_dstrect.h * renderer->scale.y;
slouken@6528
  1685
slouken@6528
  1686
    return renderer->RenderCopy(renderer, texture, &real_srcrect, &frect);
slouken@5154
  1687
}
slouken@5154
  1688
gabomdq@6320
  1689
gabomdq@6320
  1690
int
gabomdq@6320
  1691
SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
gabomdq@6320
  1692
               const SDL_Rect * srcrect, const SDL_Rect * dstrect,
gabomdq@6320
  1693
               const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
gabomdq@6320
  1694
{
icculus@6546
  1695
    SDL_Rect real_srcrect = { 0, 0, 0, 0 };
icculus@6546
  1696
    SDL_Rect real_dstrect = { 0, 0, 0, 0 };
gabomdq@6320
  1697
    SDL_Point real_center;
slouken@6528
  1698
    SDL_FRect frect;
slouken@6528
  1699
    SDL_FPoint fcenter;
gabomdq@6320
  1700
gabomdq@6320
  1701
    CHECK_RENDERER_MAGIC(renderer, -1);
gabomdq@6320
  1702
    CHECK_TEXTURE_MAGIC(texture, -1);
gabomdq@6320
  1703
gabomdq@6320
  1704
    if (renderer != texture->renderer) {
icculus@7037
  1705
        return SDL_SetError("Texture was not created with this renderer");
gabomdq@6320
  1706
    }
gabomdq@6320
  1707
    if (!renderer->RenderCopyEx) {
icculus@7037
  1708
        return SDL_SetError("Renderer does not support RenderCopyEx");
gabomdq@6320
  1709
    }
slouken@7191
  1710
gabomdq@6320
  1711
    real_srcrect.x = 0;
gabomdq@6320
  1712
    real_srcrect.y = 0;
gabomdq@6320
  1713
    real_srcrect.w = texture->w;
gabomdq@6320
  1714
    real_srcrect.h = texture->h;
gabomdq@6320
  1715
    if (srcrect) {
gabomdq@6320
  1716
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
gabomdq@6320
  1717
            return 0;
gabomdq@6320
  1718
        }
gabomdq@6320
  1719
    }
gabomdq@6320
  1720
gabomdq@6320
  1721
    /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
slouken@6528
  1722
    if (dstrect) {
slouken@6528
  1723
        real_dstrect = *dstrect;
slouken@6528
  1724
    } else {
slouken@6528
  1725
        SDL_RenderGetViewport(renderer, &real_dstrect);
gabomdq@6399
  1726
        real_dstrect.x = 0;
gabomdq@6399
  1727
        real_dstrect.y = 0;
gabomdq@6320
  1728
    }
gabomdq@6320
  1729
gabomdq@6320
  1730
    if (texture->native) {
gabomdq@6320
  1731
        texture = texture->native;
gabomdq@6320
  1732
    }
gabomdq@6320
  1733
gabomdq@6320
  1734
    if(center) real_center = *center;
gabomdq@6320
  1735
    else {
gabomdq@6320
  1736
        real_center.x = real_dstrect.w/2;
gabomdq@6320
  1737
        real_center.y = real_dstrect.h/2;
gabomdq@6320
  1738
    }
gabomdq@6320
  1739
slouken@6528
  1740
    frect.x = real_dstrect.x * renderer->scale.x;
slouken@6528
  1741
    frect.y = real_dstrect.y * renderer->scale.y;
slouken@6528
  1742
    frect.w = real_dstrect.w * renderer->scale.x;
slouken@6528
  1743
    frect.h = real_dstrect.h * renderer->scale.y;
slouken@6528
  1744
slouken@6528
  1745
    fcenter.x = real_center.x * renderer->scale.x;
slouken@6528
  1746
    fcenter.y = real_center.y * renderer->scale.y;
slouken@6528
  1747
slouken@6528
  1748
    return renderer->RenderCopyEx(renderer, texture, &real_srcrect, &frect, angle, &fcenter, flip);
gabomdq@6320
  1749
}
gabomdq@6320
  1750
slouken@5154
  1751
int
slouken@5154
  1752
SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@5154
  1753
                     Uint32 format, void * pixels, int pitch)
slouken@5154
  1754
{
slouken@5154
  1755
    SDL_Rect real_rect;
slouken@5154
  1756
slouken@5154
  1757
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1758
slouken@5154
  1759
    if (!renderer->RenderReadPixels) {
icculus@7037
  1760
        return SDL_Unsupported();
slouken@5154
  1761
    }
slouken@5154
  1762
slouken@5154
  1763
    if (!format) {
icculus@6389
  1764
        format = SDL_GetWindowPixelFormat(renderer->window);
slouken@5154
  1765
    }
slouken@5154
  1766
slouken@5464
  1767
    real_rect.x = renderer->viewport.x;
slouken@5464
  1768
    real_rect.y = renderer->viewport.y;
slouken@5297
  1769
    real_rect.w = renderer->viewport.w;
slouken@5297
  1770
    real_rect.h = renderer->viewport.h;
slouken@5154
  1771
    if (rect) {
slouken@5154
  1772
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@5154
  1773
            return 0;
slouken@5154
  1774
        }
slouken@5154
  1775
        if (real_rect.y > rect->y) {
slouken@5154
  1776
            pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@5154
  1777
        }
slouken@5154
  1778
        if (real_rect.x > rect->x) {
slouken@5464
  1779
            int bpp = SDL_BYTESPERPIXEL(format);
slouken@5154
  1780
            pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@5154
  1781
        }
slouken@5154
  1782
    }
slouken@5154
  1783
slouken@5154
  1784
    return renderer->RenderReadPixels(renderer, &real_rect,
slouken@5154
  1785
                                      format, pixels, pitch);
slouken@5154
  1786
}
slouken@5154
  1787
slouken@5154
  1788
void
slouken@5154
  1789
SDL_RenderPresent(SDL_Renderer * renderer)
slouken@5154
  1790
{
slouken@5154
  1791
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1792
slouken@6260
  1793
    /* Don't draw while we're hidden */
slouken@6260
  1794
    if (renderer->hidden) {
slouken@6060
  1795
        return;
slouken@6060
  1796
    }
slouken@5154
  1797
    renderer->RenderPresent(renderer);
slouken@5154
  1798
}
slouken@5154
  1799
slouken@5154
  1800
void
slouken@5154
  1801
SDL_DestroyTexture(SDL_Texture * texture)
slouken@5154
  1802
{
slouken@5154
  1803
    SDL_Renderer *renderer;
slouken@5154
  1804
slouken@5154
  1805
    CHECK_TEXTURE_MAGIC(texture, );
slouken@7419
  1806
slouken@7419
  1807
    renderer = texture->renderer;
slouken@7419
  1808
    if (texture == renderer->target) {
slouken@7419
  1809
        SDL_SetRenderTarget(renderer, NULL);
slouken@7419
  1810
    }
slouken@7419
  1811
slouken@5154
  1812
    texture->magic = NULL;
slouken@5154
  1813
slouken@5154
  1814
    if (texture->next) {
slouken@5154
  1815
        texture->next->prev = texture->prev;
slouken@5154
  1816
    }
slouken@5154
  1817
    if (texture->prev) {
slouken@5154
  1818
        texture->prev->next = texture->next;
slouken@5154
  1819
    } else {
slouken@5154
  1820
        renderer->textures = texture->next;
slouken@5154
  1821
    }
slouken@5154
  1822
slouken@5156
  1823
    if (texture->native) {
slouken@5156
  1824
        SDL_DestroyTexture(texture->native);
slouken@5156
  1825
    }
slouken@5156
  1826
    if (texture->yuv) {
slouken@5156
  1827
        SDL_SW_DestroyYUVTexture(texture->yuv);
slouken@5156
  1828
    }
slouken@7719
  1829
    SDL_free(texture->pixels);
slouken@5156
  1830
slouken@5154
  1831
    renderer->DestroyTexture(renderer, texture);
slouken@5154
  1832
    SDL_free(texture);
slouken@5154
  1833
}
slouken@5154
  1834
slouken@5154
  1835
void
slouken@5154
  1836
SDL_DestroyRenderer(SDL_Renderer * renderer)
slouken@5154
  1837
{
slouken@5154
  1838
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1839
slouken@5154
  1840
    SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
slouken@5154
  1841
slouken@5154
  1842
    /* Free existing textures for this renderer */
slouken@5154
  1843
    while (renderer->textures) {
slouken@5154
  1844
        SDL_DestroyTexture(renderer->textures);
slouken@5154
  1845
    }
slouken@5154
  1846
slouken@6417
  1847
    if (renderer->window) {
slouken@6417
  1848
        SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
slouken@6417
  1849
    }
slouken@5528
  1850
slouken@5154
  1851
    /* It's no longer magical... */
slouken@5154
  1852
    renderer->magic = NULL;
slouken@5154
  1853
slouken@5154
  1854
    /* Free the renderer instance */
slouken@5154
  1855
    renderer->DestroyRenderer(renderer);
slouken@5154
  1856
}
slouken@5154
  1857
gabomdq@6414
  1858
int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
gabomdq@6414
  1859
{
gabomdq@6414
  1860
    SDL_Renderer *renderer;
gabomdq@6414
  1861
gabomdq@6415
  1862
    CHECK_TEXTURE_MAGIC(texture, -1);
gabomdq@6414
  1863
    renderer = texture->renderer;
slouken@7318
  1864
    if (texture->native) {
slouken@7318
  1865
        return SDL_GL_BindTexture(texture->native, texw, texh);
slouken@7318
  1866
    } else if (renderer && renderer->GL_BindTexture) {
gabomdq@6414
  1867
        return renderer->GL_BindTexture(renderer, texture, texw, texh);
slouken@7318
  1868
    } else {
slouken@7318
  1869
        return SDL_Unsupported();
gabomdq@6414
  1870
    }
gabomdq@6414
  1871
}
gabomdq@6414
  1872
gabomdq@6414
  1873
int SDL_GL_UnbindTexture(SDL_Texture *texture)
gabomdq@6414
  1874
{
gabomdq@6414
  1875
    SDL_Renderer *renderer;
gabomdq@6414
  1876
gabomdq@6415
  1877
    CHECK_TEXTURE_MAGIC(texture, -1);
gabomdq@6414
  1878
    renderer = texture->renderer;
gabomdq@8249
  1879
    if (texture->native) {
gabomdq@8249
  1880
        return SDL_GL_UnbindTexture(texture->native);
gabomdq@8249
  1881
    } else if (renderer && renderer->GL_UnbindTexture) {
gabomdq@6414
  1882
        return renderer->GL_UnbindTexture(renderer, texture);
gabomdq@6414
  1883
    }
gabomdq@6414
  1884
icculus@7037
  1885
    return SDL_Unsupported();
gabomdq@6414
  1886
}
gabomdq@6414
  1887
slouken@5154
  1888
/* vi: set ts=4 sw=4 expandtab: */