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