src/render/SDL_render.c
author David Ludwig <dludwig@pobox.com>
Thu, 28 Nov 2013 22:09:21 -0500
changeset 8543 b9dd3cf38585
parent 8535 e8ee0708ef5c
parent 7898 72af77fc6683
child 8583 fb2933ca805f
permissions -rw-r--r--
WinRT: merged with latest SDL 2.x/HG code

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