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