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