src/render/SDL_render.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 01 Jun 2012 19:51:08 -0300
changeset 6320 6077a1310907
parent 6260 fd494c5f305b
child 6337 3c91a153f398
permissions -rwxr-xr-x
RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
slouken@5154
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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@5154
    47
static const SDL_RenderDriver *render_drivers[] = {
slouken@5226
    48
#if !SDL_RENDER_DISABLED
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
slouken@5423
    64
#if SDL_VIDEO_RENDER_NDS
slouken@5423
    65
    &NDS_RenderDriver,
slouken@5423
    66
#endif
slouken@5154
    67
    &SW_RenderDriver
slouken@5226
    68
#endif /* !SDL_RENDER_DISABLED */
slouken@5154
    69
};
slouken@5154
    70
static char renderer_magic;
slouken@5154
    71
static char texture_magic;
slouken@5154
    72
slouken@5154
    73
int
slouken@5154
    74
SDL_GetNumRenderDrivers(void)
slouken@5154
    75
{
slouken@5154
    76
    return SDL_arraysize(render_drivers);
slouken@5154
    77
}
slouken@5154
    78
slouken@5154
    79
int
slouken@5154
    80
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
slouken@5154
    81
{
slouken@5154
    82
    if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
slouken@5154
    83
        SDL_SetError("index must be in the range of 0 - %d",
slouken@5154
    84
                     SDL_GetNumRenderDrivers() - 1);
slouken@5154
    85
        return -1;
slouken@5154
    86
    }
slouken@5154
    87
    *info = render_drivers[index]->info;
slouken@5154
    88
    return 0;
slouken@5154
    89
}
slouken@5154
    90
slouken@5154
    91
static int
slouken@5154
    92
SDL_RendererEventWatch(void *userdata, SDL_Event *event)
slouken@5154
    93
{
slouken@5154
    94
    SDL_Renderer *renderer = (SDL_Renderer *)userdata;
slouken@5154
    95
slouken@5297
    96
    if (event->type == SDL_WINDOWEVENT) {
slouken@5154
    97
        SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
slouken@5154
    98
        if (window == renderer->window) {
slouken@5297
    99
            if (renderer->WindowEvent) {
slouken@5297
   100
                renderer->WindowEvent(renderer, &event->window);
slouken@5297
   101
            }
slouken@5297
   102
slouken@6260
   103
            if (event->window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@5297
   104
                /* Try to keep the previous viewport centered */
slouken@5297
   105
                int w, h;
slouken@5297
   106
                SDL_Rect viewport;
slouken@5297
   107
slouken@5297
   108
                SDL_GetWindowSize(window, &w, &h);
slouken@6246
   109
                if (renderer->target) {
slouken@6246
   110
                    renderer->viewport_backup.x = (w - renderer->viewport_backup.w) / 2;
slouken@6246
   111
                    renderer->viewport_backup.y = (h - renderer->viewport_backup.h) / 2;
slouken@6246
   112
                } else {
slouken@6246
   113
                    viewport.x = (w - renderer->viewport.w) / 2;
slouken@6246
   114
                    viewport.y = (h - renderer->viewport.h) / 2;
slouken@6246
   115
                    viewport.w = renderer->viewport.w;
slouken@6246
   116
                    viewport.h = renderer->viewport.h;
slouken@6246
   117
                    SDL_RenderSetViewport(renderer, &viewport);
slouken@6246
   118
                }
slouken@6260
   119
                renderer->resized = SDL_TRUE;
slouken@6260
   120
            } else if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@6260
   121
                int w, h;
slouken@6260
   122
                SDL_Rect viewport;
slouken@6260
   123
slouken@6260
   124
                if (!renderer->resized) {
slouken@6260
   125
                    /* Window was programmatically resized, reset viewport */
slouken@6260
   126
                    SDL_GetWindowSize(window, &w, &h);
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@6260
   133
                        viewport.x = 0;
slouken@6260
   134
                        viewport.y = 0;
slouken@6260
   135
                        viewport.w = w;
slouken@6260
   136
                        viewport.h = h;
slouken@6260
   137
                        SDL_RenderSetViewport(renderer, &viewport);
slouken@6260
   138
                    }
slouken@6260
   139
                    renderer->resized = SDL_FALSE;
slouken@6260
   140
                }
slouken@6260
   141
            } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
slouken@6260
   142
                renderer->hidden = SDL_TRUE;
slouken@6260
   143
            } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
slouken@6260
   144
                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
slouken@6260
   145
                    renderer->hidden = SDL_FALSE;
slouken@6260
   146
                }
slouken@6060
   147
            } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
slouken@6260
   148
                renderer->hidden = SDL_TRUE;
slouken@6060
   149
            } else if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
slouken@6260
   150
                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
slouken@6260
   151
                    renderer->hidden = SDL_FALSE;
slouken@6260
   152
                }
slouken@5297
   153
            }
slouken@5154
   154
        }
slouken@5154
   155
    }
slouken@5154
   156
    return 0;
slouken@5154
   157
}
slouken@5154
   158
slouken@6258
   159
int
slouken@6258
   160
SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
slouken@6258
   161
                            SDL_Window **window, SDL_Renderer **renderer)
slouken@6258
   162
{
slouken@6258
   163
    *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
slouken@6258
   164
                                     SDL_WINDOWPOS_UNDEFINED,
slouken@6258
   165
                                     width, height, window_flags);
slouken@6258
   166
    if (!*window) {
slouken@6258
   167
        *renderer = NULL;
slouken@6258
   168
        return -1;
slouken@6258
   169
    }
slouken@6258
   170
slouken@6258
   171
    *renderer = SDL_CreateRenderer(*window, -1, 0);
slouken@6258
   172
    if (!*renderer) {
slouken@6258
   173
        return -1;
slouken@6258
   174
    }
slouken@6258
   175
slouken@6258
   176
    return 0;
slouken@6258
   177
}
slouken@6258
   178
slouken@5154
   179
SDL_Renderer *
slouken@5154
   180
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
slouken@5154
   181
{
slouken@5154
   182
    SDL_Renderer *renderer = NULL;
slouken@5154
   183
    int n = SDL_GetNumRenderDrivers();
slouken@5192
   184
    const char *hint;
slouken@5192
   185
slouken@5528
   186
    if (!window) {
slouken@5528
   187
        SDL_SetError("Invalid window");
slouken@5528
   188
        return NULL;
slouken@5528
   189
    }
slouken@5528
   190
slouken@5528
   191
    if (SDL_GetRenderer(window)) {
slouken@5528
   192
        SDL_SetError("Renderer already associated with window");
slouken@5528
   193
        return NULL;
slouken@5528
   194
    }
slouken@5528
   195
slouken@5192
   196
    hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
slouken@5192
   197
    if (hint) {
slouken@5192
   198
        if (*hint == '0') {
slouken@5192
   199
            flags &= ~SDL_RENDERER_PRESENTVSYNC;
slouken@5192
   200
        } else {
slouken@5192
   201
            flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@5192
   202
        }
slouken@5192
   203
    }
slouken@5154
   204
slouken@5154
   205
    if (index < 0) {
slouken@5192
   206
        hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
slouken@5192
   207
        if (hint) {
slouken@5154
   208
            for (index = 0; index < n; ++index) {
slouken@5154
   209
                const SDL_RenderDriver *driver = render_drivers[index];
slouken@5154
   210
slouken@5192
   211
                if (SDL_strcasecmp(hint, driver->info.name) == 0) {
slouken@5154
   212
                    /* Create a new renderer instance */
slouken@5154
   213
                    renderer = driver->CreateRenderer(window, flags);
slouken@5154
   214
                    break;
slouken@5154
   215
                }
slouken@5154
   216
            }
slouken@5192
   217
        }
slouken@5192
   218
slouken@5192
   219
        if (!renderer) {
slouken@5154
   220
            for (index = 0; index < n; ++index) {
slouken@5154
   221
                const SDL_RenderDriver *driver = render_drivers[index];
slouken@5154
   222
slouken@5154
   223
                if ((driver->info.flags & flags) == flags) {
slouken@5154
   224
                    /* Create a new renderer instance */
slouken@5154
   225
                    renderer = driver->CreateRenderer(window, flags);
slouken@5154
   226
                    if (renderer) {
slouken@5154
   227
                        /* Yay, we got one! */
slouken@5154
   228
                        break;
slouken@5154
   229
                    }
slouken@5154
   230
                }
slouken@5154
   231
            }
slouken@5154
   232
        }
slouken@5154
   233
        if (index == n) {
slouken@5154
   234
            SDL_SetError("Couldn't find matching render driver");
slouken@5154
   235
            return NULL;
slouken@5154
   236
        }
slouken@5154
   237
    } else {
slouken@5154
   238
        if (index >= SDL_GetNumRenderDrivers()) {
slouken@5154
   239
            SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@5154
   240
                         SDL_GetNumRenderDrivers() - 1);
slouken@5154
   241
            return NULL;
slouken@5154
   242
        }
slouken@5154
   243
        /* Create a new renderer instance */
slouken@5154
   244
        renderer = render_drivers[index]->CreateRenderer(window, flags);
slouken@5154
   245
    }
slouken@5154
   246
slouken@5154
   247
    if (renderer) {
slouken@5154
   248
        renderer->magic = &renderer_magic;
slouken@5166
   249
        renderer->window = window;
slouken@5154
   250
slouken@6260
   251
        if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
slouken@6260
   252
            renderer->hidden = SDL_TRUE;
slouken@6060
   253
        } else {
slouken@6260
   254
            renderer->hidden = SDL_FALSE;
slouken@6060
   255
        }
slouken@6060
   256
slouken@5528
   257
        SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
slouken@5528
   258
slouken@5297
   259
        SDL_RenderSetViewport(renderer, NULL);
slouken@5297
   260
slouken@5154
   261
        SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
slouken@5221
   262
slouken@5221
   263
        SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
slouken@5221
   264
                    "Created renderer: %s", renderer->info.name);
slouken@5154
   265
    }
slouken@5154
   266
    return renderer;
slouken@5154
   267
}
slouken@5154
   268
slouken@5166
   269
SDL_Renderer *
slouken@5166
   270
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
slouken@5166
   271
{
slouken@5226
   272
#if !SDL_RENDER_DISABLED
slouken@5297
   273
    SDL_Renderer *renderer;
slouken@5297
   274
slouken@5297
   275
    renderer = SW_CreateRendererForSurface(surface);
slouken@5297
   276
slouken@5297
   277
    if (renderer) {
slouken@5297
   278
        renderer->magic = &renderer_magic;
slouken@5297
   279
slouken@5297
   280
        SDL_RenderSetViewport(renderer, NULL);
slouken@5297
   281
    }
slouken@5297
   282
    return renderer;
slouken@5226
   283
#else
slouken@5226
   284
    SDL_SetError("SDL not built with rendering support");
slouken@5226
   285
    return NULL;
slouken@5226
   286
#endif /* !SDL_RENDER_DISABLED */
slouken@5166
   287
}
slouken@5166
   288
slouken@5528
   289
SDL_Renderer *
slouken@5528
   290
SDL_GetRenderer(SDL_Window * window)
slouken@5528
   291
{
slouken@5528
   292
    return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
slouken@5528
   293
}
slouken@5528
   294
slouken@5154
   295
int
slouken@5154
   296
SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
slouken@5154
   297
{
slouken@5154
   298
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   299
slouken@5154
   300
    *info = renderer->info;
slouken@5154
   301
    return 0;
slouken@5154
   302
}
slouken@5154
   303
slouken@5156
   304
static SDL_bool
slouken@5156
   305
IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
slouken@5156
   306
{
slouken@5156
   307
    Uint32 i;
slouken@5156
   308
slouken@5156
   309
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5156
   310
        if (renderer->info.texture_formats[i] == format) {
slouken@5156
   311
            return SDL_TRUE;
slouken@5156
   312
        }
slouken@5156
   313
    }
slouken@5156
   314
    return SDL_FALSE;
slouken@5156
   315
}
slouken@5156
   316
slouken@5156
   317
static Uint32
slouken@5156
   318
GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
slouken@5156
   319
{
slouken@5156
   320
    Uint32 i;
slouken@5156
   321
slouken@5268
   322
    if (SDL_ISPIXELFORMAT_FOURCC(format)) {
slouken@5268
   323
        /* Look for an exact match */
slouken@5268
   324
        for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5268
   325
            if (renderer->info.texture_formats[i] == format) {
slouken@5268
   326
                return renderer->info.texture_formats[i];
slouken@5268
   327
            }
slouken@5268
   328
        }
slouken@5268
   329
    } else {
slouken@5268
   330
        SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
slouken@5268
   331
slouken@5268
   332
        /* We just want to match the first format that has the same channels */
slouken@5268
   333
        for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5300
   334
            if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
slouken@5300
   335
                SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
slouken@5268
   336
                return renderer->info.texture_formats[i];
slouken@5268
   337
            }
slouken@5156
   338
        }
slouken@5156
   339
    }
slouken@5156
   340
    return renderer->info.texture_formats[0];
slouken@5156
   341
}
slouken@5156
   342
slouken@5154
   343
SDL_Texture *
slouken@5154
   344
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
slouken@5154
   345
{
slouken@5154
   346
    SDL_Texture *texture;
slouken@5154
   347
slouken@5154
   348
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   349
slouken@5413
   350
    if (!format) {
slouken@5413
   351
        format = renderer->info.texture_formats[0];
slouken@5413
   352
    }
slouken@5156
   353
    if (SDL_ISPIXELFORMAT_INDEXED(format)) {
slouken@5156
   354
        SDL_SetError("Palettized textures are not supported");
slouken@5156
   355
        return NULL;
slouken@5156
   356
    }
slouken@5154
   357
    if (w <= 0 || h <= 0) {
slouken@5154
   358
        SDL_SetError("Texture dimensions can't be 0");
slouken@5156
   359
        return NULL;
slouken@5154
   360
    }
slouken@5154
   361
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@5154
   362
    if (!texture) {
slouken@5154
   363
        SDL_OutOfMemory();
slouken@5156
   364
        return NULL;
slouken@5154
   365
    }
slouken@5154
   366
    texture->magic = &texture_magic;
slouken@5154
   367
    texture->format = format;
slouken@5154
   368
    texture->access = access;
slouken@5154
   369
    texture->w = w;
slouken@5154
   370
    texture->h = h;
slouken@5154
   371
    texture->r = 255;
slouken@5154
   372
    texture->g = 255;
slouken@5154
   373
    texture->b = 255;
slouken@5154
   374
    texture->a = 255;
slouken@5154
   375
    texture->renderer = renderer;
slouken@5154
   376
    texture->next = renderer->textures;
slouken@5154
   377
    if (renderer->textures) {
slouken@5154
   378
        renderer->textures->prev = texture;
slouken@5154
   379
    }
slouken@5154
   380
    renderer->textures = texture;
slouken@5154
   381
slouken@5156
   382
    if (IsSupportedFormat(renderer, format)) {
slouken@5156
   383
        if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@5156
   384
            SDL_DestroyTexture(texture);
slouken@5156
   385
            return 0;
slouken@5156
   386
        }
slouken@5156
   387
    } else {
slouken@5156
   388
        texture->native = SDL_CreateTexture(renderer,
slouken@5156
   389
                                GetClosestSupportedFormat(renderer, format),
slouken@5156
   390
                                access, w, h);
slouken@5156
   391
        if (!texture->native) {
slouken@5156
   392
            SDL_DestroyTexture(texture);
slouken@5156
   393
            return NULL;
slouken@5156
   394
        }
slouken@5156
   395
slouken@5156
   396
        if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@5156
   397
            texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
slouken@5156
   398
            if (!texture->yuv) {
slouken@5156
   399
                SDL_DestroyTexture(texture);
slouken@5156
   400
                return NULL;
slouken@5156
   401
            }
slouken@5156
   402
        } else if (access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   403
            /* The pitch is 4 byte aligned */
slouken@5156
   404
            texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
slouken@5342
   405
            texture->pixels = SDL_calloc(1, texture->pitch * h);
slouken@5156
   406
            if (!texture->pixels) {
slouken@5156
   407
                SDL_DestroyTexture(texture);
slouken@5156
   408
                return NULL;
slouken@5156
   409
            }
slouken@5156
   410
        }
slouken@5154
   411
    }
slouken@5154
   412
    return texture;
slouken@5154
   413
}
slouken@5154
   414
slouken@5154
   415
SDL_Texture *
slouken@5158
   416
SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
slouken@5154
   417
{
slouken@5158
   418
    const SDL_PixelFormat *fmt;
slouken@5158
   419
    SDL_bool needAlpha;
slouken@5158
   420
    Uint32 i;
slouken@5158
   421
    Uint32 format;
slouken@5158
   422
    SDL_Texture *texture;
slouken@5154
   423
slouken@5154
   424
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   425
slouken@5154
   426
    if (!surface) {
slouken@5154
   427
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@5154
   428
        return NULL;
slouken@5154
   429
    }
slouken@5158
   430
slouken@5158
   431
    /* See what the best texture format is */
slouken@5154
   432
    fmt = surface->format;
slouken@5158
   433
    if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
slouken@5158
   434
        needAlpha = SDL_TRUE;
slouken@5154
   435
    } else {
slouken@5158
   436
        needAlpha = SDL_FALSE;
slouken@5158
   437
    }
slouken@5158
   438
    format = renderer->info.texture_formats[0];
slouken@5158
   439
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5268
   440
        if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
slouken@5268
   441
            SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
slouken@5158
   442
            format = renderer->info.texture_formats[i];
slouken@5158
   443
            break;
slouken@5154
   444
        }
slouken@5154
   445
    }
slouken@5154
   446
slouken@5158
   447
    texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
slouken@5158
   448
                                surface->w, surface->h);
slouken@5154
   449
    if (!texture) {
slouken@5158
   450
        return NULL;
slouken@5154
   451
    }
slouken@5158
   452
slouken@5288
   453
    if (format == surface->format->format) {
slouken@5154
   454
        if (SDL_MUSTLOCK(surface)) {
slouken@5154
   455
            SDL_LockSurface(surface);
slouken@5158
   456
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
slouken@5154
   457
            SDL_UnlockSurface(surface);
slouken@5154
   458
        } else {
slouken@5158
   459
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
slouken@5154
   460
        }
slouken@5154
   461
    } else {
slouken@5297
   462
        SDL_PixelFormat *dst_fmt;
slouken@5158
   463
        SDL_Surface *temp = NULL;
slouken@5154
   464
slouken@5154
   465
        /* Set up a destination surface for the texture update */
slouken@5297
   466
        dst_fmt = SDL_AllocFormat(format);
slouken@5297
   467
        temp = SDL_ConvertSurface(surface, dst_fmt, 0);
slouken@5297
   468
        SDL_FreeFormat(dst_fmt);
slouken@5158
   469
        if (temp) {
slouken@5158
   470
            SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
slouken@5158
   471
            SDL_FreeSurface(temp);
slouken@5158
   472
        } else {
slouken@5154
   473
            SDL_DestroyTexture(texture);
slouken@5158
   474
            return NULL;
slouken@5154
   475
        }
slouken@5154
   476
    }
slouken@5154
   477
slouken@5154
   478
    {
slouken@5154
   479
        Uint8 r, g, b, a;
slouken@5154
   480
        SDL_BlendMode blendMode;
slouken@5154
   481
slouken@5154
   482
        SDL_GetSurfaceColorMod(surface, &r, &g, &b);
slouken@5154
   483
        SDL_SetTextureColorMod(texture, r, g, b);
slouken@5154
   484
slouken@5154
   485
        SDL_GetSurfaceAlphaMod(surface, &a);
slouken@5154
   486
        SDL_SetTextureAlphaMod(texture, a);
slouken@5154
   487
slouken@5154
   488
        if (SDL_GetColorKey(surface, NULL) == 0) {
slouken@5154
   489
            /* We converted to a texture with alpha format */
slouken@5154
   490
            SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
slouken@5154
   491
        } else {
slouken@5154
   492
            SDL_GetSurfaceBlendMode(surface, &blendMode);
slouken@5154
   493
            SDL_SetTextureBlendMode(texture, blendMode);
slouken@5154
   494
        }
slouken@5154
   495
    }
slouken@5154
   496
    return texture;
slouken@5154
   497
}
slouken@5154
   498
slouken@5154
   499
int
slouken@5154
   500
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
slouken@5154
   501
                 int *w, int *h)
slouken@5154
   502
{
slouken@5154
   503
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   504
slouken@5154
   505
    if (format) {
slouken@5154
   506
        *format = texture->format;
slouken@5154
   507
    }
slouken@5154
   508
    if (access) {
slouken@5154
   509
        *access = texture->access;
slouken@5154
   510
    }
slouken@5154
   511
    if (w) {
slouken@5154
   512
        *w = texture->w;
slouken@5154
   513
    }
slouken@5154
   514
    if (h) {
slouken@5154
   515
        *h = texture->h;
slouken@5154
   516
    }
slouken@5154
   517
    return 0;
slouken@5154
   518
}
slouken@5154
   519
slouken@5154
   520
int
slouken@5154
   521
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
slouken@5154
   522
{
slouken@5154
   523
    SDL_Renderer *renderer;
slouken@5154
   524
slouken@5154
   525
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   526
slouken@5154
   527
    renderer = texture->renderer;
slouken@5154
   528
    if (r < 255 || g < 255 || b < 255) {
slouken@5154
   529
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   530
    } else {
slouken@5154
   531
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   532
    }
slouken@5154
   533
    texture->r = r;
slouken@5154
   534
    texture->g = g;
slouken@5154
   535
    texture->b = b;
slouken@5156
   536
    if (texture->native) {
slouken@5156
   537
        return SDL_SetTextureColorMod(texture->native, r, g, b);
slouken@5156
   538
    } else if (renderer->SetTextureColorMod) {
slouken@5154
   539
        return renderer->SetTextureColorMod(renderer, texture);
slouken@5154
   540
    } else {
slouken@5154
   541
        return 0;
slouken@5154
   542
    }
slouken@5154
   543
}
slouken@5154
   544
slouken@5154
   545
int
slouken@5154
   546
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
slouken@5154
   547
                       Uint8 * b)
slouken@5154
   548
{
slouken@5154
   549
    SDL_Renderer *renderer;
slouken@5154
   550
slouken@5154
   551
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   552
slouken@5154
   553
    renderer = texture->renderer;
slouken@5154
   554
    if (r) {
slouken@5154
   555
        *r = texture->r;
slouken@5154
   556
    }
slouken@5154
   557
    if (g) {
slouken@5154
   558
        *g = texture->g;
slouken@5154
   559
    }
slouken@5154
   560
    if (b) {
slouken@5154
   561
        *b = texture->b;
slouken@5154
   562
    }
slouken@5154
   563
    return 0;
slouken@5154
   564
}
slouken@5154
   565
slouken@5154
   566
int
slouken@5154
   567
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
slouken@5154
   568
{
slouken@5154
   569
    SDL_Renderer *renderer;
slouken@5154
   570
slouken@5154
   571
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   572
slouken@5154
   573
    renderer = texture->renderer;
slouken@5154
   574
    if (alpha < 255) {
slouken@5154
   575
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   576
    } else {
slouken@5154
   577
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   578
    }
slouken@5154
   579
    texture->a = alpha;
slouken@5156
   580
    if (texture->native) {
slouken@5156
   581
        return SDL_SetTextureAlphaMod(texture->native, alpha);
slouken@5156
   582
    } else if (renderer->SetTextureAlphaMod) {
slouken@5154
   583
        return renderer->SetTextureAlphaMod(renderer, texture);
slouken@5154
   584
    } else {
slouken@5154
   585
        return 0;
slouken@5154
   586
    }
slouken@5154
   587
}
slouken@5154
   588
slouken@5154
   589
int
slouken@5154
   590
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
slouken@5154
   591
{
slouken@5154
   592
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   593
slouken@5154
   594
    if (alpha) {
slouken@5154
   595
        *alpha = texture->a;
slouken@5154
   596
    }
slouken@5154
   597
    return 0;
slouken@5154
   598
}
slouken@5154
   599
slouken@5154
   600
int
slouken@5154
   601
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
slouken@5154
   602
{
slouken@5154
   603
    SDL_Renderer *renderer;
slouken@5154
   604
slouken@5154
   605
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   606
slouken@5154
   607
    renderer = texture->renderer;
slouken@5154
   608
    texture->blendMode = blendMode;
slouken@5156
   609
    if (texture->native) {
slouken@5180
   610
        return SDL_SetTextureBlendMode(texture->native, blendMode);
slouken@5156
   611
    } else if (renderer->SetTextureBlendMode) {
slouken@5154
   612
        return renderer->SetTextureBlendMode(renderer, texture);
slouken@5154
   613
    } else {
slouken@5154
   614
        return 0;
slouken@5154
   615
    }
slouken@5154
   616
}
slouken@5154
   617
slouken@5154
   618
int
slouken@5154
   619
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
slouken@5154
   620
{
slouken@5154
   621
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   622
slouken@5154
   623
    if (blendMode) {
slouken@5154
   624
        *blendMode = texture->blendMode;
slouken@5154
   625
    }
slouken@5154
   626
    return 0;
slouken@5154
   627
}
slouken@5154
   628
slouken@5156
   629
static int
slouken@5156
   630
SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   631
                     const void *pixels, int pitch)
slouken@5156
   632
{
slouken@5156
   633
    SDL_Texture *native = texture->native;
slouken@5156
   634
    SDL_Rect full_rect;
slouken@5156
   635
slouken@5156
   636
    if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
slouken@5156
   637
        return -1;
slouken@5156
   638
    }
slouken@5156
   639
slouken@5156
   640
    full_rect.x = 0;
slouken@5156
   641
    full_rect.y = 0;
slouken@5156
   642
    full_rect.w = texture->w;
slouken@5156
   643
    full_rect.h = texture->h;
slouken@5156
   644
    rect = &full_rect;
slouken@5156
   645
slouken@5156
   646
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   647
        /* We can lock the texture and copy to it */
slouken@5156
   648
        void *native_pixels;
slouken@5156
   649
        int native_pitch;
slouken@5156
   650
slouken@5156
   651
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   652
            return -1;
slouken@5156
   653
        }
slouken@5156
   654
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@5156
   655
                            rect->w, rect->h, native_pixels, native_pitch);
slouken@5156
   656
        SDL_UnlockTexture(native);
slouken@5156
   657
    } else {
slouken@5156
   658
        /* Use a temporary buffer for updating */
slouken@5156
   659
        void *temp_pixels;
slouken@5156
   660
        int temp_pitch;
slouken@5156
   661
slouken@5156
   662
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@5156
   663
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5156
   664
        if (!temp_pixels) {
slouken@5156
   665
            SDL_OutOfMemory();
slouken@5156
   666
            return -1;
slouken@5156
   667
        }
slouken@5156
   668
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@5156
   669
                            rect->w, rect->h, temp_pixels, temp_pitch);
slouken@5156
   670
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@5156
   671
        SDL_free(temp_pixels);
slouken@5156
   672
    }
slouken@5156
   673
    return 0;
slouken@5156
   674
}
slouken@5156
   675
slouken@5156
   676
static int
slouken@5156
   677
SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   678
                        const void *pixels, int pitch)
slouken@5156
   679
{
slouken@5156
   680
    SDL_Texture *native = texture->native;
slouken@5156
   681
slouken@5156
   682
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   683
        /* We can lock the texture and copy to it */
slouken@5156
   684
        void *native_pixels;
slouken@5156
   685
        int native_pitch;
slouken@5156
   686
slouken@5156
   687
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   688
            return -1;
slouken@5156
   689
        }
slouken@5156
   690
        SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   691
                          texture->format, pixels, pitch,
slouken@5156
   692
                          native->format, native_pixels, native_pitch);
slouken@5156
   693
        SDL_UnlockTexture(native);
slouken@5156
   694
    } else {
slouken@5156
   695
        /* Use a temporary buffer for updating */
slouken@5156
   696
        void *temp_pixels;
slouken@5156
   697
        int temp_pitch;
slouken@5156
   698
slouken@5156
   699
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@5156
   700
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5156
   701
        if (!temp_pixels) {
slouken@5156
   702
            SDL_OutOfMemory();
slouken@5156
   703
            return -1;
slouken@5156
   704
        }
slouken@5156
   705
        SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   706
                          texture->format, pixels, pitch,
slouken@5156
   707
                          native->format, temp_pixels, temp_pitch);
slouken@5156
   708
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@5156
   709
        SDL_free(temp_pixels);
slouken@5156
   710
    }
slouken@5156
   711
    return 0;
slouken@5156
   712
}
slouken@5156
   713
slouken@5154
   714
int
slouken@5154
   715
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   716
                  const void *pixels, int pitch)
slouken@5154
   717
{
slouken@5154
   718
    SDL_Renderer *renderer;
slouken@5154
   719
    SDL_Rect full_rect;
slouken@5154
   720
slouken@5154
   721
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   722
slouken@5154
   723
    if (!rect) {
slouken@5154
   724
        full_rect.x = 0;
slouken@5154
   725
        full_rect.y = 0;
slouken@5154
   726
        full_rect.w = texture->w;
slouken@5154
   727
        full_rect.h = texture->h;
slouken@5154
   728
        rect = &full_rect;
slouken@5154
   729
    }
slouken@5156
   730
slouken@5156
   731
    if (texture->yuv) {
slouken@5156
   732
        return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
slouken@5156
   733
    } else if (texture->native) {
slouken@5156
   734
        return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
slouken@5156
   735
    } else {
slouken@5156
   736
        renderer = texture->renderer;
slouken@5156
   737
        return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@5156
   738
    }
slouken@5156
   739
}
slouken@5156
   740
slouken@5156
   741
static int
slouken@5156
   742
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   743
                   void **pixels, int *pitch)
slouken@5156
   744
{
slouken@5156
   745
    return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
slouken@5156
   746
}
slouken@5156
   747
slouken@5156
   748
static int
slouken@5156
   749
SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   750
                      void **pixels, int *pitch)
slouken@5156
   751
{
slouken@5156
   752
    texture->locked_rect = *rect;
slouken@5156
   753
    *pixels = (void *) ((Uint8 *) texture->pixels +
slouken@5156
   754
                        rect->y * texture->pitch +
slouken@5156
   755
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5156
   756
    *pitch = texture->pitch;
slouken@5156
   757
    return 0;
slouken@5154
   758
}
slouken@5154
   759
slouken@5154
   760
int
slouken@5156
   761
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   762
                void **pixels, int *pitch)
slouken@5154
   763
{
slouken@5154
   764
    SDL_Renderer *renderer;
slouken@5154
   765
    SDL_Rect full_rect;
slouken@5154
   766
slouken@5154
   767
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   768
slouken@5154
   769
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   770
        SDL_SetError("SDL_LockTexture(): texture must be streaming");
slouken@5154
   771
        return -1;
slouken@5154
   772
    }
slouken@5156
   773
slouken@5154
   774
    if (!rect) {
slouken@5154
   775
        full_rect.x = 0;
slouken@5154
   776
        full_rect.y = 0;
slouken@5154
   777
        full_rect.w = texture->w;
slouken@5154
   778
        full_rect.h = texture->h;
slouken@5154
   779
        rect = &full_rect;
slouken@5154
   780
    }
slouken@5156
   781
slouken@5156
   782
    if (texture->yuv) {
slouken@5156
   783
        return SDL_LockTextureYUV(texture, rect, pixels, pitch);
slouken@5156
   784
    } else if (texture->native) {
slouken@5156
   785
        return SDL_LockTextureNative(texture, rect, pixels, pitch);
slouken@5156
   786
    } else {
slouken@5156
   787
        renderer = texture->renderer;
slouken@5156
   788
        return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
slouken@5156
   789
    }
slouken@5156
   790
}
slouken@5156
   791
slouken@5156
   792
static void
slouken@5156
   793
SDL_UnlockTextureYUV(SDL_Texture * texture)
slouken@5156
   794
{
slouken@5156
   795
    SDL_Texture *native = texture->native;
slouken@5156
   796
    void *native_pixels;
slouken@5156
   797
    int native_pitch;
slouken@5156
   798
    SDL_Rect rect;
slouken@5156
   799
slouken@5156
   800
    rect.x = 0;
slouken@5156
   801
    rect.y = 0;
slouken@5156
   802
    rect.w = texture->w;
slouken@5156
   803
    rect.h = texture->h;
slouken@5156
   804
slouken@5156
   805
    if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   806
        return;
slouken@5156
   807
    }
slouken@5156
   808
    SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
slouken@5156
   809
                        rect.w, rect.h, native_pixels, native_pitch);
slouken@5156
   810
    SDL_UnlockTexture(native);
slouken@5156
   811
}
slouken@5156
   812
slouken@6044
   813
static void
slouken@5156
   814
SDL_UnlockTextureNative(SDL_Texture * texture)
slouken@5156
   815
{
slouken@5156
   816
    SDL_Texture *native = texture->native;
slouken@5156
   817
    void *native_pixels;
slouken@5156
   818
    int native_pitch;
slouken@5156
   819
    const SDL_Rect *rect = &texture->locked_rect;
slouken@5156
   820
    const void* pixels = (void *) ((Uint8 *) texture->pixels +
slouken@5156
   821
                        rect->y * texture->pitch +
slouken@5156
   822
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5156
   823
    int pitch = texture->pitch;
slouken@5156
   824
slouken@5156
   825
    if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   826
        return;
slouken@5156
   827
    }
slouken@5156
   828
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   829
                      texture->format, pixels, pitch,
slouken@5156
   830
                      native->format, native_pixels, native_pitch);
slouken@5156
   831
    SDL_UnlockTexture(native);
slouken@5154
   832
}
slouken@5154
   833
slouken@5154
   834
void
slouken@5154
   835
SDL_UnlockTexture(SDL_Texture * texture)
slouken@5154
   836
{
slouken@5154
   837
    SDL_Renderer *renderer;
slouken@5154
   838
slouken@5154
   839
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
   840
slouken@5154
   841
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   842
        return;
slouken@5154
   843
    }
slouken@5156
   844
    if (texture->yuv) {
slouken@5156
   845
        SDL_UnlockTextureYUV(texture);
slouken@5156
   846
    } else if (texture->native) {
slouken@5156
   847
        SDL_UnlockTextureNative(texture);
slouken@5156
   848
    } else {
slouken@5156
   849
        renderer = texture->renderer;
slouken@5156
   850
        renderer->UnlockTexture(renderer, texture);
slouken@5154
   851
    }
slouken@5154
   852
}
slouken@5154
   853
slouken@6246
   854
SDL_bool
slouken@6246
   855
SDL_RenderTargetSupported(SDL_Renderer *renderer)
slouken@6246
   856
{
slouken@6247
   857
    if (!renderer || !renderer->SetRenderTarget) {
slouken@6246
   858
        return SDL_FALSE;
slouken@6246
   859
    }
slouken@6246
   860
    return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
slouken@6246
   861
}
slouken@6246
   862
slouken@6246
   863
int
slouken@6247
   864
SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@6246
   865
{
slouken@6246
   866
    SDL_Rect viewport;
slouken@6246
   867
slouken@6246
   868
    if (!SDL_RenderTargetSupported(renderer)) {
slouken@6246
   869
        SDL_Unsupported();
slouken@6246
   870
        return -1;
slouken@6246
   871
    }
slouken@6246
   872
    if (texture == renderer->target) {
slouken@6246
   873
        /* Nothing to do! */
slouken@6246
   874
        return 0;
slouken@6246
   875
    }
slouken@6246
   876
slouken@6246
   877
    /* texture == NULL is valid and means reset the target to the window */
slouken@6246
   878
    if (texture) {
slouken@6246
   879
        CHECK_TEXTURE_MAGIC(texture, -1);
slouken@6246
   880
        if (renderer != texture->renderer) {
slouken@6246
   881
            SDL_SetError("Texture was not created with this renderer");
slouken@6246
   882
            return -1;
slouken@6246
   883
        }
slouken@6246
   884
        if (!(texture->access & SDL_TEXTUREACCESS_TARGET)) {
slouken@6246
   885
            SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
slouken@6246
   886
            return -1;
slouken@6246
   887
        }
slouken@6246
   888
        if (texture->native) {
slouken@6246
   889
            /* Always render to the native texture */
slouken@6246
   890
            texture = texture->native;
slouken@6246
   891
        }
slouken@6246
   892
    }
slouken@6246
   893
slouken@6246
   894
    if (texture && !renderer->target) {
slouken@6246
   895
        /* Make a backup of the viewport */
slouken@6246
   896
        renderer->viewport_backup = renderer->viewport;
slouken@6246
   897
    }
slouken@6246
   898
    renderer->target = texture;
slouken@6246
   899
slouken@6247
   900
    if (renderer->SetRenderTarget(renderer, texture) < 0) {
slouken@6246
   901
        return -1;
slouken@6246
   902
    }
slouken@6246
   903
slouken@6246
   904
    if (texture) {
slouken@6246
   905
        viewport.x = 0;
slouken@6246
   906
        viewport.y = 0;
slouken@6246
   907
        viewport.w = texture->w;
slouken@6246
   908
        viewport.h = texture->h;
slouken@6246
   909
    } else {
slouken@6246
   910
        viewport = renderer->viewport_backup;
slouken@6246
   911
    }
slouken@6246
   912
    if (SDL_RenderSetViewport(renderer, &viewport) < 0) {
slouken@6246
   913
        return -1;
slouken@6246
   914
    }
slouken@6246
   915
slouken@6246
   916
    /* All set! */
slouken@6246
   917
    return 0;
slouken@6246
   918
}
slouken@6246
   919
slouken@5297
   920
int
slouken@5297
   921
SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5297
   922
{
slouken@5297
   923
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5297
   924
slouken@5297
   925
    if (rect) {
slouken@5297
   926
        renderer->viewport = *rect;
slouken@5297
   927
    } else {
slouken@5297
   928
        renderer->viewport.x = 0;
slouken@5297
   929
        renderer->viewport.y = 0;
slouken@5297
   930
        if (renderer->window) {
slouken@5297
   931
            SDL_GetWindowSize(renderer->window,
slouken@5297
   932
                              &renderer->viewport.w, &renderer->viewport.h);
slouken@5297
   933
        } else {
slouken@5297
   934
            /* This will be filled in by UpdateViewport() */
slouken@5297
   935
            renderer->viewport.w = 0;
slouken@5297
   936
            renderer->viewport.h = 0;
slouken@5297
   937
        }
slouken@5297
   938
    }
slouken@5297
   939
    return renderer->UpdateViewport(renderer);
slouken@5297
   940
}
slouken@5297
   941
slouken@5224
   942
void
slouken@5297
   943
SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
slouken@5224
   944
{
slouken@5224
   945
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5224
   946
slouken@5297
   947
    *rect = renderer->viewport;
slouken@5224
   948
}
slouken@5224
   949
slouken@5154
   950
int
slouken@5154
   951
SDL_SetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
   952
                       Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5154
   953
{
slouken@5154
   954
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   955
slouken@5154
   956
    renderer->r = r;
slouken@5154
   957
    renderer->g = g;
slouken@5154
   958
    renderer->b = b;
slouken@5154
   959
    renderer->a = a;
slouken@5154
   960
    return 0;
slouken@5154
   961
}
slouken@5154
   962
slouken@5154
   963
int
slouken@5154
   964
SDL_GetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
   965
                       Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@5154
   966
{
slouken@5154
   967
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   968
slouken@5154
   969
    if (r) {
slouken@5154
   970
        *r = renderer->r;
slouken@5154
   971
    }
slouken@5154
   972
    if (g) {
slouken@5154
   973
        *g = renderer->g;
slouken@5154
   974
    }
slouken@5154
   975
    if (b) {
slouken@5154
   976
        *b = renderer->b;
slouken@5154
   977
    }
slouken@5154
   978
    if (a) {
slouken@5154
   979
        *a = renderer->a;
slouken@5154
   980
    }
slouken@5154
   981
    return 0;
slouken@5154
   982
}
slouken@5154
   983
slouken@5154
   984
int
slouken@5154
   985
SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
slouken@5154
   986
{
slouken@5154
   987
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   988
slouken@5154
   989
    renderer->blendMode = blendMode;
slouken@5154
   990
    return 0;
slouken@5154
   991
}
slouken@5154
   992
slouken@5154
   993
int
slouken@5154
   994
SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
slouken@5154
   995
{
slouken@5154
   996
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   997
slouken@5154
   998
    *blendMode = renderer->blendMode;
slouken@5154
   999
    return 0;
slouken@5154
  1000
}
slouken@5154
  1001
slouken@5154
  1002
int
slouken@5154
  1003
SDL_RenderClear(SDL_Renderer * renderer)
slouken@5154
  1004
{
slouken@5154
  1005
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1006
slouken@6260
  1007
    /* Don't draw while we're hidden */
slouken@6260
  1008
    if (renderer->hidden) {
slouken@6060
  1009
        return 0;
slouken@6060
  1010
    }
slouken@5154
  1011
    return renderer->RenderClear(renderer);
slouken@5154
  1012
}
slouken@5154
  1013
slouken@5154
  1014
int
slouken@5154
  1015
SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
slouken@5154
  1016
{
slouken@5154
  1017
    SDL_Point point;
slouken@5154
  1018
slouken@5154
  1019
    point.x = x;
slouken@5154
  1020
    point.y = y;
slouken@5154
  1021
    return SDL_RenderDrawPoints(renderer, &point, 1);
slouken@5154
  1022
}
slouken@5154
  1023
slouken@5154
  1024
int
slouken@5154
  1025
SDL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@5154
  1026
                     const SDL_Point * points, int count)
slouken@5154
  1027
{
slouken@5154
  1028
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1029
slouken@5154
  1030
    if (!points) {
slouken@5154
  1031
        SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
slouken@5154
  1032
        return -1;
slouken@5154
  1033
    }
slouken@5154
  1034
    if (count < 1) {
slouken@5154
  1035
        return 0;
slouken@5154
  1036
    }
slouken@6260
  1037
    /* Don't draw while we're hidden */
slouken@6260
  1038
    if (renderer->hidden) {
slouken@6060
  1039
        return 0;
slouken@6060
  1040
    }
slouken@5154
  1041
    return renderer->RenderDrawPoints(renderer, points, count);
slouken@5154
  1042
}
slouken@5154
  1043
slouken@5154
  1044
int
slouken@5154
  1045
SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
slouken@5154
  1046
{
slouken@5154
  1047
    SDL_Point points[2];
slouken@5154
  1048
slouken@5154
  1049
    points[0].x = x1;
slouken@5154
  1050
    points[0].y = y1;
slouken@5154
  1051
    points[1].x = x2;
slouken@5154
  1052
    points[1].y = y2;
slouken@5154
  1053
    return SDL_RenderDrawLines(renderer, points, 2);
slouken@5154
  1054
}
slouken@5154
  1055
slouken@5154
  1056
int
slouken@5154
  1057
SDL_RenderDrawLines(SDL_Renderer * renderer,
slouken@5154
  1058
                    const SDL_Point * points, int count)
slouken@5154
  1059
{
slouken@5154
  1060
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1061
slouken@5154
  1062
    if (!points) {
slouken@5154
  1063
        SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
slouken@5154
  1064
        return -1;
slouken@5154
  1065
    }
slouken@5154
  1066
    if (count < 2) {
slouken@5154
  1067
        return 0;
slouken@5154
  1068
    }
slouken@6260
  1069
    /* Don't draw while we're hidden */
slouken@6260
  1070
    if (renderer->hidden) {
slouken@6060
  1071
        return 0;
slouken@6060
  1072
    }
slouken@5154
  1073
    return renderer->RenderDrawLines(renderer, points, count);
slouken@5154
  1074
}
slouken@5154
  1075
slouken@5154
  1076
int
slouken@5154
  1077
SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
  1078
{
slouken@5154
  1079
    SDL_Rect full_rect;
slouken@5154
  1080
    SDL_Point points[5];
slouken@5154
  1081
slouken@5154
  1082
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1083
slouken@5154
  1084
    /* If 'rect' == NULL, then outline the whole surface */
slouken@5154
  1085
    if (!rect) {
slouken@5154
  1086
        full_rect.x = 0;
slouken@5154
  1087
        full_rect.y = 0;
slouken@5297
  1088
        full_rect.w = renderer->viewport.w;
slouken@5297
  1089
        full_rect.h = renderer->viewport.h;
slouken@5154
  1090
        rect = &full_rect;
slouken@5154
  1091
    }
slouken@5154
  1092
slouken@5154
  1093
    points[0].x = rect->x;
slouken@5154
  1094
    points[0].y = rect->y;
slouken@5154
  1095
    points[1].x = rect->x+rect->w-1;
slouken@5154
  1096
    points[1].y = rect->y;
slouken@5154
  1097
    points[2].x = rect->x+rect->w-1;
slouken@5154
  1098
    points[2].y = rect->y+rect->h-1;
slouken@5154
  1099
    points[3].x = rect->x;
slouken@5154
  1100
    points[3].y = rect->y+rect->h-1;
slouken@5154
  1101
    points[4].x = rect->x;
slouken@5154
  1102
    points[4].y = rect->y;
slouken@5154
  1103
    return SDL_RenderDrawLines(renderer, points, 5);
slouken@5154
  1104
}
slouken@5154
  1105
slouken@5154
  1106
int
slouken@5154
  1107
SDL_RenderDrawRects(SDL_Renderer * renderer,
slouken@5297
  1108
                    const SDL_Rect * rects, int count)
slouken@5154
  1109
{
slouken@5154
  1110
    int i;
slouken@5154
  1111
slouken@5154
  1112
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1113
slouken@5154
  1114
    if (!rects) {
slouken@5154
  1115
        SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
slouken@5154
  1116
        return -1;
slouken@5154
  1117
    }
slouken@5154
  1118
    if (count < 1) {
slouken@5154
  1119
        return 0;
slouken@5154
  1120
    }
slouken@5154
  1121
slouken@6260
  1122
    /* Don't draw while we're hidden */
slouken@6260
  1123
    if (renderer->hidden) {
slouken@6060
  1124
        return 0;
slouken@6060
  1125
    }
slouken@5154
  1126
    for (i = 0; i < count; ++i) {
slouken@5297
  1127
        if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
slouken@5154
  1128
            return -1;
slouken@5154
  1129
        }
slouken@5154
  1130
    }
slouken@5154
  1131
    return 0;
slouken@5154
  1132
}
slouken@5154
  1133
slouken@5154
  1134
int
slouken@5154
  1135
SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
  1136
{
slouken@5331
  1137
    SDL_Rect full_rect;
slouken@6232
  1138
slouken@5331
  1139
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@6232
  1140
slouken@5331
  1141
    /* If 'rect' == NULL, then outline the whole surface */
slouken@5331
  1142
    if (!rect) {
slouken@5331
  1143
        full_rect.x = 0;
slouken@5331
  1144
        full_rect.y = 0;
slouken@5331
  1145
        full_rect.w = renderer->viewport.w;
slouken@5331
  1146
        full_rect.h = renderer->viewport.h;
slouken@5331
  1147
        rect = &full_rect;
slouken@5331
  1148
    }
slouken@5297
  1149
    return SDL_RenderFillRects(renderer, rect, 1);
slouken@5154
  1150
}
slouken@5154
  1151
slouken@5154
  1152
int
slouken@5154
  1153
SDL_RenderFillRects(SDL_Renderer * renderer,
slouken@5297
  1154
                    const SDL_Rect * rects, int count)
slouken@5154
  1155
{
slouken@5154
  1156
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1157
slouken@5154
  1158
    if (!rects) {
slouken@5154
  1159
        SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
slouken@5154
  1160
        return -1;
slouken@5154
  1161
    }
slouken@5154
  1162
    if (count < 1) {
slouken@5154
  1163
        return 0;
slouken@5154
  1164
    }
slouken@6260
  1165
    /* Don't draw while we're hidden */
slouken@6260
  1166
    if (renderer->hidden) {
slouken@6060
  1167
        return 0;
slouken@6060
  1168
    }
slouken@5154
  1169
    return renderer->RenderFillRects(renderer, rects, count);
slouken@5154
  1170
}
slouken@5154
  1171
slouken@5154
  1172
int
slouken@5154
  1173
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5154
  1174
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@5154
  1175
{
slouken@5154
  1176
    SDL_Window *window;
slouken@5154
  1177
    SDL_Rect real_srcrect;
slouken@5154
  1178
    SDL_Rect real_dstrect;
slouken@5154
  1179
slouken@5154
  1180
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1181
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
  1182
slouken@5154
  1183
    if (renderer != texture->renderer) {
slouken@5154
  1184
        SDL_SetError("Texture was not created with this renderer");
slouken@5154
  1185
        return -1;
slouken@5154
  1186
    }
slouken@5154
  1187
    window = renderer->window;
slouken@5154
  1188
slouken@5154
  1189
    real_srcrect.x = 0;
slouken@5154
  1190
    real_srcrect.y = 0;
slouken@5154
  1191
    real_srcrect.w = texture->w;
slouken@5154
  1192
    real_srcrect.h = texture->h;
slouken@5154
  1193
    if (srcrect) {
slouken@5154
  1194
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
slouken@5154
  1195
            return 0;
slouken@5154
  1196
        }
slouken@5154
  1197
    }
slouken@5154
  1198
slouken@5154
  1199
    real_dstrect.x = 0;
slouken@5154
  1200
    real_dstrect.y = 0;
slouken@5297
  1201
    real_dstrect.w = renderer->viewport.w;
slouken@5297
  1202
    real_dstrect.h = renderer->viewport.h;
slouken@5369
  1203
    if (dstrect) {
slouken@5369
  1204
        if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
slouken@5369
  1205
            return 0;
slouken@5369
  1206
        }
slouken@5369
  1207
        /* Clip srcrect by the same amount as dstrect was clipped */
slouken@5369
  1208
        if (dstrect->w != real_dstrect.w) {
slouken@5369
  1209
            int deltax = (real_dstrect.x - dstrect->x);
slouken@5369
  1210
            int deltaw = (real_dstrect.w - dstrect->w);
slouken@5369
  1211
            real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
slouken@5369
  1212
            real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
slouken@5369
  1213
        }
slouken@5369
  1214
        if (dstrect->h != real_dstrect.h) {
slouken@5369
  1215
            int deltay = (real_dstrect.y - dstrect->y);
slouken@5369
  1216
            int deltah = (real_dstrect.h - dstrect->h);
slouken@5369
  1217
            real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
slouken@5369
  1218
            real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
slouken@5369
  1219
        }
slouken@5154
  1220
    }
slouken@5154
  1221
slouken@5156
  1222
    if (texture->native) {
slouken@5156
  1223
        texture = texture->native;
slouken@5156
  1224
    }
slouken@5156
  1225
slouken@6260
  1226
    /* Don't draw while we're hidden */
slouken@6260
  1227
    if (renderer->hidden) {
slouken@6060
  1228
        return 0;
slouken@6060
  1229
    }
slouken@5154
  1230
    return renderer->RenderCopy(renderer, texture, &real_srcrect,
slouken@5154
  1231
                                &real_dstrect);
slouken@5154
  1232
}
slouken@5154
  1233
gabomdq@6320
  1234
gabomdq@6320
  1235
int
gabomdq@6320
  1236
SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
gabomdq@6320
  1237
               const SDL_Rect * srcrect, const SDL_Rect * dstrect,
gabomdq@6320
  1238
               const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
gabomdq@6320
  1239
{
gabomdq@6320
  1240
    SDL_Window *window;
gabomdq@6320
  1241
    SDL_Rect real_srcrect, real_dstrect;
gabomdq@6320
  1242
    SDL_Point real_center;
gabomdq@6320
  1243
gabomdq@6320
  1244
    CHECK_RENDERER_MAGIC(renderer, -1);
gabomdq@6320
  1245
    CHECK_TEXTURE_MAGIC(texture, -1);
gabomdq@6320
  1246
gabomdq@6320
  1247
    if (renderer != texture->renderer) {
gabomdq@6320
  1248
        SDL_SetError("Texture was not created with this renderer");
gabomdq@6320
  1249
        return -1;
gabomdq@6320
  1250
    }
gabomdq@6320
  1251
    if (!renderer->RenderCopyEx) {
gabomdq@6320
  1252
        SDL_SetError("Renderer does not support RenderCopyEx");
gabomdq@6320
  1253
        return -1;
gabomdq@6320
  1254
    }
gabomdq@6320
  1255
    
gabomdq@6320
  1256
    window = renderer->window;
gabomdq@6320
  1257
gabomdq@6320
  1258
    real_srcrect.x = 0;
gabomdq@6320
  1259
    real_srcrect.y = 0;
gabomdq@6320
  1260
    real_srcrect.w = texture->w;
gabomdq@6320
  1261
    real_srcrect.h = texture->h;
gabomdq@6320
  1262
    if (srcrect) {
gabomdq@6320
  1263
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
gabomdq@6320
  1264
            return 0;
gabomdq@6320
  1265
        }
gabomdq@6320
  1266
    }
gabomdq@6320
  1267
gabomdq@6320
  1268
    /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
gabomdq@6320
  1269
    if (dstrect) real_dstrect = *dstrect;
gabomdq@6320
  1270
    else {
gabomdq@6320
  1271
        real_srcrect.x = 0;
gabomdq@6320
  1272
        real_srcrect.y = 0;
gabomdq@6320
  1273
        real_srcrect.w = renderer->viewport.w;
gabomdq@6320
  1274
        real_srcrect.h = renderer->viewport.h;
gabomdq@6320
  1275
    }
gabomdq@6320
  1276
gabomdq@6320
  1277
    if (texture->native) {
gabomdq@6320
  1278
        texture = texture->native;
gabomdq@6320
  1279
    }
gabomdq@6320
  1280
gabomdq@6320
  1281
    if(center) real_center = *center;
gabomdq@6320
  1282
    else {
gabomdq@6320
  1283
        real_center.x = real_dstrect.w/2;
gabomdq@6320
  1284
        real_center.y = real_dstrect.h/2;
gabomdq@6320
  1285
    }
gabomdq@6320
  1286
gabomdq@6320
  1287
    return renderer->RenderCopyEx(renderer, texture, &real_srcrect, &real_dstrect, angle, &real_center, flip);
gabomdq@6320
  1288
}
gabomdq@6320
  1289
slouken@5154
  1290
int
slouken@5154
  1291
SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@5154
  1292
                     Uint32 format, void * pixels, int pitch)
slouken@5154
  1293
{
slouken@5154
  1294
    SDL_Window *window;
slouken@5154
  1295
    SDL_Rect real_rect;
slouken@5154
  1296
slouken@5154
  1297
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1298
slouken@5154
  1299
    if (!renderer->RenderReadPixels) {
slouken@5154
  1300
        SDL_Unsupported();
slouken@5154
  1301
        return -1;
slouken@5154
  1302
    }
slouken@5154
  1303
    window = renderer->window;
slouken@5154
  1304
slouken@5154
  1305
    if (!format) {
slouken@5154
  1306
        format = SDL_GetWindowPixelFormat(window);
slouken@5154
  1307
    }
slouken@5154
  1308
slouken@5464
  1309
    real_rect.x = renderer->viewport.x;
slouken@5464
  1310
    real_rect.y = renderer->viewport.y;
slouken@5297
  1311
    real_rect.w = renderer->viewport.w;
slouken@5297
  1312
    real_rect.h = renderer->viewport.h;
slouken@5154
  1313
    if (rect) {
slouken@5154
  1314
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@5154
  1315
            return 0;
slouken@5154
  1316
        }
slouken@5154
  1317
        if (real_rect.y > rect->y) {
slouken@5154
  1318
            pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@5154
  1319
        }
slouken@5154
  1320
        if (real_rect.x > rect->x) {
slouken@5464
  1321
            int bpp = SDL_BYTESPERPIXEL(format);
slouken@5154
  1322
            pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@5154
  1323
        }
slouken@5154
  1324
    }
slouken@5154
  1325
slouken@5154
  1326
    return renderer->RenderReadPixels(renderer, &real_rect,
slouken@5154
  1327
                                      format, pixels, pitch);
slouken@5154
  1328
}
slouken@5154
  1329
slouken@5154
  1330
void
slouken@5154
  1331
SDL_RenderPresent(SDL_Renderer * renderer)
slouken@5154
  1332
{
slouken@5154
  1333
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1334
slouken@6260
  1335
    /* Don't draw while we're hidden */
slouken@6260
  1336
    if (renderer->hidden) {
slouken@6060
  1337
        return;
slouken@6060
  1338
    }
slouken@5154
  1339
    renderer->RenderPresent(renderer);
slouken@5154
  1340
}
slouken@5154
  1341
slouken@5154
  1342
void
slouken@5154
  1343
SDL_DestroyTexture(SDL_Texture * texture)
slouken@5154
  1344
{
slouken@5154
  1345
    SDL_Renderer *renderer;
slouken@5154
  1346
slouken@5154
  1347
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
  1348
    texture->magic = NULL;
slouken@5154
  1349
slouken@5154
  1350
    renderer = texture->renderer;
slouken@5154
  1351
    if (texture->next) {
slouken@5154
  1352
        texture->next->prev = texture->prev;
slouken@5154
  1353
    }
slouken@5154
  1354
    if (texture->prev) {
slouken@5154
  1355
        texture->prev->next = texture->next;
slouken@5154
  1356
    } else {
slouken@5154
  1357
        renderer->textures = texture->next;
slouken@5154
  1358
    }
slouken@5154
  1359
slouken@5156
  1360
    if (texture->native) {
slouken@5156
  1361
        SDL_DestroyTexture(texture->native);
slouken@5156
  1362
    }
slouken@5156
  1363
    if (texture->yuv) {
slouken@5156
  1364
        SDL_SW_DestroyYUVTexture(texture->yuv);
slouken@5156
  1365
    }
slouken@5156
  1366
    if (texture->pixels) {
slouken@5156
  1367
        SDL_free(texture->pixels);
slouken@5156
  1368
    }
slouken@5156
  1369
slouken@5154
  1370
    renderer->DestroyTexture(renderer, texture);
slouken@5154
  1371
    SDL_free(texture);
slouken@5154
  1372
}
slouken@5154
  1373
slouken@5154
  1374
void
slouken@5154
  1375
SDL_DestroyRenderer(SDL_Renderer * renderer)
slouken@5154
  1376
{
slouken@5154
  1377
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1378
slouken@5154
  1379
    SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
slouken@5154
  1380
slouken@5154
  1381
    /* Free existing textures for this renderer */
slouken@5154
  1382
    while (renderer->textures) {
slouken@5154
  1383
        SDL_DestroyTexture(renderer->textures);
slouken@5154
  1384
    }
slouken@5154
  1385
slouken@5528
  1386
    SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
slouken@5528
  1387
slouken@5154
  1388
    /* It's no longer magical... */
slouken@5154
  1389
    renderer->magic = NULL;
slouken@5154
  1390
slouken@5154
  1391
    /* Free the renderer instance */
slouken@5154
  1392
    renderer->DestroyRenderer(renderer);
slouken@5154
  1393
}
slouken@5154
  1394
slouken@5154
  1395
/* vi: set ts=4 sw=4 expandtab: */