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