src/render/software/SDL_render_sw.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5296 48067bfc300c
child 5298 7b1cac2c2230
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

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