src/render/SDL_render.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 01 Oct 2012 20:59:33 -0700
changeset 6528 e978048ced60
parent 6497 e844e2632149
child 6530 ba5de88eab60
permissions -rw-r--r--
Added SDL_RenderSetScale() and SDL_RenderGetScale()
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@6260
   103
            if (event->window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@5297
   104
                /* Try to keep the previous viewport centered */
slouken@5297
   105
                int w, h;
slouken@5297
   106
slouken@5297
   107
                SDL_GetWindowSize(window, &w, &h);
slouken@6246
   108
                if (renderer->target) {
slouken@6246
   109
                    renderer->viewport_backup.x = (w - renderer->viewport_backup.w) / 2;
slouken@6246
   110
                    renderer->viewport_backup.y = (h - renderer->viewport_backup.h) / 2;
slouken@6246
   111
                } else {
slouken@6528
   112
                    renderer->viewport.x = (w - renderer->viewport.w) / 2;
slouken@6528
   113
                    renderer->viewport.y = (h - renderer->viewport.h) / 2;
slouken@6528
   114
                    renderer->UpdateViewport(renderer);
slouken@6246
   115
                }
slouken@6260
   116
                renderer->resized = SDL_TRUE;
slouken@6260
   117
            } else if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@6260
   118
                if (!renderer->resized) {
slouken@6260
   119
                    /* Window was programmatically resized, reset viewport */
slouken@6528
   120
                    int w, h;
slouken@6528
   121
slouken@6260
   122
                    SDL_GetWindowSize(window, &w, &h);
slouken@6260
   123
                    if (renderer->target) {
slouken@6260
   124
                        renderer->viewport_backup.x = 0;
slouken@6260
   125
                        renderer->viewport_backup.y = 0;
slouken@6260
   126
                        renderer->viewport_backup.w = w;
slouken@6260
   127
                        renderer->viewport_backup.h = h;
slouken@6260
   128
                    } else {
slouken@6528
   129
                        renderer->viewport.x = 0;
slouken@6528
   130
                        renderer->viewport.y = 0;
slouken@6528
   131
                        renderer->viewport.w = w;
slouken@6528
   132
                        renderer->viewport.h = h;
slouken@6528
   133
                        renderer->UpdateViewport(renderer);
slouken@6260
   134
                    }
slouken@6260
   135
                }
slouken@6528
   136
                renderer->resized = SDL_FALSE;
slouken@6260
   137
            } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
slouken@6260
   138
                renderer->hidden = SDL_TRUE;
slouken@6260
   139
            } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
slouken@6260
   140
                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
slouken@6260
   141
                    renderer->hidden = SDL_FALSE;
slouken@6260
   142
                }
slouken@6060
   143
            } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
slouken@6260
   144
                renderer->hidden = SDL_TRUE;
slouken@6060
   145
            } else if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
slouken@6260
   146
                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
slouken@6260
   147
                    renderer->hidden = SDL_FALSE;
slouken@6260
   148
                }
slouken@5297
   149
            }
slouken@5154
   150
        }
slouken@5154
   151
    }
slouken@5154
   152
    return 0;
slouken@5154
   153
}
slouken@5154
   154
slouken@6258
   155
int
slouken@6258
   156
SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
slouken@6258
   157
                            SDL_Window **window, SDL_Renderer **renderer)
slouken@6258
   158
{
slouken@6258
   159
    *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
slouken@6258
   160
                                     SDL_WINDOWPOS_UNDEFINED,
slouken@6258
   161
                                     width, height, window_flags);
slouken@6258
   162
    if (!*window) {
slouken@6258
   163
        *renderer = NULL;
slouken@6258
   164
        return -1;
slouken@6258
   165
    }
slouken@6258
   166
slouken@6258
   167
    *renderer = SDL_CreateRenderer(*window, -1, 0);
slouken@6258
   168
    if (!*renderer) {
slouken@6258
   169
        return -1;
slouken@6258
   170
    }
slouken@6258
   171
slouken@6258
   172
    return 0;
slouken@6258
   173
}
slouken@6258
   174
slouken@5154
   175
SDL_Renderer *
slouken@5154
   176
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
slouken@5154
   177
{
slouken@5154
   178
    SDL_Renderer *renderer = NULL;
slouken@5154
   179
    int n = SDL_GetNumRenderDrivers();
slouken@5192
   180
    const char *hint;
slouken@5192
   181
slouken@5528
   182
    if (!window) {
slouken@5528
   183
        SDL_SetError("Invalid window");
slouken@5528
   184
        return NULL;
slouken@5528
   185
    }
slouken@5528
   186
slouken@5528
   187
    if (SDL_GetRenderer(window)) {
slouken@5528
   188
        SDL_SetError("Renderer already associated with window");
slouken@5528
   189
        return NULL;
slouken@5528
   190
    }
slouken@5528
   191
slouken@5192
   192
    hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
slouken@5192
   193
    if (hint) {
slouken@5192
   194
        if (*hint == '0') {
slouken@5192
   195
            flags &= ~SDL_RENDERER_PRESENTVSYNC;
slouken@5192
   196
        } else {
slouken@5192
   197
            flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@5192
   198
        }
slouken@5192
   199
    }
slouken@5154
   200
slouken@5154
   201
    if (index < 0) {
slouken@5192
   202
        hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
slouken@5192
   203
        if (hint) {
slouken@5154
   204
            for (index = 0; index < n; ++index) {
slouken@5154
   205
                const SDL_RenderDriver *driver = render_drivers[index];
slouken@5154
   206
slouken@5192
   207
                if (SDL_strcasecmp(hint, driver->info.name) == 0) {
slouken@5154
   208
                    /* Create a new renderer instance */
slouken@5154
   209
                    renderer = driver->CreateRenderer(window, flags);
slouken@5154
   210
                    break;
slouken@5154
   211
                }
slouken@5154
   212
            }
slouken@5192
   213
        }
slouken@5192
   214
slouken@5192
   215
        if (!renderer) {
slouken@5154
   216
            for (index = 0; index < n; ++index) {
slouken@5154
   217
                const SDL_RenderDriver *driver = render_drivers[index];
slouken@5154
   218
slouken@5154
   219
                if ((driver->info.flags & flags) == flags) {
slouken@5154
   220
                    /* Create a new renderer instance */
slouken@5154
   221
                    renderer = driver->CreateRenderer(window, flags);
slouken@5154
   222
                    if (renderer) {
slouken@5154
   223
                        /* Yay, we got one! */
slouken@5154
   224
                        break;
slouken@5154
   225
                    }
slouken@5154
   226
                }
slouken@5154
   227
            }
slouken@5154
   228
        }
slouken@5154
   229
        if (index == n) {
slouken@5154
   230
            SDL_SetError("Couldn't find matching render driver");
slouken@5154
   231
            return NULL;
slouken@5154
   232
        }
slouken@5154
   233
    } else {
slouken@5154
   234
        if (index >= SDL_GetNumRenderDrivers()) {
slouken@5154
   235
            SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@5154
   236
                         SDL_GetNumRenderDrivers() - 1);
slouken@5154
   237
            return NULL;
slouken@5154
   238
        }
slouken@5154
   239
        /* Create a new renderer instance */
slouken@5154
   240
        renderer = render_drivers[index]->CreateRenderer(window, flags);
slouken@5154
   241
    }
slouken@5154
   242
slouken@5154
   243
    if (renderer) {
slouken@5154
   244
        renderer->magic = &renderer_magic;
slouken@5166
   245
        renderer->window = window;
slouken@6528
   246
        renderer->scale.x = 1.0f;
slouken@6528
   247
        renderer->scale.y = 1.0f;
slouken@5154
   248
slouken@6260
   249
        if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
slouken@6260
   250
            renderer->hidden = SDL_TRUE;
slouken@6060
   251
        } else {
slouken@6260
   252
            renderer->hidden = SDL_FALSE;
slouken@6060
   253
        }
slouken@6060
   254
slouken@5528
   255
        SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
slouken@5528
   256
slouken@5297
   257
        SDL_RenderSetViewport(renderer, NULL);
slouken@5297
   258
slouken@5154
   259
        SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
slouken@5221
   260
slouken@5221
   261
        SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
slouken@5221
   262
                    "Created renderer: %s", renderer->info.name);
slouken@5154
   263
    }
slouken@5154
   264
    return renderer;
slouken@5154
   265
}
slouken@5154
   266
slouken@5166
   267
SDL_Renderer *
slouken@5166
   268
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
slouken@5166
   269
{
slouken@5226
   270
#if !SDL_RENDER_DISABLED
slouken@5297
   271
    SDL_Renderer *renderer;
slouken@5297
   272
slouken@5297
   273
    renderer = SW_CreateRendererForSurface(surface);
slouken@5297
   274
slouken@5297
   275
    if (renderer) {
slouken@5297
   276
        renderer->magic = &renderer_magic;
slouken@5297
   277
slouken@5297
   278
        SDL_RenderSetViewport(renderer, NULL);
slouken@5297
   279
    }
slouken@5297
   280
    return renderer;
slouken@5226
   281
#else
slouken@5226
   282
    SDL_SetError("SDL not built with rendering support");
slouken@5226
   283
    return NULL;
slouken@5226
   284
#endif /* !SDL_RENDER_DISABLED */
slouken@5166
   285
}
slouken@5166
   286
slouken@5528
   287
SDL_Renderer *
slouken@5528
   288
SDL_GetRenderer(SDL_Window * window)
slouken@5528
   289
{
slouken@5528
   290
    return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
slouken@5528
   291
}
slouken@5528
   292
slouken@5154
   293
int
slouken@5154
   294
SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
slouken@5154
   295
{
slouken@5154
   296
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   297
slouken@5154
   298
    *info = renderer->info;
slouken@5154
   299
    return 0;
slouken@5154
   300
}
slouken@5154
   301
slouken@5156
   302
static SDL_bool
slouken@5156
   303
IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
slouken@5156
   304
{
slouken@5156
   305
    Uint32 i;
slouken@5156
   306
slouken@5156
   307
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5156
   308
        if (renderer->info.texture_formats[i] == format) {
slouken@5156
   309
            return SDL_TRUE;
slouken@5156
   310
        }
slouken@5156
   311
    }
slouken@5156
   312
    return SDL_FALSE;
slouken@5156
   313
}
slouken@5156
   314
slouken@5156
   315
static Uint32
slouken@5156
   316
GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
slouken@5156
   317
{
slouken@5156
   318
    Uint32 i;
slouken@5156
   319
slouken@5268
   320
    if (SDL_ISPIXELFORMAT_FOURCC(format)) {
slouken@5268
   321
        /* Look for an exact match */
slouken@5268
   322
        for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5268
   323
            if (renderer->info.texture_formats[i] == format) {
slouken@5268
   324
                return renderer->info.texture_formats[i];
slouken@5268
   325
            }
slouken@5268
   326
        }
slouken@5268
   327
    } else {
slouken@5268
   328
        SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
slouken@5268
   329
slouken@5268
   330
        /* We just want to match the first format that has the same channels */
slouken@5268
   331
        for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5300
   332
            if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
slouken@5300
   333
                SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
slouken@5268
   334
                return renderer->info.texture_formats[i];
slouken@5268
   335
            }
slouken@5156
   336
        }
slouken@5156
   337
    }
slouken@5156
   338
    return renderer->info.texture_formats[0];
slouken@5156
   339
}
slouken@5156
   340
slouken@5154
   341
SDL_Texture *
slouken@5154
   342
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
slouken@5154
   343
{
slouken@5154
   344
    SDL_Texture *texture;
slouken@5154
   345
slouken@5154
   346
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   347
slouken@5413
   348
    if (!format) {
slouken@5413
   349
        format = renderer->info.texture_formats[0];
slouken@5413
   350
    }
slouken@5156
   351
    if (SDL_ISPIXELFORMAT_INDEXED(format)) {
slouken@5156
   352
        SDL_SetError("Palettized textures are not supported");
slouken@5156
   353
        return NULL;
slouken@5156
   354
    }
slouken@5154
   355
    if (w <= 0 || h <= 0) {
slouken@5154
   356
        SDL_SetError("Texture dimensions can't be 0");
slouken@5156
   357
        return NULL;
slouken@5154
   358
    }
slouken@5154
   359
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@5154
   360
    if (!texture) {
slouken@5154
   361
        SDL_OutOfMemory();
slouken@5156
   362
        return NULL;
slouken@5154
   363
    }
slouken@5154
   364
    texture->magic = &texture_magic;
slouken@5154
   365
    texture->format = format;
slouken@5154
   366
    texture->access = access;
slouken@5154
   367
    texture->w = w;
slouken@5154
   368
    texture->h = h;
slouken@5154
   369
    texture->r = 255;
slouken@5154
   370
    texture->g = 255;
slouken@5154
   371
    texture->b = 255;
slouken@5154
   372
    texture->a = 255;
slouken@5154
   373
    texture->renderer = renderer;
slouken@5154
   374
    texture->next = renderer->textures;
slouken@5154
   375
    if (renderer->textures) {
slouken@5154
   376
        renderer->textures->prev = texture;
slouken@5154
   377
    }
slouken@5154
   378
    renderer->textures = texture;
slouken@5154
   379
slouken@5156
   380
    if (IsSupportedFormat(renderer, format)) {
slouken@5156
   381
        if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@5156
   382
            SDL_DestroyTexture(texture);
slouken@5156
   383
            return 0;
slouken@5156
   384
        }
slouken@5156
   385
    } else {
slouken@5156
   386
        texture->native = SDL_CreateTexture(renderer,
slouken@5156
   387
                                GetClosestSupportedFormat(renderer, format),
slouken@5156
   388
                                access, w, h);
slouken@5156
   389
        if (!texture->native) {
slouken@5156
   390
            SDL_DestroyTexture(texture);
slouken@5156
   391
            return NULL;
slouken@5156
   392
        }
slouken@5156
   393
slouken@6497
   394
        /* Swap textures to have texture before texture->native in the list */
slouken@6497
   395
        texture->native->next = texture->next;
slouken@6497
   396
        texture->prev = texture->native->prev;
slouken@6497
   397
        texture->native->prev = texture;
slouken@6497
   398
        texture->next = texture->native;
slouken@6497
   399
        renderer->textures = texture;
slouken@6497
   400
slouken@5156
   401
        if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@5156
   402
            texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
slouken@5156
   403
            if (!texture->yuv) {
slouken@5156
   404
                SDL_DestroyTexture(texture);
slouken@5156
   405
                return NULL;
slouken@5156
   406
            }
slouken@5156
   407
        } else if (access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   408
            /* The pitch is 4 byte aligned */
slouken@5156
   409
            texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
slouken@5342
   410
            texture->pixels = SDL_calloc(1, texture->pitch * h);
slouken@5156
   411
            if (!texture->pixels) {
slouken@5156
   412
                SDL_DestroyTexture(texture);
slouken@5156
   413
                return NULL;
slouken@5156
   414
            }
slouken@5156
   415
        }
slouken@5154
   416
    }
slouken@5154
   417
    return texture;
slouken@5154
   418
}
slouken@5154
   419
slouken@5154
   420
SDL_Texture *
slouken@5158
   421
SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
slouken@5154
   422
{
slouken@5158
   423
    const SDL_PixelFormat *fmt;
slouken@5158
   424
    SDL_bool needAlpha;
slouken@5158
   425
    Uint32 i;
slouken@5158
   426
    Uint32 format;
slouken@5158
   427
    SDL_Texture *texture;
slouken@5154
   428
slouken@5154
   429
    CHECK_RENDERER_MAGIC(renderer, NULL);
slouken@5154
   430
slouken@5154
   431
    if (!surface) {
slouken@5154
   432
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@5154
   433
        return NULL;
slouken@5154
   434
    }
slouken@5158
   435
slouken@5158
   436
    /* See what the best texture format is */
slouken@5154
   437
    fmt = surface->format;
slouken@5158
   438
    if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
slouken@5158
   439
        needAlpha = SDL_TRUE;
slouken@5154
   440
    } else {
slouken@5158
   441
        needAlpha = SDL_FALSE;
slouken@5158
   442
    }
slouken@5158
   443
    format = renderer->info.texture_formats[0];
slouken@5158
   444
    for (i = 0; i < renderer->info.num_texture_formats; ++i) {
slouken@5268
   445
        if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
slouken@5268
   446
            SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
slouken@5158
   447
            format = renderer->info.texture_formats[i];
slouken@5158
   448
            break;
slouken@5154
   449
        }
slouken@5154
   450
    }
slouken@5154
   451
slouken@5158
   452
    texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
slouken@5158
   453
                                surface->w, surface->h);
slouken@5154
   454
    if (!texture) {
slouken@5158
   455
        return NULL;
slouken@5154
   456
    }
slouken@5158
   457
slouken@5288
   458
    if (format == surface->format->format) {
slouken@5154
   459
        if (SDL_MUSTLOCK(surface)) {
slouken@5154
   460
            SDL_LockSurface(surface);
slouken@5158
   461
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
slouken@5154
   462
            SDL_UnlockSurface(surface);
slouken@5154
   463
        } else {
slouken@5158
   464
            SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
slouken@5154
   465
        }
slouken@5154
   466
    } else {
slouken@5297
   467
        SDL_PixelFormat *dst_fmt;
slouken@5158
   468
        SDL_Surface *temp = NULL;
slouken@5154
   469
slouken@5154
   470
        /* Set up a destination surface for the texture update */
slouken@5297
   471
        dst_fmt = SDL_AllocFormat(format);
slouken@5297
   472
        temp = SDL_ConvertSurface(surface, dst_fmt, 0);
slouken@5297
   473
        SDL_FreeFormat(dst_fmt);
slouken@5158
   474
        if (temp) {
slouken@5158
   475
            SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
slouken@5158
   476
            SDL_FreeSurface(temp);
slouken@5158
   477
        } else {
slouken@5154
   478
            SDL_DestroyTexture(texture);
slouken@5158
   479
            return NULL;
slouken@5154
   480
        }
slouken@5154
   481
    }
slouken@5154
   482
slouken@5154
   483
    {
slouken@5154
   484
        Uint8 r, g, b, a;
slouken@5154
   485
        SDL_BlendMode blendMode;
slouken@5154
   486
slouken@5154
   487
        SDL_GetSurfaceColorMod(surface, &r, &g, &b);
slouken@5154
   488
        SDL_SetTextureColorMod(texture, r, g, b);
slouken@5154
   489
slouken@5154
   490
        SDL_GetSurfaceAlphaMod(surface, &a);
slouken@5154
   491
        SDL_SetTextureAlphaMod(texture, a);
slouken@5154
   492
slouken@5154
   493
        if (SDL_GetColorKey(surface, NULL) == 0) {
slouken@5154
   494
            /* We converted to a texture with alpha format */
slouken@5154
   495
            SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
slouken@5154
   496
        } else {
slouken@5154
   497
            SDL_GetSurfaceBlendMode(surface, &blendMode);
slouken@5154
   498
            SDL_SetTextureBlendMode(texture, blendMode);
slouken@5154
   499
        }
slouken@5154
   500
    }
slouken@5154
   501
    return texture;
slouken@5154
   502
}
slouken@5154
   503
slouken@5154
   504
int
slouken@5154
   505
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
slouken@5154
   506
                 int *w, int *h)
slouken@5154
   507
{
slouken@5154
   508
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   509
slouken@5154
   510
    if (format) {
slouken@5154
   511
        *format = texture->format;
slouken@5154
   512
    }
slouken@5154
   513
    if (access) {
slouken@5154
   514
        *access = texture->access;
slouken@5154
   515
    }
slouken@5154
   516
    if (w) {
slouken@5154
   517
        *w = texture->w;
slouken@5154
   518
    }
slouken@5154
   519
    if (h) {
slouken@5154
   520
        *h = texture->h;
slouken@5154
   521
    }
slouken@5154
   522
    return 0;
slouken@5154
   523
}
slouken@5154
   524
slouken@5154
   525
int
slouken@5154
   526
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
slouken@5154
   527
{
slouken@5154
   528
    SDL_Renderer *renderer;
slouken@5154
   529
slouken@5154
   530
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   531
slouken@5154
   532
    renderer = texture->renderer;
slouken@5154
   533
    if (r < 255 || g < 255 || b < 255) {
slouken@5154
   534
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   535
    } else {
slouken@5154
   536
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
slouken@5154
   537
    }
slouken@5154
   538
    texture->r = r;
slouken@5154
   539
    texture->g = g;
slouken@5154
   540
    texture->b = b;
slouken@5156
   541
    if (texture->native) {
slouken@5156
   542
        return SDL_SetTextureColorMod(texture->native, r, g, b);
slouken@5156
   543
    } else if (renderer->SetTextureColorMod) {
slouken@5154
   544
        return renderer->SetTextureColorMod(renderer, texture);
slouken@5154
   545
    } else {
slouken@5154
   546
        return 0;
slouken@5154
   547
    }
slouken@5154
   548
}
slouken@5154
   549
slouken@5154
   550
int
slouken@5154
   551
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
slouken@5154
   552
                       Uint8 * b)
slouken@5154
   553
{
slouken@5154
   554
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   555
slouken@5154
   556
    if (r) {
slouken@5154
   557
        *r = texture->r;
slouken@5154
   558
    }
slouken@5154
   559
    if (g) {
slouken@5154
   560
        *g = texture->g;
slouken@5154
   561
    }
slouken@5154
   562
    if (b) {
slouken@5154
   563
        *b = texture->b;
slouken@5154
   564
    }
slouken@5154
   565
    return 0;
slouken@5154
   566
}
slouken@5154
   567
slouken@5154
   568
int
slouken@5154
   569
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
slouken@5154
   570
{
slouken@5154
   571
    SDL_Renderer *renderer;
slouken@5154
   572
slouken@5154
   573
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   574
slouken@5154
   575
    renderer = texture->renderer;
slouken@5154
   576
    if (alpha < 255) {
slouken@5154
   577
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   578
    } else {
slouken@5154
   579
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
slouken@5154
   580
    }
slouken@5154
   581
    texture->a = alpha;
slouken@5156
   582
    if (texture->native) {
slouken@5156
   583
        return SDL_SetTextureAlphaMod(texture->native, alpha);
slouken@5156
   584
    } else if (renderer->SetTextureAlphaMod) {
slouken@5154
   585
        return renderer->SetTextureAlphaMod(renderer, texture);
slouken@5154
   586
    } else {
slouken@5154
   587
        return 0;
slouken@5154
   588
    }
slouken@5154
   589
}
slouken@5154
   590
slouken@5154
   591
int
slouken@5154
   592
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
slouken@5154
   593
{
slouken@5154
   594
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   595
slouken@5154
   596
    if (alpha) {
slouken@5154
   597
        *alpha = texture->a;
slouken@5154
   598
    }
slouken@5154
   599
    return 0;
slouken@5154
   600
}
slouken@5154
   601
slouken@5154
   602
int
slouken@5154
   603
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
slouken@5154
   604
{
slouken@5154
   605
    SDL_Renderer *renderer;
slouken@5154
   606
slouken@5154
   607
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   608
slouken@5154
   609
    renderer = texture->renderer;
slouken@5154
   610
    texture->blendMode = blendMode;
slouken@5156
   611
    if (texture->native) {
slouken@5180
   612
        return SDL_SetTextureBlendMode(texture->native, blendMode);
slouken@5156
   613
    } else if (renderer->SetTextureBlendMode) {
slouken@5154
   614
        return renderer->SetTextureBlendMode(renderer, texture);
slouken@5154
   615
    } else {
slouken@5154
   616
        return 0;
slouken@5154
   617
    }
slouken@5154
   618
}
slouken@5154
   619
slouken@5154
   620
int
slouken@5154
   621
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
slouken@5154
   622
{
slouken@5154
   623
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   624
slouken@5154
   625
    if (blendMode) {
slouken@5154
   626
        *blendMode = texture->blendMode;
slouken@5154
   627
    }
slouken@5154
   628
    return 0;
slouken@5154
   629
}
slouken@5154
   630
slouken@5156
   631
static int
slouken@5156
   632
SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   633
                     const void *pixels, int pitch)
slouken@5156
   634
{
slouken@5156
   635
    SDL_Texture *native = texture->native;
slouken@5156
   636
    SDL_Rect full_rect;
slouken@5156
   637
slouken@5156
   638
    if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
slouken@5156
   639
        return -1;
slouken@5156
   640
    }
slouken@5156
   641
slouken@5156
   642
    full_rect.x = 0;
slouken@5156
   643
    full_rect.y = 0;
slouken@5156
   644
    full_rect.w = texture->w;
slouken@5156
   645
    full_rect.h = texture->h;
slouken@5156
   646
    rect = &full_rect;
slouken@5156
   647
slouken@5156
   648
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   649
        /* We can lock the texture and copy to it */
slouken@5156
   650
        void *native_pixels;
slouken@5156
   651
        int native_pitch;
slouken@5156
   652
slouken@5156
   653
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   654
            return -1;
slouken@5156
   655
        }
slouken@5156
   656
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@5156
   657
                            rect->w, rect->h, native_pixels, native_pitch);
slouken@5156
   658
        SDL_UnlockTexture(native);
slouken@5156
   659
    } else {
slouken@5156
   660
        /* Use a temporary buffer for updating */
slouken@5156
   661
        void *temp_pixels;
slouken@5156
   662
        int temp_pitch;
slouken@5156
   663
slouken@5156
   664
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@5156
   665
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5156
   666
        if (!temp_pixels) {
slouken@5156
   667
            SDL_OutOfMemory();
slouken@5156
   668
            return -1;
slouken@5156
   669
        }
slouken@5156
   670
        SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
slouken@5156
   671
                            rect->w, rect->h, temp_pixels, temp_pitch);
slouken@5156
   672
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@5156
   673
        SDL_free(temp_pixels);
slouken@5156
   674
    }
slouken@5156
   675
    return 0;
slouken@5156
   676
}
slouken@5156
   677
slouken@5156
   678
static int
slouken@5156
   679
SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   680
                        const void *pixels, int pitch)
slouken@5156
   681
{
slouken@5156
   682
    SDL_Texture *native = texture->native;
slouken@5156
   683
slouken@5156
   684
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   685
        /* We can lock the texture and copy to it */
slouken@5156
   686
        void *native_pixels;
slouken@5156
   687
        int native_pitch;
slouken@5156
   688
slouken@5156
   689
        if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   690
            return -1;
slouken@5156
   691
        }
slouken@5156
   692
        SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   693
                          texture->format, pixels, pitch,
slouken@5156
   694
                          native->format, native_pixels, native_pitch);
slouken@5156
   695
        SDL_UnlockTexture(native);
slouken@5156
   696
    } else {
slouken@5156
   697
        /* Use a temporary buffer for updating */
slouken@5156
   698
        void *temp_pixels;
slouken@5156
   699
        int temp_pitch;
slouken@5156
   700
slouken@5156
   701
        temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
slouken@5156
   702
        temp_pixels = SDL_malloc(rect->h * temp_pitch);
slouken@5156
   703
        if (!temp_pixels) {
slouken@5156
   704
            SDL_OutOfMemory();
slouken@5156
   705
            return -1;
slouken@5156
   706
        }
slouken@5156
   707
        SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   708
                          texture->format, pixels, pitch,
slouken@5156
   709
                          native->format, temp_pixels, temp_pitch);
slouken@5156
   710
        SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
slouken@5156
   711
        SDL_free(temp_pixels);
slouken@5156
   712
    }
slouken@5156
   713
    return 0;
slouken@5156
   714
}
slouken@5156
   715
slouken@5154
   716
int
slouken@5154
   717
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   718
                  const void *pixels, int pitch)
slouken@5154
   719
{
slouken@5154
   720
    SDL_Renderer *renderer;
slouken@5154
   721
    SDL_Rect full_rect;
slouken@5154
   722
slouken@5154
   723
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   724
slouken@5154
   725
    if (!rect) {
slouken@5154
   726
        full_rect.x = 0;
slouken@5154
   727
        full_rect.y = 0;
slouken@5154
   728
        full_rect.w = texture->w;
slouken@5154
   729
        full_rect.h = texture->h;
slouken@5154
   730
        rect = &full_rect;
slouken@5154
   731
    }
slouken@5156
   732
slouken@5156
   733
    if (texture->yuv) {
slouken@5156
   734
        return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
slouken@5156
   735
    } else if (texture->native) {
slouken@5156
   736
        return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
slouken@5156
   737
    } else {
slouken@5156
   738
        renderer = texture->renderer;
slouken@5156
   739
        return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@5156
   740
    }
slouken@5156
   741
}
slouken@5156
   742
slouken@5156
   743
static int
slouken@5156
   744
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   745
                   void **pixels, int *pitch)
slouken@5156
   746
{
slouken@5156
   747
    return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
slouken@5156
   748
}
slouken@5156
   749
slouken@5156
   750
static int
slouken@5156
   751
SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5156
   752
                      void **pixels, int *pitch)
slouken@5156
   753
{
slouken@5156
   754
    texture->locked_rect = *rect;
slouken@5156
   755
    *pixels = (void *) ((Uint8 *) texture->pixels +
slouken@5156
   756
                        rect->y * texture->pitch +
slouken@5156
   757
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5156
   758
    *pitch = texture->pitch;
slouken@5156
   759
    return 0;
slouken@5154
   760
}
slouken@5154
   761
slouken@5154
   762
int
slouken@5156
   763
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@5154
   764
                void **pixels, int *pitch)
slouken@5154
   765
{
slouken@5154
   766
    SDL_Renderer *renderer;
slouken@5154
   767
    SDL_Rect full_rect;
slouken@5154
   768
slouken@5154
   769
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
   770
slouken@5154
   771
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   772
        SDL_SetError("SDL_LockTexture(): texture must be streaming");
slouken@5154
   773
        return -1;
slouken@5154
   774
    }
slouken@5156
   775
slouken@5154
   776
    if (!rect) {
slouken@5154
   777
        full_rect.x = 0;
slouken@5154
   778
        full_rect.y = 0;
slouken@5154
   779
        full_rect.w = texture->w;
slouken@5154
   780
        full_rect.h = texture->h;
slouken@5154
   781
        rect = &full_rect;
slouken@5154
   782
    }
slouken@5156
   783
slouken@5156
   784
    if (texture->yuv) {
slouken@5156
   785
        return SDL_LockTextureYUV(texture, rect, pixels, pitch);
slouken@5156
   786
    } else if (texture->native) {
slouken@5156
   787
        return SDL_LockTextureNative(texture, rect, pixels, pitch);
slouken@5156
   788
    } else {
slouken@5156
   789
        renderer = texture->renderer;
slouken@5156
   790
        return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
slouken@5156
   791
    }
slouken@5156
   792
}
slouken@5156
   793
slouken@5156
   794
static void
slouken@5156
   795
SDL_UnlockTextureYUV(SDL_Texture * texture)
slouken@5156
   796
{
slouken@5156
   797
    SDL_Texture *native = texture->native;
slouken@5156
   798
    void *native_pixels;
slouken@5156
   799
    int native_pitch;
slouken@5156
   800
    SDL_Rect rect;
slouken@5156
   801
slouken@5156
   802
    rect.x = 0;
slouken@5156
   803
    rect.y = 0;
slouken@5156
   804
    rect.w = texture->w;
slouken@5156
   805
    rect.h = texture->h;
slouken@5156
   806
slouken@5156
   807
    if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   808
        return;
slouken@5156
   809
    }
slouken@5156
   810
    SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
slouken@5156
   811
                        rect.w, rect.h, native_pixels, native_pitch);
slouken@5156
   812
    SDL_UnlockTexture(native);
slouken@5156
   813
}
slouken@5156
   814
slouken@6044
   815
static void
slouken@5156
   816
SDL_UnlockTextureNative(SDL_Texture * texture)
slouken@5156
   817
{
slouken@5156
   818
    SDL_Texture *native = texture->native;
slouken@5156
   819
    void *native_pixels;
slouken@5156
   820
    int native_pitch;
slouken@5156
   821
    const SDL_Rect *rect = &texture->locked_rect;
slouken@5156
   822
    const void* pixels = (void *) ((Uint8 *) texture->pixels +
slouken@5156
   823
                        rect->y * texture->pitch +
slouken@5156
   824
                        rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5156
   825
    int pitch = texture->pitch;
slouken@5156
   826
slouken@5156
   827
    if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
slouken@5156
   828
        return;
slouken@5156
   829
    }
slouken@5156
   830
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   831
                      texture->format, pixels, pitch,
slouken@5156
   832
                      native->format, native_pixels, native_pitch);
slouken@5156
   833
    SDL_UnlockTexture(native);
slouken@5154
   834
}
slouken@5154
   835
slouken@5154
   836
void
slouken@5154
   837
SDL_UnlockTexture(SDL_Texture * texture)
slouken@5154
   838
{
slouken@5154
   839
    SDL_Renderer *renderer;
slouken@5154
   840
slouken@5154
   841
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
   842
slouken@5154
   843
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@5154
   844
        return;
slouken@5154
   845
    }
slouken@5156
   846
    if (texture->yuv) {
slouken@5156
   847
        SDL_UnlockTextureYUV(texture);
slouken@5156
   848
    } else if (texture->native) {
slouken@5156
   849
        SDL_UnlockTextureNative(texture);
slouken@5156
   850
    } else {
slouken@5156
   851
        renderer = texture->renderer;
slouken@5156
   852
        renderer->UnlockTexture(renderer, texture);
slouken@5154
   853
    }
slouken@5154
   854
}
slouken@5154
   855
slouken@6246
   856
SDL_bool
slouken@6246
   857
SDL_RenderTargetSupported(SDL_Renderer *renderer)
slouken@6246
   858
{
slouken@6247
   859
    if (!renderer || !renderer->SetRenderTarget) {
slouken@6246
   860
        return SDL_FALSE;
slouken@6246
   861
    }
slouken@6246
   862
    return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
slouken@6246
   863
}
slouken@6246
   864
slouken@6246
   865
int
slouken@6247
   866
SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
slouken@6246
   867
{
slouken@6246
   868
    if (!SDL_RenderTargetSupported(renderer)) {
slouken@6246
   869
        SDL_Unsupported();
slouken@6246
   870
        return -1;
slouken@6246
   871
    }
slouken@6246
   872
    if (texture == renderer->target) {
slouken@6246
   873
        /* Nothing to do! */
slouken@6246
   874
        return 0;
slouken@6246
   875
    }
slouken@6246
   876
slouken@6246
   877
    /* texture == NULL is valid and means reset the target to the window */
slouken@6246
   878
    if (texture) {
slouken@6246
   879
        CHECK_TEXTURE_MAGIC(texture, -1);
slouken@6246
   880
        if (renderer != texture->renderer) {
slouken@6246
   881
            SDL_SetError("Texture was not created with this renderer");
slouken@6246
   882
            return -1;
slouken@6246
   883
        }
gabomdq@6337
   884
        if (texture->access != SDL_TEXTUREACCESS_TARGET) {
slouken@6246
   885
            SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
slouken@6246
   886
            return -1;
slouken@6246
   887
        }
slouken@6246
   888
        if (texture->native) {
slouken@6246
   889
            /* Always render to the native texture */
slouken@6246
   890
            texture = texture->native;
slouken@6246
   891
        }
slouken@6246
   892
    }
slouken@6246
   893
slouken@6246
   894
    if (texture && !renderer->target) {
slouken@6246
   895
        /* Make a backup of the viewport */
slouken@6246
   896
        renderer->viewport_backup = renderer->viewport;
slouken@6528
   897
        renderer->scale_backup = renderer->scale;
slouken@6246
   898
    }
slouken@6246
   899
    renderer->target = texture;
slouken@6246
   900
slouken@6247
   901
    if (renderer->SetRenderTarget(renderer, texture) < 0) {
slouken@6246
   902
        return -1;
slouken@6246
   903
    }
slouken@6246
   904
slouken@6246
   905
    if (texture) {
slouken@6528
   906
        renderer->viewport.x = 0;
slouken@6528
   907
        renderer->viewport.y = 0;
slouken@6528
   908
        renderer->viewport.w = texture->w;
slouken@6528
   909
        renderer->viewport.h = texture->h;
slouken@6528
   910
        renderer->scale.x = 1.0f;
slouken@6528
   911
        renderer->scale.y = 1.0f;
slouken@6246
   912
    } else {
slouken@6528
   913
        renderer->viewport = renderer->viewport_backup;
slouken@6528
   914
        renderer->scale = renderer->scale_backup;
slouken@6246
   915
    }
slouken@6528
   916
    if (renderer->UpdateViewport(renderer) < 0) {
slouken@6246
   917
        return -1;
slouken@6246
   918
    }
slouken@6246
   919
slouken@6246
   920
    /* All set! */
slouken@6246
   921
    return 0;
slouken@6246
   922
}
slouken@6246
   923
slouken@5297
   924
int
slouken@5297
   925
SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5297
   926
{
slouken@5297
   927
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5297
   928
slouken@5297
   929
    if (rect) {
slouken@6528
   930
        renderer->viewport.x = (int)SDL_floor(rect->x * renderer->scale.x);
slouken@6528
   931
        renderer->viewport.y = (int)SDL_floor(rect->y * renderer->scale.y);
slouken@6528
   932
        renderer->viewport.w = (int)SDL_ceil(rect->w * renderer->scale.x);
slouken@6528
   933
        renderer->viewport.h = (int)SDL_ceil(rect->h * renderer->scale.y);
slouken@5297
   934
    } else {
slouken@5297
   935
        renderer->viewport.x = 0;
slouken@5297
   936
        renderer->viewport.y = 0;
slouken@5297
   937
        if (renderer->window) {
slouken@5297
   938
            SDL_GetWindowSize(renderer->window,
slouken@5297
   939
                              &renderer->viewport.w, &renderer->viewport.h);
slouken@5297
   940
        } else {
slouken@5297
   941
            /* This will be filled in by UpdateViewport() */
slouken@5297
   942
            renderer->viewport.w = 0;
slouken@5297
   943
            renderer->viewport.h = 0;
slouken@5297
   944
        }
slouken@5297
   945
    }
slouken@5297
   946
    return renderer->UpdateViewport(renderer);
slouken@5297
   947
}
slouken@5297
   948
slouken@5224
   949
void
slouken@5297
   950
SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
slouken@5224
   951
{
slouken@5224
   952
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5224
   953
slouken@6528
   954
    if (rect) {
slouken@6528
   955
        rect->x = (int)(renderer->viewport.x / renderer->scale.x);
slouken@6528
   956
        rect->y = (int)(renderer->viewport.y / renderer->scale.y);
slouken@6528
   957
        rect->w = (int)(renderer->viewport.w / renderer->scale.x);
slouken@6528
   958
        rect->h = (int)(renderer->viewport.h / renderer->scale.y);
slouken@6528
   959
    }
slouken@6528
   960
}
slouken@6528
   961
slouken@6528
   962
int
slouken@6528
   963
SDL_RenderSetScale(SDL_Renderer * renderer, float scaleX, float scaleY)
slouken@6528
   964
{
slouken@6528
   965
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@6528
   966
slouken@6528
   967
    renderer->scale.x = scaleX;
slouken@6528
   968
    renderer->scale.y = scaleY;
slouken@6528
   969
    return 0;
slouken@6528
   970
}
slouken@6528
   971
slouken@6528
   972
void
slouken@6528
   973
SDL_RenderGetScale(SDL_Renderer * renderer, float *scaleX, float *scaleY)
slouken@6528
   974
{
slouken@6528
   975
    CHECK_RENDERER_MAGIC(renderer, );
slouken@6528
   976
slouken@6528
   977
    if (scaleX) {
slouken@6528
   978
        *scaleX = renderer->scale.x;
slouken@6528
   979
    }
slouken@6528
   980
    if (scaleY) {
slouken@6528
   981
        *scaleY = renderer->scale.y;
slouken@6528
   982
    }
slouken@5224
   983
}
slouken@5224
   984
slouken@5154
   985
int
slouken@5154
   986
SDL_SetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
   987
                       Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@5154
   988
{
slouken@5154
   989
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
   990
slouken@5154
   991
    renderer->r = r;
slouken@5154
   992
    renderer->g = g;
slouken@5154
   993
    renderer->b = b;
slouken@5154
   994
    renderer->a = a;
slouken@5154
   995
    return 0;
slouken@5154
   996
}
slouken@5154
   997
slouken@5154
   998
int
slouken@5154
   999
SDL_GetRenderDrawColor(SDL_Renderer * renderer,
slouken@5154
  1000
                       Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@5154
  1001
{
slouken@5154
  1002
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1003
slouken@5154
  1004
    if (r) {
slouken@5154
  1005
        *r = renderer->r;
slouken@5154
  1006
    }
slouken@5154
  1007
    if (g) {
slouken@5154
  1008
        *g = renderer->g;
slouken@5154
  1009
    }
slouken@5154
  1010
    if (b) {
slouken@5154
  1011
        *b = renderer->b;
slouken@5154
  1012
    }
slouken@5154
  1013
    if (a) {
slouken@5154
  1014
        *a = renderer->a;
slouken@5154
  1015
    }
slouken@5154
  1016
    return 0;
slouken@5154
  1017
}
slouken@5154
  1018
slouken@5154
  1019
int
slouken@5154
  1020
SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
slouken@5154
  1021
{
slouken@5154
  1022
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1023
slouken@5154
  1024
    renderer->blendMode = blendMode;
slouken@5154
  1025
    return 0;
slouken@5154
  1026
}
slouken@5154
  1027
slouken@5154
  1028
int
slouken@5154
  1029
SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
slouken@5154
  1030
{
slouken@5154
  1031
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1032
slouken@5154
  1033
    *blendMode = renderer->blendMode;
slouken@5154
  1034
    return 0;
slouken@5154
  1035
}
slouken@5154
  1036
slouken@5154
  1037
int
slouken@5154
  1038
SDL_RenderClear(SDL_Renderer * renderer)
slouken@5154
  1039
{
slouken@5154
  1040
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1041
slouken@6260
  1042
    /* Don't draw while we're hidden */
slouken@6260
  1043
    if (renderer->hidden) {
slouken@6060
  1044
        return 0;
slouken@6060
  1045
    }
slouken@5154
  1046
    return renderer->RenderClear(renderer);
slouken@5154
  1047
}
slouken@5154
  1048
slouken@5154
  1049
int
slouken@5154
  1050
SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
slouken@5154
  1051
{
slouken@5154
  1052
    SDL_Point point;
slouken@5154
  1053
slouken@5154
  1054
    point.x = x;
slouken@5154
  1055
    point.y = y;
slouken@5154
  1056
    return SDL_RenderDrawPoints(renderer, &point, 1);
slouken@5154
  1057
}
slouken@5154
  1058
slouken@6528
  1059
static int
slouken@6528
  1060
RenderDrawPointsWithRects(SDL_Renderer * renderer,
slouken@6528
  1061
                     const SDL_Point * points, int count)
slouken@6528
  1062
{
slouken@6528
  1063
    SDL_FRect *frects;
slouken@6528
  1064
    int i;
slouken@6528
  1065
    int status;
slouken@6528
  1066
slouken@6528
  1067
    frects = SDL_stack_alloc(SDL_FRect, count);
slouken@6528
  1068
    if (!frects) {
slouken@6528
  1069
        SDL_OutOfMemory();
slouken@6528
  1070
        return -1;
slouken@6528
  1071
    }
slouken@6528
  1072
    for (i = 0; i < count; ++i) {
slouken@6528
  1073
        frects[i].x = points[i].x * renderer->scale.x;
slouken@6528
  1074
        frects[i].y = points[i].y * renderer->scale.y;
slouken@6528
  1075
        frects[i].w = renderer->scale.x;
slouken@6528
  1076
        frects[i].h = renderer->scale.y;
slouken@6528
  1077
    }
slouken@6528
  1078
slouken@6528
  1079
    status = renderer->RenderFillRects(renderer, frects, count);
slouken@6528
  1080
slouken@6528
  1081
    SDL_stack_free(frects);
slouken@6528
  1082
slouken@6528
  1083
    return status;
slouken@6528
  1084
}
slouken@6528
  1085
slouken@5154
  1086
int
slouken@5154
  1087
SDL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@5154
  1088
                     const SDL_Point * points, int count)
slouken@5154
  1089
{
slouken@6528
  1090
    SDL_FPoint *fpoints;
slouken@6528
  1091
    int i;
slouken@6528
  1092
    int status;
slouken@6528
  1093
slouken@5154
  1094
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1095
slouken@5154
  1096
    if (!points) {
slouken@5154
  1097
        SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
slouken@5154
  1098
        return -1;
slouken@5154
  1099
    }
slouken@5154
  1100
    if (count < 1) {
slouken@5154
  1101
        return 0;
slouken@5154
  1102
    }
slouken@6260
  1103
    /* Don't draw while we're hidden */
slouken@6260
  1104
    if (renderer->hidden) {
slouken@6060
  1105
        return 0;
slouken@6060
  1106
    }
slouken@6528
  1107
slouken@6528
  1108
    if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
slouken@6528
  1109
        return RenderDrawPointsWithRects(renderer, points, count);
slouken@6528
  1110
    }
slouken@6528
  1111
slouken@6528
  1112
    fpoints = SDL_stack_alloc(SDL_FPoint, count);
slouken@6528
  1113
    if (!fpoints) {
slouken@6528
  1114
        SDL_OutOfMemory();
slouken@6528
  1115
        return -1;
slouken@6528
  1116
    }
slouken@6528
  1117
    for (i = 0; i < count; ++i) {
slouken@6528
  1118
        fpoints[i].x = points[i].x * renderer->scale.x;
slouken@6528
  1119
        fpoints[i].y = points[i].y * renderer->scale.y;
slouken@6528
  1120
    }
slouken@6528
  1121
slouken@6528
  1122
    status = renderer->RenderDrawPoints(renderer, fpoints, count);
slouken@6528
  1123
slouken@6528
  1124
    SDL_stack_free(fpoints);
slouken@6528
  1125
slouken@6528
  1126
    return status;
slouken@5154
  1127
}
slouken@5154
  1128
slouken@5154
  1129
int
slouken@5154
  1130
SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
slouken@5154
  1131
{
slouken@5154
  1132
    SDL_Point points[2];
slouken@5154
  1133
slouken@5154
  1134
    points[0].x = x1;
slouken@5154
  1135
    points[0].y = y1;
slouken@5154
  1136
    points[1].x = x2;
slouken@5154
  1137
    points[1].y = y2;
slouken@5154
  1138
    return SDL_RenderDrawLines(renderer, points, 2);
slouken@5154
  1139
}
slouken@5154
  1140
slouken@6528
  1141
static int
slouken@6528
  1142
RenderDrawLinesWithRects(SDL_Renderer * renderer,
slouken@6528
  1143
                     const SDL_Point * points, int count)
slouken@6528
  1144
{
slouken@6528
  1145
    SDL_FRect *frect;
slouken@6528
  1146
    SDL_FRect *frects;
slouken@6528
  1147
    SDL_FPoint fpoints[2];
slouken@6528
  1148
    int i, nrects;
slouken@6528
  1149
    int status;
slouken@6528
  1150
slouken@6528
  1151
    frects = SDL_stack_alloc(SDL_FRect, count-1);
slouken@6528
  1152
    if (!frects) {
slouken@6528
  1153
        SDL_OutOfMemory();
slouken@6528
  1154
        return -1;
slouken@6528
  1155
    }
slouken@6528
  1156
slouken@6528
  1157
    status = 0;
slouken@6528
  1158
    nrects = 0;
slouken@6528
  1159
    for (i = 0; i < count-1; ++i) {
slouken@6528
  1160
        if (points[i].x == points[i+1].x) {
slouken@6528
  1161
            int minY = SDL_min(points[i].y, points[i+1].y);
slouken@6528
  1162
            int maxY = SDL_max(points[i].y, points[i+1].y);
slouken@6528
  1163
slouken@6528
  1164
            frect = &frects[nrects++];
slouken@6528
  1165
            frect->x = points[i].x * renderer->scale.x;
slouken@6528
  1166
            frect->y = minY * renderer->scale.y;
slouken@6528
  1167
            frect->w = renderer->scale.x;
slouken@6528
  1168
            frect->h = (maxY - minY + 1) * renderer->scale.y;
slouken@6528
  1169
        } else if (points[i].y == points[i+1].y) {
slouken@6528
  1170
            int minX = SDL_min(points[i].x, points[i+1].x);
slouken@6528
  1171
            int maxX = SDL_max(points[i].x, points[i+1].x);
slouken@6528
  1172
slouken@6528
  1173
            frect = &frects[nrects++];
slouken@6528
  1174
            frect->x = minX * renderer->scale.x;
slouken@6528
  1175
            frect->y = points[i].y * renderer->scale.y;
slouken@6528
  1176
            frect->w = (maxX - minX + 1) * renderer->scale.x;
slouken@6528
  1177
            frect->h = renderer->scale.y;
slouken@6528
  1178
        } else {
slouken@6528
  1179
            /* FIXME: We can't use a rect for this line... */
slouken@6528
  1180
            frects[0].x = points[i].x * renderer->scale.x;
slouken@6528
  1181
            frects[0].y = points[i].y * renderer->scale.y;
slouken@6528
  1182
            frects[1].x = points[i+1].x * renderer->scale.x;
slouken@6528
  1183
            frects[1].y = points[i+1].y * renderer->scale.y;
slouken@6528
  1184
            status += renderer->RenderDrawLines(renderer, fpoints, 2);
slouken@6528
  1185
        }
slouken@6528
  1186
    }
slouken@6528
  1187
slouken@6528
  1188
    status += renderer->RenderFillRects(renderer, frects, nrects);
slouken@6528
  1189
slouken@6528
  1190
    SDL_stack_free(frects);
slouken@6528
  1191
slouken@6528
  1192
    if (status < 0) {
slouken@6528
  1193
        status = -1;
slouken@6528
  1194
    }
slouken@6528
  1195
    return status;
slouken@6528
  1196
}
slouken@6528
  1197
slouken@5154
  1198
int
slouken@5154
  1199
SDL_RenderDrawLines(SDL_Renderer * renderer,
slouken@5154
  1200
                    const SDL_Point * points, int count)
slouken@5154
  1201
{
slouken@6528
  1202
    SDL_FPoint *fpoints;
slouken@6528
  1203
    int i;
slouken@6528
  1204
    int status;
slouken@6528
  1205
slouken@5154
  1206
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1207
slouken@5154
  1208
    if (!points) {
slouken@5154
  1209
        SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
slouken@5154
  1210
        return -1;
slouken@5154
  1211
    }
slouken@5154
  1212
    if (count < 2) {
slouken@5154
  1213
        return 0;
slouken@5154
  1214
    }
slouken@6260
  1215
    /* Don't draw while we're hidden */
slouken@6260
  1216
    if (renderer->hidden) {
slouken@6060
  1217
        return 0;
slouken@6060
  1218
    }
slouken@6528
  1219
slouken@6528
  1220
    if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
slouken@6528
  1221
        return RenderDrawLinesWithRects(renderer, points, count);
slouken@6528
  1222
    }
slouken@6528
  1223
slouken@6528
  1224
    fpoints = SDL_stack_alloc(SDL_FPoint, count);
slouken@6528
  1225
    if (!fpoints) {
slouken@6528
  1226
        SDL_OutOfMemory();
slouken@6528
  1227
        return -1;
slouken@6528
  1228
    }
slouken@6528
  1229
    for (i = 0; i < count; ++i) {
slouken@6528
  1230
        fpoints[i].x = points[i].x * renderer->scale.x;
slouken@6528
  1231
        fpoints[i].y = points[i].y * renderer->scale.y;
slouken@6528
  1232
    }
slouken@6528
  1233
slouken@6528
  1234
    status = renderer->RenderDrawLines(renderer, fpoints, count);
slouken@6528
  1235
slouken@6528
  1236
    SDL_stack_free(fpoints);
slouken@6528
  1237
slouken@6528
  1238
    return status;
slouken@5154
  1239
}
slouken@5154
  1240
slouken@5154
  1241
int
slouken@5154
  1242
SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
  1243
{
slouken@5154
  1244
    SDL_Rect full_rect;
slouken@5154
  1245
    SDL_Point points[5];
slouken@5154
  1246
slouken@5154
  1247
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1248
slouken@5154
  1249
    /* If 'rect' == NULL, then outline the whole surface */
slouken@5154
  1250
    if (!rect) {
slouken@6528
  1251
        SDL_RenderGetViewport(renderer, &full_rect);
slouken@5154
  1252
        full_rect.x = 0;
slouken@5154
  1253
        full_rect.y = 0;
slouken@5154
  1254
        rect = &full_rect;
slouken@5154
  1255
    }
slouken@5154
  1256
slouken@5154
  1257
    points[0].x = rect->x;
slouken@5154
  1258
    points[0].y = rect->y;
slouken@5154
  1259
    points[1].x = rect->x+rect->w-1;
slouken@5154
  1260
    points[1].y = rect->y;
slouken@5154
  1261
    points[2].x = rect->x+rect->w-1;
slouken@5154
  1262
    points[2].y = rect->y+rect->h-1;
slouken@5154
  1263
    points[3].x = rect->x;
slouken@5154
  1264
    points[3].y = rect->y+rect->h-1;
slouken@5154
  1265
    points[4].x = rect->x;
slouken@5154
  1266
    points[4].y = rect->y;
slouken@5154
  1267
    return SDL_RenderDrawLines(renderer, points, 5);
slouken@5154
  1268
}
slouken@5154
  1269
slouken@5154
  1270
int
slouken@5154
  1271
SDL_RenderDrawRects(SDL_Renderer * renderer,
slouken@5297
  1272
                    const SDL_Rect * rects, int count)
slouken@5154
  1273
{
slouken@5154
  1274
    int i;
slouken@5154
  1275
slouken@5154
  1276
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1277
slouken@5154
  1278
    if (!rects) {
slouken@5154
  1279
        SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
slouken@5154
  1280
        return -1;
slouken@5154
  1281
    }
slouken@5154
  1282
    if (count < 1) {
slouken@5154
  1283
        return 0;
slouken@5154
  1284
    }
slouken@5154
  1285
slouken@6260
  1286
    /* Don't draw while we're hidden */
slouken@6260
  1287
    if (renderer->hidden) {
slouken@6060
  1288
        return 0;
slouken@6060
  1289
    }
slouken@5154
  1290
    for (i = 0; i < count; ++i) {
slouken@5297
  1291
        if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
slouken@5154
  1292
            return -1;
slouken@5154
  1293
        }
slouken@5154
  1294
    }
slouken@5154
  1295
    return 0;
slouken@5154
  1296
}
slouken@5154
  1297
slouken@5154
  1298
int
slouken@5154
  1299
SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5154
  1300
{
slouken@5331
  1301
    SDL_Rect full_rect;
slouken@6232
  1302
slouken@5331
  1303
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@6232
  1304
slouken@5331
  1305
    /* If 'rect' == NULL, then outline the whole surface */
slouken@5331
  1306
    if (!rect) {
slouken@6528
  1307
        SDL_RenderGetViewport(renderer, &full_rect);
slouken@5331
  1308
        full_rect.x = 0;
slouken@5331
  1309
        full_rect.y = 0;
slouken@5331
  1310
        rect = &full_rect;
slouken@5331
  1311
    }
slouken@5297
  1312
    return SDL_RenderFillRects(renderer, rect, 1);
slouken@5154
  1313
}
slouken@5154
  1314
slouken@5154
  1315
int
slouken@5154
  1316
SDL_RenderFillRects(SDL_Renderer * renderer,
slouken@5297
  1317
                    const SDL_Rect * rects, int count)
slouken@5154
  1318
{
slouken@6528
  1319
    SDL_FRect *frects;
slouken@6528
  1320
    int i;
slouken@6528
  1321
    int status;
slouken@6528
  1322
slouken@5154
  1323
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1324
slouken@5154
  1325
    if (!rects) {
slouken@5154
  1326
        SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
slouken@5154
  1327
        return -1;
slouken@5154
  1328
    }
slouken@5154
  1329
    if (count < 1) {
slouken@5154
  1330
        return 0;
slouken@5154
  1331
    }
slouken@6260
  1332
    /* Don't draw while we're hidden */
slouken@6260
  1333
    if (renderer->hidden) {
slouken@6060
  1334
        return 0;
slouken@6060
  1335
    }
slouken@6528
  1336
slouken@6528
  1337
    frects = SDL_stack_alloc(SDL_FRect, count);
slouken@6528
  1338
    if (!frects) {
slouken@6528
  1339
        SDL_OutOfMemory();
slouken@6528
  1340
        return -1;
slouken@6528
  1341
    }
slouken@6528
  1342
    for (i = 0; i < count; ++i) {
slouken@6528
  1343
        frects[i].x = rects[i].x * renderer->scale.x;
slouken@6528
  1344
        frects[i].y = rects[i].y * renderer->scale.y;
slouken@6528
  1345
        frects[i].w = rects[i].w * renderer->scale.x;
slouken@6528
  1346
        frects[i].h = rects[i].h * renderer->scale.y;
slouken@6528
  1347
    }
slouken@6528
  1348
slouken@6528
  1349
    status = renderer->RenderFillRects(renderer, frects, count);
slouken@6528
  1350
slouken@6528
  1351
    SDL_stack_free(frects);
slouken@6528
  1352
slouken@6528
  1353
    return status;
slouken@5154
  1354
}
slouken@5154
  1355
slouken@5154
  1356
int
slouken@5154
  1357
SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5154
  1358
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@5154
  1359
{
slouken@5154
  1360
    SDL_Rect real_srcrect;
slouken@5154
  1361
    SDL_Rect real_dstrect;
slouken@6528
  1362
    SDL_FRect frect;
slouken@5154
  1363
slouken@5154
  1364
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1365
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@5154
  1366
slouken@5154
  1367
    if (renderer != texture->renderer) {
slouken@5154
  1368
        SDL_SetError("Texture was not created with this renderer");
slouken@5154
  1369
        return -1;
slouken@5154
  1370
    }
slouken@5154
  1371
slouken@5154
  1372
    real_srcrect.x = 0;
slouken@5154
  1373
    real_srcrect.y = 0;
slouken@5154
  1374
    real_srcrect.w = texture->w;
slouken@5154
  1375
    real_srcrect.h = texture->h;
slouken@5154
  1376
    if (srcrect) {
slouken@5154
  1377
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
slouken@5154
  1378
            return 0;
slouken@5154
  1379
        }
slouken@5154
  1380
    }
slouken@5154
  1381
slouken@6528
  1382
    SDL_RenderGetViewport(renderer, &real_dstrect);
slouken@5154
  1383
    real_dstrect.x = 0;
slouken@5154
  1384
    real_dstrect.y = 0;
slouken@5369
  1385
    if (dstrect) {
slouken@5369
  1386
        if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
slouken@5369
  1387
            return 0;
slouken@5369
  1388
        }
slouken@5369
  1389
        /* Clip srcrect by the same amount as dstrect was clipped */
slouken@5369
  1390
        if (dstrect->w != real_dstrect.w) {
slouken@5369
  1391
            int deltax = (real_dstrect.x - dstrect->x);
slouken@5369
  1392
            int deltaw = (real_dstrect.w - dstrect->w);
slouken@5369
  1393
            real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
slouken@5369
  1394
            real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
slouken@5369
  1395
        }
slouken@5369
  1396
        if (dstrect->h != real_dstrect.h) {
slouken@5369
  1397
            int deltay = (real_dstrect.y - dstrect->y);
slouken@5369
  1398
            int deltah = (real_dstrect.h - dstrect->h);
slouken@5369
  1399
            real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
slouken@5369
  1400
            real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
slouken@5369
  1401
        }
slouken@5154
  1402
    }
slouken@5154
  1403
slouken@5156
  1404
    if (texture->native) {
slouken@5156
  1405
        texture = texture->native;
slouken@5156
  1406
    }
slouken@5156
  1407
slouken@6260
  1408
    /* Don't draw while we're hidden */
slouken@6260
  1409
    if (renderer->hidden) {
slouken@6060
  1410
        return 0;
slouken@6060
  1411
    }
slouken@6528
  1412
slouken@6528
  1413
    frect.x = real_dstrect.x * renderer->scale.x;
slouken@6528
  1414
    frect.y = real_dstrect.y * renderer->scale.y;
slouken@6528
  1415
    frect.w = real_dstrect.w * renderer->scale.x;
slouken@6528
  1416
    frect.h = real_dstrect.h * renderer->scale.y;
slouken@6528
  1417
slouken@6528
  1418
    return renderer->RenderCopy(renderer, texture, &real_srcrect, &frect);
slouken@5154
  1419
}
slouken@5154
  1420
gabomdq@6320
  1421
gabomdq@6320
  1422
int
gabomdq@6320
  1423
SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
gabomdq@6320
  1424
               const SDL_Rect * srcrect, const SDL_Rect * dstrect,
gabomdq@6320
  1425
               const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
gabomdq@6320
  1426
{
gabomdq@6320
  1427
    SDL_Rect real_srcrect, real_dstrect;
gabomdq@6320
  1428
    SDL_Point real_center;
slouken@6528
  1429
    SDL_FRect frect;
slouken@6528
  1430
    SDL_FPoint fcenter;
gabomdq@6320
  1431
gabomdq@6320
  1432
    CHECK_RENDERER_MAGIC(renderer, -1);
gabomdq@6320
  1433
    CHECK_TEXTURE_MAGIC(texture, -1);
gabomdq@6320
  1434
gabomdq@6320
  1435
    if (renderer != texture->renderer) {
gabomdq@6320
  1436
        SDL_SetError("Texture was not created with this renderer");
gabomdq@6320
  1437
        return -1;
gabomdq@6320
  1438
    }
gabomdq@6320
  1439
    if (!renderer->RenderCopyEx) {
gabomdq@6320
  1440
        SDL_SetError("Renderer does not support RenderCopyEx");
gabomdq@6320
  1441
        return -1;
gabomdq@6320
  1442
    }
gabomdq@6320
  1443
    
gabomdq@6320
  1444
    real_srcrect.x = 0;
gabomdq@6320
  1445
    real_srcrect.y = 0;
gabomdq@6320
  1446
    real_srcrect.w = texture->w;
gabomdq@6320
  1447
    real_srcrect.h = texture->h;
gabomdq@6320
  1448
    if (srcrect) {
gabomdq@6320
  1449
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
gabomdq@6320
  1450
            return 0;
gabomdq@6320
  1451
        }
gabomdq@6320
  1452
    }
gabomdq@6320
  1453
gabomdq@6320
  1454
    /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
slouken@6528
  1455
    if (dstrect) {
slouken@6528
  1456
        real_dstrect = *dstrect;
slouken@6528
  1457
    } else {
slouken@6528
  1458
        SDL_RenderGetViewport(renderer, &real_dstrect);
gabomdq@6399
  1459
        real_dstrect.x = 0;
gabomdq@6399
  1460
        real_dstrect.y = 0;
gabomdq@6320
  1461
    }
gabomdq@6320
  1462
gabomdq@6320
  1463
    if (texture->native) {
gabomdq@6320
  1464
        texture = texture->native;
gabomdq@6320
  1465
    }
gabomdq@6320
  1466
gabomdq@6320
  1467
    if(center) real_center = *center;
gabomdq@6320
  1468
    else {
gabomdq@6320
  1469
        real_center.x = real_dstrect.w/2;
gabomdq@6320
  1470
        real_center.y = real_dstrect.h/2;
gabomdq@6320
  1471
    }
gabomdq@6320
  1472
slouken@6528
  1473
    frect.x = real_dstrect.x * renderer->scale.x;
slouken@6528
  1474
    frect.y = real_dstrect.y * renderer->scale.y;
slouken@6528
  1475
    frect.w = real_dstrect.w * renderer->scale.x;
slouken@6528
  1476
    frect.h = real_dstrect.h * renderer->scale.y;
slouken@6528
  1477
slouken@6528
  1478
    fcenter.x = real_center.x * renderer->scale.x;
slouken@6528
  1479
    fcenter.y = real_center.y * renderer->scale.y;
slouken@6528
  1480
slouken@6528
  1481
    return renderer->RenderCopyEx(renderer, texture, &real_srcrect, &frect, angle, &fcenter, flip);
gabomdq@6320
  1482
}
gabomdq@6320
  1483
slouken@5154
  1484
int
slouken@5154
  1485
SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@5154
  1486
                     Uint32 format, void * pixels, int pitch)
slouken@5154
  1487
{
slouken@5154
  1488
    SDL_Rect real_rect;
slouken@5154
  1489
slouken@5154
  1490
    CHECK_RENDERER_MAGIC(renderer, -1);
slouken@5154
  1491
slouken@5154
  1492
    if (!renderer->RenderReadPixels) {
slouken@5154
  1493
        SDL_Unsupported();
slouken@5154
  1494
        return -1;
slouken@5154
  1495
    }
slouken@5154
  1496
slouken@5154
  1497
    if (!format) {
icculus@6389
  1498
        format = SDL_GetWindowPixelFormat(renderer->window);
slouken@5154
  1499
    }
slouken@5154
  1500
slouken@5464
  1501
    real_rect.x = renderer->viewport.x;
slouken@5464
  1502
    real_rect.y = renderer->viewport.y;
slouken@5297
  1503
    real_rect.w = renderer->viewport.w;
slouken@5297
  1504
    real_rect.h = renderer->viewport.h;
slouken@5154
  1505
    if (rect) {
slouken@5154
  1506
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@5154
  1507
            return 0;
slouken@5154
  1508
        }
slouken@5154
  1509
        if (real_rect.y > rect->y) {
slouken@5154
  1510
            pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@5154
  1511
        }
slouken@5154
  1512
        if (real_rect.x > rect->x) {
slouken@5464
  1513
            int bpp = SDL_BYTESPERPIXEL(format);
slouken@5154
  1514
            pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@5154
  1515
        }
slouken@5154
  1516
    }
slouken@5154
  1517
slouken@5154
  1518
    return renderer->RenderReadPixels(renderer, &real_rect,
slouken@5154
  1519
                                      format, pixels, pitch);
slouken@5154
  1520
}
slouken@5154
  1521
slouken@5154
  1522
void
slouken@5154
  1523
SDL_RenderPresent(SDL_Renderer * renderer)
slouken@5154
  1524
{
slouken@5154
  1525
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1526
slouken@6260
  1527
    /* Don't draw while we're hidden */
slouken@6260
  1528
    if (renderer->hidden) {
slouken@6060
  1529
        return;
slouken@6060
  1530
    }
slouken@5154
  1531
    renderer->RenderPresent(renderer);
slouken@5154
  1532
}
slouken@5154
  1533
slouken@5154
  1534
void
slouken@5154
  1535
SDL_DestroyTexture(SDL_Texture * texture)
slouken@5154
  1536
{
slouken@5154
  1537
    SDL_Renderer *renderer;
slouken@5154
  1538
slouken@5154
  1539
    CHECK_TEXTURE_MAGIC(texture, );
slouken@5154
  1540
    texture->magic = NULL;
slouken@5154
  1541
slouken@5154
  1542
    renderer = texture->renderer;
slouken@5154
  1543
    if (texture->next) {
slouken@5154
  1544
        texture->next->prev = texture->prev;
slouken@5154
  1545
    }
slouken@5154
  1546
    if (texture->prev) {
slouken@5154
  1547
        texture->prev->next = texture->next;
slouken@5154
  1548
    } else {
slouken@5154
  1549
        renderer->textures = texture->next;
slouken@5154
  1550
    }
slouken@5154
  1551
slouken@5156
  1552
    if (texture->native) {
slouken@5156
  1553
        SDL_DestroyTexture(texture->native);
slouken@5156
  1554
    }
slouken@5156
  1555
    if (texture->yuv) {
slouken@5156
  1556
        SDL_SW_DestroyYUVTexture(texture->yuv);
slouken@5156
  1557
    }
slouken@5156
  1558
    if (texture->pixels) {
slouken@5156
  1559
        SDL_free(texture->pixels);
slouken@5156
  1560
    }
slouken@5156
  1561
slouken@5154
  1562
    renderer->DestroyTexture(renderer, texture);
slouken@5154
  1563
    SDL_free(texture);
slouken@5154
  1564
}
slouken@5154
  1565
slouken@5154
  1566
void
slouken@5154
  1567
SDL_DestroyRenderer(SDL_Renderer * renderer)
slouken@5154
  1568
{
slouken@5154
  1569
    CHECK_RENDERER_MAGIC(renderer, );
slouken@5154
  1570
slouken@5154
  1571
    SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
slouken@5154
  1572
slouken@5154
  1573
    /* Free existing textures for this renderer */
slouken@5154
  1574
    while (renderer->textures) {
slouken@5154
  1575
        SDL_DestroyTexture(renderer->textures);
slouken@5154
  1576
    }
slouken@5154
  1577
slouken@6417
  1578
    if (renderer->window) {
slouken@6417
  1579
        SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
slouken@6417
  1580
    }
slouken@5528
  1581
slouken@5154
  1582
    /* It's no longer magical... */
slouken@5154
  1583
    renderer->magic = NULL;
slouken@5154
  1584
slouken@5154
  1585
    /* Free the renderer instance */
slouken@5154
  1586
    renderer->DestroyRenderer(renderer);
slouken@5154
  1587
}
slouken@5154
  1588
gabomdq@6414
  1589
int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
gabomdq@6414
  1590
{
gabomdq@6414
  1591
    SDL_Renderer *renderer;
gabomdq@6414
  1592
gabomdq@6415
  1593
    CHECK_TEXTURE_MAGIC(texture, -1);
gabomdq@6414
  1594
    renderer = texture->renderer;
gabomdq@6414
  1595
    if (renderer && renderer->GL_BindTexture) {
gabomdq@6414
  1596
        return renderer->GL_BindTexture(renderer, texture, texw, texh);
gabomdq@6414
  1597
    }
gabomdq@6414
  1598
gabomdq@6414
  1599
    SDL_Unsupported();
gabomdq@6414
  1600
    return -1;
gabomdq@6414
  1601
}
gabomdq@6414
  1602
gabomdq@6414
  1603
int SDL_GL_UnbindTexture(SDL_Texture *texture)
gabomdq@6414
  1604
{
gabomdq@6414
  1605
    SDL_Renderer *renderer;
gabomdq@6414
  1606
gabomdq@6415
  1607
    CHECK_TEXTURE_MAGIC(texture, -1);
gabomdq@6414
  1608
    renderer = texture->renderer;
gabomdq@6414
  1609
    if (renderer && renderer->GL_UnbindTexture) {
gabomdq@6414
  1610
        return renderer->GL_UnbindTexture(renderer, texture);
gabomdq@6414
  1611
    }
gabomdq@6414
  1612
gabomdq@6414
  1613
    SDL_Unsupported();
gabomdq@6414
  1614
    return -1;
gabomdq@6414
  1615
}
gabomdq@6414
  1616
slouken@5154
  1617
/* vi: set ts=4 sw=4 expandtab: */