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