src/render/software/SDL_render_sw.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 01 Jun 2012 19:51:08 -0300
changeset 6320 6077a1310907
parent 6283 03a7ceb3487b
child 6332 019660f4cc2b
permissions -rwxr-xr-x
RenderCopyEx,rotation and flipping for all hardware/software backends (#1308)
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"
gabomdq@6320
    27
#include "SDL_hints.h"
slouken@1895
    28
slouken@5163
    29
#include "SDL_draw.h"
slouken@5163
    30
#include "SDL_blendfillrect.h"
slouken@5163
    31
#include "SDL_blendline.h"
slouken@5163
    32
#include "SDL_blendpoint.h"
slouken@5163
    33
#include "SDL_drawline.h"
slouken@5163
    34
#include "SDL_drawpoint.h"
gabomdq@6320
    35
#include "SDL_rotate.h"
slouken@1895
    36
slouken@1895
    37
/* SDL surface based renderer implementation */
slouken@1895
    38
slouken@1918
    39
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5147
    40
static void SW_WindowEvent(SDL_Renderer * renderer,
slouken@5147
    41
                           const SDL_WindowEvent *event);
slouken@1918
    42
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1985
    43
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    44
                                 SDL_Texture * texture);
slouken@1985
    45
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    46
                                 SDL_Texture * texture);
slouken@5141
    47
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@5141
    48
                                  SDL_Texture * texture);
slouken@1985
    49
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    50
                            const SDL_Rect * rect, const void *pixels,
slouken@1985
    51
                            int pitch);
slouken@1918
    52
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
    53
                          const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1918
    54
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@6247
    55
static int SW_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
    56
static int SW_UpdateViewport(SDL_Renderer * renderer);
slouken@5297
    57
static int SW_RenderClear(SDL_Renderer * renderer);
slouken@3596
    58
static int SW_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3596
    59
                               const SDL_Point * points, int count);
slouken@3596
    60
static int SW_RenderDrawLines(SDL_Renderer * renderer,
slouken@3596
    61
                              const SDL_Point * points, int count);
slouken@3596
    62
static int SW_RenderFillRects(SDL_Renderer * renderer,
slouken@5297
    63
                              const SDL_Rect * rects, int count);
slouken@1918
    64
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    65
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
gabomdq@6320
    66
static int SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
gabomdq@6320
    67
                          const SDL_Rect * srcrect, const SDL_Rect * dstrect,
gabomdq@6320
    68
                          const double angle, const SDL_Point * center, const SDL_RendererFlip flip);
slouken@3427
    69
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    70
                               Uint32 format, void * pixels, int pitch);
slouken@1918
    71
static void SW_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    72
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    73
static void SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    74
slouken@1895
    75
slouken@1918
    76
SDL_RenderDriver SW_RenderDriver = {
slouken@1918
    77
    SW_CreateRenderer,
slouken@1895
    78
    {
slouken@1895
    79
     "software",
slouken@6246
    80
     SDL_RENDERER_SOFTWARE | SDL_RENDERER_TARGETTEXTURE,
slouken@5156
    81
     8,
slouken@1895
    82
     {
slouken@1965
    83
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    84
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    85
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    86
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    87
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    88
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    89
      SDL_PIXELFORMAT_ABGR8888,
slouken@5156
    90
      SDL_PIXELFORMAT_BGRA8888
slouken@5156
    91
     },
slouken@1895
    92
     0,
slouken@1895
    93
     0}
slouken@1895
    94
};
slouken@1895
    95
slouken@1895
    96
typedef struct
slouken@1895
    97
{
slouken@5166
    98
    SDL_Surface *surface;
slouken@6246
    99
    SDL_Surface *window;
slouken@1918
   100
} SW_RenderData;
slouken@1895
   101
slouken@1907
   102
slouken@5297
   103
static SDL_Surface *
slouken@5297
   104
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   105
{
slouken@5297
   106
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5297
   107
slouken@5297
   108
    if (!data->surface) {
slouken@6246
   109
        data->surface = data->window;
slouken@6246
   110
    }
slouken@6246
   111
    if (!data->surface) {
slouken@6246
   112
        data->surface = data->window = SDL_GetWindowSurface(renderer->window);
slouken@5297
   113
slouken@5297
   114
        SW_UpdateViewport(renderer);
slouken@5297
   115
    }
slouken@5297
   116
    return data->surface;
slouken@5297
   117
}
slouken@5297
   118
slouken@1895
   119
SDL_Renderer *
slouken@5166
   120
SW_CreateRendererForSurface(SDL_Surface * surface)
slouken@1895
   121
{
slouken@1895
   122
    SDL_Renderer *renderer;
slouken@1918
   123
    SW_RenderData *data;
slouken@1895
   124
slouken@5166
   125
    if (!surface) {
slouken@5166
   126
        SDL_SetError("Can't create renderer for NULL surface");
slouken@1895
   127
        return NULL;
slouken@1895
   128
    }
slouken@1895
   129
slouken@1895
   130
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   131
    if (!renderer) {
slouken@1895
   132
        SDL_OutOfMemory();
slouken@1895
   133
        return NULL;
slouken@1895
   134
    }
slouken@1895
   135
slouken@1920
   136
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   137
    if (!data) {
slouken@1918
   138
        SW_DestroyRenderer(renderer);
slouken@1895
   139
        SDL_OutOfMemory();
slouken@1895
   140
        return NULL;
slouken@1895
   141
    }
slouken@5166
   142
    data->surface = surface;
slouken@5166
   143
slouken@5147
   144
    renderer->WindowEvent = SW_WindowEvent;
slouken@5154
   145
    renderer->CreateTexture = SW_CreateTexture;
slouken@5154
   146
    renderer->SetTextureColorMod = SW_SetTextureColorMod;
slouken@5154
   147
    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
slouken@5154
   148
    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
slouken@5154
   149
    renderer->UpdateTexture = SW_UpdateTexture;
slouken@5154
   150
    renderer->LockTexture = SW_LockTexture;
slouken@5154
   151
    renderer->UnlockTexture = SW_UnlockTexture;
slouken@6247
   152
    renderer->SetRenderTarget = SW_SetRenderTarget;
slouken@5297
   153
    renderer->UpdateViewport = SW_UpdateViewport;
slouken@5297
   154
    renderer->RenderClear = SW_RenderClear;
slouken@3596
   155
    renderer->RenderDrawPoints = SW_RenderDrawPoints;
slouken@3596
   156
    renderer->RenderDrawLines = SW_RenderDrawLines;
slouken@3596
   157
    renderer->RenderFillRects = SW_RenderFillRects;
slouken@1918
   158
    renderer->RenderCopy = SW_RenderCopy;
gabomdq@6320
   159
    renderer->RenderCopyEx = SW_RenderCopyEx;
slouken@3427
   160
    renderer->RenderReadPixels = SW_RenderReadPixels;
slouken@1918
   161
    renderer->RenderPresent = SW_RenderPresent;
slouken@6246
   162
    renderer->DestroyTexture = SW_DestroyTexture;
slouken@1918
   163
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@5154
   164
    renderer->info = SW_RenderDriver.info;
slouken@1895
   165
    renderer->driverdata = data;
slouken@1895
   166
slouken@5297
   167
    SW_ActivateRenderer(renderer);
slouken@5297
   168
slouken@1895
   169
    return renderer;
slouken@1895
   170
}
slouken@1895
   171
slouken@5166
   172
SDL_Renderer *
slouken@5166
   173
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@5166
   174
{
slouken@5166
   175
    SDL_Surface *surface;
slouken@5166
   176
slouken@5166
   177
    surface = SDL_GetWindowSurface(window);
slouken@5166
   178
    if (!surface) {
slouken@5166
   179
        return NULL;
slouken@5166
   180
    }
slouken@5166
   181
    return SW_CreateRendererForSurface(surface);
slouken@5166
   182
}
slouken@5166
   183
slouken@5147
   184
static void
slouken@5147
   185
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   186
{
slouken@1970
   187
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   188
slouken@5276
   189
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
   190
        data->surface = NULL;
slouken@6283
   191
        data->window = NULL;
slouken@1970
   192
    }
slouken@1970
   193
}
slouken@1970
   194
slouken@1970
   195
static int
slouken@1918
   196
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   197
{
slouken@5156
   198
    int bpp;
slouken@5156
   199
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   200
slouken@5156
   201
    if (!SDL_PixelFormatEnumToMasks
slouken@5156
   202
        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5156
   203
        SDL_SetError("Unknown texture format");
slouken@5156
   204
        return -1;
slouken@5156
   205
    }
slouken@1895
   206
slouken@5156
   207
    texture->driverdata =
slouken@5156
   208
        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@5156
   209
                             Bmask, Amask);
slouken@5156
   210
    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
slouken@5156
   211
                           texture->b);
slouken@5156
   212
    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
slouken@5156
   213
    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
slouken@3053
   214
slouken@5156
   215
    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
slouken@5156
   216
        SDL_SetSurfaceRLE(texture->driverdata, 1);
slouken@1895
   217
    }
slouken@1895
   218
slouken@1895
   219
    if (!texture->driverdata) {
slouken@1895
   220
        return -1;
slouken@1895
   221
    }
slouken@1895
   222
    return 0;
slouken@1895
   223
}
slouken@1895
   224
slouken@1895
   225
static int
slouken@1985
   226
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   227
{
slouken@2267
   228
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   229
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
slouken@2267
   230
                                  texture->b);
slouken@1985
   231
}
slouken@1985
   232
slouken@1985
   233
static int
slouken@1985
   234
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   235
{
slouken@2267
   236
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   237
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
slouken@1985
   238
}
slouken@1985
   239
slouken@1985
   240
static int
slouken@1985
   241
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   242
{
slouken@2267
   243
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   244
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
slouken@1985
   245
}
slouken@1985
   246
slouken@1985
   247
static int
slouken@1918
   248
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   249
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   250
{
slouken@5156
   251
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@5156
   252
    Uint8 *src, *dst;
slouken@5156
   253
    int row;
slouken@5156
   254
    size_t length;
slouken@1895
   255
dimitris@5567
   256
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   257
        SDL_LockSurface(surface);
slouken@5156
   258
    src = (Uint8 *) pixels;
slouken@5156
   259
    dst = (Uint8 *) surface->pixels +
slouken@5156
   260
                        rect->y * surface->pitch +
slouken@5156
   261
                        rect->x * surface->format->BytesPerPixel;
slouken@5156
   262
    length = rect->w * surface->format->BytesPerPixel;
slouken@5156
   263
    for (row = 0; row < rect->h; ++row) {
slouken@5156
   264
        SDL_memcpy(dst, src, length);
slouken@5156
   265
        src += pitch;
slouken@5156
   266
        dst += surface->pitch;
slouken@1895
   267
    }
dimitris@5567
   268
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   269
        SDL_UnlockSurface(surface);
slouken@5156
   270
    return 0;
slouken@1895
   271
}
slouken@1895
   272
slouken@1895
   273
static int
slouken@1918
   274
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   275
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   276
{
slouken@5156
   277
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   278
slouken@5156
   279
    *pixels =
slouken@5156
   280
        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@5156
   281
                  rect->x * surface->format->BytesPerPixel);
slouken@5156
   282
    *pitch = surface->pitch;
slouken@5156
   283
    return 0;
slouken@1895
   284
}
slouken@1895
   285
slouken@1895
   286
static void
slouken@1918
   287
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   288
{
slouken@1895
   289
}
slouken@1895
   290
slouken@5297
   291
static int
slouken@6247
   292
SW_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   293
{
slouken@6246
   294
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@6246
   295
slouken@6246
   296
    if (texture ) {
slouken@6246
   297
        data->surface = (SDL_Surface *) texture->driverdata;
slouken@6246
   298
    } else {
slouken@6246
   299
        data->surface = data->window;
slouken@6246
   300
    }
slouken@6246
   301
    return 0;
slouken@6246
   302
}
slouken@6246
   303
slouken@6246
   304
static int
slouken@5297
   305
SW_UpdateViewport(SDL_Renderer * renderer)
slouken@5297
   306
{
slouken@5297
   307
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5297
   308
    SDL_Surface *surface = data->surface;
slouken@5297
   309
slouken@5297
   310
    if (!surface) {
slouken@5297
   311
        /* We'll update the viewport after we recreate the surface */
slouken@5297
   312
        return 0;
slouken@5297
   313
    }
slouken@5297
   314
slouken@5297
   315
    if (!renderer->viewport.w && !renderer->viewport.h) {
slouken@5297
   316
        /* There may be no window, so update the viewport directly */
slouken@5297
   317
        renderer->viewport.w = surface->w;
slouken@5297
   318
        renderer->viewport.h = surface->h;
slouken@5297
   319
    }
slouken@5298
   320
    SDL_SetClipRect(data->surface, &renderer->viewport);
slouken@5297
   321
    return 0;
slouken@5297
   322
}
slouken@5297
   323
slouken@5297
   324
static int
slouken@5297
   325
SW_RenderClear(SDL_Renderer * renderer)
slouken@5224
   326
{
slouken@5224
   327
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   328
    Uint32 color;
slouken@5297
   329
    SDL_Rect clip_rect;
slouken@5224
   330
slouken@5224
   331
    if (!surface) {
slouken@5297
   332
        return -1;
slouken@5224
   333
    }
slouken@5297
   334
slouken@5297
   335
    color = SDL_MapRGBA(surface->format,
slouken@5297
   336
                        renderer->r, renderer->g, renderer->b, renderer->a);
slouken@5297
   337
slouken@5297
   338
    /* By definition the clear ignores the clip rect */
slouken@5297
   339
    clip_rect = surface->clip_rect;
slouken@5297
   340
    SDL_SetClipRect(surface, NULL);
slouken@5297
   341
    SDL_FillRect(surface, NULL, color);
slouken@5297
   342
    SDL_SetClipRect(surface, &clip_rect);
slouken@5297
   343
    return 0;
slouken@5224
   344
}
slouken@5224
   345
slouken@1895
   346
static int
slouken@3596
   347
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   348
                    int count)
slouken@2901
   349
{
slouken@5166
   350
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   351
    SDL_Point *temp = NULL;
slouken@5297
   352
    int status;
slouken@2901
   353
slouken@5166
   354
    if (!surface) {
slouken@5147
   355
        return -1;
slouken@5147
   356
    }
slouken@5147
   357
slouken@5297
   358
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   359
        int i;
slouken@5297
   360
        int x = renderer->viewport.x;
slouken@5297
   361
        int y = renderer->viewport.y;
slouken@5297
   362
slouken@5297
   363
        temp = SDL_stack_alloc(SDL_Point, count);
slouken@5297
   364
        for (i = 0; i < count; ++i) {
slouken@5297
   365
            temp[i].x = x + points[i].x;
slouken@5297
   366
            temp[i].y = y + points[i].x;
slouken@5297
   367
        }
slouken@5297
   368
        points = temp;
slouken@5297
   369
    }
slouken@5297
   370
slouken@3536
   371
    /* Draw the points! */
slouken@5140
   372
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   373
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   374
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   375
                                   renderer->a);
slouken@2888
   376
slouken@5297
   377
        status = SDL_DrawPoints(surface, points, count, color);
slouken@2888
   378
    } else {
slouken@5297
   379
        status = SDL_BlendPoints(surface, points, count,
slouken@5297
   380
                                renderer->blendMode,
slouken@5297
   381
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   382
                                renderer->a);
slouken@2888
   383
    }
slouken@5297
   384
slouken@5297
   385
    if (temp) {
slouken@5297
   386
        SDL_stack_free(temp);
slouken@5297
   387
    }
slouken@5297
   388
    return status;
slouken@2888
   389
}
slouken@2888
   390
slouken@2888
   391
static int
slouken@3596
   392
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   393
                   int count)
slouken@1895
   394
{
slouken@5166
   395
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   396
    SDL_Point *temp = NULL;
slouken@5297
   397
    int status;
slouken@3536
   398
slouken@5166
   399
    if (!surface) {
slouken@5147
   400
        return -1;
slouken@5147
   401
    }
slouken@5147
   402
slouken@5297
   403
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   404
        int i;
slouken@5297
   405
        int x = renderer->viewport.x;
slouken@5297
   406
        int y = renderer->viewport.y;
slouken@5297
   407
slouken@5297
   408
        temp = SDL_stack_alloc(SDL_Point, count);
slouken@5297
   409
        for (i = 0; i < count; ++i) {
slouken@5297
   410
            temp[i].x = x + points[i].x;
slouken@5297
   411
            temp[i].y = y + points[i].y;
slouken@5297
   412
        }
slouken@5297
   413
        points = temp;
slouken@5297
   414
    }
slouken@5297
   415
slouken@5166
   416
    /* Draw the lines! */
slouken@5140
   417
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   418
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   419
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   420
                                   renderer->a);
slouken@2888
   421
slouken@5297
   422
        status = SDL_DrawLines(surface, points, count, color);
slouken@2888
   423
    } else {
slouken@5297
   424
        status = SDL_BlendLines(surface, points, count,
slouken@5297
   425
                                renderer->blendMode,
slouken@5297
   426
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   427
                                renderer->a);
slouken@2888
   428
    }
slouken@5297
   429
slouken@5297
   430
    if (temp) {
slouken@5297
   431
        SDL_stack_free(temp);
slouken@5297
   432
    }
slouken@5297
   433
    return status;
slouken@3536
   434
}
slouken@3536
   435
slouken@3536
   436
static int
slouken@5297
   437
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
slouken@3536
   438
{
slouken@5166
   439
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   440
    SDL_Rect *temp = NULL;
slouken@5297
   441
    int status;
slouken@3536
   442
slouken@5166
   443
    if (!surface) {
slouken@5147
   444
        return -1;
slouken@5147
   445
    }
slouken@5147
   446
slouken@5297
   447
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   448
        int i;
slouken@5297
   449
        int x = renderer->viewport.x;
slouken@5297
   450
        int y = renderer->viewport.y;
slouken@5297
   451
slouken@5297
   452
        temp = SDL_stack_alloc(SDL_Rect, count);
slouken@5297
   453
        for (i = 0; i < count; ++i) {
slouken@5297
   454
            temp[i].x = x + rects[i].x;
slouken@5297
   455
            temp[i].y = y + rects[i].y;
slouken@5297
   456
            temp[i].w = rects[i].w;
slouken@5297
   457
            temp[i].h = rects[i].h;
slouken@5297
   458
        }
slouken@5297
   459
        rects = temp;
slouken@5297
   460
    }
slouken@5297
   461
slouken@5140
   462
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   463
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@5166
   464
                                   renderer->r, renderer->g, renderer->b,
slouken@5166
   465
                                   renderer->a);
slouken@5297
   466
        status = SDL_FillRects(surface, rects, count, color);
slouken@5166
   467
    } else {
slouken@5297
   468
        status = SDL_BlendFillRects(surface, rects, count,
slouken@5297
   469
                                    renderer->blendMode,
slouken@5297
   470
                                    renderer->r, renderer->g, renderer->b,
slouken@5297
   471
                                    renderer->a);
slouken@3536
   472
    }
slouken@5297
   473
slouken@5297
   474
    if (temp) {
slouken@5297
   475
        SDL_stack_free(temp);
slouken@5297
   476
    }
slouken@5297
   477
    return status;
slouken@1895
   478
}
slouken@1895
   479
slouken@1895
   480
static int
slouken@1918
   481
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   482
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   483
{
slouken@5166
   484
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   485
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
slouken@5166
   486
    SDL_Rect final_rect = *dstrect;
slouken@1895
   487
slouken@5166
   488
    if (!surface) {
slouken@5147
   489
        return -1;
slouken@5147
   490
    }
ken@5296
   491
slouken@5297
   492
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   493
        final_rect.x += renderer->viewport.x;
slouken@5297
   494
        final_rect.y += renderer->viewport.y;
slouken@5297
   495
    }
ken@5296
   496
    if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
ken@5296
   497
        return SDL_BlitSurface(src, srcrect, surface, &final_rect);
ken@5296
   498
    } else {
ken@5296
   499
        return SDL_BlitScaled(src, srcrect, surface, &final_rect);
ken@5296
   500
    }
slouken@1895
   501
}
slouken@1895
   502
slouken@3427
   503
static int
gabomdq@6320
   504
GetScaleQuality(void)
gabomdq@6320
   505
{
gabomdq@6320
   506
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
gabomdq@6320
   507
gabomdq@6320
   508
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
gabomdq@6320
   509
        return 0;
gabomdq@6320
   510
    } else {
gabomdq@6320
   511
        return 1;
gabomdq@6320
   512
    }
gabomdq@6320
   513
}
gabomdq@6320
   514
gabomdq@6320
   515
static int
gabomdq@6320
   516
SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
gabomdq@6320
   517
                const SDL_Rect * srcrect, const SDL_Rect * dstrect,
gabomdq@6320
   518
                const double angle, const SDL_Point * center, const SDL_RendererFlip flip)
gabomdq@6320
   519
{
gabomdq@6320
   520
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
gabomdq@6320
   521
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
gabomdq@6320
   522
    SDL_Rect final_rect = *dstrect, tmp_rect;
gabomdq@6320
   523
    SDL_Surface *surface_rotated, *surface_scaled;
gabomdq@6320
   524
    SDL_Point final_rect_center;
gabomdq@6320
   525
    Uint32 colorkey;
gabomdq@6320
   526
    int retval, dstwidth, dstheight, abscenterx, abscentery;
gabomdq@6320
   527
    double cangle, sangle, px, py, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y;
gabomdq@6320
   528
gabomdq@6320
   529
    if (!surface) {
gabomdq@6320
   530
        return -1;
gabomdq@6320
   531
    }
gabomdq@6320
   532
gabomdq@6320
   533
    if (renderer->viewport.x || renderer->viewport.y) {
gabomdq@6320
   534
        final_rect.x += renderer->viewport.x;
gabomdq@6320
   535
        final_rect.y += renderer->viewport.y;
gabomdq@6320
   536
    }
gabomdq@6320
   537
gabomdq@6320
   538
    surface_scaled = SDL_CreateRGBSurface(SDL_SWSURFACE, final_rect.w, final_rect.h, src->format->BitsPerPixel,
gabomdq@6320
   539
                                          src->format->Rmask, src->format->Gmask,
gabomdq@6320
   540
                                          src->format->Bmask, src->format->Amask );
gabomdq@6320
   541
    SDL_GetColorKey(src, &colorkey);
gabomdq@6320
   542
    SDL_SetColorKey(surface_scaled, SDL_TRUE, colorkey);
gabomdq@6320
   543
    tmp_rect = final_rect;
gabomdq@6320
   544
    tmp_rect.x = 0;
gabomdq@6320
   545
    tmp_rect.y = 0;
gabomdq@6320
   546
    if (surface_scaled) {
gabomdq@6320
   547
        retval = SDL_BlitScaled(src, srcrect, surface_scaled, &tmp_rect);
gabomdq@6320
   548
        if (!retval) {
gabomdq@6320
   549
            _rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, -angle, &dstwidth, &dstheight, &cangle, &sangle);
gabomdq@6320
   550
            surface_rotated = _rotateSurface(surface_scaled, -angle, dstwidth/2, dstheight/2, GetScaleQuality(), flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
gabomdq@6320
   551
            if(surface_rotated) {
gabomdq@6320
   552
                /* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
gabomdq@6320
   553
                abscenterx = final_rect.x + center->x;
gabomdq@6320
   554
                abscentery = final_rect.y + center->y;
gabomdq@6320
   555
                /* Compensate the angle inversion to match the behaviour of the other backends */
gabomdq@6320
   556
                sangle = -sangle;
gabomdq@6320
   557
gabomdq@6320
   558
                /* Top Left */
gabomdq@6320
   559
                px = final_rect.x - abscenterx;
gabomdq@6320
   560
                py = final_rect.y - abscentery;
gabomdq@6320
   561
                p1x = px * cangle - py * sangle + abscenterx;
gabomdq@6320
   562
                p1y = px * sangle + py * cangle + abscentery;
gabomdq@6320
   563
gabomdq@6320
   564
                /* Top Right */
gabomdq@6320
   565
                px = final_rect.x + final_rect.w - abscenterx;
gabomdq@6320
   566
                py = final_rect.y - abscentery;
gabomdq@6320
   567
                p2x = px * cangle - py * sangle + abscenterx;
gabomdq@6320
   568
                p2y = px * sangle + py * cangle + abscentery;
gabomdq@6320
   569
gabomdq@6320
   570
                /* Bottom Left */
gabomdq@6320
   571
                px = final_rect.x - abscenterx;
gabomdq@6320
   572
                py = final_rect.y + final_rect.h - abscentery;
gabomdq@6320
   573
                p3x = px * cangle - py * sangle + abscenterx;
gabomdq@6320
   574
                p3y = px * sangle + py * cangle + abscentery;
gabomdq@6320
   575
gabomdq@6320
   576
                /* Bottom Right */
gabomdq@6320
   577
                px = final_rect.x + final_rect.w - abscenterx;
gabomdq@6320
   578
                py = final_rect.y + final_rect.h - abscentery;
gabomdq@6320
   579
                p4x = px * cangle - py * sangle + abscenterx;
gabomdq@6320
   580
                p4y = px * sangle + py * cangle + abscentery;
gabomdq@6320
   581
gabomdq@6320
   582
                tmp_rect.x = (int)MIN(MIN(p1x, p2x), MIN(p3x, p4x));
gabomdq@6320
   583
                tmp_rect.y = (int)MIN(MIN(p1y, p2y), MIN(p3y, p4y));
gabomdq@6320
   584
                tmp_rect.w = dstwidth;
gabomdq@6320
   585
                tmp_rect.h = dstheight;
gabomdq@6320
   586
gabomdq@6320
   587
                retval = SDL_BlitSurface(surface_rotated, NULL, surface, &tmp_rect);
gabomdq@6320
   588
                SDL_FreeSurface(surface_scaled);
gabomdq@6320
   589
                SDL_FreeSurface(surface_rotated);
gabomdq@6320
   590
                return retval;
gabomdq@6320
   591
            }
gabomdq@6320
   592
        }
gabomdq@6320
   593
        return retval;
gabomdq@6320
   594
    }
gabomdq@6320
   595
gabomdq@6320
   596
    return -1;
gabomdq@6320
   597
}
gabomdq@6320
   598
gabomdq@6320
   599
static int
slouken@3427
   600
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   601
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   602
{
slouken@5166
   603
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   604
    Uint32 src_format;
slouken@5166
   605
    void *src_pixels;
slouken@5297
   606
    SDL_Rect final_rect;
slouken@3427
   607
slouken@5166
   608
    if (!surface) {
slouken@5147
   609
        return -1;
slouken@5147
   610
    }
slouken@5147
   611
slouken@5297
   612
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   613
        final_rect.x = renderer->viewport.x + rect->x;
slouken@5297
   614
        final_rect.y = renderer->viewport.y + rect->y;
slouken@5297
   615
        final_rect.w = rect->w;
slouken@5297
   616
        final_rect.h = rect->h;
slouken@5297
   617
        rect = &final_rect;
slouken@5297
   618
    }
slouken@5297
   619
slouken@5166
   620
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
slouken@5166
   621
        rect->y < 0 || rect->y+rect->h > surface->h) {
slouken@5166
   622
        SDL_SetError("Tried to read outside of surface bounds");
slouken@3427
   623
        return -1;
slouken@3427
   624
    }
slouken@3427
   625
slouken@5297
   626
    src_format = surface->format->format;
slouken@5166
   627
    src_pixels = (void*)((Uint8 *) surface->pixels +
slouken@5166
   628
                    rect->y * surface->pitch +
slouken@5166
   629
                    rect->x * surface->format->BytesPerPixel);
slouken@5166
   630
slouken@5166
   631
    return SDL_ConvertPixels(rect->w, rect->h,
slouken@5166
   632
                             src_format, src_pixels, surface->pitch,
slouken@5166
   633
                             format, pixels, pitch);
slouken@3427
   634
}
slouken@3427
   635
slouken@1895
   636
static void
slouken@1918
   637
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   638
{
slouken@5166
   639
    SDL_Window *window = renderer->window;
slouken@1895
   640
slouken@5166
   641
    if (window) {
slouken@5166
   642
        SDL_UpdateWindowSurface(window);
slouken@5147
   643
    }
slouken@1895
   644
}
slouken@1895
   645
slouken@1895
   646
static void
slouken@1918
   647
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   648
{
slouken@5156
   649
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   650
slouken@5156
   651
    SDL_FreeSurface(surface);
slouken@1895
   652
}
slouken@1895
   653
slouken@1895
   654
static void
slouken@1918
   655
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   656
{
slouken@1918
   657
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   658
slouken@1895
   659
    if (data) {
slouken@1895
   660
        SDL_free(data);
slouken@1895
   661
    }
slouken@1895
   662
    SDL_free(renderer);
slouken@1895
   663
}
slouken@1895
   664
slouken@5226
   665
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   666
slouken@1895
   667
/* vi: set ts=4 sw=4 expandtab: */