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