src/render/SDL_render.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 02 Feb 2011 14:34:54 -0800
changeset 5154 fb424691cfc7
child 5156 307ccc9c135e
permissions -rw-r--r--
Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
slouken@5154
     1
/*
slouken@5154
     2
    SDL - Simple DirectMedia Layer
slouken@5154
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@5154
     4
slouken@5154
     5
    This library is free software; you can redistribute it and/or
slouken@5154
     6
    modify it under the terms of the GNU Lesser General Public
slouken@5154
     7
    License as published by the Free Software Foundation; either
slouken@5154
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@5154
     9
slouken@5154
    10
    This library is distributed in the hope that it will be useful,
slouken@5154
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5154
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5154
    13
    Lesser General Public License for more details.
slouken@5154
    14
slouken@5154
    15
    You should have received a copy of the GNU Lesser General Public
slouken@5154
    16
    License along with this library; if not, write to the Free Software
slouken@5154
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5154
    18
slouken@5154
    19
    Sam Lantinga
slouken@5154
    20
    slouken@libsdl.org
slouken@5154
    21
*/
slouken@5154
    22
#include "SDL_config.h"
slouken@5154
    23
slouken@5154
    24
/* The SDL 2D rendering system */
slouken@5154
    25
slouken@5154
    26
#include "SDL_render.h"
slouken@5154
    27
#include "SDL_sysrender.h"
slouken@5154
    28
#include "../video/SDL_pixels_c.h"
slouken@5154
    29
slouken@5154
    30
slouken@5154
    31
#define CHECK_RENDERER_MAGIC(renderer, retval) \
slouken@5154
    32
    if (!renderer || renderer->magic != &renderer_magic) { \
slouken@5154
    33
        SDL_SetError("Invalid renderer"); \
slouken@5154
    34
        return retval; \
slouken@5154
    35
    }
slouken@5154
    36
slouken@5154
    37
#define CHECK_TEXTURE_MAGIC(texture, retval) \
slouken@5154
    38
    if (!texture || texture->magic != &texture_magic) { \
slouken@5154
    39
        SDL_SetError("Invalid texture"); \
slouken@5154
    40
        return retval; \
slouken@5154
    41
    }
slouken@5154
    42
slouken@5154
    43
slouken@5154
    44
static const SDL_RenderDriver *render_drivers[] = {
slouken@5154
    45
#if SDL_VIDEO_RENDER_D3D
slouken@5154
    46
    &D3D_RenderDriver,
slouken@5154
    47
#endif
slouken@5154
    48
#if SDL_VIDEO_RENDER_OGL
slouken@5154
    49
    &GL_RenderDriver,
slouken@5154
    50
#endif
slouken@5154
    51
#if SDL_VIDEO_RENDER_OGL_ES
slouken@5154
    52
    &GL_ES_RenderDriver,
slouken@5154
    53
#endif
slouken@5154
    54
    &SW_RenderDriver
slouken@5154
    55
};
slouken@5154
    56
static char renderer_magic;
slouken@5154
    57
static char texture_magic;
slouken@5154
    58
slouken@5154
    59
int
slouken@5154
    60
SDL_GetNumRenderDrivers(void)
slouken@5154
    61
{
slouken@5154
    62
    return SDL_arraysize(render_drivers);
slouken@5154
    63
}
slouken@5154
    64
slouken@5154
    65
int
slouken@5154
    66
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
slouken@5154
    67
{
slouken@5154
    68
    if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
slouken@5154
    69
        SDL_SetError("index must be in the range of 0 - %d",
slouken@5154
    70
                     SDL_GetNumRenderDrivers() - 1);
slouken@5154
    71
        return -1;
slouken@5154
    72
    }
slouken@5154
    73
    *info = render_drivers[index]->info;
slouken@5154
    74
    return 0;
slouken@5154
    75
}
slouken@5154
    76
slouken@5154
    77
static int
slouken@5154
    78
SDL_RendererEventWatch(void *userdata, SDL_Event *event)
slouken@5154
    79
{
slouken@5154
    80
    SDL_Renderer *renderer = (SDL_Renderer *)userdata;
slouken@5154
    81
slouken@5154
    82
    if (event->type == SDL_WINDOWEVENT && renderer->WindowEvent) {
slouken@5154
    83
        SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
slouken@5154
    84
        if (window == renderer->window) {
slouken@5154
    85
            renderer->WindowEvent(renderer, &event->window);
slouken@5154
    86
        }
slouken@5154
    87
    }
slouken@5154
    88
    return 0;
slouken@5154
    89
}
slouken@5154
    90
slouken@5154
    91
SDL_Renderer *
slouken@5154
    92
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
slouken@5154
    93
{
slouken@5154
    94
    SDL_Renderer *renderer = NULL;
slouken@5154
    95
    int n = SDL_GetNumRenderDrivers();
slouken@5154
    96
slouken@5154
    97
    if (index < 0) {
slouken@5154
    98
        char *override = SDL_getenv("SDL_VIDEO_RENDERER");
slouken@5154
    99
slouken@5154
   100
        if (override) {
slouken@5154
   101
            for (index = 0; index < n; ++index) {
slouken@5154
   102
                const SDL_RenderDriver *driver = render_drivers[index];
slouken@5154
   103
slouken@5154
   104
                if (SDL_strcasecmp(override, driver->info.name) == 0) {
slouken@5154
   105
                    /* Create a new renderer instance */
slouken@5154
   106
                    renderer = driver->CreateRenderer(window, flags);
slouken@5154
   107
                    break;
slouken@5154
   108
                }
slouken@5154
   109
            }
slouken@5154
   110
        } else {
slouken@5154
   111
            for (index = 0; index < n; ++index) {
slouken@5154
   112
                const SDL_RenderDriver *driver = render_drivers[index];
slouken@5154
   113
slouken@5154
   114
                if ((driver->info.flags & flags) == flags) {
slouken@5154
   115
                    /* Create a new renderer instance */
slouken@5154
   116
                    renderer = driver->CreateRenderer(window, flags);
slouken@5154
   117
                    if (renderer) {
slouken@5154
   118
                        /* Yay, we got one! */
slouken@5154
   119
                        break;
slouken@5154
   120
                    }
slouken@5154
   121
                }
slouken@5154
   122
            }
slouken@5154
   123
        }
slouken@5154
   124
        if (index == n) {
slouken@5154
   125
            SDL_SetError("Couldn't find matching render driver");
slouken@5154
   126
            return NULL;
slouken@5154
   127
        }
slouken@5154
   128
    } else {
slouken@5154
   129
        if (index >= SDL_GetNumRenderDrivers()) {
slouken@5154
   130
            SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@5154
   131
                         SDL_GetNumRenderDrivers() - 1);
slouken@5154
   132
            return NULL;
slouken@5154
   133
        }
slouken@5154
   134
        /* Create a new renderer instance */
slouken@5154
   135
        renderer = render_drivers[index]->CreateRenderer(window, flags);
slouken@5154
   136
    }
slouken@5154
   137
slouken@5154
   138
    if (renderer) {
slouken@5154
   139
        renderer->magic = &renderer_magic;
slouken@5154
   140
slouken@5154
   141
        SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
slouken@5154
   142
    }
slouken@5154
   143
    return renderer;
slouken@5154
   144
}
slouken@5154
   145
slouken@5154
   146
int
slouken@5154
   147
SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
slouken@5154
   148
{
slouken@5154
   149
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   150
slouken@5154
   151
    *info = renderer->info;
slouken@5154
   152
    return 0;
slouken@5154
   153
}
slouken@5154
   154
slouken@5154
   155
SDL_Texture *
slouken@5154
   156
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
slouken@5154
   157
{
slouken@5154
   158
    SDL_Texture *texture;
slouken@5154
   159
slouken@5154
   160
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   161
slouken@5154
   162
    if (w <= 0 || h <= 0) {
slouken@5154
   163
        SDL_SetError("Texture dimensions can't be 0");
slouken@5154
   164
        return 0;
slouken@5154
   165
    }
slouken@5154
   166
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@5154
   167
    if (!texture) {
slouken@5154
   168
        SDL_OutOfMemory();
slouken@5154
   169
        return 0;
slouken@5154
   170
    }
slouken@5154
   171
    texture->magic = &texture_magic;
slouken@5154
   172
    texture->format = format;
slouken@5154
   173
    texture->access = access;
slouken@5154
   174
    texture->w = w;
slouken@5154
   175
    texture->h = h;
slouken@5154
   176
    texture->r = 255;
slouken@5154
   177
    texture->g = 255;
slouken@5154
   178
    texture->b = 255;
slouken@5154
   179
    texture->a = 255;
slouken@5154
   180
    texture->renderer = renderer;
slouken@5154
   181
    texture->next = renderer->textures;
slouken@5154
   182
    if (renderer->textures) {
slouken@5154
   183
        renderer->textures->prev = texture;
slouken@5154
   184
    }
slouken@5154
   185
    renderer->textures = texture;
slouken@5154
   186
slouken@5154
   187
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@5154
   188
        SDL_DestroyTexture(texture);
slouken@5154
   189
        return 0;
slouken@5154
   190
    }
slouken@5154
   191
    return texture;
slouken@5154
   192
}
slouken@5154
   193
slouken@5154
   194
SDL_Texture *
slouken@5154
   195
SDL_CreateTextureFromSurface(SDL_Renderer * renderer, Uint32 format, SDL_Surface * surface)
slouken@5154
   196
{
slouken@5154
   197
    SDL_Texture *texture;
slouken@5154
   198
    Uint32 requested_format = format;
slouken@5154
   199
    SDL_PixelFormat *fmt;
slouken@5154
   200
    int bpp;
slouken@5154
   201
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@5154
   202
slouken@5154
   203
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   204
slouken@5154
   205
    if (!surface) {
slouken@5154
   206
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@5154
   207
        return NULL;
slouken@5154
   208
    }
slouken@5154
   209
    fmt = surface->format;
slouken@5154
   210
slouken@5154
   211
    if (format) {
slouken@5154
   212
        if (!SDL_PixelFormatEnumToMasks
slouken@5154
   213
            (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5154
   214
            SDL_SetError("Unknown pixel format");
slouken@5154
   215
            return 0;
slouken@5154
   216
        }
slouken@5154
   217
    } else {
slouken@5154
   218
        SDL_bool hasColorkey;
slouken@5154
   219
        SDL_BlendMode blendMode;
slouken@5154
   220
        SDL_bool hasBlending;
slouken@5154
   221
slouken@5154
   222
        hasColorkey = (SDL_GetColorKey(surface, NULL) == 0);
slouken@5154
   223
slouken@5154
   224
        SDL_GetSurfaceBlendMode(surface, &blendMode);
slouken@5154
   225
        hasBlending = (blendMode == SDL_BLENDMODE_BLEND);
slouken@5154
   226
slouken@5154
   227
        if (surface->format->Amask || (!hasColorkey && !hasBlending)) {
slouken@5154
   228
            Uint32 it;
slouken@5154
   229
            int pfmt;
slouken@5154
   230
slouken@5154
   231
            /* Pixel formats, sorted by best first */
slouken@5154
   232
            static const Uint32 sdl_pformats[] = {
slouken@5154
   233
                SDL_PIXELFORMAT_ARGB8888,
slouken@5154
   234
                SDL_PIXELFORMAT_RGBA8888,
slouken@5154
   235
                SDL_PIXELFORMAT_ABGR8888,
slouken@5154
   236
                SDL_PIXELFORMAT_BGRA8888,
slouken@5154
   237
                SDL_PIXELFORMAT_RGB888,
slouken@5154
   238
                SDL_PIXELFORMAT_BGR888,
slouken@5154
   239
                SDL_PIXELFORMAT_RGB24,
slouken@5154
   240
                SDL_PIXELFORMAT_BGR24,
slouken@5154
   241
                SDL_PIXELFORMAT_RGB565,
slouken@5154
   242
                SDL_PIXELFORMAT_BGR565,
slouken@5154
   243
                SDL_PIXELFORMAT_ARGB1555,
slouken@5154
   244
                SDL_PIXELFORMAT_RGBA5551,
slouken@5154
   245
                SDL_PIXELFORMAT_ABGR1555,
slouken@5154
   246
                SDL_PIXELFORMAT_BGRA5551,
slouken@5154
   247
                SDL_PIXELFORMAT_RGB555,
slouken@5154
   248
                SDL_PIXELFORMAT_BGR555,
slouken@5154
   249
                SDL_PIXELFORMAT_ARGB4444,
slouken@5154
   250
                SDL_PIXELFORMAT_RGBA4444,
slouken@5154
   251
                SDL_PIXELFORMAT_ABGR4444,
slouken@5154
   252
                SDL_PIXELFORMAT_BGRA4444,
slouken@5154
   253
                SDL_PIXELFORMAT_RGB444,
slouken@5154
   254
                SDL_PIXELFORMAT_ARGB2101010,
slouken@5154
   255
                SDL_PIXELFORMAT_RGB332,
slouken@5154
   256
                SDL_PIXELFORMAT_UNKNOWN
slouken@5154
   257
            };
slouken@5154
   258
slouken@5154
   259
            bpp = fmt->BitsPerPixel;
slouken@5154
   260
            Rmask = fmt->Rmask;
slouken@5154
   261
            Gmask = fmt->Gmask;
slouken@5154
   262
            Bmask = fmt->Bmask;
slouken@5154
   263
            Amask = fmt->Amask;
slouken@5154
   264
slouken@5154
   265
            format =
slouken@5154
   266
                SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@5154
   267
            if (!format) {
slouken@5154
   268
                SDL_SetError("Unknown pixel format");
slouken@5154
   269
                return 0;
slouken@5154
   270
            }
slouken@5154
   271
slouken@5154
   272
            /* Search requested format in the supported texture */
slouken@5154
   273
            /* formats by current renderer                      */
slouken@5154
   274
            for (it = 0; it < renderer->info.num_texture_formats; it++) {
slouken@5154
   275
                if (renderer->info.texture_formats[it] == format) {
slouken@5154
   276
                    break;
slouken@5154
   277
                }
slouken@5154
   278
            }
slouken@5154
   279
slouken@5154
   280
            /* If requested format can't be found, search any best */
slouken@5154
   281
            /* format which renderer provides                      */
slouken@5154
   282
            if (it == renderer->info.num_texture_formats) {
slouken@5154
   283
                pfmt = 0;
slouken@5154
   284
                for (;;) {
slouken@5154
   285
                    if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
slouken@5154
   286
                        break;
slouken@5154
   287
                    }
slouken@5154
   288
slouken@5154
   289
                    for (it = 0; it < renderer->info.num_texture_formats;
slouken@5154
   290
                         it++) {
slouken@5154
   291
                        if (renderer->info.texture_formats[it] ==
slouken@5154
   292
                            sdl_pformats[pfmt]) {
slouken@5154
   293
                            break;
slouken@5154
   294
                        }
slouken@5154
   295
                    }
slouken@5154
   296
slouken@5154
   297
                    if (it != renderer->info.num_texture_formats) {
slouken@5154
   298
                        /* The best format has been found */
slouken@5154
   299
                        break;
slouken@5154
   300
                    }
slouken@5154
   301
                    pfmt++;
slouken@5154
   302
                }
slouken@5154
   303
slouken@5154
   304
                /* If any format can't be found, then return an error */
slouken@5154
   305
                if (it == renderer->info.num_texture_formats) {
slouken@5154
   306
                    SDL_SetError
slouken@5154
   307
                        ("Any of the supported pixel formats can't be found");
slouken@5154
   308
                    return 0;
slouken@5154
   309
                }
slouken@5154
   310
slouken@5154
   311
                /* Convert found pixel format back to color masks */
slouken@5154
   312
                if (SDL_PixelFormatEnumToMasks
slouken@5154
   313
                    (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
slouken@5154
   314
                     &Bmask, &Amask) != SDL_TRUE) {
slouken@5154
   315
                    SDL_SetError("Unknown pixel format");
slouken@5154
   316
                    return 0;
slouken@5154
   317
                }
slouken@5154
   318
            }
slouken@5154
   319
        } else {
slouken@5154
   320
            /* Need a format with alpha */
slouken@5154
   321
            Uint32 it;
slouken@5154
   322
            int apfmt;
slouken@5154
   323
slouken@5154
   324
            /* Pixel formats with alpha, sorted by best first */
slouken@5154
   325
            static const Uint32 sdl_alpha_pformats[] = {
slouken@5154
   326
                SDL_PIXELFORMAT_ARGB8888,
slouken@5154
   327
                SDL_PIXELFORMAT_RGBA8888,
slouken@5154
   328
                SDL_PIXELFORMAT_ABGR8888,
slouken@5154
   329
                SDL_PIXELFORMAT_BGRA8888,
slouken@5154
   330
                SDL_PIXELFORMAT_ARGB1555,
slouken@5154
   331
                SDL_PIXELFORMAT_RGBA5551,
slouken@5154
   332
                SDL_PIXELFORMAT_ABGR1555,
slouken@5154
   333
                SDL_PIXELFORMAT_BGRA5551,
slouken@5154
   334
                SDL_PIXELFORMAT_ARGB4444,
slouken@5154
   335
                SDL_PIXELFORMAT_RGBA4444,
slouken@5154
   336
                SDL_PIXELFORMAT_ABGR4444,
slouken@5154
   337
                SDL_PIXELFORMAT_BGRA4444,
slouken@5154
   338
                SDL_PIXELFORMAT_ARGB2101010,
slouken@5154
   339
                SDL_PIXELFORMAT_UNKNOWN
slouken@5154
   340
            };
slouken@5154
   341
slouken@5154
   342
            if (surface->format->Amask) {
slouken@5154
   343
                /* If surface already has alpha, then try an original */
slouken@5154
   344
                /* surface format first                               */
slouken@5154
   345
                bpp = fmt->BitsPerPixel;
slouken@5154
   346
                Rmask = fmt->Rmask;
slouken@5154
   347
                Gmask = fmt->Gmask;
slouken@5154
   348
                Bmask = fmt->Bmask;
slouken@5154
   349
                Amask = fmt->Amask;
slouken@5154
   350
            } else {
slouken@5154
   351
                bpp = 32;
slouken@5154
   352
                Rmask = 0x00FF0000;
slouken@5154
   353
                Gmask = 0x0000FF00;
slouken@5154
   354
                Bmask = 0x000000FF;
slouken@5154
   355
                Amask = 0xFF000000;
slouken@5154
   356
            }
slouken@5154
   357
slouken@5154
   358
            format =
slouken@5154
   359
                SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@5154
   360
            if (!format) {
slouken@5154
   361
                SDL_SetError("Unknown pixel format");
slouken@5154
   362
                return 0;
slouken@5154
   363
            }
slouken@5154
   364
slouken@5154
   365
            /* Search this format in the supported texture formats */
slouken@5154
   366
            /* by current renderer                                 */
slouken@5154
   367
            for (it = 0; it < renderer->info.num_texture_formats; it++) {
slouken@5154
   368
                if (renderer->info.texture_formats[it] == format) {
slouken@5154
   369
                    break;
slouken@5154
   370
                }
slouken@5154
   371
            }
slouken@5154
   372
slouken@5154
   373
            /* If this format can't be found, search any best       */
slouken@5154
   374
            /* compatible format with alpha which renderer provides */
slouken@5154
   375
            if (it == renderer->info.num_texture_formats) {
slouken@5154
   376
                apfmt = 0;
slouken@5154
   377
                for (;;) {
slouken@5154
   378
                    if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
slouken@5154
   379
                        break;
slouken@5154
   380
                    }
slouken@5154
   381
slouken@5154
   382
                    for (it = 0; it < renderer->info.num_texture_formats;
slouken@5154
   383
                         it++) {
slouken@5154
   384
                        if (renderer->info.texture_formats[it] ==
slouken@5154
   385
                            sdl_alpha_pformats[apfmt]) {
slouken@5154
   386
                            break;
slouken@5154
   387
                        }
slouken@5154
   388
                    }
slouken@5154
   389
slouken@5154
   390
                    if (it != renderer->info.num_texture_formats) {
slouken@5154
   391
                        /* Compatible format has been found */
slouken@5154
   392
                        break;
slouken@5154
   393
                    }
slouken@5154
   394
                    apfmt++;
slouken@5154
   395
                }
slouken@5154
   396
slouken@5154
   397
                /* If compatible format can't be found, then return an error */
slouken@5154
   398
                if (it == renderer->info.num_texture_formats) {
slouken@5154
   399
                    SDL_SetError("Compatible pixel format can't be found");
slouken@5154
   400
                    return 0;
slouken@5154
   401
                }
slouken@5154
   402
slouken@5154
   403
                /* Convert found pixel format back to color masks */
slouken@5154
   404
                if (SDL_PixelFormatEnumToMasks
slouken@5154
   405
                    (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
slouken@5154
   406
                     &Bmask, &Amask) != SDL_TRUE) {
slouken@5154
   407
                    SDL_SetError("Unknown pixel format");
slouken@5154
   408
                    return 0;
slouken@5154
   409
                }
slouken@5154
   410
            }
slouken@5154
   411
        }
slouken@5154
   412
slouken@5154
   413
        format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@5154
   414
        if (!format) {
slouken@5154
   415
            SDL_SetError("Unknown pixel format");
slouken@5154
   416
            return 0;
slouken@5154
   417
        }
slouken@5154
   418
    }
slouken@5154
   419
slouken@5154
   420
    texture =
slouken@5154
   421
        SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
slouken@5154
   422
                          surface->w, surface->h);
slouken@5154
   423
    if (!texture && !requested_format) {
slouken@5154
   424
        SDL_DisplayMode desktop_mode;
slouken@5154
   425
        SDL_GetDesktopDisplayMode(&desktop_mode);
slouken@5154
   426
        format = desktop_mode.format;
slouken@5154
   427
        texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
slouken@5154
   428
                                    surface->w, surface->h);
slouken@5154
   429
    }
slouken@5154
   430
    if (!texture) {
slouken@5154
   431
        return 0;
slouken@5154
   432
    }
slouken@5154
   433
    if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
slouken@5154
   434
        && Bmask == fmt->Bmask && Amask == fmt->Amask) {
slouken@5154
   435
        if (SDL_MUSTLOCK(surface)) {
slouken@5154
   436
            SDL_LockSurface(surface);
slouken@5154
   437
            SDL_UpdateTexture(texture, NULL, surface->pixels,
slouken@5154
   438
                              surface->pitch);
slouken@5154
   439
            SDL_UnlockSurface(surface);
slouken@5154
   440
        } else {
slouken@5154
   441
            SDL_UpdateTexture(texture, NULL, surface->pixels,
slouken@5154
   442
                              surface->pitch);
slouken@5154
   443
        }
slouken@5154
   444
    } else {
slouken@5154
   445
        SDL_PixelFormat dst_fmt;
slouken@5154
   446
        SDL_Surface *dst = NULL;
slouken@5154
   447
slouken@5154
   448
        /* Set up a destination surface for the texture update */
slouken@5154
   449
        SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
slouken@5154
   450
        dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
slouken@5154
   451
        if (dst) {
slouken@5154
   452
            SDL_UpdateTexture(texture, NULL, dst->pixels, dst->pitch);
slouken@5154
   453
            SDL_FreeSurface(dst);
slouken@5154
   454
        }
slouken@5154
   455
        if (!dst) {
slouken@5154
   456
            SDL_DestroyTexture(texture);
slouken@5154
   457
            return 0;
slouken@5154
   458
        }
slouken@5154
   459
    }
slouken@5154
   460
slouken@5154
   461
    {
slouken@5154
   462
        Uint8 r, g, b, a;
slouken@5154
   463
        SDL_BlendMode blendMode;
slouken@5154
   464
slouken@5154
   465
        SDL_GetSurfaceColorMod(surface, &r, &g, &b);
slouken@5154
   466
        SDL_SetTextureColorMod(texture, r, g, b);
slouken@5154
   467
slouken@5154
   468
        SDL_GetSurfaceAlphaMod(surface, &a);
slouken@5154
   469
        SDL_SetTextureAlphaMod(texture, a);
slouken@5154
   470
slouken@5154
   471
        if (SDL_GetColorKey(surface, NULL) == 0) {
slouken@5154
   472
            /* We converted to a texture with alpha format */
slouken@5154
   473
            SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
slouken@5154
   474
        } else {
slouken@5154
   475
            SDL_GetSurfaceBlendMode(surface, &blendMode);
slouken@5154
   476
            SDL_SetTextureBlendMode(texture, blendMode);
slouken@5154
   477
        }
slouken@5154
   478
    }
slouken@5154
   479
    return texture;
slouken@5154
   480
}
slouken@5154
   481
slouken@5154
   482
int
slouken@5154
   483
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
slouken@5154
   484
                 int *w, int *h)
slouken@5154
   485
{
slouken@5154
   486
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   487
slouken@5154
   488
    if (format) {
slouken@5154
   489
        *format = texture->format;
slouken@5154
   490
    }
slouken@5154
   491
    if (access) {
slouken@5154
   492
        *access = texture->access;
slouken@5154
   493
    }
slouken@5154
   494
    if (w) {
slouken@5154
   495
        *w = texture->w;
slouken@5154
   496
    }
slouken@5154
   497
    if (h) {
slouken@5154
   498
        *h = texture->h;
slouken@5154
   499
    }
slouken@5154
   500
    return 0;
slouken@5154
   501
}
slouken@5154
   502
slouken@5154
   503
int
slouken@5154
   504
SDL_QueryTexturePixels(SDL_Texture * texture, void **pixels, int *pitch)
slouken@5154
   505
{
slouken@5154
   506
    SDL_Renderer *renderer;
slouken@5154
   507
slouken@5154
   508
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   509
slouken@5154
   510
    renderer = texture->renderer;
slouken@5154
   511
    if (!renderer->QueryTexturePixels) {
slouken@5154
   512
        SDL_Unsupported();
slouken@5154
   513
        return -1;
slouken@5154
   514
    }
slouken@5154
   515
    return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
slouken@5154
   516
}
slouken@5154
   517
slouken@5154
   518
int
slouken@5154
   519
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
slouken@5154
   520
{
slouken@5154
   521
    SDL_Renderer *renderer;
slouken@5154
   522
slouken@5154
   523
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   524
slouken@5154
   525
    renderer = texture->renderer;
slouken@5154
   526
    if (r < 255 || g < 255 || b < 255) {
slouken@5154
   527
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   528
    } else {
slouken@5154
   529
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   530
    }
slouken@5154
   531
    texture->r = r;
slouken@5154
   532
    texture->g = g;
slouken@5154
   533
    texture->b = b;
slouken@5154
   534
    if (renderer->SetTextureColorMod) {
slouken@5154
   535
        return renderer->SetTextureColorMod(renderer, texture);
slouken@5154
   536
    } else {
slouken@5154
   537
        return 0;
slouken@5154
   538
    }
slouken@5154
   539
}
slouken@5154
   540
slouken@5154
   541
int
slouken@5154
   542
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
slouken@5154
   543
                       Uint8 * b)
slouken@5154
   544
{
slouken@5154
   545
    SDL_Renderer *renderer;
slouken@5154
   546
slouken@5154
   547
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   548
slouken@5154
   549
    renderer = texture->renderer;
slouken@5154
   550
    if (r) {
slouken@5154
   551
        *r = texture->r;
slouken@5154
   552
    }
slouken@5154
   553
    if (g) {
slouken@5154
   554
        *g = texture->g;
slouken@5154
   555
    }
slouken@5154
   556
    if (b) {
slouken@5154
   557
        *b = texture->b;
slouken@5154
   558
    }
slouken@5154
   559
    return 0;
slouken@5154
   560
}
slouken@5154
   561
slouken@5154
   562
int
slouken@5154
   563
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
slouken@5154
   564
{
slouken@5154
   565
    SDL_Renderer *renderer;
slouken@5154
   566
slouken@5154
   567
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   568
slouken@5154
   569
    renderer = texture->renderer;
slouken@5154
   570
    if (alpha < 255) {
slouken@5154
   571
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   572
    } else {
slouken@5154
   573
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   574
    }
slouken@5154
   575
    texture->a = alpha;
slouken@5154
   576
    if (renderer->SetTextureAlphaMod) {
slouken@5154
   577
        return renderer->SetTextureAlphaMod(renderer, texture);
slouken@5154
   578
    } else {
slouken@5154
   579
        return 0;
slouken@5154
   580
    }
slouken@5154
   581
}
slouken@5154
   582
slouken@5154
   583
int
slouken@5154
   584
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
slouken@5154
   585
{
slouken@5154
   586
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   587
slouken@5154
   588
    if (alpha) {
slouken@5154
   589
        *alpha = texture->a;
slouken@5154
   590
    }
slouken@5154
   591
    return 0;
slouken@5154
   592
}
slouken@5154
   593
slouken@5154
   594
int
slouken@5154
   595
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
slouken@5154
   596
{
slouken@5154
   597
    SDL_Renderer *renderer;
slouken@5154
   598
slouken@5154
   599
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   600
slouken@5154
   601
    renderer = texture->renderer;
slouken@5154
   602
    texture->blendMode = blendMode;
slouken@5154
   603
    if (renderer->SetTextureBlendMode) {
slouken@5154
   604
        return renderer->SetTextureBlendMode(renderer, texture);
slouken@5154
   605
    } else {
slouken@5154
   606
        return 0;
slouken@5154
   607
    }
slouken@5154
   608
}
slouken@5154
   609
slouken@5154
   610
int
slouken@5154
   611
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
slouken@5154
   612
{
slouken@5154
   613
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   614
slouken@5154
   615
    if (blendMode) {
slouken@5154
   616
        *blendMode = texture->blendMode;
slouken@5154
   617
    }
slouken@5154
   618
    return 0;
slouken@5154
   619
}
slouken@5154
   620
slouken@5154
   621
int
slouken@5154
   622
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   623
                  const void *pixels, int pitch)
slouken@5154
   624
{
slouken@5154
   625
    SDL_Renderer *renderer;
slouken@5154
   626
    SDL_Rect full_rect;
slouken@5154
   627
slouken@5154
   628
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   629
slouken@5154
   630
    renderer = texture->renderer;
slouken@5154
   631
    if (!renderer->UpdateTexture) {
slouken@5154
   632
        SDL_Unsupported();
slouken@5154
   633
        return -1;
slouken@5154
   634
    }
slouken@5154
   635
    if (!rect) {
slouken@5154
   636
        full_rect.x = 0;
slouken@5154
   637
        full_rect.y = 0;
slouken@5154
   638
        full_rect.w = texture->w;
slouken@5154
   639
        full_rect.h = texture->h;
slouken@5154
   640
        rect = &full_rect;
slouken@5154
   641
    }
slouken@5154
   642
    return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@5154
   643
}
slouken@5154
   644
slouken@5154
   645
int
slouken@5154
   646
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect, int markDirty,
slouken@5154
   647
                void **pixels, int *pitch)
slouken@5154
   648
{
slouken@5154
   649
    SDL_Renderer *renderer;
slouken@5154
   650
    SDL_Rect full_rect;
slouken@5154
   651
slouken@5154
   652
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   653
slouken@5154
   654
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   655
        SDL_SetError("SDL_LockTexture(): texture must be streaming");
slouken@5154
   656
        return -1;
slouken@5154
   657
    }
slouken@5154
   658
    renderer = texture->renderer;
slouken@5154
   659
    if (!renderer->LockTexture) {
slouken@5154
   660
        SDL_Unsupported();
slouken@5154
   661
        return -1;
slouken@5154
   662
    }
slouken@5154
   663
    if (!rect) {
slouken@5154
   664
        full_rect.x = 0;
slouken@5154
   665
        full_rect.y = 0;
slouken@5154
   666
        full_rect.w = texture->w;
slouken@5154
   667
        full_rect.h = texture->h;
slouken@5154
   668
        rect = &full_rect;
slouken@5154
   669
    }
slouken@5154
   670
    return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
slouken@5154
   671
                                 pitch);
slouken@5154
   672
}
slouken@5154
   673
slouken@5154
   674
void
slouken@5154
   675
SDL_UnlockTexture(SDL_Texture * texture)
slouken@5154
   676
{
slouken@5154
   677
    SDL_Renderer *renderer;
slouken@5154
   678
slouken@5154
   679
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
   680
slouken@5154
   681
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   682
        return;
slouken@5154
   683
    }
slouken@5154
   684
    renderer = texture->renderer;
slouken@5154
   685
    if (!renderer->UnlockTexture) {
slouken@5154
   686
        return;
slouken@5154
   687
    }
slouken@5154
   688
    renderer->UnlockTexture(renderer, texture);
slouken@5154
   689
}
slouken@5154
   690
slouken@5154
   691
void
slouken@5154
   692
SDL_DirtyTexture(SDL_Texture * texture, int numrects,
slouken@5154
   693
                 const SDL_Rect * rects)
slouken@5154
   694
{
slouken@5154
   695
    SDL_Renderer *renderer;
slouken@5154
   696
slouken@5154
   697
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
   698
slouken@5154
   699
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   700
        return;
slouken@5154
   701
    }
slouken@5154
   702
    renderer = texture->renderer;
slouken@5154
   703
    if (!renderer->DirtyTexture) {
slouken@5154
   704
        return;
slouken@5154
   705
    }
slouken@5154
   706
    renderer->DirtyTexture(renderer, texture, numrects, rects);
slouken@5154
   707
}
slouken@5154
   708
slouken@5154
   709
int
slouken@5154
   710
SDL_SetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
   711
                       Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5154
   712
{
slouken@5154
   713
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   714
slouken@5154
   715
    renderer->r = r;
slouken@5154
   716
    renderer->g = g;
slouken@5154
   717
    renderer->b = b;
slouken@5154
   718
    renderer->a = a;
slouken@5154
   719
    return 0;
slouken@5154
   720
}
slouken@5154
   721
slouken@5154
   722
int
slouken@5154
   723
SDL_GetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
   724
                       Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@5154
   725
{
slouken@5154
   726
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   727
slouken@5154
   728
    if (r) {
slouken@5154
   729
        *r = renderer->r;
slouken@5154
   730
    }
slouken@5154
   731
    if (g) {
slouken@5154
   732
        *g = renderer->g;
slouken@5154
   733
    }
slouken@5154
   734
    if (b) {
slouken@5154
   735
        *b = renderer->b;
slouken@5154
   736
    }
slouken@5154
   737
    if (a) {
slouken@5154
   738
        *a = renderer->a;
slouken@5154
   739
    }
slouken@5154
   740
    return 0;
slouken@5154
   741
}
slouken@5154
   742
slouken@5154
   743
int
slouken@5154
   744
SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
slouken@5154
   745
{
slouken@5154
   746
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   747
slouken@5154
   748
    renderer->blendMode = blendMode;
slouken@5154
   749
    return 0;
slouken@5154
   750
}
slouken@5154
   751
slouken@5154
   752
int
slouken@5154
   753
SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
slouken@5154
   754
{
slouken@5154
   755
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   756
slouken@5154
   757
    *blendMode = renderer->blendMode;
slouken@5154
   758
    return 0;
slouken@5154
   759
}
slouken@5154
   760
slouken@5154
   761
int
slouken@5154
   762
SDL_RenderClear(SDL_Renderer * renderer)
slouken@5154
   763
{
slouken@5154
   764
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   765
slouken@5154
   766
    if (!renderer->RenderClear) {
slouken@5154
   767
        SDL_BlendMode blendMode = renderer->blendMode;
slouken@5154
   768
        int status;
slouken@5154
   769
slouken@5154
   770
        if (blendMode >= SDL_BLENDMODE_BLEND) {
slouken@5154
   771
            SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
slouken@5154
   772
        }
slouken@5154
   773
slouken@5154
   774
        status = SDL_RenderFillRect(renderer, NULL);
slouken@5154
   775
slouken@5154
   776
        if (blendMode >= SDL_BLENDMODE_BLEND) {
slouken@5154
   777
            SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5154
   778
        }
slouken@5154
   779
        return status;
slouken@5154
   780
    }
slouken@5154
   781
    return renderer->RenderClear(renderer);
slouken@5154
   782
}
slouken@5154
   783
slouken@5154
   784
int
slouken@5154
   785
SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
slouken@5154
   786
{
slouken@5154
   787
    SDL_Point point;
slouken@5154
   788
slouken@5154
   789
    point.x = x;
slouken@5154
   790
    point.y = y;
slouken@5154
   791
    return SDL_RenderDrawPoints(renderer, &point, 1);
slouken@5154
   792
}
slouken@5154
   793
slouken@5154
   794
int
slouken@5154
   795
SDL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@5154
   796
                     const SDL_Point * points, int count)
slouken@5154
   797
{
slouken@5154
   798
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   799
slouken@5154
   800
    if (!points) {
slouken@5154
   801
        SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
slouken@5154
   802
        return -1;
slouken@5154
   803
    }
slouken@5154
   804
    if (count < 1) {
slouken@5154
   805
        return 0;
slouken@5154
   806
    }
slouken@5154
   807
    return renderer->RenderDrawPoints(renderer, points, count);
slouken@5154
   808
}
slouken@5154
   809
slouken@5154
   810
int
slouken@5154
   811
SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
slouken@5154
   812
{
slouken@5154
   813
    SDL_Point points[2];
slouken@5154
   814
slouken@5154
   815
    points[0].x = x1;
slouken@5154
   816
    points[0].y = y1;
slouken@5154
   817
    points[1].x = x2;
slouken@5154
   818
    points[1].y = y2;
slouken@5154
   819
    return SDL_RenderDrawLines(renderer, points, 2);
slouken@5154
   820
}
slouken@5154
   821
slouken@5154
   822
int
slouken@5154
   823
SDL_RenderDrawLines(SDL_Renderer * renderer,
slouken@5154
   824
                    const SDL_Point * points, int count)
slouken@5154
   825
{
slouken@5154
   826
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   827
slouken@5154
   828
    if (!points) {
slouken@5154
   829
        SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
slouken@5154
   830
        return -1;
slouken@5154
   831
    }
slouken@5154
   832
    if (count < 2) {
slouken@5154
   833
        return 0;
slouken@5154
   834
    }
slouken@5154
   835
    return renderer->RenderDrawLines(renderer, points, count);
slouken@5154
   836
}
slouken@5154
   837
slouken@5154
   838
int
slouken@5154
   839
SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
   840
{
slouken@5154
   841
    SDL_Rect full_rect;
slouken@5154
   842
    SDL_Point points[5];
slouken@5154
   843
slouken@5154
   844
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   845
slouken@5154
   846
    /* If 'rect' == NULL, then outline the whole surface */
slouken@5154
   847
    if (!rect) {
slouken@5154
   848
        SDL_Window *window = renderer->window;
slouken@5154
   849
slouken@5154
   850
        full_rect.x = 0;
slouken@5154
   851
        full_rect.y = 0;
slouken@5154
   852
        SDL_GetWindowSize(window, &full_rect.w, &full_rect.h);
slouken@5154
   853
        rect = &full_rect;
slouken@5154
   854
    }
slouken@5154
   855
slouken@5154
   856
    points[0].x = rect->x;
slouken@5154
   857
    points[0].y = rect->y;
slouken@5154
   858
    points[1].x = rect->x+rect->w-1;
slouken@5154
   859
    points[1].y = rect->y;
slouken@5154
   860
    points[2].x = rect->x+rect->w-1;
slouken@5154
   861
    points[2].y = rect->y+rect->h-1;
slouken@5154
   862
    points[3].x = rect->x;
slouken@5154
   863
    points[3].y = rect->y+rect->h-1;
slouken@5154
   864
    points[4].x = rect->x;
slouken@5154
   865
    points[4].y = rect->y;
slouken@5154
   866
    return SDL_RenderDrawLines(renderer, points, 5);
slouken@5154
   867
}
slouken@5154
   868
slouken@5154
   869
int
slouken@5154
   870
SDL_RenderDrawRects(SDL_Renderer * renderer,
slouken@5154
   871
                    const SDL_Rect ** rects, int count)
slouken@5154
   872
{
slouken@5154
   873
    int i;
slouken@5154
   874
slouken@5154
   875
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   876
slouken@5154
   877
    if (!rects) {
slouken@5154
   878
        SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
slouken@5154
   879
        return -1;
slouken@5154
   880
    }
slouken@5154
   881
    if (count < 1) {
slouken@5154
   882
        return 0;
slouken@5154
   883
    }
slouken@5154
   884
slouken@5154
   885
    /* Check for NULL rect, which means fill entire window */
slouken@5154
   886
    for (i = 0; i < count; ++i) {
slouken@5154
   887
        if (SDL_RenderDrawRect(renderer, rects[i]) < 0) {
slouken@5154
   888
            return -1;
slouken@5154
   889
        }
slouken@5154
   890
    }
slouken@5154
   891
    return 0;
slouken@5154
   892
}
slouken@5154
   893
slouken@5154
   894
int
slouken@5154
   895
SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
   896
{
slouken@5154
   897
    return SDL_RenderFillRects(renderer, &rect, 1);
slouken@5154
   898
}
slouken@5154
   899
slouken@5154
   900
int
slouken@5154
   901
SDL_RenderFillRects(SDL_Renderer * renderer,
slouken@5154
   902
                    const SDL_Rect ** rects, int count)
slouken@5154
   903
{
slouken@5154
   904
    int i;
slouken@5154
   905
slouken@5154
   906
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   907
slouken@5154
   908
    if (!rects) {
slouken@5154
   909
        SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
slouken@5154
   910
        return -1;
slouken@5154
   911
    }
slouken@5154
   912
    if (count < 1) {
slouken@5154
   913
        return 0;
slouken@5154
   914
    }
slouken@5154
   915
slouken@5154
   916
    /* Check for NULL rect, which means fill entire window */
slouken@5154
   917
    for (i = 0; i < count; ++i) {
slouken@5154
   918
        if (rects[i] == NULL) {
slouken@5154
   919
            SDL_Window *window = renderer->window;
slouken@5154
   920
            SDL_Rect full_rect;
slouken@5154
   921
            const SDL_Rect *rect;
slouken@5154
   922
slouken@5154
   923
            full_rect.x = 0;
slouken@5154
   924
            full_rect.y = 0;
slouken@5154
   925
            SDL_GetWindowSize(window, &full_rect.w, &full_rect.h);
slouken@5154
   926
            rect = &full_rect;
slouken@5154
   927
            return renderer->RenderFillRects(renderer, &rect, 1);
slouken@5154
   928
        }
slouken@5154
   929
    }
slouken@5154
   930
    return renderer->RenderFillRects(renderer, rects, count);
slouken@5154
   931
}
slouken@5154
   932
slouken@5154
   933
int
slouken@5154
   934
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5154
   935
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@5154
   936
{
slouken@5154
   937
    SDL_Window *window;
slouken@5154
   938
    SDL_Rect real_srcrect;
slouken@5154
   939
    SDL_Rect real_dstrect;
slouken@5154
   940
slouken@5154
   941
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   942
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   943
slouken@5154
   944
    if (renderer != texture->renderer) {
slouken@5154
   945
        SDL_SetError("Texture was not created with this renderer");
slouken@5154
   946
        return -1;
slouken@5154
   947
    }
slouken@5154
   948
    window = renderer->window;
slouken@5154
   949
slouken@5154
   950
    real_srcrect.x = 0;
slouken@5154
   951
    real_srcrect.y = 0;
slouken@5154
   952
    real_srcrect.w = texture->w;
slouken@5154
   953
    real_srcrect.h = texture->h;
slouken@5154
   954
    if (srcrect) {
slouken@5154
   955
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
slouken@5154
   956
            return 0;
slouken@5154
   957
        }
slouken@5154
   958
    }
slouken@5154
   959
slouken@5154
   960
    real_dstrect.x = 0;
slouken@5154
   961
    real_dstrect.y = 0;
slouken@5154
   962
    SDL_GetWindowSize(window, &real_dstrect.w, &real_dstrect.h);
slouken@5154
   963
    if (dstrect) {
slouken@5154
   964
        if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
slouken@5154
   965
            return 0;
slouken@5154
   966
        }
slouken@5154
   967
        /* Clip srcrect by the same amount as dstrect was clipped */
slouken@5154
   968
        if (dstrect->w != real_dstrect.w) {
slouken@5154
   969
            int deltax = (real_dstrect.x - dstrect->x);
slouken@5154
   970
            int deltaw = (real_dstrect.w - dstrect->w);
slouken@5154
   971
            real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
slouken@5154
   972
            real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
slouken@5154
   973
        }
slouken@5154
   974
        if (dstrect->h != real_dstrect.h) {
slouken@5154
   975
            int deltay = (real_dstrect.y - dstrect->y);
slouken@5154
   976
            int deltah = (real_dstrect.h - dstrect->h);
slouken@5154
   977
            real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
slouken@5154
   978
            real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
slouken@5154
   979
        }
slouken@5154
   980
    }
slouken@5154
   981
slouken@5154
   982
    return renderer->RenderCopy(renderer, texture, &real_srcrect,
slouken@5154
   983
                                &real_dstrect);
slouken@5154
   984
}
slouken@5154
   985
slouken@5154
   986
int
slouken@5154
   987
SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@5154
   988
                     Uint32 format, void * pixels, int pitch)
slouken@5154
   989
{
slouken@5154
   990
    SDL_Window *window;
slouken@5154
   991
    SDL_Rect real_rect;
slouken@5154
   992
slouken@5154
   993
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   994
slouken@5154
   995
    if (!renderer->RenderReadPixels) {
slouken@5154
   996
        SDL_Unsupported();
slouken@5154
   997
        return -1;
slouken@5154
   998
    }
slouken@5154
   999
    window = renderer->window;
slouken@5154
  1000
slouken@5154
  1001
    if (!format) {
slouken@5154
  1002
        format = SDL_GetWindowPixelFormat(window);
slouken@5154
  1003
    }
slouken@5154
  1004
slouken@5154
  1005
    real_rect.x = 0;
slouken@5154
  1006
    real_rect.y = 0;
slouken@5154
  1007
    SDL_GetWindowSize(window, &real_rect.w, &real_rect.h);
slouken@5154
  1008
    if (rect) {
slouken@5154
  1009
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@5154
  1010
            return 0;
slouken@5154
  1011
        }
slouken@5154
  1012
        if (real_rect.y > rect->y) {
slouken@5154
  1013
            pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@5154
  1014
        }
slouken@5154
  1015
        if (real_rect.x > rect->x) {
slouken@5154
  1016
            int bpp = SDL_BYTESPERPIXEL(SDL_GetWindowPixelFormat(window));
slouken@5154
  1017
            pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@5154
  1018
        }
slouken@5154
  1019
    }
slouken@5154
  1020
slouken@5154
  1021
    return renderer->RenderReadPixels(renderer, &real_rect,
slouken@5154
  1022
                                      format, pixels, pitch);
slouken@5154
  1023
}
slouken@5154
  1024
slouken@5154
  1025
int
slouken@5154
  1026
SDL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@5154
  1027
                      Uint32 format, const void * pixels, int pitch)
slouken@5154
  1028
{
slouken@5154
  1029
    SDL_Window *window;
slouken@5154
  1030
    SDL_Rect real_rect;
slouken@5154
  1031
slouken@5154
  1032
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1033
slouken@5154
  1034
    if (!renderer->RenderWritePixels) {
slouken@5154
  1035
        SDL_Unsupported();
slouken@5154
  1036
        return -1;
slouken@5154
  1037
    }
slouken@5154
  1038
    window = renderer->window;
slouken@5154
  1039
slouken@5154
  1040
    if (!format) {
slouken@5154
  1041
        format = SDL_GetWindowPixelFormat(window);
slouken@5154
  1042
    }
slouken@5154
  1043
slouken@5154
  1044
    real_rect.x = 0;
slouken@5154
  1045
    real_rect.y = 0;
slouken@5154
  1046
    SDL_GetWindowSize(window, &real_rect.w, &real_rect.h);
slouken@5154
  1047
    if (rect) {
slouken@5154
  1048
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@5154
  1049
            return 0;
slouken@5154
  1050
        }
slouken@5154
  1051
        if (real_rect.y > rect->y) {
slouken@5154
  1052
            pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@5154
  1053
        }
slouken@5154
  1054
        if (real_rect.x > rect->x) {
slouken@5154
  1055
            int bpp = SDL_BYTESPERPIXEL(SDL_GetWindowPixelFormat(window));
slouken@5154
  1056
            pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@5154
  1057
        }
slouken@5154
  1058
    }
slouken@5154
  1059
slouken@5154
  1060
    return renderer->RenderWritePixels(renderer, &real_rect,
slouken@5154
  1061
                                       format, pixels, pitch);
slouken@5154
  1062
}
slouken@5154
  1063
slouken@5154
  1064
void
slouken@5154
  1065
SDL_RenderPresent(SDL_Renderer * renderer)
slouken@5154
  1066
{
slouken@5154
  1067
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1068
slouken@5154
  1069
    renderer->RenderPresent(renderer);
slouken@5154
  1070
}
slouken@5154
  1071
slouken@5154
  1072
void
slouken@5154
  1073
SDL_DestroyTexture(SDL_Texture * texture)
slouken@5154
  1074
{
slouken@5154
  1075
    SDL_Renderer *renderer;
slouken@5154
  1076
slouken@5154
  1077
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
  1078
    texture->magic = NULL;
slouken@5154
  1079
slouken@5154
  1080
    renderer = texture->renderer;
slouken@5154
  1081
    if (texture->next) {
slouken@5154
  1082
        texture->next->prev = texture->prev;
slouken@5154
  1083
    }
slouken@5154
  1084
    if (texture->prev) {
slouken@5154
  1085
        texture->prev->next = texture->next;
slouken@5154
  1086
    } else {
slouken@5154
  1087
        renderer->textures = texture->next;
slouken@5154
  1088
    }
slouken@5154
  1089
slouken@5154
  1090
    renderer->DestroyTexture(renderer, texture);
slouken@5154
  1091
    SDL_free(texture);
slouken@5154
  1092
}
slouken@5154
  1093
slouken@5154
  1094
void
slouken@5154
  1095
SDL_DestroyRenderer(SDL_Renderer * renderer)
slouken@5154
  1096
{
slouken@5154
  1097
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1098
slouken@5154
  1099
    SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
slouken@5154
  1100
slouken@5154
  1101
    /* Free existing textures for this renderer */
slouken@5154
  1102
    while (renderer->textures) {
slouken@5154
  1103
        SDL_DestroyTexture(renderer->textures);
slouken@5154
  1104
    }
slouken@5154
  1105
slouken@5154
  1106
    /* It's no longer magical... */
slouken@5154
  1107
    renderer->magic = NULL;
slouken@5154
  1108
slouken@5154
  1109
    /* Free the renderer instance */
slouken@5154
  1110
    renderer->DestroyRenderer(renderer);
slouken@5154
  1111
}
slouken@5154
  1112
slouken@5154
  1113
/* vi: set ts=4 sw=4 expandtab: */