src/render/software/SDL_render_sw.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5337 d5d312a82d40
child 5567 441c3036440c
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
*/
slouken@1895
    21
#include "SDL_config.h"
slouken@1895
    22
slouken@5226
    23
#if !SDL_RENDER_DISABLED
slouken@5226
    24
slouken@5154
    25
#include "../SDL_sysrender.h"
slouken@1895
    26
slouken@5163
    27
#include "SDL_draw.h"
slouken@5163
    28
#include "SDL_blendfillrect.h"
slouken@5163
    29
#include "SDL_blendline.h"
slouken@5163
    30
#include "SDL_blendpoint.h"
slouken@5163
    31
#include "SDL_drawline.h"
slouken@5163
    32
#include "SDL_drawpoint.h"
slouken@5163
    33
slouken@1895
    34
slouken@1895
    35
/* SDL surface based renderer implementation */
slouken@1895
    36
slouken@1918
    37
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5147
    38
static void SW_WindowEvent(SDL_Renderer * renderer,
slouken@5147
    39
                           const SDL_WindowEvent *event);
slouken@1918
    40
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1985
    41
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    42
                                 SDL_Texture * texture);
slouken@1985
    43
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    44
                                 SDL_Texture * texture);
slouken@5141
    45
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@5141
    46
                                  SDL_Texture * texture);
slouken@1985
    47
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    48
                            const SDL_Rect * rect, const void *pixels,
slouken@1985
    49
                            int pitch);
slouken@1918
    50
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
    51
                          const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1918
    52
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
    53
static int SW_UpdateViewport(SDL_Renderer * renderer);
slouken@5297
    54
static int SW_RenderClear(SDL_Renderer * renderer);
slouken@3596
    55
static int SW_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3596
    56
                               const SDL_Point * points, int count);
slouken@3596
    57
static int SW_RenderDrawLines(SDL_Renderer * renderer,
slouken@3596
    58
                              const SDL_Point * points, int count);
slouken@3596
    59
static int SW_RenderFillRects(SDL_Renderer * renderer,
slouken@5297
    60
                              const SDL_Rect * rects, int count);
slouken@1918
    61
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    62
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3427
    63
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    64
                               Uint32 format, void * pixels, int pitch);
slouken@1918
    65
static void SW_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    66
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    67
static void SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    68
slouken@1895
    69
slouken@1918
    70
SDL_RenderDriver SW_RenderDriver = {
slouken@1918
    71
    SW_CreateRenderer,
slouken@1895
    72
    {
slouken@1895
    73
     "software",
slouken@5337
    74
     SDL_RENDERER_SOFTWARE,
slouken@5156
    75
     8,
slouken@1895
    76
     {
slouken@1965
    77
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    78
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    79
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    80
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    81
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    82
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    83
      SDL_PIXELFORMAT_ABGR8888,
slouken@5156
    84
      SDL_PIXELFORMAT_BGRA8888
slouken@5156
    85
     },
slouken@1895
    86
     0,
slouken@1895
    87
     0}
slouken@1895
    88
};
slouken@1895
    89
slouken@1895
    90
typedef struct
slouken@1895
    91
{
slouken@5166
    92
    SDL_Surface *surface;
slouken@1918
    93
} SW_RenderData;
slouken@1895
    94
slouken@1907
    95
slouken@5297
    96
static SDL_Surface *
slouken@5297
    97
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
    98
{
slouken@5297
    99
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5297
   100
slouken@5297
   101
    if (!data->surface) {
slouken@5297
   102
        data->surface = SDL_GetWindowSurface(renderer->window);
slouken@5297
   103
slouken@5297
   104
        SW_UpdateViewport(renderer);
slouken@5297
   105
    }
slouken@5297
   106
    return data->surface;
slouken@5297
   107
}
slouken@5297
   108
slouken@1895
   109
SDL_Renderer *
slouken@5166
   110
SW_CreateRendererForSurface(SDL_Surface * surface)
slouken@1895
   111
{
slouken@1895
   112
    SDL_Renderer *renderer;
slouken@1918
   113
    SW_RenderData *data;
slouken@1895
   114
slouken@5166
   115
    if (!surface) {
slouken@5166
   116
        SDL_SetError("Can't create renderer for NULL surface");
slouken@1895
   117
        return NULL;
slouken@1895
   118
    }
slouken@1895
   119
slouken@1895
   120
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   121
    if (!renderer) {
slouken@1895
   122
        SDL_OutOfMemory();
slouken@1895
   123
        return NULL;
slouken@1895
   124
    }
slouken@1895
   125
slouken@1920
   126
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   127
    if (!data) {
slouken@1918
   128
        SW_DestroyRenderer(renderer);
slouken@1895
   129
        SDL_OutOfMemory();
slouken@1895
   130
        return NULL;
slouken@1895
   131
    }
slouken@5166
   132
    data->surface = surface;
slouken@5166
   133
slouken@5147
   134
    renderer->WindowEvent = SW_WindowEvent;
slouken@5154
   135
    renderer->CreateTexture = SW_CreateTexture;
slouken@5154
   136
    renderer->SetTextureColorMod = SW_SetTextureColorMod;
slouken@5154
   137
    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
slouken@5154
   138
    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
slouken@5154
   139
    renderer->UpdateTexture = SW_UpdateTexture;
slouken@5154
   140
    renderer->LockTexture = SW_LockTexture;
slouken@5154
   141
    renderer->UnlockTexture = SW_UnlockTexture;
slouken@5297
   142
    renderer->UpdateViewport = SW_UpdateViewport;
slouken@5154
   143
    renderer->DestroyTexture = SW_DestroyTexture;
slouken@5297
   144
    renderer->RenderClear = SW_RenderClear;
slouken@3596
   145
    renderer->RenderDrawPoints = SW_RenderDrawPoints;
slouken@3596
   146
    renderer->RenderDrawLines = SW_RenderDrawLines;
slouken@3596
   147
    renderer->RenderFillRects = SW_RenderFillRects;
slouken@1918
   148
    renderer->RenderCopy = SW_RenderCopy;
slouken@3427
   149
    renderer->RenderReadPixels = SW_RenderReadPixels;
slouken@1918
   150
    renderer->RenderPresent = SW_RenderPresent;
slouken@1918
   151
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@5154
   152
    renderer->info = SW_RenderDriver.info;
slouken@1895
   153
    renderer->driverdata = data;
slouken@1895
   154
slouken@5297
   155
    SW_ActivateRenderer(renderer);
slouken@5297
   156
slouken@1895
   157
    return renderer;
slouken@1895
   158
}
slouken@1895
   159
slouken@5166
   160
SDL_Renderer *
slouken@5166
   161
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@5166
   162
{
slouken@5166
   163
    SDL_Surface *surface;
slouken@5166
   164
slouken@5166
   165
    surface = SDL_GetWindowSurface(window);
slouken@5166
   166
    if (!surface) {
slouken@5166
   167
        return NULL;
slouken@5166
   168
    }
slouken@5166
   169
    return SW_CreateRendererForSurface(surface);
slouken@5166
   170
}
slouken@5166
   171
slouken@5147
   172
static void
slouken@5147
   173
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   174
{
slouken@1970
   175
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   176
slouken@5276
   177
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
   178
        data->surface = NULL;
slouken@1970
   179
    }
slouken@1970
   180
}
slouken@1970
   181
slouken@1970
   182
static int
slouken@1918
   183
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   184
{
slouken@5156
   185
    int bpp;
slouken@5156
   186
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   187
slouken@5156
   188
    if (!SDL_PixelFormatEnumToMasks
slouken@5156
   189
        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5156
   190
        SDL_SetError("Unknown texture format");
slouken@5156
   191
        return -1;
slouken@5156
   192
    }
slouken@1895
   193
slouken@5156
   194
    texture->driverdata =
slouken@5156
   195
        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@5156
   196
                             Bmask, Amask);
slouken@5156
   197
    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
slouken@5156
   198
                           texture->b);
slouken@5156
   199
    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
slouken@5156
   200
    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
slouken@3053
   201
slouken@5156
   202
    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
slouken@5156
   203
        SDL_SetSurfaceRLE(texture->driverdata, 1);
slouken@1895
   204
    }
slouken@1895
   205
slouken@1895
   206
    if (!texture->driverdata) {
slouken@1895
   207
        return -1;
slouken@1895
   208
    }
slouken@1895
   209
    return 0;
slouken@1895
   210
}
slouken@1895
   211
slouken@1895
   212
static int
slouken@1985
   213
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   214
{
slouken@2267
   215
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   216
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
slouken@2267
   217
                                  texture->b);
slouken@1985
   218
}
slouken@1985
   219
slouken@1985
   220
static int
slouken@1985
   221
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   222
{
slouken@2267
   223
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   224
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
slouken@1985
   225
}
slouken@1985
   226
slouken@1985
   227
static int
slouken@1985
   228
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   229
{
slouken@2267
   230
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   231
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
slouken@1985
   232
}
slouken@1985
   233
slouken@1985
   234
static int
slouken@1918
   235
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   236
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   237
{
slouken@5156
   238
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@5156
   239
    Uint8 *src, *dst;
slouken@5156
   240
    int row;
slouken@5156
   241
    size_t length;
slouken@1895
   242
slouken@5156
   243
    src = (Uint8 *) pixels;
slouken@5156
   244
    dst = (Uint8 *) surface->pixels +
slouken@5156
   245
                        rect->y * surface->pitch +
slouken@5156
   246
                        rect->x * surface->format->BytesPerPixel;
slouken@5156
   247
    length = rect->w * surface->format->BytesPerPixel;
slouken@5156
   248
    for (row = 0; row < rect->h; ++row) {
slouken@5156
   249
        SDL_memcpy(dst, src, length);
slouken@5156
   250
        src += pitch;
slouken@5156
   251
        dst += surface->pitch;
slouken@1895
   252
    }
slouken@5156
   253
    return 0;
slouken@1895
   254
}
slouken@1895
   255
slouken@1895
   256
static int
slouken@1918
   257
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   258
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   259
{
slouken@5156
   260
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   261
slouken@5156
   262
    *pixels =
slouken@5156
   263
        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@5156
   264
                  rect->x * surface->format->BytesPerPixel);
slouken@5156
   265
    *pitch = surface->pitch;
slouken@5156
   266
    return 0;
slouken@1895
   267
}
slouken@1895
   268
slouken@1895
   269
static void
slouken@1918
   270
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   271
{
slouken@1895
   272
}
slouken@1895
   273
slouken@5297
   274
static int
slouken@5297
   275
SW_UpdateViewport(SDL_Renderer * renderer)
slouken@5297
   276
{
slouken@5297
   277
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5297
   278
    SDL_Surface *surface = data->surface;
slouken@5297
   279
slouken@5297
   280
    if (!surface) {
slouken@5297
   281
        /* We'll update the viewport after we recreate the surface */
slouken@5297
   282
        return 0;
slouken@5297
   283
    }
slouken@5297
   284
slouken@5297
   285
    if (!renderer->viewport.w && !renderer->viewport.h) {
slouken@5297
   286
        /* There may be no window, so update the viewport directly */
slouken@5297
   287
        renderer->viewport.w = surface->w;
slouken@5297
   288
        renderer->viewport.h = surface->h;
slouken@5297
   289
    }
slouken@5298
   290
    SDL_SetClipRect(data->surface, &renderer->viewport);
slouken@5297
   291
    return 0;
slouken@5297
   292
}
slouken@5297
   293
slouken@5297
   294
static int
slouken@5297
   295
SW_RenderClear(SDL_Renderer * renderer)
slouken@5224
   296
{
slouken@5224
   297
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   298
    Uint32 color;
slouken@5297
   299
    SDL_Rect clip_rect;
slouken@5224
   300
slouken@5224
   301
    if (!surface) {
slouken@5297
   302
        return -1;
slouken@5224
   303
    }
slouken@5297
   304
slouken@5297
   305
    color = SDL_MapRGBA(surface->format,
slouken@5297
   306
                        renderer->r, renderer->g, renderer->b, renderer->a);
slouken@5297
   307
slouken@5297
   308
    /* By definition the clear ignores the clip rect */
slouken@5297
   309
    clip_rect = surface->clip_rect;
slouken@5297
   310
    SDL_SetClipRect(surface, NULL);
slouken@5297
   311
    SDL_FillRect(surface, NULL, color);
slouken@5297
   312
    SDL_SetClipRect(surface, &clip_rect);
slouken@5297
   313
    return 0;
slouken@5224
   314
}
slouken@5224
   315
slouken@1895
   316
static int
slouken@3596
   317
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   318
                    int count)
slouken@2901
   319
{
slouken@5166
   320
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   321
    SDL_Point *temp = NULL;
slouken@5297
   322
    int status;
slouken@2901
   323
slouken@5166
   324
    if (!surface) {
slouken@5147
   325
        return -1;
slouken@5147
   326
    }
slouken@5147
   327
slouken@5297
   328
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   329
        int i;
slouken@5297
   330
        int x = renderer->viewport.x;
slouken@5297
   331
        int y = renderer->viewport.y;
slouken@5297
   332
slouken@5297
   333
        temp = SDL_stack_alloc(SDL_Point, count);
slouken@5297
   334
        for (i = 0; i < count; ++i) {
slouken@5297
   335
            temp[i].x = x + points[i].x;
slouken@5297
   336
            temp[i].y = y + points[i].x;
slouken@5297
   337
        }
slouken@5297
   338
        points = temp;
slouken@5297
   339
    }
slouken@5297
   340
slouken@3536
   341
    /* Draw the points! */
slouken@5140
   342
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   343
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   344
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   345
                                   renderer->a);
slouken@2888
   346
slouken@5297
   347
        status = SDL_DrawPoints(surface, points, count, color);
slouken@2888
   348
    } else {
slouken@5297
   349
        status = SDL_BlendPoints(surface, points, count,
slouken@5297
   350
                                renderer->blendMode,
slouken@5297
   351
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   352
                                renderer->a);
slouken@2888
   353
    }
slouken@5297
   354
slouken@5297
   355
    if (temp) {
slouken@5297
   356
        SDL_stack_free(temp);
slouken@5297
   357
    }
slouken@5297
   358
    return status;
slouken@2888
   359
}
slouken@2888
   360
slouken@2888
   361
static int
slouken@3596
   362
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   363
                   int count)
slouken@1895
   364
{
slouken@5166
   365
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   366
    SDL_Point *temp = NULL;
slouken@5297
   367
    int status;
slouken@3536
   368
slouken@5166
   369
    if (!surface) {
slouken@5147
   370
        return -1;
slouken@5147
   371
    }
slouken@5147
   372
slouken@5297
   373
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   374
        int i;
slouken@5297
   375
        int x = renderer->viewport.x;
slouken@5297
   376
        int y = renderer->viewport.y;
slouken@5297
   377
slouken@5297
   378
        temp = SDL_stack_alloc(SDL_Point, count);
slouken@5297
   379
        for (i = 0; i < count; ++i) {
slouken@5297
   380
            temp[i].x = x + points[i].x;
slouken@5297
   381
            temp[i].y = y + points[i].y;
slouken@5297
   382
        }
slouken@5297
   383
        points = temp;
slouken@5297
   384
    }
slouken@5297
   385
slouken@5166
   386
    /* Draw the lines! */
slouken@5140
   387
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   388
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   389
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   390
                                   renderer->a);
slouken@2888
   391
slouken@5297
   392
        status = SDL_DrawLines(surface, points, count, color);
slouken@2888
   393
    } else {
slouken@5297
   394
        status = SDL_BlendLines(surface, points, count,
slouken@5297
   395
                                renderer->blendMode,
slouken@5297
   396
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   397
                                renderer->a);
slouken@2888
   398
    }
slouken@5297
   399
slouken@5297
   400
    if (temp) {
slouken@5297
   401
        SDL_stack_free(temp);
slouken@5297
   402
    }
slouken@5297
   403
    return status;
slouken@3536
   404
}
slouken@3536
   405
slouken@3536
   406
static int
slouken@5297
   407
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
slouken@3536
   408
{
slouken@5166
   409
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   410
    SDL_Rect *temp = NULL;
slouken@5297
   411
    int status;
slouken@3536
   412
slouken@5166
   413
    if (!surface) {
slouken@5147
   414
        return -1;
slouken@5147
   415
    }
slouken@5147
   416
slouken@5297
   417
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   418
        int i;
slouken@5297
   419
        int x = renderer->viewport.x;
slouken@5297
   420
        int y = renderer->viewport.y;
slouken@5297
   421
slouken@5297
   422
        temp = SDL_stack_alloc(SDL_Rect, count);
slouken@5297
   423
        for (i = 0; i < count; ++i) {
slouken@5297
   424
            temp[i].x = x + rects[i].x;
slouken@5297
   425
            temp[i].y = y + rects[i].y;
slouken@5297
   426
            temp[i].w = rects[i].w;
slouken@5297
   427
            temp[i].h = rects[i].h;
slouken@5297
   428
        }
slouken@5297
   429
        rects = temp;
slouken@5297
   430
    }
slouken@5297
   431
slouken@5140
   432
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   433
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@5166
   434
                                   renderer->r, renderer->g, renderer->b,
slouken@5166
   435
                                   renderer->a);
slouken@5297
   436
        status = SDL_FillRects(surface, rects, count, color);
slouken@5166
   437
    } else {
slouken@5297
   438
        status = SDL_BlendFillRects(surface, rects, count,
slouken@5297
   439
                                    renderer->blendMode,
slouken@5297
   440
                                    renderer->r, renderer->g, renderer->b,
slouken@5297
   441
                                    renderer->a);
slouken@3536
   442
    }
slouken@5297
   443
slouken@5297
   444
    if (temp) {
slouken@5297
   445
        SDL_stack_free(temp);
slouken@5297
   446
    }
slouken@5297
   447
    return status;
slouken@1895
   448
}
slouken@1895
   449
slouken@1895
   450
static int
slouken@1918
   451
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   452
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   453
{
slouken@5166
   454
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   455
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
slouken@5166
   456
    SDL_Rect final_rect = *dstrect;
slouken@1895
   457
slouken@5166
   458
    if (!surface) {
slouken@5147
   459
        return -1;
slouken@5147
   460
    }
ken@5296
   461
slouken@5297
   462
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   463
        final_rect.x += renderer->viewport.x;
slouken@5297
   464
        final_rect.y += renderer->viewport.y;
slouken@5297
   465
    }
ken@5296
   466
    if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
ken@5296
   467
        return SDL_BlitSurface(src, srcrect, surface, &final_rect);
ken@5296
   468
    } else {
ken@5296
   469
        return SDL_BlitScaled(src, srcrect, surface, &final_rect);
ken@5296
   470
    }
slouken@1895
   471
}
slouken@1895
   472
slouken@3427
   473
static int
slouken@3427
   474
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   475
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   476
{
slouken@5166
   477
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   478
    Uint32 src_format;
slouken@5166
   479
    void *src_pixels;
slouken@5297
   480
    SDL_Rect final_rect;
slouken@3427
   481
slouken@5166
   482
    if (!surface) {
slouken@5147
   483
        return -1;
slouken@5147
   484
    }
slouken@5147
   485
slouken@5297
   486
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   487
        final_rect.x = renderer->viewport.x + rect->x;
slouken@5297
   488
        final_rect.y = renderer->viewport.y + rect->y;
slouken@5297
   489
        final_rect.w = rect->w;
slouken@5297
   490
        final_rect.h = rect->h;
slouken@5297
   491
        rect = &final_rect;
slouken@5297
   492
    }
slouken@5297
   493
slouken@5166
   494
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
slouken@5166
   495
        rect->y < 0 || rect->y+rect->h > surface->h) {
slouken@5166
   496
        SDL_SetError("Tried to read outside of surface bounds");
slouken@3427
   497
        return -1;
slouken@3427
   498
    }
slouken@3427
   499
slouken@5297
   500
    src_format = surface->format->format;
slouken@5166
   501
    src_pixels = (void*)((Uint8 *) surface->pixels +
slouken@5166
   502
                    rect->y * surface->pitch +
slouken@5166
   503
                    rect->x * surface->format->BytesPerPixel);
slouken@5166
   504
slouken@5166
   505
    return SDL_ConvertPixels(rect->w, rect->h,
slouken@5166
   506
                             src_format, src_pixels, surface->pitch,
slouken@5166
   507
                             format, pixels, pitch);
slouken@3427
   508
}
slouken@3427
   509
slouken@1895
   510
static void
slouken@1918
   511
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   512
{
slouken@5166
   513
    SDL_Window *window = renderer->window;
slouken@1895
   514
slouken@5166
   515
    if (window) {
slouken@5166
   516
        SDL_UpdateWindowSurface(window);
slouken@5147
   517
    }
slouken@1895
   518
}
slouken@1895
   519
slouken@1895
   520
static void
slouken@1918
   521
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   522
{
slouken@5156
   523
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   524
slouken@5156
   525
    SDL_FreeSurface(surface);
slouken@1895
   526
}
slouken@1895
   527
slouken@1895
   528
static void
slouken@1918
   529
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   530
{
slouken@1918
   531
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   532
slouken@1895
   533
    if (data) {
slouken@1895
   534
        SDL_free(data);
slouken@1895
   535
    }
slouken@1895
   536
    SDL_free(renderer);
slouken@1895
   537
}
slouken@1895
   538
slouken@5226
   539
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   540
slouken@1895
   541
/* vi: set ts=4 sw=4 expandtab: */