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