src/render/software/SDL_render_sw.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Jan 2012 01:26:28 -0500
changeset 6247 b6212690f78d
parent 6246 c70ec935a4bb
child 6283 03a7ceb3487b
permissions -rwxr-xr-x
Renamed SetTargetTexture() to SetRenderTarget()
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@1970
   186
    }
slouken@1970
   187
}
slouken@1970
   188
slouken@1970
   189
static int
slouken@1918
   190
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   191
{
slouken@5156
   192
    int bpp;
slouken@5156
   193
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   194
slouken@5156
   195
    if (!SDL_PixelFormatEnumToMasks
slouken@5156
   196
        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5156
   197
        SDL_SetError("Unknown texture format");
slouken@5156
   198
        return -1;
slouken@5156
   199
    }
slouken@1895
   200
slouken@5156
   201
    texture->driverdata =
slouken@5156
   202
        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@5156
   203
                             Bmask, Amask);
slouken@5156
   204
    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
slouken@5156
   205
                           texture->b);
slouken@5156
   206
    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
slouken@5156
   207
    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
slouken@3053
   208
slouken@5156
   209
    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
slouken@5156
   210
        SDL_SetSurfaceRLE(texture->driverdata, 1);
slouken@1895
   211
    }
slouken@1895
   212
slouken@1895
   213
    if (!texture->driverdata) {
slouken@1895
   214
        return -1;
slouken@1895
   215
    }
slouken@1895
   216
    return 0;
slouken@1895
   217
}
slouken@1895
   218
slouken@1895
   219
static int
slouken@1985
   220
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   221
{
slouken@2267
   222
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   223
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
slouken@2267
   224
                                  texture->b);
slouken@1985
   225
}
slouken@1985
   226
slouken@1985
   227
static int
slouken@1985
   228
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   229
{
slouken@2267
   230
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   231
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
slouken@1985
   232
}
slouken@1985
   233
slouken@1985
   234
static int
slouken@1985
   235
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   236
{
slouken@2267
   237
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   238
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
slouken@1985
   239
}
slouken@1985
   240
slouken@1985
   241
static int
slouken@1918
   242
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   243
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   244
{
slouken@5156
   245
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@5156
   246
    Uint8 *src, *dst;
slouken@5156
   247
    int row;
slouken@5156
   248
    size_t length;
slouken@1895
   249
dimitris@5567
   250
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   251
        SDL_LockSurface(surface);
slouken@5156
   252
    src = (Uint8 *) pixels;
slouken@5156
   253
    dst = (Uint8 *) surface->pixels +
slouken@5156
   254
                        rect->y * surface->pitch +
slouken@5156
   255
                        rect->x * surface->format->BytesPerPixel;
slouken@5156
   256
    length = rect->w * surface->format->BytesPerPixel;
slouken@5156
   257
    for (row = 0; row < rect->h; ++row) {
slouken@5156
   258
        SDL_memcpy(dst, src, length);
slouken@5156
   259
        src += pitch;
slouken@5156
   260
        dst += surface->pitch;
slouken@1895
   261
    }
dimitris@5567
   262
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   263
        SDL_UnlockSurface(surface);
slouken@5156
   264
    return 0;
slouken@1895
   265
}
slouken@1895
   266
slouken@1895
   267
static int
slouken@1918
   268
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   269
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   270
{
slouken@5156
   271
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   272
slouken@5156
   273
    *pixels =
slouken@5156
   274
        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@5156
   275
                  rect->x * surface->format->BytesPerPixel);
slouken@5156
   276
    *pitch = surface->pitch;
slouken@5156
   277
    return 0;
slouken@1895
   278
}
slouken@1895
   279
slouken@1895
   280
static void
slouken@1918
   281
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   282
{
slouken@1895
   283
}
slouken@1895
   284
slouken@5297
   285
static int
slouken@6247
   286
SW_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   287
{
slouken@6246
   288
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@6246
   289
slouken@6246
   290
    if (texture ) {
slouken@6246
   291
        data->surface = (SDL_Surface *) texture->driverdata;
slouken@6246
   292
    } else {
slouken@6246
   293
        data->surface = data->window;
slouken@6246
   294
    }
slouken@6246
   295
    return 0;
slouken@6246
   296
}
slouken@6246
   297
slouken@6246
   298
static int
slouken@5297
   299
SW_UpdateViewport(SDL_Renderer * renderer)
slouken@5297
   300
{
slouken@5297
   301
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5297
   302
    SDL_Surface *surface = data->surface;
slouken@5297
   303
slouken@5297
   304
    if (!surface) {
slouken@5297
   305
        /* We'll update the viewport after we recreate the surface */
slouken@5297
   306
        return 0;
slouken@5297
   307
    }
slouken@5297
   308
slouken@5297
   309
    if (!renderer->viewport.w && !renderer->viewport.h) {
slouken@5297
   310
        /* There may be no window, so update the viewport directly */
slouken@5297
   311
        renderer->viewport.w = surface->w;
slouken@5297
   312
        renderer->viewport.h = surface->h;
slouken@5297
   313
    }
slouken@5298
   314
    SDL_SetClipRect(data->surface, &renderer->viewport);
slouken@5297
   315
    return 0;
slouken@5297
   316
}
slouken@5297
   317
slouken@5297
   318
static int
slouken@5297
   319
SW_RenderClear(SDL_Renderer * renderer)
slouken@5224
   320
{
slouken@5224
   321
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   322
    Uint32 color;
slouken@5297
   323
    SDL_Rect clip_rect;
slouken@5224
   324
slouken@5224
   325
    if (!surface) {
slouken@5297
   326
        return -1;
slouken@5224
   327
    }
slouken@5297
   328
slouken@5297
   329
    color = SDL_MapRGBA(surface->format,
slouken@5297
   330
                        renderer->r, renderer->g, renderer->b, renderer->a);
slouken@5297
   331
slouken@5297
   332
    /* By definition the clear ignores the clip rect */
slouken@5297
   333
    clip_rect = surface->clip_rect;
slouken@5297
   334
    SDL_SetClipRect(surface, NULL);
slouken@5297
   335
    SDL_FillRect(surface, NULL, color);
slouken@5297
   336
    SDL_SetClipRect(surface, &clip_rect);
slouken@5297
   337
    return 0;
slouken@5224
   338
}
slouken@5224
   339
slouken@1895
   340
static int
slouken@3596
   341
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   342
                    int count)
slouken@2901
   343
{
slouken@5166
   344
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   345
    SDL_Point *temp = NULL;
slouken@5297
   346
    int status;
slouken@2901
   347
slouken@5166
   348
    if (!surface) {
slouken@5147
   349
        return -1;
slouken@5147
   350
    }
slouken@5147
   351
slouken@5297
   352
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   353
        int i;
slouken@5297
   354
        int x = renderer->viewport.x;
slouken@5297
   355
        int y = renderer->viewport.y;
slouken@5297
   356
slouken@5297
   357
        temp = SDL_stack_alloc(SDL_Point, count);
slouken@5297
   358
        for (i = 0; i < count; ++i) {
slouken@5297
   359
            temp[i].x = x + points[i].x;
slouken@5297
   360
            temp[i].y = y + points[i].x;
slouken@5297
   361
        }
slouken@5297
   362
        points = temp;
slouken@5297
   363
    }
slouken@5297
   364
slouken@3536
   365
    /* Draw the points! */
slouken@5140
   366
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   367
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   368
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   369
                                   renderer->a);
slouken@2888
   370
slouken@5297
   371
        status = SDL_DrawPoints(surface, points, count, color);
slouken@2888
   372
    } else {
slouken@5297
   373
        status = SDL_BlendPoints(surface, points, count,
slouken@5297
   374
                                renderer->blendMode,
slouken@5297
   375
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   376
                                renderer->a);
slouken@2888
   377
    }
slouken@5297
   378
slouken@5297
   379
    if (temp) {
slouken@5297
   380
        SDL_stack_free(temp);
slouken@5297
   381
    }
slouken@5297
   382
    return status;
slouken@2888
   383
}
slouken@2888
   384
slouken@2888
   385
static int
slouken@3596
   386
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   387
                   int count)
slouken@1895
   388
{
slouken@5166
   389
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   390
    SDL_Point *temp = NULL;
slouken@5297
   391
    int status;
slouken@3536
   392
slouken@5166
   393
    if (!surface) {
slouken@5147
   394
        return -1;
slouken@5147
   395
    }
slouken@5147
   396
slouken@5297
   397
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   398
        int i;
slouken@5297
   399
        int x = renderer->viewport.x;
slouken@5297
   400
        int y = renderer->viewport.y;
slouken@5297
   401
slouken@5297
   402
        temp = SDL_stack_alloc(SDL_Point, count);
slouken@5297
   403
        for (i = 0; i < count; ++i) {
slouken@5297
   404
            temp[i].x = x + points[i].x;
slouken@5297
   405
            temp[i].y = y + points[i].y;
slouken@5297
   406
        }
slouken@5297
   407
        points = temp;
slouken@5297
   408
    }
slouken@5297
   409
slouken@5166
   410
    /* Draw the lines! */
slouken@5140
   411
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   412
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   413
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   414
                                   renderer->a);
slouken@2888
   415
slouken@5297
   416
        status = SDL_DrawLines(surface, points, count, color);
slouken@2888
   417
    } else {
slouken@5297
   418
        status = SDL_BlendLines(surface, points, count,
slouken@5297
   419
                                renderer->blendMode,
slouken@5297
   420
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   421
                                renderer->a);
slouken@2888
   422
    }
slouken@5297
   423
slouken@5297
   424
    if (temp) {
slouken@5297
   425
        SDL_stack_free(temp);
slouken@5297
   426
    }
slouken@5297
   427
    return status;
slouken@3536
   428
}
slouken@3536
   429
slouken@3536
   430
static int
slouken@5297
   431
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
slouken@3536
   432
{
slouken@5166
   433
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   434
    SDL_Rect *temp = NULL;
slouken@5297
   435
    int status;
slouken@3536
   436
slouken@5166
   437
    if (!surface) {
slouken@5147
   438
        return -1;
slouken@5147
   439
    }
slouken@5147
   440
slouken@5297
   441
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   442
        int i;
slouken@5297
   443
        int x = renderer->viewport.x;
slouken@5297
   444
        int y = renderer->viewport.y;
slouken@5297
   445
slouken@5297
   446
        temp = SDL_stack_alloc(SDL_Rect, count);
slouken@5297
   447
        for (i = 0; i < count; ++i) {
slouken@5297
   448
            temp[i].x = x + rects[i].x;
slouken@5297
   449
            temp[i].y = y + rects[i].y;
slouken@5297
   450
            temp[i].w = rects[i].w;
slouken@5297
   451
            temp[i].h = rects[i].h;
slouken@5297
   452
        }
slouken@5297
   453
        rects = temp;
slouken@5297
   454
    }
slouken@5297
   455
slouken@5140
   456
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   457
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@5166
   458
                                   renderer->r, renderer->g, renderer->b,
slouken@5166
   459
                                   renderer->a);
slouken@5297
   460
        status = SDL_FillRects(surface, rects, count, color);
slouken@5166
   461
    } else {
slouken@5297
   462
        status = SDL_BlendFillRects(surface, rects, count,
slouken@5297
   463
                                    renderer->blendMode,
slouken@5297
   464
                                    renderer->r, renderer->g, renderer->b,
slouken@5297
   465
                                    renderer->a);
slouken@3536
   466
    }
slouken@5297
   467
slouken@5297
   468
    if (temp) {
slouken@5297
   469
        SDL_stack_free(temp);
slouken@5297
   470
    }
slouken@5297
   471
    return status;
slouken@1895
   472
}
slouken@1895
   473
slouken@1895
   474
static int
slouken@1918
   475
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   476
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   477
{
slouken@5166
   478
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   479
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
slouken@5166
   480
    SDL_Rect final_rect = *dstrect;
slouken@1895
   481
slouken@5166
   482
    if (!surface) {
slouken@5147
   483
        return -1;
slouken@5147
   484
    }
ken@5296
   485
slouken@5297
   486
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   487
        final_rect.x += renderer->viewport.x;
slouken@5297
   488
        final_rect.y += renderer->viewport.y;
slouken@5297
   489
    }
ken@5296
   490
    if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
ken@5296
   491
        return SDL_BlitSurface(src, srcrect, surface, &final_rect);
ken@5296
   492
    } else {
ken@5296
   493
        return SDL_BlitScaled(src, srcrect, surface, &final_rect);
ken@5296
   494
    }
slouken@1895
   495
}
slouken@1895
   496
slouken@3427
   497
static int
slouken@3427
   498
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   499
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   500
{
slouken@5166
   501
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   502
    Uint32 src_format;
slouken@5166
   503
    void *src_pixels;
slouken@5297
   504
    SDL_Rect final_rect;
slouken@3427
   505
slouken@5166
   506
    if (!surface) {
slouken@5147
   507
        return -1;
slouken@5147
   508
    }
slouken@5147
   509
slouken@5297
   510
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   511
        final_rect.x = renderer->viewport.x + rect->x;
slouken@5297
   512
        final_rect.y = renderer->viewport.y + rect->y;
slouken@5297
   513
        final_rect.w = rect->w;
slouken@5297
   514
        final_rect.h = rect->h;
slouken@5297
   515
        rect = &final_rect;
slouken@5297
   516
    }
slouken@5297
   517
slouken@5166
   518
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
slouken@5166
   519
        rect->y < 0 || rect->y+rect->h > surface->h) {
slouken@5166
   520
        SDL_SetError("Tried to read outside of surface bounds");
slouken@3427
   521
        return -1;
slouken@3427
   522
    }
slouken@3427
   523
slouken@5297
   524
    src_format = surface->format->format;
slouken@5166
   525
    src_pixels = (void*)((Uint8 *) surface->pixels +
slouken@5166
   526
                    rect->y * surface->pitch +
slouken@5166
   527
                    rect->x * surface->format->BytesPerPixel);
slouken@5166
   528
slouken@5166
   529
    return SDL_ConvertPixels(rect->w, rect->h,
slouken@5166
   530
                             src_format, src_pixels, surface->pitch,
slouken@5166
   531
                             format, pixels, pitch);
slouken@3427
   532
}
slouken@3427
   533
slouken@1895
   534
static void
slouken@1918
   535
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   536
{
slouken@5166
   537
    SDL_Window *window = renderer->window;
slouken@1895
   538
slouken@5166
   539
    if (window) {
slouken@5166
   540
        SDL_UpdateWindowSurface(window);
slouken@5147
   541
    }
slouken@1895
   542
}
slouken@1895
   543
slouken@1895
   544
static void
slouken@1918
   545
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   546
{
slouken@5156
   547
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   548
slouken@5156
   549
    SDL_FreeSurface(surface);
slouken@1895
   550
}
slouken@1895
   551
slouken@1895
   552
static void
slouken@1918
   553
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   554
{
slouken@1918
   555
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   556
slouken@1895
   557
    if (data) {
slouken@1895
   558
        SDL_free(data);
slouken@1895
   559
    }
slouken@1895
   560
    SDL_free(renderer);
slouken@1895
   561
}
slouken@1895
   562
slouken@5226
   563
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   564
slouken@1895
   565
/* vi: set ts=4 sw=4 expandtab: */