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