src/render/SDL_render.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 29 May 2013 03:07:55 -0700
changeset 7239 04dda95ba22c
parent 7191 75360622e65f
child 7240 39eb4958950a
permissions -rw-r--r--
Fixed bug 1622 - SDL_RenderSetViewport with empty SDL_Rect raises wrong error for OpenGL rendering backend

It's now legal to set an empty viewport rect - it will prevent any rendering.

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