src/render/software/SDL_render_sw.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 07 Feb 2012 19:34:24 -0500
changeset 6283 03a7ceb3487b
parent 6247 b6212690f78d
child 6320 6077a1310907
permissions -rwxr-xr-x
Fixed bug 1412 - Patch - Software renderer crash

Dimitris Zenios Date: 2012-02-06 15:12:37 GMT

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