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