src/render/SDL_render.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 06 Sep 2014 23:20:14 +0200
changeset 9125 e004bdb812ce
parent 9084 a8cbb653aea4
child 9619 b94b6d0bff0f
child 11072 4d8984d051a2
permissions -rw-r--r--
Fixed example in header file.

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