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