src/render/software/SDL_render_sw.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 03 Oct 2018 23:37:29 -0400
branchSDL-ryan-batching-renderer
changeset 12288 005e1d1bcf05
parent 11958 d7582d7286aa
child 12381 dc9108cd4340
permissions -rw-r--r--
render: moved software renderer to new interface.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     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@1895
     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@1895
    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@1895
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1895
    22
slouken@5226
    23
#if !SDL_RENDER_DISABLED
slouken@5226
    24
slouken@5154
    25
#include "../SDL_sysrender.h"
slouken@6044
    26
#include "SDL_render_sw_c.h"
gabomdq@6320
    27
#include "SDL_hints.h"
icculus@12288
    28
#include "SDL_assert.h"
slouken@1895
    29
slouken@5163
    30
#include "SDL_draw.h"
slouken@5163
    31
#include "SDL_blendfillrect.h"
slouken@5163
    32
#include "SDL_blendline.h"
slouken@5163
    33
#include "SDL_blendpoint.h"
slouken@5163
    34
#include "SDL_drawline.h"
slouken@5163
    35
#include "SDL_drawpoint.h"
gabomdq@6320
    36
#include "SDL_rotate.h"
slouken@1895
    37
slouken@1895
    38
/* SDL surface based renderer implementation */
slouken@1895
    39
slouken@1895
    40
typedef struct
slouken@1895
    41
{
slouken@5166
    42
    SDL_Surface *surface;
slouken@6246
    43
    SDL_Surface *window;
slouken@1918
    44
} SW_RenderData;
slouken@1895
    45
slouken@1907
    46
slouken@5297
    47
static SDL_Surface *
slouken@5297
    48
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
    49
{
slouken@5297
    50
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5297
    51
slouken@5297
    52
    if (!data->surface) {
slouken@6246
    53
        data->surface = data->window;
slouken@6246
    54
    }
slouken@6246
    55
    if (!data->surface) {
slouken@7239
    56
        SDL_Surface *surface = SDL_GetWindowSurface(renderer->window);
slouken@7239
    57
        if (surface) {
slouken@7239
    58
            data->surface = data->window = surface;
slouken@7239
    59
        }
slouken@5297
    60
    }
slouken@5297
    61
    return data->surface;
slouken@5297
    62
}
slouken@5297
    63
slouken@5147
    64
static void
slouken@5147
    65
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
    66
{
slouken@1970
    67
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
    68
slouken@5276
    69
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
    70
        data->surface = NULL;
slouken@6283
    71
        data->window = NULL;
slouken@1970
    72
    }
slouken@1970
    73
}
slouken@1970
    74
slouken@1970
    75
static int
slouken@7239
    76
SW_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
slouken@7239
    77
{
slouken@7239
    78
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@7239
    79
slouken@7239
    80
    if (surface) {
slouken@7239
    81
        if (w) {
slouken@7239
    82
            *w = surface->w;
slouken@7239
    83
        }
slouken@7239
    84
        if (h) {
slouken@7239
    85
            *h = surface->h;
slouken@7239
    86
        }
slouken@7239
    87
        return 0;
slouken@7239
    88
    } else {
slouken@7239
    89
        SDL_SetError("Software renderer doesn't have an output surface");
slouken@7239
    90
        return -1;
slouken@7239
    91
    }
slouken@7239
    92
}
slouken@7239
    93
slouken@7239
    94
static int
slouken@1918
    95
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
    96
{
slouken@5156
    97
    int bpp;
slouken@5156
    98
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
    99
slouken@5156
   100
    if (!SDL_PixelFormatEnumToMasks
slouken@5156
   101
        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
icculus@7037
   102
        return SDL_SetError("Unknown texture format");
slouken@5156
   103
    }
slouken@1895
   104
slouken@5156
   105
    texture->driverdata =
slouken@5156
   106
        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@5156
   107
                             Bmask, Amask);
slouken@5156
   108
    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
slouken@5156
   109
                           texture->b);
slouken@5156
   110
    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
slouken@5156
   111
    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
slouken@3053
   112
slouken@10612
   113
    /* Only RLE encode textures without an alpha channel since the RLE coder
slouken@10612
   114
     * discards the color values of pixels with an alpha value of zero.
slouken@10612
   115
     */
slouken@10612
   116
    if (texture->access == SDL_TEXTUREACCESS_STATIC && !Amask) {
slouken@5156
   117
        SDL_SetSurfaceRLE(texture->driverdata, 1);
slouken@1895
   118
    }
slouken@1895
   119
slouken@1895
   120
    if (!texture->driverdata) {
slouken@1895
   121
        return -1;
slouken@1895
   122
    }
slouken@1895
   123
    return 0;
slouken@1895
   124
}
slouken@1895
   125
slouken@1895
   126
static int
slouken@1918
   127
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   128
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   129
{
slouken@5156
   130
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@5156
   131
    Uint8 *src, *dst;
slouken@5156
   132
    int row;
slouken@5156
   133
    size_t length;
slouken@1895
   134
dimitris@5567
   135
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   136
        SDL_LockSurface(surface);
slouken@5156
   137
    src = (Uint8 *) pixels;
slouken@5156
   138
    dst = (Uint8 *) surface->pixels +
slouken@5156
   139
                        rect->y * surface->pitch +
slouken@5156
   140
                        rect->x * surface->format->BytesPerPixel;
slouken@5156
   141
    length = rect->w * surface->format->BytesPerPixel;
slouken@5156
   142
    for (row = 0; row < rect->h; ++row) {
slouken@5156
   143
        SDL_memcpy(dst, src, length);
slouken@5156
   144
        src += pitch;
slouken@5156
   145
        dst += surface->pitch;
slouken@1895
   146
    }
dimitris@5567
   147
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   148
        SDL_UnlockSurface(surface);
slouken@5156
   149
    return 0;
slouken@1895
   150
}
slouken@1895
   151
slouken@1895
   152
static int
slouken@1918
   153
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   154
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   155
{
slouken@5156
   156
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   157
slouken@5156
   158
    *pixels =
slouken@5156
   159
        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@5156
   160
                  rect->x * surface->format->BytesPerPixel);
slouken@5156
   161
    *pitch = surface->pitch;
slouken@5156
   162
    return 0;
slouken@1895
   163
}
slouken@1895
   164
slouken@1895
   165
static void
slouken@1918
   166
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   167
{
slouken@1895
   168
}
slouken@1895
   169
slouken@5297
   170
static int
slouken@6247
   171
SW_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   172
{
slouken@6246
   173
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@6246
   174
slouken@6246
   175
    if (texture ) {
slouken@6246
   176
        data->surface = (SDL_Surface *) texture->driverdata;
slouken@6246
   177
    } else {
slouken@6246
   178
        data->surface = data->window;
slouken@6246
   179
    }
slouken@6246
   180
    return 0;
slouken@6246
   181
}
slouken@6246
   182
slouken@6246
   183
static int
icculus@12288
   184
SW_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
slouken@5297
   185
{
icculus@12288
   186
    return 0;  /* nothing to do in this backend. */
icculus@12288
   187
}
slouken@5297
   188
icculus@12288
   189
static int
icculus@12288
   190
SW_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
icculus@12288
   191
{
icculus@12288
   192
    SDL_Point *verts = (SDL_Point *) SDL_AllocateRenderVertices(renderer, count * sizeof (SDL_Point), 0, &cmd->data.draw.first);
icculus@12288
   193
    size_t i;
icculus@12288
   194
icculus@12288
   195
    if (!verts) {
icculus@12288
   196
        return -1;
slouken@5297
   197
    }
slouken@5297
   198
icculus@12288
   199
    cmd->data.draw.count = count;
icculus@12288
   200
icculus@12288
   201
    if (renderer->viewport.x || renderer->viewport.y) {
icculus@12288
   202
        const int x = renderer->viewport.x;
icculus@12288
   203
        const int y = renderer->viewport.y;
icculus@12288
   204
        for (i = 0; i < count; i++, verts++, points++) {
icculus@12288
   205
            verts->x = (int)(x + points->x);
icculus@12288
   206
            verts->y = (int)(y + points->y);
icculus@12288
   207
        }
icculus@12288
   208
    } else {
icculus@12288
   209
        for (i = 0; i < count; i++, verts++, points++) {
icculus@12288
   210
            verts->x = (int)points->x;
icculus@12288
   211
            verts->y = (int)points->y;
icculus@12288
   212
        }
icculus@12288
   213
    }
icculus@12288
   214
slouken@5297
   215
    return 0;
slouken@5297
   216
}
slouken@5297
   217
slouken@5297
   218
static int
icculus@12288
   219
SW_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
slouken@7141
   220
{
icculus@12288
   221
    SDL_Rect *verts = (SDL_Rect *) SDL_AllocateRenderVertices(renderer, count * sizeof (SDL_Rect), 0, &cmd->data.draw.first);
icculus@12288
   222
    size_t i;
icculus@12288
   223
icculus@12288
   224
    if (!verts) {
icculus@12288
   225
        return -1;
icculus@12288
   226
    }
icculus@12288
   227
icculus@12288
   228
    cmd->data.draw.count = count;
icculus@12288
   229
icculus@12288
   230
    if (renderer->viewport.x || renderer->viewport.y) {
icculus@12288
   231
        const int x = renderer->viewport.x;
icculus@12288
   232
        const int y = renderer->viewport.y;
icculus@12288
   233
icculus@12288
   234
        for (i = 0; i < count; i++, verts++, rects++) {
icculus@12288
   235
            verts->x = (int)(x + rects->x);
icculus@12288
   236
            verts->y = (int)(y + rects->y);
icculus@12288
   237
            verts->w = SDL_max((int)rects->w, 1);
icculus@12288
   238
            verts->h = SDL_max((int)rects->h, 1);
icculus@12288
   239
        }
icculus@12288
   240
    } else {
icculus@12288
   241
        for (i = 0; i < count; i++, verts++, rects++) {
icculus@12288
   242
            verts->x = (int)rects->x;
icculus@12288
   243
            verts->y = (int)rects->y;
icculus@12288
   244
            verts->w = SDL_max((int)rects->w, 1);
icculus@12288
   245
            verts->h = SDL_max((int)rects->h, 1);
slouken@7322
   246
        }
slouken@7141
   247
    }
icculus@12288
   248
slouken@7141
   249
    return 0;
slouken@7141
   250
}
slouken@7141
   251
slouken@7141
   252
static int
icculus@12288
   253
SW_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
icculus@12288
   254
             const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@5224
   255
{
icculus@12288
   256
    SDL_Rect *verts = (SDL_Rect *) SDL_AllocateRenderVertices(renderer, 2 * sizeof (SDL_Rect), 0, &cmd->data.draw.first);
slouken@5224
   257
icculus@12288
   258
    if (!verts) {
slouken@5297
   259
        return -1;
slouken@5224
   260
    }
slouken@5297
   261
icculus@12288
   262
    cmd->data.draw.count = 1;
slouken@5297
   263
icculus@12288
   264
    SDL_memcpy(verts, srcrect, sizeof (SDL_Rect));
icculus@12288
   265
    verts++;
icculus@12288
   266
icculus@12288
   267
    if (renderer->viewport.x || renderer->viewport.y) {
icculus@12288
   268
        verts->x = (int)(renderer->viewport.x + dstrect->x);
icculus@12288
   269
        verts->y = (int)(renderer->viewport.y + dstrect->y);
icculus@12288
   270
    } else {
icculus@12288
   271
        verts->x = (int)dstrect->x;
icculus@12288
   272
        verts->y = (int)dstrect->y;
icculus@12288
   273
    }
icculus@12288
   274
    verts->w = (int)dstrect->w;
icculus@12288
   275
    verts->h = (int)dstrect->h;
icculus@12288
   276
icculus@12288
   277
    return 0;
icculus@12288
   278
}
icculus@12288
   279
icculus@12288
   280
typedef struct CopyExData
icculus@12288
   281
{
icculus@12288
   282
    SDL_Rect srcrect;
icculus@12288
   283
    SDL_Rect dstrect;
icculus@12288
   284
    double angle;
icculus@12288
   285
    SDL_FPoint center;
icculus@12288
   286
    SDL_RendererFlip flip;
icculus@12288
   287
} CopyExData;
icculus@12288
   288
icculus@12288
   289
static int
icculus@12288
   290
SW_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
icculus@12288
   291
               const SDL_Rect * srcrect, const SDL_FRect * dstrect,
icculus@12288
   292
               const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
icculus@12288
   293
{
icculus@12288
   294
    CopyExData *verts = (CopyExData *) SDL_AllocateRenderVertices(renderer, sizeof (CopyExData), 0, &cmd->data.draw.first);
icculus@12288
   295
icculus@12288
   296
    if (!verts) {
icculus@12288
   297
        return -1;
icculus@12288
   298
    }
icculus@12288
   299
icculus@12288
   300
    cmd->data.draw.count = 1;
icculus@12288
   301
icculus@12288
   302
    SDL_memcpy(&verts->srcrect, srcrect, sizeof (SDL_Rect));
icculus@12288
   303
icculus@12288
   304
    if (renderer->viewport.x || renderer->viewport.y) {
icculus@12288
   305
        verts->dstrect.x = (int)(renderer->viewport.x + dstrect->x);
icculus@12288
   306
        verts->dstrect.y = (int)(renderer->viewport.y + dstrect->y);
icculus@12288
   307
    } else {
icculus@12288
   308
        verts->dstrect.x = (int)dstrect->x;
icculus@12288
   309
        verts->dstrect.y = (int)dstrect->y;
icculus@12288
   310
    }
icculus@12288
   311
    verts->dstrect.w = (int)dstrect->w;
icculus@12288
   312
    verts->dstrect.h = (int)dstrect->h;
icculus@12288
   313
    verts->angle = angle;
icculus@12288
   314
    SDL_memcpy(&verts->center, center, sizeof (SDL_FPoint));
icculus@12288
   315
    verts->flip = flip;
icculus@12288
   316
slouken@5297
   317
    return 0;
slouken@5224
   318
}
slouken@5224
   319
slouken@1895
   320
static int
icculus@12288
   321
SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Surface *surface, SDL_Texture * texture,
icculus@12288
   322
                const SDL_Rect * srcrect, const SDL_Rect * final_rect,
slouken@6528
   323
                const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
gabomdq@6320
   324
{
gabomdq@6320
   325
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
icculus@12288
   326
    SDL_Rect tmp_rect;
slouken@10612
   327
    SDL_Surface *src_clone, *src_rotated, *src_scaled;
slouken@10612
   328
    SDL_Surface *mask = NULL, *mask_rotated = NULL;
slouken@10612
   329
    int retval = 0, dstwidth, dstheight, abscenterx, abscentery;
gabomdq@6320
   330
    double cangle, sangle, px, py, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y;
slouken@10612
   331
    SDL_BlendMode blendmode;
slouken@10612
   332
    Uint8 alphaMod, rMod, gMod, bMod;
slouken@10612
   333
    int applyModulation = SDL_FALSE;
slouken@10612
   334
    int blitRequired = SDL_FALSE;
slouken@10612
   335
    int isOpaque = SDL_FALSE;
gabomdq@6320
   336
gabomdq@6320
   337
    if (!surface) {
gabomdq@6320
   338
        return -1;
gabomdq@6320
   339
    }
gabomdq@6320
   340
slouken@9762
   341
    tmp_rect.x = 0;
slouken@9762
   342
    tmp_rect.y = 0;
icculus@12288
   343
    tmp_rect.w = final_rect->w;
icculus@12288
   344
    tmp_rect.h = final_rect->h;
slouken@6528
   345
slouken@10612
   346
    /* It is possible to encounter an RLE encoded surface here and locking it is
slouken@10612
   347
     * necessary because this code is going to access the pixel buffer directly.
slouken@10612
   348
     */
slouken@10612
   349
    if (SDL_MUSTLOCK(src)) {
slouken@10612
   350
        SDL_LockSurface(src);
slouken@10612
   351
    }
slouken@10612
   352
slouken@10612
   353
    /* Clone the source surface but use its pixel buffer directly.
slouken@10612
   354
     * The original source surface must be treated as read-only.
slouken@10612
   355
     */
slouken@10612
   356
    src_clone = SDL_CreateRGBSurfaceFrom(src->pixels, src->w, src->h, src->format->BitsPerPixel, src->pitch,
slouken@10612
   357
                                         src->format->Rmask, src->format->Gmask,
slouken@10612
   358
                                         src->format->Bmask, src->format->Amask);
slouken@10612
   359
    if (src_clone == NULL) {
slouken@10612
   360
        if (SDL_MUSTLOCK(src)) {
slouken@10612
   361
            SDL_UnlockSurface(src);
slouken@9762
   362
        }
slouken@10612
   363
        return -1;
slouken@10612
   364
    }
gabomdq@6320
   365
slouken@10612
   366
    SDL_GetSurfaceBlendMode(src, &blendmode);
slouken@10612
   367
    SDL_GetSurfaceAlphaMod(src, &alphaMod);
slouken@10612
   368
    SDL_GetSurfaceColorMod(src, &rMod, &gMod, &bMod);
gabomdq@6320
   369
slouken@10612
   370
    /* SDLgfx_rotateSurface only accepts 32-bit surfaces with a 8888 layout. Everything else has to be converted. */
slouken@10612
   371
    if (src->format->BitsPerPixel != 32 || SDL_PIXELLAYOUT(src->format->format) != SDL_PACKEDLAYOUT_8888 || !src->format->Amask) {
slouken@10612
   372
        blitRequired = SDL_TRUE;
slouken@10612
   373
    }
gabomdq@6320
   374
slouken@10612
   375
    /* If scaling and cropping is necessary, it has to be taken care of before the rotation. */
icculus@12288
   376
    if (!(srcrect->w == final_rect->w && srcrect->h == final_rect->h && srcrect->x == 0 && srcrect->y == 0)) {
slouken@10612
   377
        blitRequired = SDL_TRUE;
slouken@10612
   378
    }
gabomdq@6320
   379
slouken@10612
   380
    /* The color and alpha modulation has to be applied before the rotation when using the NONE and MOD blend modes. */
slouken@10612
   381
    if ((blendmode == SDL_BLENDMODE_NONE || blendmode == SDL_BLENDMODE_MOD) && (alphaMod & rMod & gMod & bMod) != 255) {
slouken@10612
   382
        applyModulation = SDL_TRUE;
slouken@10612
   383
        SDL_SetSurfaceAlphaMod(src_clone, alphaMod);
slouken@10612
   384
        SDL_SetSurfaceColorMod(src_clone, rMod, gMod, bMod);
slouken@10612
   385
    }
slouken@10612
   386
slouken@10612
   387
    /* Opaque surfaces are much easier to handle with the NONE blend mode. */
slouken@10612
   388
    if (blendmode == SDL_BLENDMODE_NONE && !src->format->Amask && alphaMod == 255) {
slouken@10612
   389
        isOpaque = SDL_TRUE;
slouken@10612
   390
    }
slouken@10612
   391
slouken@10612
   392
    /* The NONE blend mode requires a mask for non-opaque surfaces. This mask will be used
slouken@10612
   393
     * to clear the pixels in the destination surface. The other steps are explained below.
slouken@10612
   394
     */
slouken@10612
   395
    if (blendmode == SDL_BLENDMODE_NONE && !isOpaque) {
icculus@12288
   396
        mask = SDL_CreateRGBSurface(0, final_rect->w, final_rect->h, 32,
slouken@10612
   397
                                    0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
slouken@10612
   398
        if (mask == NULL) {
slouken@10612
   399
            retval = -1;
slouken@10612
   400
        } else {
slouken@10612
   401
            SDL_SetSurfaceBlendMode(mask, SDL_BLENDMODE_MOD);
gabomdq@6320
   402
        }
gabomdq@6320
   403
    }
gabomdq@6320
   404
slouken@10612
   405
    /* Create a new surface should there be a format mismatch or if scaling, cropping,
slouken@10612
   406
     * or modulation is required. It's possible to use the source surface directly otherwise.
slouken@10612
   407
     */
slouken@10612
   408
    if (!retval && (blitRequired || applyModulation)) {
slouken@10612
   409
        SDL_Rect scale_rect = tmp_rect;
icculus@12288
   410
        src_scaled = SDL_CreateRGBSurface(0, final_rect->w, final_rect->h, 32,
slouken@10612
   411
                                          0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
slouken@10612
   412
        if (src_scaled == NULL) {
slouken@10612
   413
            retval = -1;
slouken@10612
   414
        } else {
slouken@10612
   415
            SDL_SetSurfaceBlendMode(src_clone, SDL_BLENDMODE_NONE);
slouken@10612
   416
            retval = SDL_BlitScaled(src_clone, srcrect, src_scaled, &scale_rect);
slouken@10612
   417
            SDL_FreeSurface(src_clone);
slouken@10612
   418
            src_clone = src_scaled;
slouken@10612
   419
            src_scaled = NULL;
slouken@10612
   420
        }
slouken@10612
   421
    }
slouken@10612
   422
slouken@10612
   423
    /* SDLgfx_rotateSurface is going to make decisions depending on the blend mode. */
slouken@10612
   424
    SDL_SetSurfaceBlendMode(src_clone, blendmode);
slouken@10612
   425
slouken@9762
   426
    if (!retval) {
slouken@10489
   427
        SDLgfx_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, angle, &dstwidth, &dstheight, &cangle, &sangle);
slouken@11958
   428
        src_rotated = SDLgfx_rotateSurface(src_clone, angle, dstwidth/2, dstheight/2, (texture->scaleMode == SDL_ScaleModeNearest) ? 0 : 1, flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
slouken@10612
   429
        if (src_rotated == NULL) {
slouken@10612
   430
            retval = -1;
slouken@10612
   431
        }
slouken@10612
   432
        if (!retval && mask != NULL) {
slouken@10612
   433
            /* The mask needed for the NONE blend mode gets rotated with the same parameters. */
slouken@10612
   434
            mask_rotated = SDLgfx_rotateSurface(mask, angle, dstwidth/2, dstheight/2, SDL_FALSE, 0, 0, dstwidth, dstheight, cangle, sangle);
slouken@10612
   435
            if (mask_rotated == NULL) {
slouken@10612
   436
                retval = -1;
slouken@10612
   437
            }
slouken@10612
   438
        }
slouken@10612
   439
        if (!retval) {
slouken@9762
   440
            /* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
icculus@12288
   441
            abscenterx = final_rect->x + (int)center->x;
icculus@12288
   442
            abscentery = final_rect->y + (int)center->y;
slouken@9762
   443
            /* Compensate the angle inversion to match the behaviour of the other backends */
slouken@9762
   444
            sangle = -sangle;
slouken@9762
   445
slouken@9762
   446
            /* Top Left */
icculus@12288
   447
            px = final_rect->x - abscenterx;
icculus@12288
   448
            py = final_rect->y - abscentery;
slouken@9762
   449
            p1x = px * cangle - py * sangle + abscenterx;
slouken@9762
   450
            p1y = px * sangle + py * cangle + abscentery;
slouken@9762
   451
slouken@9762
   452
            /* Top Right */
icculus@12288
   453
            px = final_rect->x + final_rect->w - abscenterx;
icculus@12288
   454
            py = final_rect->y - abscentery;
slouken@9762
   455
            p2x = px * cangle - py * sangle + abscenterx;
slouken@9762
   456
            p2y = px * sangle + py * cangle + abscentery;
slouken@9762
   457
slouken@9762
   458
            /* Bottom Left */
icculus@12288
   459
            px = final_rect->x - abscenterx;
icculus@12288
   460
            py = final_rect->y + final_rect->h - abscentery;
slouken@9762
   461
            p3x = px * cangle - py * sangle + abscenterx;
slouken@9762
   462
            p3y = px * sangle + py * cangle + abscentery;
slouken@9762
   463
slouken@9762
   464
            /* Bottom Right */
icculus@12288
   465
            px = final_rect->x + final_rect->w - abscenterx;
icculus@12288
   466
            py = final_rect->y + final_rect->h - abscentery;
slouken@9762
   467
            p4x = px * cangle - py * sangle + abscenterx;
slouken@9762
   468
            p4y = px * sangle + py * cangle + abscentery;
slouken@9762
   469
slouken@9762
   470
            tmp_rect.x = (int)MIN(MIN(p1x, p2x), MIN(p3x, p4x));
slouken@9762
   471
            tmp_rect.y = (int)MIN(MIN(p1y, p2y), MIN(p3y, p4y));
slouken@9762
   472
            tmp_rect.w = dstwidth;
slouken@9762
   473
            tmp_rect.h = dstheight;
slouken@9762
   474
slouken@10612
   475
            /* The NONE blend mode needs some special care with non-opaque surfaces.
slouken@10612
   476
             * Other blend modes or opaque surfaces can be blitted directly.
slouken@10612
   477
             */
slouken@10612
   478
            if (blendmode != SDL_BLENDMODE_NONE || isOpaque) {
slouken@10612
   479
                if (applyModulation == SDL_FALSE) {
slouken@10612
   480
                    /* If the modulation wasn't already applied, make it happen now. */
slouken@10612
   481
                    SDL_SetSurfaceAlphaMod(src_rotated, alphaMod);
slouken@10612
   482
                    SDL_SetSurfaceColorMod(src_rotated, rMod, gMod, bMod);
slouken@10612
   483
                }
slouken@10612
   484
                retval = SDL_BlitSurface(src_rotated, NULL, surface, &tmp_rect);
slouken@10612
   485
            } else {
slouken@10612
   486
                /* The NONE blend mode requires three steps to get the pixels onto the destination surface.
slouken@10612
   487
                 * First, the area where the rotated pixels will be blitted to get set to zero.
slouken@10612
   488
                 * This is accomplished by simply blitting a mask with the NONE blend mode.
slouken@10612
   489
                 * The colorkey set by the rotate function will discard the correct pixels.
slouken@10612
   490
                 */
slouken@10612
   491
                SDL_Rect mask_rect = tmp_rect;
slouken@10612
   492
                SDL_SetSurfaceBlendMode(mask_rotated, SDL_BLENDMODE_NONE);
slouken@10612
   493
                retval = SDL_BlitSurface(mask_rotated, NULL, surface, &mask_rect);
slouken@10612
   494
                if (!retval) {
slouken@10612
   495
                    /* The next step copies the alpha value. This is done with the BLEND blend mode and
slouken@10612
   496
                     * by modulating the source colors with 0. Since the destination is all zeros, this
slouken@10612
   497
                     * will effectively set the destination alpha to the source alpha.
slouken@10612
   498
                     */
slouken@10612
   499
                    SDL_SetSurfaceColorMod(src_rotated, 0, 0, 0);
slouken@10612
   500
                    mask_rect = tmp_rect;
slouken@10612
   501
                    retval = SDL_BlitSurface(src_rotated, NULL, surface, &mask_rect);
slouken@10612
   502
                    if (!retval) {
slouken@10612
   503
                        /* The last step gets the color values in place. The ADD blend mode simply adds them to
slouken@10612
   504
                         * the destination (where the color values are all zero). However, because the ADD blend
slouken@10612
   505
                         * mode modulates the colors with the alpha channel, a surface without an alpha mask needs
slouken@10612
   506
                         * to be created. This makes all source pixels opaque and the colors get copied correctly.
slouken@10612
   507
                         */
slouken@10612
   508
                        SDL_Surface *src_rotated_rgb;
slouken@10612
   509
                        src_rotated_rgb = SDL_CreateRGBSurfaceFrom(src_rotated->pixels, src_rotated->w, src_rotated->h,
slouken@10612
   510
                                                                   src_rotated->format->BitsPerPixel, src_rotated->pitch,
slouken@10612
   511
                                                                   src_rotated->format->Rmask, src_rotated->format->Gmask,
slouken@10612
   512
                                                                   src_rotated->format->Bmask, 0);
slouken@10612
   513
                        if (src_rotated_rgb == NULL) {
slouken@10612
   514
                            retval = -1;
slouken@10612
   515
                        } else {
slouken@10612
   516
                            SDL_SetSurfaceBlendMode(src_rotated_rgb, SDL_BLENDMODE_ADD);
slouken@10612
   517
                            retval = SDL_BlitSurface(src_rotated_rgb, NULL, surface, &tmp_rect);
slouken@10612
   518
                            SDL_FreeSurface(src_rotated_rgb);
slouken@10612
   519
                        }
slouken@10612
   520
                    }
slouken@10612
   521
                }
slouken@10612
   522
                SDL_FreeSurface(mask_rotated);
slouken@10612
   523
            }
slouken@10612
   524
            if (src_rotated != NULL) {
slouken@10612
   525
                SDL_FreeSurface(src_rotated);
slouken@10612
   526
            }
slouken@9762
   527
        }
slouken@9762
   528
    }
slouken@9762
   529
slouken@10612
   530
    if (SDL_MUSTLOCK(src)) {
slouken@10612
   531
        SDL_UnlockSurface(src);
slouken@10612
   532
    }
slouken@10612
   533
    if (mask != NULL) {
slouken@10612
   534
        SDL_FreeSurface(mask);
slouken@10612
   535
    }
slouken@10612
   536
    if (src_clone != NULL) {
slouken@10612
   537
        SDL_FreeSurface(src_clone);
slouken@9762
   538
    }
slouken@9762
   539
    return retval;
gabomdq@6320
   540
}
gabomdq@6320
   541
icculus@12288
   542
static void
icculus@12288
   543
PrepTextureForCopy(const SDL_RenderCommand *cmd)
icculus@12288
   544
{
icculus@12288
   545
    const Uint8 r = cmd->data.draw.r;
icculus@12288
   546
    const Uint8 g = cmd->data.draw.g;
icculus@12288
   547
    const Uint8 b = cmd->data.draw.b;
icculus@12288
   548
    const Uint8 a = cmd->data.draw.a;
icculus@12288
   549
    const SDL_BlendMode blend = cmd->data.draw.blend;
icculus@12288
   550
    SDL_Texture *texture = cmd->data.draw.texture;
icculus@12288
   551
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
icculus@12288
   552
icculus@12288
   553
    if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
icculus@12288
   554
        SDL_SetSurfaceRLE(surface, 0);
icculus@12288
   555
        SDL_SetSurfaceColorMod(surface, r, g, b);
icculus@12288
   556
    }
icculus@12288
   557
icculus@12288
   558
    if ((texture->modMode & SDL_TEXTUREMODULATE_ALPHA) && surface->format->Amask) {
icculus@12288
   559
        SDL_SetSurfaceRLE(surface, 0);
icculus@12288
   560
        SDL_SetSurfaceAlphaMod(surface, a);
icculus@12288
   561
    }
icculus@12288
   562
icculus@12288
   563
    if ((blend == SDL_BLENDMODE_ADD) || (blend == SDL_BLENDMODE_MOD)) {
icculus@12288
   564
        SDL_SetSurfaceRLE(surface, 0);
icculus@12288
   565
    }
icculus@12288
   566
    SDL_SetSurfaceBlendMode(surface, blend);
icculus@12288
   567
}
icculus@12288
   568
icculus@12288
   569
static int
icculus@12288
   570
SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
icculus@12288
   571
{
icculus@12288
   572
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
icculus@12288
   573
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
icculus@12288
   574
    const SDL_Rect *viewport = NULL;
icculus@12288
   575
    const SDL_Rect *cliprect = NULL;
icculus@12288
   576
icculus@12288
   577
    if (!surface) {
icculus@12288
   578
        return -1;
icculus@12288
   579
    }
icculus@12288
   580
icculus@12288
   581
    while (cmd) {
icculus@12288
   582
        switch (cmd->command) {
icculus@12288
   583
            case SDL_RENDERCMD_SETDRAWCOLOR: {
icculus@12288
   584
                break;  /* Not used in this backend. */
icculus@12288
   585
            }
icculus@12288
   586
icculus@12288
   587
            case SDL_RENDERCMD_SETVIEWPORT: {
icculus@12288
   588
                viewport = &cmd->data.viewport.rect;
icculus@12288
   589
                SDL_SetClipRect(data->surface, viewport);
icculus@12288
   590
                break;
icculus@12288
   591
            }
icculus@12288
   592
icculus@12288
   593
            case SDL_RENDERCMD_SETCLIPRECT: {
icculus@12288
   594
                SDL_assert(viewport != NULL);
icculus@12288
   595
                cliprect = cmd->data.cliprect.enabled ? &cmd->data.cliprect.rect : NULL;
icculus@12288
   596
                if (cliprect) {
icculus@12288
   597
                    SDL_Rect clip_rect = { cliprect->x + viewport->x, cliprect->y + viewport->y, cliprect->w, cliprect->h };
icculus@12288
   598
                    SDL_IntersectRect(viewport, &clip_rect, &clip_rect);
icculus@12288
   599
                    SDL_SetClipRect(surface, &clip_rect);
icculus@12288
   600
                } else {
icculus@12288
   601
                    SDL_SetClipRect(surface, viewport);
icculus@12288
   602
                }
icculus@12288
   603
                break;
icculus@12288
   604
            }
icculus@12288
   605
icculus@12288
   606
            case SDL_RENDERCMD_CLEAR: {
icculus@12288
   607
                const Uint8 r = cmd->data.color.r;
icculus@12288
   608
                const Uint8 g = cmd->data.color.g;
icculus@12288
   609
                const Uint8 b = cmd->data.color.b;
icculus@12288
   610
                const Uint8 a = cmd->data.color.a;
icculus@12288
   611
                const SDL_Rect clip_rect = surface->clip_rect;
icculus@12288
   612
                /* By definition the clear ignores the clip rect */
icculus@12288
   613
                SDL_SetClipRect(surface, NULL);
icculus@12288
   614
                SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, r, g, b, a));
icculus@12288
   615
                SDL_SetClipRect(surface, &clip_rect);
icculus@12288
   616
                break;
icculus@12288
   617
            }
icculus@12288
   618
icculus@12288
   619
            case SDL_RENDERCMD_DRAW_POINTS: {
icculus@12288
   620
                const Uint8 r = cmd->data.draw.r;
icculus@12288
   621
                const Uint8 g = cmd->data.draw.g;
icculus@12288
   622
                const Uint8 b = cmd->data.draw.b;
icculus@12288
   623
                const Uint8 a = cmd->data.draw.a;
icculus@12288
   624
                const size_t count = cmd->data.draw.count;
icculus@12288
   625
                const SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   626
                const SDL_BlendMode blend = cmd->data.draw.blend;
icculus@12288
   627
                if (blend == SDL_BLENDMODE_NONE) {
icculus@12288
   628
                    SDL_DrawPoints(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
icculus@12288
   629
                } else {
icculus@12288
   630
                    SDL_BlendPoints(surface, verts, count, blend, r, g, b, a);
icculus@12288
   631
                }
icculus@12288
   632
                break;
icculus@12288
   633
            }
icculus@12288
   634
icculus@12288
   635
            case SDL_RENDERCMD_DRAW_LINES: {
icculus@12288
   636
                const Uint8 r = cmd->data.draw.r;
icculus@12288
   637
                const Uint8 g = cmd->data.draw.g;
icculus@12288
   638
                const Uint8 b = cmd->data.draw.b;
icculus@12288
   639
                const Uint8 a = cmd->data.draw.a;
icculus@12288
   640
                const size_t count = cmd->data.draw.count;
icculus@12288
   641
                const SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   642
                const SDL_BlendMode blend = cmd->data.draw.blend;
icculus@12288
   643
                if (blend == SDL_BLENDMODE_NONE) {
icculus@12288
   644
                    SDL_DrawLines(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
icculus@12288
   645
                } else {
icculus@12288
   646
                    SDL_BlendLines(surface, verts, count, blend, r, g, b, a);
icculus@12288
   647
                }
icculus@12288
   648
                break;
icculus@12288
   649
            }
icculus@12288
   650
icculus@12288
   651
            case SDL_RENDERCMD_FILL_RECTS: {
icculus@12288
   652
                const Uint8 r = cmd->data.draw.r;
icculus@12288
   653
                const Uint8 g = cmd->data.draw.g;
icculus@12288
   654
                const Uint8 b = cmd->data.draw.b;
icculus@12288
   655
                const Uint8 a = cmd->data.draw.a;
icculus@12288
   656
                const size_t count = cmd->data.draw.count;
icculus@12288
   657
                const SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   658
                const SDL_BlendMode blend = cmd->data.draw.blend;
icculus@12288
   659
                if (blend == SDL_BLENDMODE_NONE) {
icculus@12288
   660
                    SDL_FillRects(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
icculus@12288
   661
                } else {
icculus@12288
   662
                    SDL_BlendFillRects(surface, verts, count, blend, r, g, b, a);
icculus@12288
   663
                }
icculus@12288
   664
                break;
icculus@12288
   665
            }
icculus@12288
   666
icculus@12288
   667
            case SDL_RENDERCMD_COPY: {
icculus@12288
   668
                SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   669
                const SDL_Rect *srcrect = verts;
icculus@12288
   670
                SDL_Rect *dstrect = verts + 1;
icculus@12288
   671
                SDL_Texture *texture = cmd->data.draw.texture;
icculus@12288
   672
                SDL_Surface *src = (SDL_Surface *) texture->driverdata;
icculus@12288
   673
icculus@12288
   674
                PrepTextureForCopy(cmd);
icculus@12288
   675
icculus@12288
   676
                if ( srcrect->w == dstrect->w && srcrect->h == dstrect->h ) {
icculus@12288
   677
                    SDL_BlitSurface(src, srcrect, surface, dstrect);
icculus@12288
   678
                } else {
icculus@12288
   679
                    /* If scaling is ever done, permanently disable RLE (which doesn't support scaling)
icculus@12288
   680
                     * to avoid potentially frequent RLE encoding/decoding.
icculus@12288
   681
                     */
icculus@12288
   682
                    SDL_SetSurfaceRLE(surface, 0);
icculus@12288
   683
                    SDL_BlitScaled(src, srcrect, surface, dstrect);
icculus@12288
   684
                }
icculus@12288
   685
                break;
icculus@12288
   686
            }
icculus@12288
   687
icculus@12288
   688
            case SDL_RENDERCMD_COPY_EX: {
icculus@12288
   689
                const CopyExData *copydata = (CopyExData *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   690
                PrepTextureForCopy(cmd);
icculus@12288
   691
                SW_RenderCopyEx(renderer, surface, cmd->data.draw.texture, &copydata->srcrect,
icculus@12288
   692
                                &copydata->dstrect, copydata->angle, &copydata->center, copydata->flip);
icculus@12288
   693
                break;
icculus@12288
   694
            }
icculus@12288
   695
icculus@12288
   696
            case SDL_RENDERCMD_NO_OP:
icculus@12288
   697
                break;
icculus@12288
   698
        }
icculus@12288
   699
icculus@12288
   700
        cmd = cmd->next;
icculus@12288
   701
    }
icculus@12288
   702
icculus@12288
   703
    return 0;
icculus@12288
   704
}
icculus@12288
   705
gabomdq@6320
   706
static int
slouken@3427
   707
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   708
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   709
{
slouken@5166
   710
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   711
    Uint32 src_format;
slouken@5166
   712
    void *src_pixels;
slouken@3427
   713
slouken@5166
   714
    if (!surface) {
slouken@5147
   715
        return -1;
slouken@5147
   716
    }
slouken@5147
   717
slouken@11223
   718
    /* NOTE: The rect is already adjusted according to the viewport by
slouken@11223
   719
     * SDL_RenderReadPixels.
slouken@11223
   720
     */
slouken@5297
   721
slouken@5166
   722
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
slouken@5166
   723
        rect->y < 0 || rect->y+rect->h > surface->h) {
icculus@7037
   724
        return SDL_SetError("Tried to read outside of surface bounds");
slouken@3427
   725
    }
slouken@3427
   726
slouken@5297
   727
    src_format = surface->format->format;
slouken@5166
   728
    src_pixels = (void*)((Uint8 *) surface->pixels +
slouken@5166
   729
                    rect->y * surface->pitch +
slouken@5166
   730
                    rect->x * surface->format->BytesPerPixel);
slouken@5166
   731
slouken@5166
   732
    return SDL_ConvertPixels(rect->w, rect->h,
slouken@5166
   733
                             src_format, src_pixels, surface->pitch,
slouken@5166
   734
                             format, pixels, pitch);
slouken@3427
   735
}
slouken@3427
   736
slouken@1895
   737
static void
slouken@1918
   738
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   739
{
slouken@5166
   740
    SDL_Window *window = renderer->window;
slouken@1895
   741
slouken@5166
   742
    if (window) {
slouken@5166
   743
        SDL_UpdateWindowSurface(window);
slouken@5147
   744
    }
slouken@1895
   745
}
slouken@1895
   746
slouken@1895
   747
static void
slouken@1918
   748
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   749
{
slouken@5156
   750
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   751
slouken@5156
   752
    SDL_FreeSurface(surface);
slouken@1895
   753
}
slouken@1895
   754
slouken@1895
   755
static void
slouken@1918
   756
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   757
{
slouken@1918
   758
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   759
slouken@7719
   760
    SDL_free(data);
slouken@1895
   761
    SDL_free(renderer);
slouken@1895
   762
}
slouken@1895
   763
icculus@12288
   764
SDL_Renderer *
icculus@12288
   765
SW_CreateRendererForSurface(SDL_Surface * surface)
icculus@12288
   766
{
icculus@12288
   767
    SDL_Renderer *renderer;
icculus@12288
   768
    SW_RenderData *data;
icculus@12288
   769
icculus@12288
   770
    if (!surface) {
icculus@12288
   771
        SDL_SetError("Can't create renderer for NULL surface");
icculus@12288
   772
        return NULL;
icculus@12288
   773
    }
icculus@12288
   774
icculus@12288
   775
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
icculus@12288
   776
    if (!renderer) {
icculus@12288
   777
        SDL_OutOfMemory();
icculus@12288
   778
        return NULL;
icculus@12288
   779
    }
icculus@12288
   780
icculus@12288
   781
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
icculus@12288
   782
    if (!data) {
icculus@12288
   783
        SW_DestroyRenderer(renderer);
icculus@12288
   784
        SDL_OutOfMemory();
icculus@12288
   785
        return NULL;
icculus@12288
   786
    }
icculus@12288
   787
    data->surface = surface;
icculus@12288
   788
    data->window = surface;
icculus@12288
   789
icculus@12288
   790
    renderer->WindowEvent = SW_WindowEvent;
icculus@12288
   791
    renderer->GetOutputSize = SW_GetOutputSize;
icculus@12288
   792
    renderer->CreateTexture = SW_CreateTexture;
icculus@12288
   793
    renderer->UpdateTexture = SW_UpdateTexture;
icculus@12288
   794
    renderer->LockTexture = SW_LockTexture;
icculus@12288
   795
    renderer->UnlockTexture = SW_UnlockTexture;
icculus@12288
   796
    renderer->SetRenderTarget = SW_SetRenderTarget;
icculus@12288
   797
    renderer->QueueSetViewport = SW_QueueSetViewport;
icculus@12288
   798
    renderer->QueueSetDrawColor = SW_QueueSetViewport;  /* SetViewport and SetDrawColor are (currently) no-ops. */
icculus@12288
   799
    renderer->QueueDrawPoints = SW_QueueDrawPoints;
icculus@12288
   800
    renderer->QueueDrawLines = SW_QueueDrawPoints;  /* lines and points queue vertices the same way. */
icculus@12288
   801
    renderer->QueueFillRects = SW_QueueFillRects;
icculus@12288
   802
    renderer->QueueCopy = SW_QueueCopy;
icculus@12288
   803
    renderer->QueueCopyEx = SW_QueueCopyEx;
icculus@12288
   804
    renderer->RunCommandQueue = SW_RunCommandQueue;
icculus@12288
   805
    renderer->RenderReadPixels = SW_RenderReadPixels;
icculus@12288
   806
    renderer->RenderPresent = SW_RenderPresent;
icculus@12288
   807
    renderer->DestroyTexture = SW_DestroyTexture;
icculus@12288
   808
    renderer->DestroyRenderer = SW_DestroyRenderer;
icculus@12288
   809
    renderer->info = SW_RenderDriver.info;
icculus@12288
   810
    renderer->driverdata = data;
icculus@12288
   811
icculus@12288
   812
    SW_ActivateRenderer(renderer);
icculus@12288
   813
icculus@12288
   814
    return renderer;
icculus@12288
   815
}
icculus@12288
   816
icculus@12288
   817
SDL_Renderer *
icculus@12288
   818
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
icculus@12288
   819
{
icculus@12288
   820
    SDL_Surface *surface;
icculus@12288
   821
icculus@12288
   822
    surface = SDL_GetWindowSurface(window);
icculus@12288
   823
    if (!surface) {
icculus@12288
   824
        return NULL;
icculus@12288
   825
    }
icculus@12288
   826
    return SW_CreateRendererForSurface(surface);
icculus@12288
   827
}
icculus@12288
   828
icculus@12288
   829
SDL_RenderDriver SW_RenderDriver = {
icculus@12288
   830
    SW_CreateRenderer,
icculus@12288
   831
    {
icculus@12288
   832
     "software",
icculus@12288
   833
     SDL_RENDERER_SOFTWARE | SDL_RENDERER_TARGETTEXTURE,
icculus@12288
   834
     8,
icculus@12288
   835
     {
icculus@12288
   836
      SDL_PIXELFORMAT_ARGB8888,
icculus@12288
   837
      SDL_PIXELFORMAT_ABGR8888,
icculus@12288
   838
      SDL_PIXELFORMAT_RGBA8888,
icculus@12288
   839
      SDL_PIXELFORMAT_BGRA8888,
icculus@12288
   840
      SDL_PIXELFORMAT_RGB888,
icculus@12288
   841
      SDL_PIXELFORMAT_BGR888,
icculus@12288
   842
      SDL_PIXELFORMAT_RGB565,
icculus@12288
   843
      SDL_PIXELFORMAT_RGB555
icculus@12288
   844
     },
icculus@12288
   845
     0,
icculus@12288
   846
     0}
icculus@12288
   847
};
icculus@12288
   848
slouken@5226
   849
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   850
slouken@1895
   851
/* vi: set ts=4 sw=4 expandtab: */