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