src/render/software/SDL_render_sw.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5567 441c3036440c
child 6138 4c64952a58fb
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
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@6044
    26
#include "SDL_render_sw_c.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@5337
    75
     SDL_RENDERER_SOFTWARE,
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
dimitris@5567
   244
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   245
        SDL_LockSurface(surface);
slouken@5156
   246
    src = (Uint8 *) pixels;
slouken@5156
   247
    dst = (Uint8 *) surface->pixels +
slouken@5156
   248
                        rect->y * surface->pitch +
slouken@5156
   249
                        rect->x * surface->format->BytesPerPixel;
slouken@5156
   250
    length = rect->w * surface->format->BytesPerPixel;
slouken@5156
   251
    for (row = 0; row < rect->h; ++row) {
slouken@5156
   252
        SDL_memcpy(dst, src, length);
slouken@5156
   253
        src += pitch;
slouken@5156
   254
        dst += surface->pitch;
slouken@1895
   255
    }
dimitris@5567
   256
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   257
        SDL_UnlockSurface(surface);
slouken@5156
   258
    return 0;
slouken@1895
   259
}
slouken@1895
   260
slouken@1895
   261
static int
slouken@1918
   262
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   263
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   264
{
slouken@5156
   265
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   266
slouken@5156
   267
    *pixels =
slouken@5156
   268
        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@5156
   269
                  rect->x * surface->format->BytesPerPixel);
slouken@5156
   270
    *pitch = surface->pitch;
slouken@5156
   271
    return 0;
slouken@1895
   272
}
slouken@1895
   273
slouken@1895
   274
static void
slouken@1918
   275
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   276
{
slouken@1895
   277
}
slouken@1895
   278
slouken@5297
   279
static int
slouken@5297
   280
SW_UpdateViewport(SDL_Renderer * renderer)
slouken@5297
   281
{
slouken@5297
   282
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5297
   283
    SDL_Surface *surface = data->surface;
slouken@5297
   284
slouken@5297
   285
    if (!surface) {
slouken@5297
   286
        /* We'll update the viewport after we recreate the surface */
slouken@5297
   287
        return 0;
slouken@5297
   288
    }
slouken@5297
   289
slouken@5297
   290
    if (!renderer->viewport.w && !renderer->viewport.h) {
slouken@5297
   291
        /* There may be no window, so update the viewport directly */
slouken@5297
   292
        renderer->viewport.w = surface->w;
slouken@5297
   293
        renderer->viewport.h = surface->h;
slouken@5297
   294
    }
slouken@5298
   295
    SDL_SetClipRect(data->surface, &renderer->viewport);
slouken@5297
   296
    return 0;
slouken@5297
   297
}
slouken@5297
   298
slouken@5297
   299
static int
slouken@5297
   300
SW_RenderClear(SDL_Renderer * renderer)
slouken@5224
   301
{
slouken@5224
   302
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   303
    Uint32 color;
slouken@5297
   304
    SDL_Rect clip_rect;
slouken@5224
   305
slouken@5224
   306
    if (!surface) {
slouken@5297
   307
        return -1;
slouken@5224
   308
    }
slouken@5297
   309
slouken@5297
   310
    color = SDL_MapRGBA(surface->format,
slouken@5297
   311
                        renderer->r, renderer->g, renderer->b, renderer->a);
slouken@5297
   312
slouken@5297
   313
    /* By definition the clear ignores the clip rect */
slouken@5297
   314
    clip_rect = surface->clip_rect;
slouken@5297
   315
    SDL_SetClipRect(surface, NULL);
slouken@5297
   316
    SDL_FillRect(surface, NULL, color);
slouken@5297
   317
    SDL_SetClipRect(surface, &clip_rect);
slouken@5297
   318
    return 0;
slouken@5224
   319
}
slouken@5224
   320
slouken@1895
   321
static int
slouken@3596
   322
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   323
                    int count)
slouken@2901
   324
{
slouken@5166
   325
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   326
    SDL_Point *temp = NULL;
slouken@5297
   327
    int status;
slouken@2901
   328
slouken@5166
   329
    if (!surface) {
slouken@5147
   330
        return -1;
slouken@5147
   331
    }
slouken@5147
   332
slouken@5297
   333
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   334
        int i;
slouken@5297
   335
        int x = renderer->viewport.x;
slouken@5297
   336
        int y = renderer->viewport.y;
slouken@5297
   337
slouken@5297
   338
        temp = SDL_stack_alloc(SDL_Point, count);
slouken@5297
   339
        for (i = 0; i < count; ++i) {
slouken@5297
   340
            temp[i].x = x + points[i].x;
slouken@5297
   341
            temp[i].y = y + points[i].x;
slouken@5297
   342
        }
slouken@5297
   343
        points = temp;
slouken@5297
   344
    }
slouken@5297
   345
slouken@3536
   346
    /* Draw the points! */
slouken@5140
   347
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   348
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   349
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   350
                                   renderer->a);
slouken@2888
   351
slouken@5297
   352
        status = SDL_DrawPoints(surface, points, count, color);
slouken@2888
   353
    } else {
slouken@5297
   354
        status = SDL_BlendPoints(surface, points, count,
slouken@5297
   355
                                renderer->blendMode,
slouken@5297
   356
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   357
                                renderer->a);
slouken@2888
   358
    }
slouken@5297
   359
slouken@5297
   360
    if (temp) {
slouken@5297
   361
        SDL_stack_free(temp);
slouken@5297
   362
    }
slouken@5297
   363
    return status;
slouken@2888
   364
}
slouken@2888
   365
slouken@2888
   366
static int
slouken@3596
   367
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   368
                   int count)
slouken@1895
   369
{
slouken@5166
   370
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   371
    SDL_Point *temp = NULL;
slouken@5297
   372
    int status;
slouken@3536
   373
slouken@5166
   374
    if (!surface) {
slouken@5147
   375
        return -1;
slouken@5147
   376
    }
slouken@5147
   377
slouken@5297
   378
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   379
        int i;
slouken@5297
   380
        int x = renderer->viewport.x;
slouken@5297
   381
        int y = renderer->viewport.y;
slouken@5297
   382
slouken@5297
   383
        temp = SDL_stack_alloc(SDL_Point, count);
slouken@5297
   384
        for (i = 0; i < count; ++i) {
slouken@5297
   385
            temp[i].x = x + points[i].x;
slouken@5297
   386
            temp[i].y = y + points[i].y;
slouken@5297
   387
        }
slouken@5297
   388
        points = temp;
slouken@5297
   389
    }
slouken@5297
   390
slouken@5166
   391
    /* Draw the lines! */
slouken@5140
   392
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   393
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   394
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   395
                                   renderer->a);
slouken@2888
   396
slouken@5297
   397
        status = SDL_DrawLines(surface, points, count, color);
slouken@2888
   398
    } else {
slouken@5297
   399
        status = SDL_BlendLines(surface, points, count,
slouken@5297
   400
                                renderer->blendMode,
slouken@5297
   401
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   402
                                renderer->a);
slouken@2888
   403
    }
slouken@5297
   404
slouken@5297
   405
    if (temp) {
slouken@5297
   406
        SDL_stack_free(temp);
slouken@5297
   407
    }
slouken@5297
   408
    return status;
slouken@3536
   409
}
slouken@3536
   410
slouken@3536
   411
static int
slouken@5297
   412
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
slouken@3536
   413
{
slouken@5166
   414
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   415
    SDL_Rect *temp = NULL;
slouken@5297
   416
    int status;
slouken@3536
   417
slouken@5166
   418
    if (!surface) {
slouken@5147
   419
        return -1;
slouken@5147
   420
    }
slouken@5147
   421
slouken@5297
   422
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   423
        int i;
slouken@5297
   424
        int x = renderer->viewport.x;
slouken@5297
   425
        int y = renderer->viewport.y;
slouken@5297
   426
slouken@5297
   427
        temp = SDL_stack_alloc(SDL_Rect, count);
slouken@5297
   428
        for (i = 0; i < count; ++i) {
slouken@5297
   429
            temp[i].x = x + rects[i].x;
slouken@5297
   430
            temp[i].y = y + rects[i].y;
slouken@5297
   431
            temp[i].w = rects[i].w;
slouken@5297
   432
            temp[i].h = rects[i].h;
slouken@5297
   433
        }
slouken@5297
   434
        rects = temp;
slouken@5297
   435
    }
slouken@5297
   436
slouken@5140
   437
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   438
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@5166
   439
                                   renderer->r, renderer->g, renderer->b,
slouken@5166
   440
                                   renderer->a);
slouken@5297
   441
        status = SDL_FillRects(surface, rects, count, color);
slouken@5166
   442
    } else {
slouken@5297
   443
        status = SDL_BlendFillRects(surface, rects, count,
slouken@5297
   444
                                    renderer->blendMode,
slouken@5297
   445
                                    renderer->r, renderer->g, renderer->b,
slouken@5297
   446
                                    renderer->a);
slouken@3536
   447
    }
slouken@5297
   448
slouken@5297
   449
    if (temp) {
slouken@5297
   450
        SDL_stack_free(temp);
slouken@5297
   451
    }
slouken@5297
   452
    return status;
slouken@1895
   453
}
slouken@1895
   454
slouken@1895
   455
static int
slouken@1918
   456
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   457
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   458
{
slouken@5166
   459
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   460
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
slouken@5166
   461
    SDL_Rect final_rect = *dstrect;
slouken@1895
   462
slouken@5166
   463
    if (!surface) {
slouken@5147
   464
        return -1;
slouken@5147
   465
    }
ken@5296
   466
slouken@5297
   467
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   468
        final_rect.x += renderer->viewport.x;
slouken@5297
   469
        final_rect.y += renderer->viewport.y;
slouken@5297
   470
    }
ken@5296
   471
    if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
ken@5296
   472
        return SDL_BlitSurface(src, srcrect, surface, &final_rect);
ken@5296
   473
    } else {
ken@5296
   474
        return SDL_BlitScaled(src, srcrect, surface, &final_rect);
ken@5296
   475
    }
slouken@1895
   476
}
slouken@1895
   477
slouken@3427
   478
static int
slouken@3427
   479
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   480
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   481
{
slouken@5166
   482
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   483
    Uint32 src_format;
slouken@5166
   484
    void *src_pixels;
slouken@5297
   485
    SDL_Rect final_rect;
slouken@3427
   486
slouken@5166
   487
    if (!surface) {
slouken@5147
   488
        return -1;
slouken@5147
   489
    }
slouken@5147
   490
slouken@5297
   491
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   492
        final_rect.x = renderer->viewport.x + rect->x;
slouken@5297
   493
        final_rect.y = renderer->viewport.y + rect->y;
slouken@5297
   494
        final_rect.w = rect->w;
slouken@5297
   495
        final_rect.h = rect->h;
slouken@5297
   496
        rect = &final_rect;
slouken@5297
   497
    }
slouken@5297
   498
slouken@5166
   499
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
slouken@5166
   500
        rect->y < 0 || rect->y+rect->h > surface->h) {
slouken@5166
   501
        SDL_SetError("Tried to read outside of surface bounds");
slouken@3427
   502
        return -1;
slouken@3427
   503
    }
slouken@3427
   504
slouken@5297
   505
    src_format = surface->format->format;
slouken@5166
   506
    src_pixels = (void*)((Uint8 *) surface->pixels +
slouken@5166
   507
                    rect->y * surface->pitch +
slouken@5166
   508
                    rect->x * surface->format->BytesPerPixel);
slouken@5166
   509
slouken@5166
   510
    return SDL_ConvertPixels(rect->w, rect->h,
slouken@5166
   511
                             src_format, src_pixels, surface->pitch,
slouken@5166
   512
                             format, pixels, pitch);
slouken@3427
   513
}
slouken@3427
   514
slouken@1895
   515
static void
slouken@1918
   516
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   517
{
slouken@5166
   518
    SDL_Window *window = renderer->window;
slouken@1895
   519
slouken@5166
   520
    if (window) {
slouken@5166
   521
        SDL_UpdateWindowSurface(window);
slouken@5147
   522
    }
slouken@1895
   523
}
slouken@1895
   524
slouken@1895
   525
static void
slouken@1918
   526
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   527
{
slouken@5156
   528
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   529
slouken@5156
   530
    SDL_FreeSurface(surface);
slouken@1895
   531
}
slouken@1895
   532
slouken@1895
   533
static void
slouken@1918
   534
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   535
{
slouken@1918
   536
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   537
slouken@1895
   538
    if (data) {
slouken@1895
   539
        SDL_free(data);
slouken@1895
   540
    }
slouken@1895
   541
    SDL_free(renderer);
slouken@1895
   542
}
slouken@1895
   543
slouken@5226
   544
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   545
slouken@1895
   546
/* vi: set ts=4 sw=4 expandtab: */