src/render/software/SDL_render_sw.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 20 May 2019 00:41:18 -0400
changeset 12743 c653e9fa9ca9
parent 12665 4743da9c3eea
child 12836 32839175c0e4
permissions -rw-r--r--
vulkan: Swapped out a free() that should have been an SDL_free().

Fixes (for real this time!) the Visual Studio builds.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 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);
slouken@12665
   193
    int 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);
slouken@12665
   222
    int 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@12372
   380
    /* srcrect is not selecting the whole src surface, so cropping is needed */
slouken@12372
   381
    if (!(srcrect->w == src->w && srcrect->h == src->h && srcrect->x == 0 && srcrect->y == 0)) {
slouken@12372
   382
        blitRequired = SDL_TRUE;
slouken@12372
   383
    }
slouken@12372
   384
slouken@10612
   385
    /* The color and alpha modulation has to be applied before the rotation when using the NONE and MOD blend modes. */
slouken@10612
   386
    if ((blendmode == SDL_BLENDMODE_NONE || blendmode == SDL_BLENDMODE_MOD) && (alphaMod & rMod & gMod & bMod) != 255) {
slouken@10612
   387
        applyModulation = SDL_TRUE;
slouken@10612
   388
        SDL_SetSurfaceAlphaMod(src_clone, alphaMod);
slouken@10612
   389
        SDL_SetSurfaceColorMod(src_clone, rMod, gMod, bMod);
slouken@10612
   390
    }
slouken@10612
   391
slouken@10612
   392
    /* Opaque surfaces are much easier to handle with the NONE blend mode. */
slouken@10612
   393
    if (blendmode == SDL_BLENDMODE_NONE && !src->format->Amask && alphaMod == 255) {
slouken@10612
   394
        isOpaque = SDL_TRUE;
slouken@10612
   395
    }
slouken@10612
   396
slouken@10612
   397
    /* The NONE blend mode requires a mask for non-opaque surfaces. This mask will be used
slouken@10612
   398
     * to clear the pixels in the destination surface. The other steps are explained below.
slouken@10612
   399
     */
slouken@10612
   400
    if (blendmode == SDL_BLENDMODE_NONE && !isOpaque) {
icculus@12288
   401
        mask = SDL_CreateRGBSurface(0, final_rect->w, final_rect->h, 32,
slouken@10612
   402
                                    0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
slouken@10612
   403
        if (mask == NULL) {
slouken@10612
   404
            retval = -1;
slouken@10612
   405
        } else {
slouken@10612
   406
            SDL_SetSurfaceBlendMode(mask, SDL_BLENDMODE_MOD);
gabomdq@6320
   407
        }
gabomdq@6320
   408
    }
gabomdq@6320
   409
slouken@10612
   410
    /* Create a new surface should there be a format mismatch or if scaling, cropping,
slouken@10612
   411
     * or modulation is required. It's possible to use the source surface directly otherwise.
slouken@10612
   412
     */
slouken@10612
   413
    if (!retval && (blitRequired || applyModulation)) {
slouken@10612
   414
        SDL_Rect scale_rect = tmp_rect;
icculus@12288
   415
        src_scaled = SDL_CreateRGBSurface(0, final_rect->w, final_rect->h, 32,
slouken@10612
   416
                                          0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
slouken@10612
   417
        if (src_scaled == NULL) {
slouken@10612
   418
            retval = -1;
slouken@10612
   419
        } else {
slouken@10612
   420
            SDL_SetSurfaceBlendMode(src_clone, SDL_BLENDMODE_NONE);
slouken@10612
   421
            retval = SDL_BlitScaled(src_clone, srcrect, src_scaled, &scale_rect);
slouken@10612
   422
            SDL_FreeSurface(src_clone);
slouken@10612
   423
            src_clone = src_scaled;
slouken@10612
   424
            src_scaled = NULL;
slouken@10612
   425
        }
slouken@10612
   426
    }
slouken@10612
   427
slouken@10612
   428
    /* SDLgfx_rotateSurface is going to make decisions depending on the blend mode. */
slouken@10612
   429
    SDL_SetSurfaceBlendMode(src_clone, blendmode);
slouken@10612
   430
slouken@9762
   431
    if (!retval) {
slouken@10489
   432
        SDLgfx_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, angle, &dstwidth, &dstheight, &cangle, &sangle);
slouken@11958
   433
        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
   434
        if (src_rotated == NULL) {
slouken@10612
   435
            retval = -1;
slouken@10612
   436
        }
slouken@10612
   437
        if (!retval && mask != NULL) {
slouken@10612
   438
            /* The mask needed for the NONE blend mode gets rotated with the same parameters. */
slouken@10612
   439
            mask_rotated = SDLgfx_rotateSurface(mask, angle, dstwidth/2, dstheight/2, SDL_FALSE, 0, 0, dstwidth, dstheight, cangle, sangle);
slouken@10612
   440
            if (mask_rotated == NULL) {
slouken@10612
   441
                retval = -1;
slouken@10612
   442
            }
slouken@10612
   443
        }
slouken@10612
   444
        if (!retval) {
slouken@9762
   445
            /* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
icculus@12288
   446
            abscenterx = final_rect->x + (int)center->x;
icculus@12288
   447
            abscentery = final_rect->y + (int)center->y;
slouken@9762
   448
            /* Compensate the angle inversion to match the behaviour of the other backends */
slouken@9762
   449
            sangle = -sangle;
slouken@9762
   450
slouken@9762
   451
            /* Top Left */
icculus@12288
   452
            px = final_rect->x - abscenterx;
icculus@12288
   453
            py = final_rect->y - abscentery;
slouken@9762
   454
            p1x = px * cangle - py * sangle + abscenterx;
slouken@9762
   455
            p1y = px * sangle + py * cangle + abscentery;
slouken@9762
   456
slouken@9762
   457
            /* Top Right */
icculus@12288
   458
            px = final_rect->x + final_rect->w - abscenterx;
icculus@12288
   459
            py = final_rect->y - abscentery;
slouken@9762
   460
            p2x = px * cangle - py * sangle + abscenterx;
slouken@9762
   461
            p2y = px * sangle + py * cangle + abscentery;
slouken@9762
   462
slouken@9762
   463
            /* Bottom Left */
icculus@12288
   464
            px = final_rect->x - abscenterx;
icculus@12288
   465
            py = final_rect->y + final_rect->h - abscentery;
slouken@9762
   466
            p3x = px * cangle - py * sangle + abscenterx;
slouken@9762
   467
            p3y = px * sangle + py * cangle + abscentery;
slouken@9762
   468
slouken@9762
   469
            /* Bottom Right */
icculus@12288
   470
            px = final_rect->x + final_rect->w - abscenterx;
icculus@12288
   471
            py = final_rect->y + final_rect->h - abscentery;
slouken@9762
   472
            p4x = px * cangle - py * sangle + abscenterx;
slouken@9762
   473
            p4y = px * sangle + py * cangle + abscentery;
slouken@9762
   474
slouken@9762
   475
            tmp_rect.x = (int)MIN(MIN(p1x, p2x), MIN(p3x, p4x));
slouken@9762
   476
            tmp_rect.y = (int)MIN(MIN(p1y, p2y), MIN(p3y, p4y));
slouken@9762
   477
            tmp_rect.w = dstwidth;
slouken@9762
   478
            tmp_rect.h = dstheight;
slouken@9762
   479
slouken@10612
   480
            /* The NONE blend mode needs some special care with non-opaque surfaces.
slouken@10612
   481
             * Other blend modes or opaque surfaces can be blitted directly.
slouken@10612
   482
             */
slouken@10612
   483
            if (blendmode != SDL_BLENDMODE_NONE || isOpaque) {
slouken@10612
   484
                if (applyModulation == SDL_FALSE) {
slouken@10612
   485
                    /* If the modulation wasn't already applied, make it happen now. */
slouken@10612
   486
                    SDL_SetSurfaceAlphaMod(src_rotated, alphaMod);
slouken@10612
   487
                    SDL_SetSurfaceColorMod(src_rotated, rMod, gMod, bMod);
slouken@10612
   488
                }
slouken@10612
   489
                retval = SDL_BlitSurface(src_rotated, NULL, surface, &tmp_rect);
slouken@10612
   490
            } else {
slouken@10612
   491
                /* The NONE blend mode requires three steps to get the pixels onto the destination surface.
slouken@10612
   492
                 * First, the area where the rotated pixels will be blitted to get set to zero.
slouken@10612
   493
                 * This is accomplished by simply blitting a mask with the NONE blend mode.
slouken@10612
   494
                 * The colorkey set by the rotate function will discard the correct pixels.
slouken@10612
   495
                 */
slouken@10612
   496
                SDL_Rect mask_rect = tmp_rect;
slouken@10612
   497
                SDL_SetSurfaceBlendMode(mask_rotated, SDL_BLENDMODE_NONE);
slouken@10612
   498
                retval = SDL_BlitSurface(mask_rotated, NULL, surface, &mask_rect);
slouken@10612
   499
                if (!retval) {
slouken@10612
   500
                    /* The next step copies the alpha value. This is done with the BLEND blend mode and
slouken@10612
   501
                     * by modulating the source colors with 0. Since the destination is all zeros, this
slouken@10612
   502
                     * will effectively set the destination alpha to the source alpha.
slouken@10612
   503
                     */
slouken@10612
   504
                    SDL_SetSurfaceColorMod(src_rotated, 0, 0, 0);
slouken@10612
   505
                    mask_rect = tmp_rect;
slouken@10612
   506
                    retval = SDL_BlitSurface(src_rotated, NULL, surface, &mask_rect);
slouken@10612
   507
                    if (!retval) {
slouken@10612
   508
                        /* The last step gets the color values in place. The ADD blend mode simply adds them to
slouken@10612
   509
                         * the destination (where the color values are all zero). However, because the ADD blend
slouken@10612
   510
                         * mode modulates the colors with the alpha channel, a surface without an alpha mask needs
slouken@10612
   511
                         * to be created. This makes all source pixels opaque and the colors get copied correctly.
slouken@10612
   512
                         */
slouken@10612
   513
                        SDL_Surface *src_rotated_rgb;
slouken@10612
   514
                        src_rotated_rgb = SDL_CreateRGBSurfaceFrom(src_rotated->pixels, src_rotated->w, src_rotated->h,
slouken@10612
   515
                                                                   src_rotated->format->BitsPerPixel, src_rotated->pitch,
slouken@10612
   516
                                                                   src_rotated->format->Rmask, src_rotated->format->Gmask,
slouken@10612
   517
                                                                   src_rotated->format->Bmask, 0);
slouken@10612
   518
                        if (src_rotated_rgb == NULL) {
slouken@10612
   519
                            retval = -1;
slouken@10612
   520
                        } else {
slouken@10612
   521
                            SDL_SetSurfaceBlendMode(src_rotated_rgb, SDL_BLENDMODE_ADD);
slouken@10612
   522
                            retval = SDL_BlitSurface(src_rotated_rgb, NULL, surface, &tmp_rect);
slouken@10612
   523
                            SDL_FreeSurface(src_rotated_rgb);
slouken@10612
   524
                        }
slouken@10612
   525
                    }
slouken@10612
   526
                }
slouken@10612
   527
                SDL_FreeSurface(mask_rotated);
slouken@10612
   528
            }
slouken@10612
   529
            if (src_rotated != NULL) {
slouken@10612
   530
                SDL_FreeSurface(src_rotated);
slouken@10612
   531
            }
slouken@9762
   532
        }
slouken@9762
   533
    }
slouken@9762
   534
slouken@10612
   535
    if (SDL_MUSTLOCK(src)) {
slouken@10612
   536
        SDL_UnlockSurface(src);
slouken@10612
   537
    }
slouken@10612
   538
    if (mask != NULL) {
slouken@10612
   539
        SDL_FreeSurface(mask);
slouken@10612
   540
    }
slouken@10612
   541
    if (src_clone != NULL) {
slouken@10612
   542
        SDL_FreeSurface(src_clone);
slouken@9762
   543
    }
slouken@9762
   544
    return retval;
gabomdq@6320
   545
}
gabomdq@6320
   546
icculus@12288
   547
static void
icculus@12288
   548
PrepTextureForCopy(const SDL_RenderCommand *cmd)
icculus@12288
   549
{
icculus@12288
   550
    const Uint8 r = cmd->data.draw.r;
icculus@12288
   551
    const Uint8 g = cmd->data.draw.g;
icculus@12288
   552
    const Uint8 b = cmd->data.draw.b;
icculus@12288
   553
    const Uint8 a = cmd->data.draw.a;
icculus@12288
   554
    const SDL_BlendMode blend = cmd->data.draw.blend;
icculus@12288
   555
    SDL_Texture *texture = cmd->data.draw.texture;
icculus@12288
   556
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
icculus@12411
   557
    const SDL_bool colormod = ((r & g & b) != 0xFF);
icculus@12411
   558
    const SDL_bool alphamod = (a != 0xFF);
icculus@12411
   559
    const SDL_bool blending = ((blend == SDL_BLENDMODE_ADD) || (blend == SDL_BLENDMODE_MOD));
icculus@12288
   560
icculus@12411
   561
    if (colormod || alphamod || blending) {
icculus@12288
   562
        SDL_SetSurfaceRLE(surface, 0);
icculus@12288
   563
    }
icculus@12288
   564
icculus@12411
   565
    /* !!! FIXME: we can probably avoid some of these calls. */
icculus@12411
   566
    SDL_SetSurfaceColorMod(surface, r, g, b);
icculus@12411
   567
    SDL_SetSurfaceAlphaMod(surface, a);
icculus@12288
   568
    SDL_SetSurfaceBlendMode(surface, blend);
icculus@12288
   569
}
icculus@12288
   570
icculus@12288
   571
static int
icculus@12288
   572
SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
icculus@12288
   573
{
icculus@12288
   574
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
icculus@12288
   575
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
icculus@12288
   576
    const SDL_Rect *viewport = NULL;
icculus@12288
   577
    const SDL_Rect *cliprect = NULL;
icculus@12288
   578
icculus@12288
   579
    if (!surface) {
icculus@12288
   580
        return -1;
icculus@12288
   581
    }
icculus@12288
   582
icculus@12288
   583
    while (cmd) {
icculus@12288
   584
        switch (cmd->command) {
icculus@12288
   585
            case SDL_RENDERCMD_SETDRAWCOLOR: {
icculus@12288
   586
                break;  /* Not used in this backend. */
icculus@12288
   587
            }
icculus@12288
   588
icculus@12288
   589
            case SDL_RENDERCMD_SETVIEWPORT: {
icculus@12288
   590
                viewport = &cmd->data.viewport.rect;
icculus@12288
   591
                SDL_SetClipRect(data->surface, viewport);
icculus@12288
   592
                break;
icculus@12288
   593
            }
icculus@12288
   594
icculus@12288
   595
            case SDL_RENDERCMD_SETCLIPRECT: {
icculus@12288
   596
                SDL_assert(viewport != NULL);
icculus@12288
   597
                cliprect = cmd->data.cliprect.enabled ? &cmd->data.cliprect.rect : NULL;
icculus@12288
   598
                if (cliprect) {
sezeroz@12383
   599
                    SDL_Rect clip_rect;
sezeroz@12383
   600
                    clip_rect.x = cliprect->x + viewport->x;
sezeroz@12383
   601
                    clip_rect.y = cliprect->y + viewport->y;
sezeroz@12383
   602
                    clip_rect.w = cliprect->w;
sezeroz@12383
   603
                    clip_rect.h = cliprect->h;
icculus@12288
   604
                    SDL_IntersectRect(viewport, &clip_rect, &clip_rect);
icculus@12288
   605
                    SDL_SetClipRect(surface, &clip_rect);
icculus@12288
   606
                } else {
icculus@12288
   607
                    SDL_SetClipRect(surface, viewport);
icculus@12288
   608
                }
icculus@12288
   609
                break;
icculus@12288
   610
            }
icculus@12288
   611
icculus@12288
   612
            case SDL_RENDERCMD_CLEAR: {
icculus@12288
   613
                const Uint8 r = cmd->data.color.r;
icculus@12288
   614
                const Uint8 g = cmd->data.color.g;
icculus@12288
   615
                const Uint8 b = cmd->data.color.b;
icculus@12288
   616
                const Uint8 a = cmd->data.color.a;
icculus@12288
   617
                const SDL_Rect clip_rect = surface->clip_rect;
icculus@12288
   618
                /* By definition the clear ignores the clip rect */
icculus@12288
   619
                SDL_SetClipRect(surface, NULL);
icculus@12288
   620
                SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, r, g, b, a));
icculus@12288
   621
                SDL_SetClipRect(surface, &clip_rect);
icculus@12288
   622
                break;
icculus@12288
   623
            }
icculus@12288
   624
icculus@12288
   625
            case SDL_RENDERCMD_DRAW_POINTS: {
icculus@12288
   626
                const Uint8 r = cmd->data.draw.r;
icculus@12288
   627
                const Uint8 g = cmd->data.draw.g;
icculus@12288
   628
                const Uint8 b = cmd->data.draw.b;
icculus@12288
   629
                const Uint8 a = cmd->data.draw.a;
icculus@12415
   630
                const int count = (int) cmd->data.draw.count;
icculus@12288
   631
                const SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   632
                const SDL_BlendMode blend = cmd->data.draw.blend;
icculus@12288
   633
                if (blend == SDL_BLENDMODE_NONE) {
icculus@12288
   634
                    SDL_DrawPoints(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
icculus@12288
   635
                } else {
icculus@12288
   636
                    SDL_BlendPoints(surface, verts, count, blend, r, g, b, a);
icculus@12288
   637
                }
icculus@12288
   638
                break;
icculus@12288
   639
            }
icculus@12288
   640
icculus@12288
   641
            case SDL_RENDERCMD_DRAW_LINES: {
icculus@12288
   642
                const Uint8 r = cmd->data.draw.r;
icculus@12288
   643
                const Uint8 g = cmd->data.draw.g;
icculus@12288
   644
                const Uint8 b = cmd->data.draw.b;
icculus@12288
   645
                const Uint8 a = cmd->data.draw.a;
icculus@12415
   646
                const int count = (int) cmd->data.draw.count;
icculus@12288
   647
                const SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   648
                const SDL_BlendMode blend = cmd->data.draw.blend;
icculus@12288
   649
                if (blend == SDL_BLENDMODE_NONE) {
icculus@12288
   650
                    SDL_DrawLines(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
icculus@12288
   651
                } else {
icculus@12288
   652
                    SDL_BlendLines(surface, verts, count, blend, r, g, b, a);
icculus@12288
   653
                }
icculus@12288
   654
                break;
icculus@12288
   655
            }
icculus@12288
   656
icculus@12288
   657
            case SDL_RENDERCMD_FILL_RECTS: {
icculus@12288
   658
                const Uint8 r = cmd->data.draw.r;
icculus@12288
   659
                const Uint8 g = cmd->data.draw.g;
icculus@12288
   660
                const Uint8 b = cmd->data.draw.b;
icculus@12288
   661
                const Uint8 a = cmd->data.draw.a;
icculus@12415
   662
                const int count = (int) cmd->data.draw.count;
icculus@12288
   663
                const SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   664
                const SDL_BlendMode blend = cmd->data.draw.blend;
icculus@12288
   665
                if (blend == SDL_BLENDMODE_NONE) {
icculus@12288
   666
                    SDL_FillRects(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
icculus@12288
   667
                } else {
icculus@12288
   668
                    SDL_BlendFillRects(surface, verts, count, blend, r, g, b, a);
icculus@12288
   669
                }
icculus@12288
   670
                break;
icculus@12288
   671
            }
icculus@12288
   672
icculus@12288
   673
            case SDL_RENDERCMD_COPY: {
icculus@12288
   674
                SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   675
                const SDL_Rect *srcrect = verts;
icculus@12288
   676
                SDL_Rect *dstrect = verts + 1;
icculus@12288
   677
                SDL_Texture *texture = cmd->data.draw.texture;
icculus@12288
   678
                SDL_Surface *src = (SDL_Surface *) texture->driverdata;
icculus@12288
   679
icculus@12288
   680
                PrepTextureForCopy(cmd);
icculus@12288
   681
icculus@12288
   682
                if ( srcrect->w == dstrect->w && srcrect->h == dstrect->h ) {
icculus@12288
   683
                    SDL_BlitSurface(src, srcrect, surface, dstrect);
icculus@12288
   684
                } else {
icculus@12288
   685
                    /* If scaling is ever done, permanently disable RLE (which doesn't support scaling)
icculus@12288
   686
                     * to avoid potentially frequent RLE encoding/decoding.
icculus@12288
   687
                     */
icculus@12288
   688
                    SDL_SetSurfaceRLE(surface, 0);
icculus@12288
   689
                    SDL_BlitScaled(src, srcrect, surface, dstrect);
icculus@12288
   690
                }
icculus@12288
   691
                break;
icculus@12288
   692
            }
icculus@12288
   693
icculus@12288
   694
            case SDL_RENDERCMD_COPY_EX: {
icculus@12288
   695
                const CopyExData *copydata = (CopyExData *) (((Uint8 *) vertices) + cmd->data.draw.first);
icculus@12288
   696
                PrepTextureForCopy(cmd);
icculus@12288
   697
                SW_RenderCopyEx(renderer, surface, cmd->data.draw.texture, &copydata->srcrect,
icculus@12288
   698
                                &copydata->dstrect, copydata->angle, &copydata->center, copydata->flip);
icculus@12288
   699
                break;
icculus@12288
   700
            }
icculus@12288
   701
icculus@12288
   702
            case SDL_RENDERCMD_NO_OP:
icculus@12288
   703
                break;
icculus@12288
   704
        }
icculus@12288
   705
icculus@12288
   706
        cmd = cmd->next;
icculus@12288
   707
    }
icculus@12288
   708
icculus@12288
   709
    return 0;
icculus@12288
   710
}
icculus@12288
   711
gabomdq@6320
   712
static int
slouken@3427
   713
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   714
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   715
{
slouken@5166
   716
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   717
    Uint32 src_format;
slouken@5166
   718
    void *src_pixels;
slouken@3427
   719
slouken@5166
   720
    if (!surface) {
slouken@5147
   721
        return -1;
slouken@5147
   722
    }
slouken@5147
   723
slouken@11223
   724
    /* NOTE: The rect is already adjusted according to the viewport by
slouken@11223
   725
     * SDL_RenderReadPixels.
slouken@11223
   726
     */
slouken@5297
   727
slouken@5166
   728
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
slouken@5166
   729
        rect->y < 0 || rect->y+rect->h > surface->h) {
icculus@7037
   730
        return SDL_SetError("Tried to read outside of surface bounds");
slouken@3427
   731
    }
slouken@3427
   732
slouken@5297
   733
    src_format = surface->format->format;
slouken@5166
   734
    src_pixels = (void*)((Uint8 *) surface->pixels +
slouken@5166
   735
                    rect->y * surface->pitch +
slouken@5166
   736
                    rect->x * surface->format->BytesPerPixel);
slouken@5166
   737
slouken@5166
   738
    return SDL_ConvertPixels(rect->w, rect->h,
slouken@5166
   739
                             src_format, src_pixels, surface->pitch,
slouken@5166
   740
                             format, pixels, pitch);
slouken@3427
   741
}
slouken@3427
   742
slouken@1895
   743
static void
slouken@1918
   744
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   745
{
slouken@5166
   746
    SDL_Window *window = renderer->window;
slouken@1895
   747
slouken@5166
   748
    if (window) {
slouken@5166
   749
        SDL_UpdateWindowSurface(window);
slouken@5147
   750
    }
slouken@1895
   751
}
slouken@1895
   752
slouken@1895
   753
static void
slouken@1918
   754
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   755
{
slouken@5156
   756
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   757
slouken@5156
   758
    SDL_FreeSurface(surface);
slouken@1895
   759
}
slouken@1895
   760
slouken@1895
   761
static void
slouken@1918
   762
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   763
{
slouken@1918
   764
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   765
slouken@7719
   766
    SDL_free(data);
slouken@1895
   767
    SDL_free(renderer);
slouken@1895
   768
}
slouken@1895
   769
icculus@12288
   770
SDL_Renderer *
icculus@12288
   771
SW_CreateRendererForSurface(SDL_Surface * surface)
icculus@12288
   772
{
icculus@12288
   773
    SDL_Renderer *renderer;
icculus@12288
   774
    SW_RenderData *data;
icculus@12288
   775
icculus@12288
   776
    if (!surface) {
icculus@12288
   777
        SDL_SetError("Can't create renderer for NULL surface");
icculus@12288
   778
        return NULL;
icculus@12288
   779
    }
icculus@12288
   780
icculus@12288
   781
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
icculus@12288
   782
    if (!renderer) {
icculus@12288
   783
        SDL_OutOfMemory();
icculus@12288
   784
        return NULL;
icculus@12288
   785
    }
icculus@12288
   786
icculus@12288
   787
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
icculus@12288
   788
    if (!data) {
icculus@12288
   789
        SW_DestroyRenderer(renderer);
icculus@12288
   790
        SDL_OutOfMemory();
icculus@12288
   791
        return NULL;
icculus@12288
   792
    }
icculus@12288
   793
    data->surface = surface;
icculus@12288
   794
    data->window = surface;
icculus@12288
   795
icculus@12288
   796
    renderer->WindowEvent = SW_WindowEvent;
icculus@12288
   797
    renderer->GetOutputSize = SW_GetOutputSize;
icculus@12288
   798
    renderer->CreateTexture = SW_CreateTexture;
icculus@12288
   799
    renderer->UpdateTexture = SW_UpdateTexture;
icculus@12288
   800
    renderer->LockTexture = SW_LockTexture;
icculus@12288
   801
    renderer->UnlockTexture = SW_UnlockTexture;
icculus@12288
   802
    renderer->SetRenderTarget = SW_SetRenderTarget;
icculus@12288
   803
    renderer->QueueSetViewport = SW_QueueSetViewport;
icculus@12288
   804
    renderer->QueueSetDrawColor = SW_QueueSetViewport;  /* SetViewport and SetDrawColor are (currently) no-ops. */
icculus@12288
   805
    renderer->QueueDrawPoints = SW_QueueDrawPoints;
icculus@12288
   806
    renderer->QueueDrawLines = SW_QueueDrawPoints;  /* lines and points queue vertices the same way. */
icculus@12288
   807
    renderer->QueueFillRects = SW_QueueFillRects;
icculus@12288
   808
    renderer->QueueCopy = SW_QueueCopy;
icculus@12288
   809
    renderer->QueueCopyEx = SW_QueueCopyEx;
icculus@12288
   810
    renderer->RunCommandQueue = SW_RunCommandQueue;
icculus@12288
   811
    renderer->RenderReadPixels = SW_RenderReadPixels;
icculus@12288
   812
    renderer->RenderPresent = SW_RenderPresent;
icculus@12288
   813
    renderer->DestroyTexture = SW_DestroyTexture;
icculus@12288
   814
    renderer->DestroyRenderer = SW_DestroyRenderer;
icculus@12288
   815
    renderer->info = SW_RenderDriver.info;
icculus@12288
   816
    renderer->driverdata = data;
icculus@12288
   817
icculus@12288
   818
    SW_ActivateRenderer(renderer);
icculus@12288
   819
icculus@12288
   820
    return renderer;
icculus@12288
   821
}
icculus@12288
   822
sylvain@12454
   823
static SDL_Renderer *
icculus@12288
   824
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
icculus@12288
   825
{
icculus@12288
   826
    SDL_Surface *surface;
icculus@12288
   827
icculus@12288
   828
    surface = SDL_GetWindowSurface(window);
icculus@12288
   829
    if (!surface) {
icculus@12288
   830
        return NULL;
icculus@12288
   831
    }
icculus@12288
   832
    return SW_CreateRendererForSurface(surface);
icculus@12288
   833
}
icculus@12288
   834
icculus@12288
   835
SDL_RenderDriver SW_RenderDriver = {
icculus@12288
   836
    SW_CreateRenderer,
icculus@12288
   837
    {
icculus@12288
   838
     "software",
icculus@12288
   839
     SDL_RENDERER_SOFTWARE | SDL_RENDERER_TARGETTEXTURE,
icculus@12288
   840
     8,
icculus@12288
   841
     {
icculus@12288
   842
      SDL_PIXELFORMAT_ARGB8888,
icculus@12288
   843
      SDL_PIXELFORMAT_ABGR8888,
icculus@12288
   844
      SDL_PIXELFORMAT_RGBA8888,
icculus@12288
   845
      SDL_PIXELFORMAT_BGRA8888,
icculus@12288
   846
      SDL_PIXELFORMAT_RGB888,
icculus@12288
   847
      SDL_PIXELFORMAT_BGR888,
icculus@12288
   848
      SDL_PIXELFORMAT_RGB565,
icculus@12288
   849
      SDL_PIXELFORMAT_RGB555
icculus@12288
   850
     },
icculus@12288
   851
     0,
icculus@12288
   852
     0}
icculus@12288
   853
};
icculus@12288
   854
slouken@5226
   855
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   856
slouken@1895
   857
/* vi: set ts=4 sw=4 expandtab: */