src/render/SDL_render.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 03 Feb 2011 00:54:29 -0800
changeset 5158 b3ccd1947786
parent 5157 657543cc92f9
child 5166 4d39eeaad00b
permissions -rw-r--r--
Simplified and improved the process of creating a texture from a surface.
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@5156
   155
static SDL_bool
slouken@5156
   156
IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
slouken@5156
   157
{
slouken@5156
   158
    Uint32 i;
slouken@5156
   159
slouken@5156
   160
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5156
   161
        if (renderer->info.texture_formats[i] == format) {
slouken@5156
   162
            return SDL_TRUE;
slouken@5156
   163
        }
slouken@5156
   164
    }
slouken@5156
   165
    return SDL_FALSE;
slouken@5156
   166
}
slouken@5156
   167
slouken@5156
   168
static Uint32
slouken@5156
   169
GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
slouken@5156
   170
{
slouken@5156
   171
    Uint32 i;
slouken@5156
   172
    SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
slouken@5156
   173
slouken@5156
   174
    /* We just want to match the first format that has the same channels */
slouken@5156
   175
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5156
   176
        if (SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
slouken@5156
   177
            return renderer->info.texture_formats[i];
slouken@5156
   178
        }
slouken@5156
   179
    }
slouken@5156
   180
    return renderer->info.texture_formats[0];
slouken@5156
   181
}
slouken@5156
   182
slouken@5154
   183
SDL_Texture *
slouken@5154
   184
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
slouken@5154
   185
{
slouken@5154
   186
    SDL_Texture *texture;
slouken@5154
   187
slouken@5154
   188
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   189
slouken@5156
   190
    if (SDL_ISPIXELFORMAT_INDEXED(format)) {
slouken@5156
   191
        SDL_SetError("Palettized textures are not supported");
slouken@5156
   192
        return NULL;
slouken@5156
   193
    }
slouken@5154
   194
    if (w <= 0 || h <= 0) {
slouken@5154
   195
        SDL_SetError("Texture dimensions can't be 0");
slouken@5156
   196
        return NULL;
slouken@5154
   197
    }
slouken@5154
   198
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@5154
   199
    if (!texture) {
slouken@5154
   200
        SDL_OutOfMemory();
slouken@5156
   201
        return NULL;
slouken@5154
   202
    }
slouken@5154
   203
    texture->magic = &texture_magic;
slouken@5154
   204
    texture->format = format;
slouken@5154
   205
    texture->access = access;
slouken@5154
   206
    texture->w = w;
slouken@5154
   207
    texture->h = h;
slouken@5154
   208
    texture->r = 255;
slouken@5154
   209
    texture->g = 255;
slouken@5154
   210
    texture->b = 255;
slouken@5154
   211
    texture->a = 255;
slouken@5154
   212
    texture->renderer = renderer;
slouken@5154
   213
    texture->next = renderer->textures;
slouken@5154
   214
    if (renderer->textures) {
slouken@5154
   215
        renderer->textures->prev = texture;
slouken@5154
   216
    }
slouken@5154
   217
    renderer->textures = texture;
slouken@5154
   218
slouken@5156
   219
    if (IsSupportedFormat(renderer, format)) {
slouken@5156
   220
        if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@5156
   221
            SDL_DestroyTexture(texture);
slouken@5156
   222
            return 0;
slouken@5156
   223
        }
slouken@5156
   224
    } else {
slouken@5156
   225
        texture->native = SDL_CreateTexture(renderer,
slouken@5156
   226
                                GetClosestSupportedFormat(renderer, format),
slouken@5156
   227
                                access, w, h);
slouken@5156
   228
        if (!texture->native) {
slouken@5156
   229
            SDL_DestroyTexture(texture);
slouken@5156
   230
            return NULL;
slouken@5156
   231
        }
slouken@5156
   232
slouken@5156
   233
        if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@5156
   234
            texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
slouken@5156
   235
            if (!texture->yuv) {
slouken@5156
   236
                SDL_DestroyTexture(texture);
slouken@5156
   237
                return NULL;
slouken@5156
   238
            }
slouken@5156
   239
        } else if (access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   240
            /* The pitch is 4 byte aligned */
slouken@5156
   241
            texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
slouken@5156
   242
            texture->pixels = SDL_malloc(texture->pitch * h);
slouken@5156
   243
            if (!texture->pixels) {
slouken@5156
   244
                SDL_DestroyTexture(texture);
slouken@5156
   245
                return NULL;
slouken@5156
   246
            }
slouken@5156
   247
        }
slouken@5154
   248
    }
slouken@5154
   249
    return texture;
slouken@5154
   250
}
slouken@5154
   251
slouken@5154
   252
SDL_Texture *
slouken@5158
   253
SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
slouken@5154
   254
{
slouken@5158
   255
    const SDL_PixelFormat *fmt;
slouken@5158
   256
    SDL_bool needAlpha;
slouken@5158
   257
    Uint32 i;
slouken@5158
   258
    Uint32 format;
slouken@5154
   259
    int bpp;
slouken@5154
   260
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@5158
   261
    SDL_Texture *texture;
slouken@5154
   262
slouken@5154
   263
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   264
slouken@5154
   265
    if (!surface) {
slouken@5154
   266
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@5154
   267
        return NULL;
slouken@5154
   268
    }
slouken@5158
   269
slouken@5158
   270
    /* See what the best texture format is */
slouken@5154
   271
    fmt = surface->format;
slouken@5158
   272
    if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
slouken@5158
   273
        needAlpha = SDL_TRUE;
slouken@5154
   274
    } else {
slouken@5158
   275
        needAlpha = SDL_FALSE;
slouken@5158
   276
    }
slouken@5158
   277
    format = renderer->info.texture_formats[0];
slouken@5158
   278
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5158
   279
        if (SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
slouken@5158
   280
            format = renderer->info.texture_formats[i];
slouken@5158
   281
            break;
slouken@5154
   282
        }
slouken@5154
   283
    }
slouken@5154
   284
slouken@5158
   285
    if (!SDL_PixelFormatEnumToMasks(format, &bpp,
slouken@5158
   286
                                    &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5158
   287
        SDL_SetError("Unknown pixel format");
slouken@5158
   288
        return NULL;
slouken@5154
   289
    }
slouken@5158
   290
slouken@5158
   291
    texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
slouken@5158
   292
                                surface->w, surface->h);
slouken@5154
   293
    if (!texture) {
slouken@5158
   294
        return NULL;
slouken@5154
   295
    }
slouken@5158
   296
slouken@5154
   297
    if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
slouken@5154
   298
        && Bmask == fmt->Bmask && Amask == fmt->Amask) {
slouken@5154
   299
        if (SDL_MUSTLOCK(surface)) {
slouken@5154
   300
            SDL_LockSurface(surface);
slouken@5158
   301
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
slouken@5154
   302
            SDL_UnlockSurface(surface);
slouken@5154
   303
        } else {
slouken@5158
   304
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
slouken@5154
   305
        }
slouken@5154
   306
    } else {
slouken@5154
   307
        SDL_PixelFormat dst_fmt;
slouken@5158
   308
        SDL_Surface *temp = NULL;
slouken@5154
   309
slouken@5154
   310
        /* Set up a destination surface for the texture update */
slouken@5154
   311
        SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
slouken@5158
   312
        temp = SDL_ConvertSurface(surface, &dst_fmt, 0);
slouken@5158
   313
        if (temp) {
slouken@5158
   314
            SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
slouken@5158
   315
            SDL_FreeSurface(temp);
slouken@5158
   316
        } else {
slouken@5154
   317
            SDL_DestroyTexture(texture);
slouken@5158
   318
            return NULL;
slouken@5154
   319
        }
slouken@5154
   320
    }
slouken@5154
   321
slouken@5154
   322
    {
slouken@5154
   323
        Uint8 r, g, b, a;
slouken@5154
   324
        SDL_BlendMode blendMode;
slouken@5154
   325
slouken@5154
   326
        SDL_GetSurfaceColorMod(surface, &r, &g, &b);
slouken@5154
   327
        SDL_SetTextureColorMod(texture, r, g, b);
slouken@5154
   328
slouken@5154
   329
        SDL_GetSurfaceAlphaMod(surface, &a);
slouken@5154
   330
        SDL_SetTextureAlphaMod(texture, a);
slouken@5154
   331
slouken@5154
   332
        if (SDL_GetColorKey(surface, NULL) == 0) {
slouken@5154
   333
            /* We converted to a texture with alpha format */
slouken@5154
   334
            SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
slouken@5154
   335
        } else {
slouken@5154
   336
            SDL_GetSurfaceBlendMode(surface, &blendMode);
slouken@5154
   337
            SDL_SetTextureBlendMode(texture, blendMode);
slouken@5154
   338
        }
slouken@5154
   339
    }
slouken@5154
   340
    return texture;
slouken@5154
   341
}
slouken@5154
   342
slouken@5154
   343
int
slouken@5154
   344
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
slouken@5154
   345
                 int *w, int *h)
slouken@5154
   346
{
slouken@5154
   347
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   348
slouken@5154
   349
    if (format) {
slouken@5154
   350
        *format = texture->format;
slouken@5154
   351
    }
slouken@5154
   352
    if (access) {
slouken@5154
   353
        *access = texture->access;
slouken@5154
   354
    }
slouken@5154
   355
    if (w) {
slouken@5154
   356
        *w = texture->w;
slouken@5154
   357
    }
slouken@5154
   358
    if (h) {
slouken@5154
   359
        *h = texture->h;
slouken@5154
   360
    }
slouken@5154
   361
    return 0;
slouken@5154
   362
}
slouken@5154
   363
slouken@5154
   364
int
slouken@5154
   365
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
slouken@5154
   366
{
slouken@5154
   367
    SDL_Renderer *renderer;
slouken@5154
   368
slouken@5154
   369
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   370
slouken@5154
   371
    renderer = texture->renderer;
slouken@5154
   372
    if (r < 255 || g < 255 || b < 255) {
slouken@5154
   373
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   374
    } else {
slouken@5154
   375
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   376
    }
slouken@5154
   377
    texture->r = r;
slouken@5154
   378
    texture->g = g;
slouken@5154
   379
    texture->b = b;
slouken@5156
   380
    if (texture->native) {
slouken@5156
   381
        return SDL_SetTextureColorMod(texture->native, r, g, b);
slouken@5156
   382
    } else if (renderer->SetTextureColorMod) {
slouken@5154
   383
        return renderer->SetTextureColorMod(renderer, texture);
slouken@5154
   384
    } else {
slouken@5154
   385
        return 0;
slouken@5154
   386
    }
slouken@5154
   387
}
slouken@5154
   388
slouken@5154
   389
int
slouken@5154
   390
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
slouken@5154
   391
                       Uint8 * b)
slouken@5154
   392
{
slouken@5154
   393
    SDL_Renderer *renderer;
slouken@5154
   394
slouken@5154
   395
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   396
slouken@5154
   397
    renderer = texture->renderer;
slouken@5154
   398
    if (r) {
slouken@5154
   399
        *r = texture->r;
slouken@5154
   400
    }
slouken@5154
   401
    if (g) {
slouken@5154
   402
        *g = texture->g;
slouken@5154
   403
    }
slouken@5154
   404
    if (b) {
slouken@5154
   405
        *b = texture->b;
slouken@5154
   406
    }
slouken@5154
   407
    return 0;
slouken@5154
   408
}
slouken@5154
   409
slouken@5154
   410
int
slouken@5154
   411
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
slouken@5154
   412
{
slouken@5154
   413
    SDL_Renderer *renderer;
slouken@5154
   414
slouken@5154
   415
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   416
slouken@5154
   417
    renderer = texture->renderer;
slouken@5154
   418
    if (alpha < 255) {
slouken@5154
   419
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   420
    } else {
slouken@5154
   421
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   422
    }
slouken@5154
   423
    texture->a = alpha;
slouken@5156
   424
    if (texture->native) {
slouken@5156
   425
        return SDL_SetTextureAlphaMod(texture->native, alpha);
slouken@5156
   426
    } else if (renderer->SetTextureAlphaMod) {
slouken@5154
   427
        return renderer->SetTextureAlphaMod(renderer, texture);
slouken@5154
   428
    } else {
slouken@5154
   429
        return 0;
slouken@5154
   430
    }
slouken@5154
   431
}
slouken@5154
   432
slouken@5154
   433
int
slouken@5154
   434
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
slouken@5154
   435
{
slouken@5154
   436
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   437
slouken@5154
   438
    if (alpha) {
slouken@5154
   439
        *alpha = texture->a;
slouken@5154
   440
    }
slouken@5154
   441
    return 0;
slouken@5154
   442
}
slouken@5154
   443
slouken@5154
   444
int
slouken@5154
   445
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
slouken@5154
   446
{
slouken@5154
   447
    SDL_Renderer *renderer;
slouken@5154
   448
slouken@5154
   449
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   450
slouken@5154
   451
    renderer = texture->renderer;
slouken@5154
   452
    texture->blendMode = blendMode;
slouken@5156
   453
    if (texture->native) {
slouken@5156
   454
        return SDL_SetTextureBlendMode(texture, blendMode);
slouken@5156
   455
    } else if (renderer->SetTextureBlendMode) {
slouken@5154
   456
        return renderer->SetTextureBlendMode(renderer, texture);
slouken@5154
   457
    } else {
slouken@5154
   458
        return 0;
slouken@5154
   459
    }
slouken@5154
   460
}
slouken@5154
   461
slouken@5154
   462
int
slouken@5154
   463
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
slouken@5154
   464
{
slouken@5154
   465
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   466
slouken@5154
   467
    if (blendMode) {
slouken@5154
   468
        *blendMode = texture->blendMode;
slouken@5154
   469
    }
slouken@5154
   470
    return 0;
slouken@5154
   471
}
slouken@5154
   472
slouken@5156
   473
static int
slouken@5156
   474
SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   475
                     const void *pixels, int pitch)
slouken@5156
   476
{
slouken@5156
   477
    SDL_Texture *native = texture->native;
slouken@5156
   478
    SDL_Rect full_rect;
slouken@5156
   479
slouken@5156
   480
    if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
slouken@5156
   481
        return -1;
slouken@5156
   482
    }
slouken@5156
   483
slouken@5156
   484
    full_rect.x = 0;
slouken@5156
   485
    full_rect.y = 0;
slouken@5156
   486
    full_rect.w = texture->w;
slouken@5156
   487
    full_rect.h = texture->h;
slouken@5156
   488
    rect = &full_rect;
slouken@5156
   489
slouken@5156
   490
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   491
        /* We can lock the texture and copy to it */
slouken@5156
   492
        void *native_pixels;
slouken@5156
   493
        int native_pitch;
slouken@5156
   494
slouken@5156
   495
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   496
            return -1;
slouken@5156
   497
        }
slouken@5156
   498
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@5156
   499
                            rect->w, rect->h, native_pixels, native_pitch);
slouken@5156
   500
        SDL_UnlockTexture(native);
slouken@5156
   501
    } else {
slouken@5156
   502
        /* Use a temporary buffer for updating */
slouken@5156
   503
        void *temp_pixels;
slouken@5156
   504
        int temp_pitch;
slouken@5156
   505
slouken@5156
   506
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@5156
   507
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5156
   508
        if (!temp_pixels) {
slouken@5156
   509
            SDL_OutOfMemory();
slouken@5156
   510
            return -1;
slouken@5156
   511
        }
slouken@5156
   512
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@5156
   513
                            rect->w, rect->h, temp_pixels, temp_pitch);
slouken@5156
   514
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@5156
   515
        SDL_free(temp_pixels);
slouken@5156
   516
    }
slouken@5156
   517
    return 0;
slouken@5156
   518
}
slouken@5156
   519
slouken@5156
   520
static int
slouken@5156
   521
SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   522
                        const void *pixels, int pitch)
slouken@5156
   523
{
slouken@5156
   524
    SDL_Texture *native = texture->native;
slouken@5156
   525
slouken@5156
   526
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   527
        /* We can lock the texture and copy to it */
slouken@5156
   528
        void *native_pixels;
slouken@5156
   529
        int native_pitch;
slouken@5156
   530
slouken@5156
   531
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   532
            return -1;
slouken@5156
   533
        }
slouken@5156
   534
        SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   535
                          texture->format, pixels, pitch,
slouken@5156
   536
                          native->format, native_pixels, native_pitch);
slouken@5156
   537
        SDL_UnlockTexture(native);
slouken@5156
   538
    } else {
slouken@5156
   539
        /* Use a temporary buffer for updating */
slouken@5156
   540
        void *temp_pixels;
slouken@5156
   541
        int temp_pitch;
slouken@5156
   542
slouken@5156
   543
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@5156
   544
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5156
   545
        if (!temp_pixels) {
slouken@5156
   546
            SDL_OutOfMemory();
slouken@5156
   547
            return -1;
slouken@5156
   548
        }
slouken@5156
   549
        SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   550
                          texture->format, pixels, pitch,
slouken@5156
   551
                          native->format, temp_pixels, temp_pitch);
slouken@5156
   552
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@5156
   553
        SDL_free(temp_pixels);
slouken@5156
   554
    }
slouken@5156
   555
    return 0;
slouken@5156
   556
}
slouken@5156
   557
slouken@5154
   558
int
slouken@5154
   559
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   560
                  const void *pixels, int pitch)
slouken@5154
   561
{
slouken@5154
   562
    SDL_Renderer *renderer;
slouken@5154
   563
    SDL_Rect full_rect;
slouken@5154
   564
slouken@5154
   565
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   566
slouken@5154
   567
    if (!rect) {
slouken@5154
   568
        full_rect.x = 0;
slouken@5154
   569
        full_rect.y = 0;
slouken@5154
   570
        full_rect.w = texture->w;
slouken@5154
   571
        full_rect.h = texture->h;
slouken@5154
   572
        rect = &full_rect;
slouken@5154
   573
    }
slouken@5156
   574
slouken@5156
   575
    if (texture->yuv) {
slouken@5156
   576
        return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
slouken@5156
   577
    } else if (texture->native) {
slouken@5156
   578
        return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
slouken@5156
   579
    } else {
slouken@5156
   580
        renderer = texture->renderer;
slouken@5156
   581
        return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@5156
   582
    }
slouken@5156
   583
}
slouken@5156
   584
slouken@5156
   585
static int
slouken@5156
   586
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   587
                   void **pixels, int *pitch)
slouken@5156
   588
{
slouken@5156
   589
    return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
slouken@5156
   590
}
slouken@5156
   591
slouken@5156
   592
static int
slouken@5156
   593
SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   594
                      void **pixels, int *pitch)
slouken@5156
   595
{
slouken@5156
   596
    texture->locked_rect = *rect;
slouken@5156
   597
    *pixels = (void *) ((Uint8 *) texture->pixels +
slouken@5156
   598
                        rect->y * texture->pitch +
slouken@5156
   599
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5156
   600
    *pitch = texture->pitch;
slouken@5156
   601
    return 0;
slouken@5154
   602
}
slouken@5154
   603
slouken@5154
   604
int
slouken@5156
   605
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   606
                void **pixels, int *pitch)
slouken@5154
   607
{
slouken@5154
   608
    SDL_Renderer *renderer;
slouken@5154
   609
    SDL_Rect full_rect;
slouken@5154
   610
slouken@5154
   611
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   612
slouken@5154
   613
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   614
        SDL_SetError("SDL_LockTexture(): texture must be streaming");
slouken@5154
   615
        return -1;
slouken@5154
   616
    }
slouken@5156
   617
slouken@5154
   618
    if (!rect) {
slouken@5154
   619
        full_rect.x = 0;
slouken@5154
   620
        full_rect.y = 0;
slouken@5154
   621
        full_rect.w = texture->w;
slouken@5154
   622
        full_rect.h = texture->h;
slouken@5154
   623
        rect = &full_rect;
slouken@5154
   624
    }
slouken@5156
   625
slouken@5156
   626
    if (texture->yuv) {
slouken@5156
   627
        return SDL_LockTextureYUV(texture, rect, pixels, pitch);
slouken@5156
   628
    } else if (texture->native) {
slouken@5156
   629
        return SDL_LockTextureNative(texture, rect, pixels, pitch);
slouken@5156
   630
    } else {
slouken@5156
   631
        renderer = texture->renderer;
slouken@5156
   632
        return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
slouken@5156
   633
    }
slouken@5156
   634
}
slouken@5156
   635
slouken@5156
   636
static void
slouken@5156
   637
SDL_UnlockTextureYUV(SDL_Texture * texture)
slouken@5156
   638
{
slouken@5156
   639
    SDL_Texture *native = texture->native;
slouken@5156
   640
    void *native_pixels;
slouken@5156
   641
    int native_pitch;
slouken@5156
   642
    SDL_Rect rect;
slouken@5156
   643
slouken@5156
   644
    rect.x = 0;
slouken@5156
   645
    rect.y = 0;
slouken@5156
   646
    rect.w = texture->w;
slouken@5156
   647
    rect.h = texture->h;
slouken@5156
   648
slouken@5156
   649
    if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   650
        return;
slouken@5156
   651
    }
slouken@5156
   652
    SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
slouken@5156
   653
                        rect.w, rect.h, native_pixels, native_pitch);
slouken@5156
   654
    SDL_UnlockTexture(native);
slouken@5156
   655
}
slouken@5156
   656
slouken@5156
   657
void
slouken@5156
   658
SDL_UnlockTextureNative(SDL_Texture * texture)
slouken@5156
   659
{
slouken@5156
   660
    SDL_Texture *native = texture->native;
slouken@5156
   661
    void *native_pixels;
slouken@5156
   662
    int native_pitch;
slouken@5156
   663
    const SDL_Rect *rect = &texture->locked_rect;
slouken@5156
   664
    const void* pixels = (void *) ((Uint8 *) texture->pixels +
slouken@5156
   665
                        rect->y * texture->pitch +
slouken@5156
   666
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5156
   667
    int pitch = texture->pitch;
slouken@5156
   668
slouken@5156
   669
    if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   670
        return;
slouken@5156
   671
    }
slouken@5156
   672
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   673
                      texture->format, pixels, pitch,
slouken@5156
   674
                      native->format, native_pixels, native_pitch);
slouken@5156
   675
    SDL_UnlockTexture(native);
slouken@5154
   676
}
slouken@5154
   677
slouken@5154
   678
void
slouken@5154
   679
SDL_UnlockTexture(SDL_Texture * texture)
slouken@5154
   680
{
slouken@5154
   681
    SDL_Renderer *renderer;
slouken@5154
   682
slouken@5154
   683
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
   684
slouken@5154
   685
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   686
        return;
slouken@5154
   687
    }
slouken@5156
   688
    if (texture->yuv) {
slouken@5156
   689
        SDL_UnlockTextureYUV(texture);
slouken@5156
   690
    } else if (texture->native) {
slouken@5156
   691
        SDL_UnlockTextureNative(texture);
slouken@5156
   692
    } else {
slouken@5156
   693
        renderer = texture->renderer;
slouken@5156
   694
        renderer->UnlockTexture(renderer, texture);
slouken@5154
   695
    }
slouken@5154
   696
}
slouken@5154
   697
slouken@5154
   698
int
slouken@5154
   699
SDL_SetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
   700
                       Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5154
   701
{
slouken@5154
   702
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   703
slouken@5154
   704
    renderer->r = r;
slouken@5154
   705
    renderer->g = g;
slouken@5154
   706
    renderer->b = b;
slouken@5154
   707
    renderer->a = a;
slouken@5154
   708
    return 0;
slouken@5154
   709
}
slouken@5154
   710
slouken@5154
   711
int
slouken@5154
   712
SDL_GetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
   713
                       Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@5154
   714
{
slouken@5154
   715
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   716
slouken@5154
   717
    if (r) {
slouken@5154
   718
        *r = renderer->r;
slouken@5154
   719
    }
slouken@5154
   720
    if (g) {
slouken@5154
   721
        *g = renderer->g;
slouken@5154
   722
    }
slouken@5154
   723
    if (b) {
slouken@5154
   724
        *b = renderer->b;
slouken@5154
   725
    }
slouken@5154
   726
    if (a) {
slouken@5154
   727
        *a = renderer->a;
slouken@5154
   728
    }
slouken@5154
   729
    return 0;
slouken@5154
   730
}
slouken@5154
   731
slouken@5154
   732
int
slouken@5154
   733
SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
slouken@5154
   734
{
slouken@5154
   735
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   736
slouken@5154
   737
    renderer->blendMode = blendMode;
slouken@5154
   738
    return 0;
slouken@5154
   739
}
slouken@5154
   740
slouken@5154
   741
int
slouken@5154
   742
SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
slouken@5154
   743
{
slouken@5154
   744
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   745
slouken@5154
   746
    *blendMode = renderer->blendMode;
slouken@5154
   747
    return 0;
slouken@5154
   748
}
slouken@5154
   749
slouken@5154
   750
int
slouken@5154
   751
SDL_RenderClear(SDL_Renderer * renderer)
slouken@5154
   752
{
slouken@5154
   753
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   754
slouken@5154
   755
    if (!renderer->RenderClear) {
slouken@5154
   756
        SDL_BlendMode blendMode = renderer->blendMode;
slouken@5154
   757
        int status;
slouken@5154
   758
slouken@5154
   759
        if (blendMode >= SDL_BLENDMODE_BLEND) {
slouken@5154
   760
            SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
slouken@5154
   761
        }
slouken@5154
   762
slouken@5154
   763
        status = SDL_RenderFillRect(renderer, NULL);
slouken@5154
   764
slouken@5154
   765
        if (blendMode >= SDL_BLENDMODE_BLEND) {
slouken@5154
   766
            SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5154
   767
        }
slouken@5154
   768
        return status;
slouken@5154
   769
    }
slouken@5154
   770
    return renderer->RenderClear(renderer);
slouken@5154
   771
}
slouken@5154
   772
slouken@5154
   773
int
slouken@5154
   774
SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
slouken@5154
   775
{
slouken@5154
   776
    SDL_Point point;
slouken@5154
   777
slouken@5154
   778
    point.x = x;
slouken@5154
   779
    point.y = y;
slouken@5154
   780
    return SDL_RenderDrawPoints(renderer, &point, 1);
slouken@5154
   781
}
slouken@5154
   782
slouken@5154
   783
int
slouken@5154
   784
SDL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@5154
   785
                     const SDL_Point * points, int count)
slouken@5154
   786
{
slouken@5154
   787
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   788
slouken@5154
   789
    if (!points) {
slouken@5154
   790
        SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
slouken@5154
   791
        return -1;
slouken@5154
   792
    }
slouken@5154
   793
    if (count < 1) {
slouken@5154
   794
        return 0;
slouken@5154
   795
    }
slouken@5154
   796
    return renderer->RenderDrawPoints(renderer, points, count);
slouken@5154
   797
}
slouken@5154
   798
slouken@5154
   799
int
slouken@5154
   800
SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
slouken@5154
   801
{
slouken@5154
   802
    SDL_Point points[2];
slouken@5154
   803
slouken@5154
   804
    points[0].x = x1;
slouken@5154
   805
    points[0].y = y1;
slouken@5154
   806
    points[1].x = x2;
slouken@5154
   807
    points[1].y = y2;
slouken@5154
   808
    return SDL_RenderDrawLines(renderer, points, 2);
slouken@5154
   809
}
slouken@5154
   810
slouken@5154
   811
int
slouken@5154
   812
SDL_RenderDrawLines(SDL_Renderer * renderer,
slouken@5154
   813
                    const SDL_Point * points, int count)
slouken@5154
   814
{
slouken@5154
   815
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   816
slouken@5154
   817
    if (!points) {
slouken@5154
   818
        SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
slouken@5154
   819
        return -1;
slouken@5154
   820
    }
slouken@5154
   821
    if (count < 2) {
slouken@5154
   822
        return 0;
slouken@5154
   823
    }
slouken@5154
   824
    return renderer->RenderDrawLines(renderer, points, count);
slouken@5154
   825
}
slouken@5154
   826
slouken@5154
   827
int
slouken@5154
   828
SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
   829
{
slouken@5154
   830
    SDL_Rect full_rect;
slouken@5154
   831
    SDL_Point points[5];
slouken@5154
   832
slouken@5154
   833
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   834
slouken@5154
   835
    /* If 'rect' == NULL, then outline the whole surface */
slouken@5154
   836
    if (!rect) {
slouken@5154
   837
        SDL_Window *window = renderer->window;
slouken@5154
   838
slouken@5154
   839
        full_rect.x = 0;
slouken@5154
   840
        full_rect.y = 0;
slouken@5154
   841
        SDL_GetWindowSize(window, &full_rect.w, &full_rect.h);
slouken@5154
   842
        rect = &full_rect;
slouken@5154
   843
    }
slouken@5154
   844
slouken@5154
   845
    points[0].x = rect->x;
slouken@5154
   846
    points[0].y = rect->y;
slouken@5154
   847
    points[1].x = rect->x+rect->w-1;
slouken@5154
   848
    points[1].y = rect->y;
slouken@5154
   849
    points[2].x = rect->x+rect->w-1;
slouken@5154
   850
    points[2].y = rect->y+rect->h-1;
slouken@5154
   851
    points[3].x = rect->x;
slouken@5154
   852
    points[3].y = rect->y+rect->h-1;
slouken@5154
   853
    points[4].x = rect->x;
slouken@5154
   854
    points[4].y = rect->y;
slouken@5154
   855
    return SDL_RenderDrawLines(renderer, points, 5);
slouken@5154
   856
}
slouken@5154
   857
slouken@5154
   858
int
slouken@5154
   859
SDL_RenderDrawRects(SDL_Renderer * renderer,
slouken@5154
   860
                    const SDL_Rect ** rects, int count)
slouken@5154
   861
{
slouken@5154
   862
    int i;
slouken@5154
   863
slouken@5154
   864
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   865
slouken@5154
   866
    if (!rects) {
slouken@5154
   867
        SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
slouken@5154
   868
        return -1;
slouken@5154
   869
    }
slouken@5154
   870
    if (count < 1) {
slouken@5154
   871
        return 0;
slouken@5154
   872
    }
slouken@5154
   873
slouken@5154
   874
    /* Check for NULL rect, which means fill entire window */
slouken@5154
   875
    for (i = 0; i < count; ++i) {
slouken@5154
   876
        if (SDL_RenderDrawRect(renderer, rects[i]) < 0) {
slouken@5154
   877
            return -1;
slouken@5154
   878
        }
slouken@5154
   879
    }
slouken@5154
   880
    return 0;
slouken@5154
   881
}
slouken@5154
   882
slouken@5154
   883
int
slouken@5154
   884
SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
   885
{
slouken@5154
   886
    return SDL_RenderFillRects(renderer, &rect, 1);
slouken@5154
   887
}
slouken@5154
   888
slouken@5154
   889
int
slouken@5154
   890
SDL_RenderFillRects(SDL_Renderer * renderer,
slouken@5154
   891
                    const SDL_Rect ** rects, int count)
slouken@5154
   892
{
slouken@5154
   893
    int i;
slouken@5154
   894
slouken@5154
   895
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   896
slouken@5154
   897
    if (!rects) {
slouken@5154
   898
        SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
slouken@5154
   899
        return -1;
slouken@5154
   900
    }
slouken@5154
   901
    if (count < 1) {
slouken@5154
   902
        return 0;
slouken@5154
   903
    }
slouken@5154
   904
slouken@5154
   905
    /* Check for NULL rect, which means fill entire window */
slouken@5154
   906
    for (i = 0; i < count; ++i) {
slouken@5154
   907
        if (rects[i] == NULL) {
slouken@5154
   908
            SDL_Window *window = renderer->window;
slouken@5154
   909
            SDL_Rect full_rect;
slouken@5154
   910
            const SDL_Rect *rect;
slouken@5154
   911
slouken@5154
   912
            full_rect.x = 0;
slouken@5154
   913
            full_rect.y = 0;
slouken@5154
   914
            SDL_GetWindowSize(window, &full_rect.w, &full_rect.h);
slouken@5154
   915
            rect = &full_rect;
slouken@5154
   916
            return renderer->RenderFillRects(renderer, &rect, 1);
slouken@5154
   917
        }
slouken@5154
   918
    }
slouken@5154
   919
    return renderer->RenderFillRects(renderer, rects, count);
slouken@5154
   920
}
slouken@5154
   921
slouken@5154
   922
int
slouken@5154
   923
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5154
   924
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@5154
   925
{
slouken@5154
   926
    SDL_Window *window;
slouken@5154
   927
    SDL_Rect real_srcrect;
slouken@5154
   928
    SDL_Rect real_dstrect;
slouken@5154
   929
slouken@5154
   930
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   931
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   932
slouken@5154
   933
    if (renderer != texture->renderer) {
slouken@5154
   934
        SDL_SetError("Texture was not created with this renderer");
slouken@5154
   935
        return -1;
slouken@5154
   936
    }
slouken@5154
   937
    window = renderer->window;
slouken@5154
   938
slouken@5154
   939
    real_srcrect.x = 0;
slouken@5154
   940
    real_srcrect.y = 0;
slouken@5154
   941
    real_srcrect.w = texture->w;
slouken@5154
   942
    real_srcrect.h = texture->h;
slouken@5154
   943
    if (srcrect) {
slouken@5154
   944
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
slouken@5154
   945
            return 0;
slouken@5154
   946
        }
slouken@5154
   947
    }
slouken@5154
   948
slouken@5154
   949
    real_dstrect.x = 0;
slouken@5154
   950
    real_dstrect.y = 0;
slouken@5154
   951
    SDL_GetWindowSize(window, &real_dstrect.w, &real_dstrect.h);
slouken@5154
   952
    if (dstrect) {
slouken@5154
   953
        if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
slouken@5154
   954
            return 0;
slouken@5154
   955
        }
slouken@5154
   956
        /* Clip srcrect by the same amount as dstrect was clipped */
slouken@5154
   957
        if (dstrect->w != real_dstrect.w) {
slouken@5154
   958
            int deltax = (real_dstrect.x - dstrect->x);
slouken@5154
   959
            int deltaw = (real_dstrect.w - dstrect->w);
slouken@5154
   960
            real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
slouken@5154
   961
            real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
slouken@5154
   962
        }
slouken@5154
   963
        if (dstrect->h != real_dstrect.h) {
slouken@5154
   964
            int deltay = (real_dstrect.y - dstrect->y);
slouken@5154
   965
            int deltah = (real_dstrect.h - dstrect->h);
slouken@5154
   966
            real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
slouken@5154
   967
            real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
slouken@5154
   968
        }
slouken@5154
   969
    }
slouken@5154
   970
slouken@5156
   971
    if (texture->native) {
slouken@5156
   972
        texture = texture->native;
slouken@5156
   973
    }
slouken@5156
   974
slouken@5154
   975
    return renderer->RenderCopy(renderer, texture, &real_srcrect,
slouken@5154
   976
                                &real_dstrect);
slouken@5154
   977
}
slouken@5154
   978
slouken@5154
   979
int
slouken@5154
   980
SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@5154
   981
                     Uint32 format, void * pixels, int pitch)
slouken@5154
   982
{
slouken@5154
   983
    SDL_Window *window;
slouken@5154
   984
    SDL_Rect real_rect;
slouken@5154
   985
slouken@5154
   986
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   987
slouken@5154
   988
    if (!renderer->RenderReadPixels) {
slouken@5154
   989
        SDL_Unsupported();
slouken@5154
   990
        return -1;
slouken@5154
   991
    }
slouken@5154
   992
    window = renderer->window;
slouken@5154
   993
slouken@5154
   994
    if (!format) {
slouken@5154
   995
        format = SDL_GetWindowPixelFormat(window);
slouken@5154
   996
    }
slouken@5154
   997
slouken@5154
   998
    real_rect.x = 0;
slouken@5154
   999
    real_rect.y = 0;
slouken@5154
  1000
    SDL_GetWindowSize(window, &real_rect.w, &real_rect.h);
slouken@5154
  1001
    if (rect) {
slouken@5154
  1002
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@5154
  1003
            return 0;
slouken@5154
  1004
        }
slouken@5154
  1005
        if (real_rect.y > rect->y) {
slouken@5154
  1006
            pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@5154
  1007
        }
slouken@5154
  1008
        if (real_rect.x > rect->x) {
slouken@5154
  1009
            int bpp = SDL_BYTESPERPIXEL(SDL_GetWindowPixelFormat(window));
slouken@5154
  1010
            pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@5154
  1011
        }
slouken@5154
  1012
    }
slouken@5154
  1013
slouken@5154
  1014
    return renderer->RenderReadPixels(renderer, &real_rect,
slouken@5154
  1015
                                      format, pixels, pitch);
slouken@5154
  1016
}
slouken@5154
  1017
slouken@5154
  1018
void
slouken@5154
  1019
SDL_RenderPresent(SDL_Renderer * renderer)
slouken@5154
  1020
{
slouken@5154
  1021
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1022
slouken@5154
  1023
    renderer->RenderPresent(renderer);
slouken@5154
  1024
}
slouken@5154
  1025
slouken@5154
  1026
void
slouken@5154
  1027
SDL_DestroyTexture(SDL_Texture * texture)
slouken@5154
  1028
{
slouken@5154
  1029
    SDL_Renderer *renderer;
slouken@5154
  1030
slouken@5154
  1031
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
  1032
    texture->magic = NULL;
slouken@5154
  1033
slouken@5154
  1034
    renderer = texture->renderer;
slouken@5154
  1035
    if (texture->next) {
slouken@5154
  1036
        texture->next->prev = texture->prev;
slouken@5154
  1037
    }
slouken@5154
  1038
    if (texture->prev) {
slouken@5154
  1039
        texture->prev->next = texture->next;
slouken@5154
  1040
    } else {
slouken@5154
  1041
        renderer->textures = texture->next;
slouken@5154
  1042
    }
slouken@5154
  1043
slouken@5156
  1044
    if (texture->native) {
slouken@5156
  1045
        SDL_DestroyTexture(texture->native);
slouken@5156
  1046
    }
slouken@5156
  1047
    if (texture->yuv) {
slouken@5156
  1048
        SDL_SW_DestroyYUVTexture(texture->yuv);
slouken@5156
  1049
    }
slouken@5156
  1050
    if (texture->pixels) {
slouken@5156
  1051
        SDL_free(texture->pixels);
slouken@5156
  1052
    }
slouken@5156
  1053
slouken@5154
  1054
    renderer->DestroyTexture(renderer, texture);
slouken@5154
  1055
    SDL_free(texture);
slouken@5154
  1056
}
slouken@5154
  1057
slouken@5154
  1058
void
slouken@5154
  1059
SDL_DestroyRenderer(SDL_Renderer * renderer)
slouken@5154
  1060
{
slouken@5154
  1061
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1062
slouken@5154
  1063
    SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
slouken@5154
  1064
slouken@5154
  1065
    /* Free existing textures for this renderer */
slouken@5154
  1066
    while (renderer->textures) {
slouken@5154
  1067
        SDL_DestroyTexture(renderer->textures);
slouken@5154
  1068
    }
slouken@5154
  1069
slouken@5154
  1070
    /* It's no longer magical... */
slouken@5154
  1071
    renderer->magic = NULL;
slouken@5154
  1072
slouken@5154
  1073
    /* Free the renderer instance */
slouken@5154
  1074
    renderer->DestroyRenderer(renderer);
slouken@5154
  1075
}
slouken@5154
  1076
slouken@5154
  1077
/* vi: set ts=4 sw=4 expandtab: */