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