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