src/render/software/SDL_render_sw.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 31 Mar 2013 12:48:50 -0400
changeset 7037 3fedf1f25b94
parent 6885 700f1b25f77f
child 7141 e276777b4247
permissions -rw-r--r--
Make SDL_SetError and friends unconditionally return -1.

This lets us change things like this...

if (Failed) {
SDL_SetError("We failed");
return -1;
}

...into this...

if (Failed) {
return SDL_SetError("We failed");
}


Fixes Bugzilla #1778.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@6528
    59
                               const SDL_FPoint * points, int count);
slouken@3596
    60
static int SW_RenderDrawLines(SDL_Renderer * renderer,
slouken@6528
    61
                              const SDL_FPoint * points, int count);
slouken@3596
    62
static int SW_RenderFillRects(SDL_Renderer * renderer,
slouken@6528
    63
                              const SDL_FRect * rects, int count);
slouken@1918
    64
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
    65
                         const SDL_Rect * srcrect, const SDL_FRect * dstrect);
gabomdq@6320
    66
static int SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
    67
                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
    68
                          const double angle, const SDL_FPoint * 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)) {
icculus@7037
   203
        return SDL_SetError("Unknown texture format");
slouken@5156
   204
    }
slouken@1895
   205
slouken@5156
   206
    texture->driverdata =
slouken@5156
   207
        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@5156
   208
                             Bmask, Amask);
slouken@5156
   209
    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
slouken@5156
   210
                           texture->b);
slouken@5156
   211
    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
slouken@5156
   212
    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
slouken@3053
   213
slouken@5156
   214
    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
slouken@5156
   215
        SDL_SetSurfaceRLE(texture->driverdata, 1);
slouken@1895
   216
    }
slouken@1895
   217
slouken@1895
   218
    if (!texture->driverdata) {
slouken@1895
   219
        return -1;
slouken@1895
   220
    }
slouken@1895
   221
    return 0;
slouken@1895
   222
}
slouken@1895
   223
slouken@1895
   224
static int
slouken@1985
   225
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   226
{
slouken@2267
   227
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   228
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
slouken@2267
   229
                                  texture->b);
slouken@1985
   230
}
slouken@1985
   231
slouken@1985
   232
static int
slouken@1985
   233
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   234
{
slouken@2267
   235
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   236
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
slouken@1985
   237
}
slouken@1985
   238
slouken@1985
   239
static int
slouken@1985
   240
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   241
{
slouken@2267
   242
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   243
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
slouken@1985
   244
}
slouken@1985
   245
slouken@1985
   246
static int
slouken@1918
   247
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   248
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   249
{
slouken@5156
   250
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@5156
   251
    Uint8 *src, *dst;
slouken@5156
   252
    int row;
slouken@5156
   253
    size_t length;
slouken@1895
   254
dimitris@5567
   255
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   256
        SDL_LockSurface(surface);
slouken@5156
   257
    src = (Uint8 *) pixels;
slouken@5156
   258
    dst = (Uint8 *) surface->pixels +
slouken@5156
   259
                        rect->y * surface->pitch +
slouken@5156
   260
                        rect->x * surface->format->BytesPerPixel;
slouken@5156
   261
    length = rect->w * surface->format->BytesPerPixel;
slouken@5156
   262
    for (row = 0; row < rect->h; ++row) {
slouken@5156
   263
        SDL_memcpy(dst, src, length);
slouken@5156
   264
        src += pitch;
slouken@5156
   265
        dst += surface->pitch;
slouken@1895
   266
    }
dimitris@5567
   267
    if(SDL_MUSTLOCK(surface))
dimitris@5567
   268
        SDL_UnlockSurface(surface);
slouken@5156
   269
    return 0;
slouken@1895
   270
}
slouken@1895
   271
slouken@1895
   272
static int
slouken@1918
   273
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   274
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   275
{
slouken@5156
   276
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   277
slouken@5156
   278
    *pixels =
slouken@5156
   279
        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@5156
   280
                  rect->x * surface->format->BytesPerPixel);
slouken@5156
   281
    *pitch = surface->pitch;
slouken@5156
   282
    return 0;
slouken@1895
   283
}
slouken@1895
   284
slouken@1895
   285
static void
slouken@1918
   286
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   287
{
slouken@1895
   288
}
slouken@1895
   289
slouken@5297
   290
static int
slouken@6247
   291
SW_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   292
{
slouken@6246
   293
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@6246
   294
slouken@6246
   295
    if (texture ) {
slouken@6246
   296
        data->surface = (SDL_Surface *) texture->driverdata;
slouken@6246
   297
    } else {
slouken@6246
   298
        data->surface = data->window;
slouken@6246
   299
    }
slouken@6246
   300
    return 0;
slouken@6246
   301
}
slouken@6246
   302
slouken@6246
   303
static int
slouken@5297
   304
SW_UpdateViewport(SDL_Renderer * renderer)
slouken@5297
   305
{
slouken@5297
   306
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5297
   307
    SDL_Surface *surface = data->surface;
slouken@5297
   308
slouken@5297
   309
    if (!surface) {
slouken@5297
   310
        /* We'll update the viewport after we recreate the surface */
slouken@5297
   311
        return 0;
slouken@5297
   312
    }
slouken@5297
   313
slouken@5297
   314
    if (!renderer->viewport.w && !renderer->viewport.h) {
slouken@5297
   315
        /* There may be no window, so update the viewport directly */
slouken@5297
   316
        renderer->viewport.w = surface->w;
slouken@5297
   317
        renderer->viewport.h = surface->h;
slouken@5297
   318
    }
slouken@5298
   319
    SDL_SetClipRect(data->surface, &renderer->viewport);
slouken@5297
   320
    return 0;
slouken@5297
   321
}
slouken@5297
   322
slouken@5297
   323
static int
slouken@5297
   324
SW_RenderClear(SDL_Renderer * renderer)
slouken@5224
   325
{
slouken@5224
   326
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5297
   327
    Uint32 color;
slouken@5297
   328
    SDL_Rect clip_rect;
slouken@5224
   329
slouken@5224
   330
    if (!surface) {
slouken@5297
   331
        return -1;
slouken@5224
   332
    }
slouken@5297
   333
slouken@5297
   334
    color = SDL_MapRGBA(surface->format,
slouken@5297
   335
                        renderer->r, renderer->g, renderer->b, renderer->a);
slouken@5297
   336
slouken@5297
   337
    /* By definition the clear ignores the clip rect */
slouken@5297
   338
    clip_rect = surface->clip_rect;
slouken@5297
   339
    SDL_SetClipRect(surface, NULL);
slouken@5297
   340
    SDL_FillRect(surface, NULL, color);
slouken@5297
   341
    SDL_SetClipRect(surface, &clip_rect);
slouken@5297
   342
    return 0;
slouken@5224
   343
}
slouken@5224
   344
slouken@1895
   345
static int
slouken@6528
   346
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3596
   347
                    int count)
slouken@2901
   348
{
slouken@5166
   349
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@6528
   350
    SDL_Point *final_points;
slouken@6528
   351
    int i, status;
slouken@2901
   352
slouken@5166
   353
    if (!surface) {
slouken@5147
   354
        return -1;
slouken@5147
   355
    }
slouken@5147
   356
slouken@6528
   357
    final_points = SDL_stack_alloc(SDL_Point, count);
slouken@6528
   358
    if (!final_points) {
icculus@7037
   359
        return SDL_OutOfMemory();
slouken@6528
   360
    }
slouken@5297
   361
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@6529
   362
        int x = renderer->viewport.x;
slouken@6529
   363
        int y = renderer->viewport.y;
slouken@5297
   364
slouken@5297
   365
        for (i = 0; i < count; ++i) {
slouken@6528
   366
            final_points[i].x = (int)(x + points[i].x);
slouken@6528
   367
            final_points[i].y = (int)(y + points[i].y);
slouken@5297
   368
        }
slouken@6528
   369
    } else {
slouken@6528
   370
        for (i = 0; i < count; ++i) {
slouken@6528
   371
            final_points[i].x = (int)points[i].x;
slouken@6528
   372
            final_points[i].y = (int)points[i].y;
slouken@6528
   373
        }
slouken@5297
   374
    }
slouken@5297
   375
slouken@3536
   376
    /* Draw the points! */
slouken@5140
   377
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   378
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   379
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   380
                                   renderer->a);
slouken@2888
   381
slouken@6528
   382
        status = SDL_DrawPoints(surface, final_points, count, color);
slouken@2888
   383
    } else {
slouken@6528
   384
        status = SDL_BlendPoints(surface, final_points, count,
slouken@5297
   385
                                renderer->blendMode,
slouken@5297
   386
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   387
                                renderer->a);
slouken@2888
   388
    }
slouken@6528
   389
    SDL_stack_free(final_points);
slouken@5297
   390
slouken@5297
   391
    return status;
slouken@2888
   392
}
slouken@2888
   393
slouken@2888
   394
static int
slouken@6528
   395
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3596
   396
                   int count)
slouken@1895
   397
{
slouken@5166
   398
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@6528
   399
    SDL_Point *final_points;
slouken@6528
   400
    int i, status;
slouken@3536
   401
slouken@5166
   402
    if (!surface) {
slouken@5147
   403
        return -1;
slouken@5147
   404
    }
slouken@5147
   405
slouken@6528
   406
    final_points = SDL_stack_alloc(SDL_Point, count);
slouken@6528
   407
    if (!final_points) {
icculus@7037
   408
        return SDL_OutOfMemory();
slouken@6528
   409
    }
slouken@5297
   410
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@6529
   411
        int x = renderer->viewport.x;
slouken@6529
   412
        int y = renderer->viewport.y;
slouken@5297
   413
slouken@5297
   414
        for (i = 0; i < count; ++i) {
slouken@6528
   415
            final_points[i].x = (int)(x + points[i].x);
slouken@6528
   416
            final_points[i].y = (int)(y + points[i].y);
slouken@5297
   417
        }
slouken@6528
   418
    } else {
slouken@6528
   419
        for (i = 0; i < count; ++i) {
slouken@6528
   420
            final_points[i].x = (int)points[i].x;
slouken@6528
   421
            final_points[i].y = (int)points[i].y;
slouken@6528
   422
        }
slouken@5297
   423
    }
slouken@5297
   424
slouken@5166
   425
    /* Draw the lines! */
slouken@5140
   426
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   427
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   428
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   429
                                   renderer->a);
slouken@2888
   430
slouken@6528
   431
        status = SDL_DrawLines(surface, final_points, count, color);
slouken@2888
   432
    } else {
slouken@6528
   433
        status = SDL_BlendLines(surface, final_points, count,
slouken@5297
   434
                                renderer->blendMode,
slouken@5297
   435
                                renderer->r, renderer->g, renderer->b,
slouken@5297
   436
                                renderer->a);
slouken@2888
   437
    }
slouken@6528
   438
    SDL_stack_free(final_points);
slouken@5297
   439
slouken@5297
   440
    return status;
slouken@3536
   441
}
slouken@3536
   442
slouken@3536
   443
static int
slouken@6528
   444
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
slouken@3536
   445
{
slouken@5166
   446
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@6528
   447
    SDL_Rect *final_rects;
slouken@6528
   448
    int i, status;
slouken@6528
   449
slouken@6528
   450
    if (!surface) {
slouken@6528
   451
        return -1;
slouken@6528
   452
    }
slouken@6528
   453
slouken@6528
   454
    final_rects = SDL_stack_alloc(SDL_Rect, count);
slouken@6528
   455
    if (!final_rects) {
icculus@7037
   456
        return SDL_OutOfMemory();
slouken@6528
   457
    }
slouken@6528
   458
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@6529
   459
        int x = renderer->viewport.x;
slouken@6529
   460
        int y = renderer->viewport.y;
slouken@6528
   461
slouken@6528
   462
        for (i = 0; i < count; ++i) {
slouken@6528
   463
            final_rects[i].x = (int)(x + rects[i].x);
slouken@6528
   464
            final_rects[i].y = (int)(y + rects[i].y);
slouken@6528
   465
            final_rects[i].w = SDL_max((int)rects[i].w, 1);
slouken@6528
   466
            final_rects[i].h = SDL_max((int)rects[i].h, 1);
slouken@6528
   467
        }
slouken@6528
   468
    } else {
slouken@6528
   469
        for (i = 0; i < count; ++i) {
slouken@6528
   470
            final_rects[i].x = (int)rects[i].x;
slouken@6528
   471
            final_rects[i].y = (int)rects[i].y;
slouken@6528
   472
            final_rects[i].w = SDL_max((int)rects[i].w, 1);
slouken@6528
   473
            final_rects[i].h = SDL_max((int)rects[i].h, 1);
slouken@6528
   474
        }
slouken@6528
   475
    }
slouken@6528
   476
slouken@6528
   477
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@6528
   478
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@6528
   479
                                   renderer->r, renderer->g, renderer->b,
slouken@6528
   480
                                   renderer->a);
slouken@6528
   481
        status = SDL_FillRects(surface, final_rects, count, color);
slouken@6528
   482
    } else {
slouken@6528
   483
        status = SDL_BlendFillRects(surface, final_rects, count,
slouken@6528
   484
                                    renderer->blendMode,
slouken@6528
   485
                                    renderer->r, renderer->g, renderer->b,
slouken@6528
   486
                                    renderer->a);
slouken@6528
   487
    }
slouken@6528
   488
    SDL_stack_free(final_rects);
slouken@6528
   489
slouken@6528
   490
    return status;
slouken@6528
   491
}
slouken@6528
   492
slouken@6528
   493
static int
slouken@6528
   494
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   495
              const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@6528
   496
{
slouken@6528
   497
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@6528
   498
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
slouken@6528
   499
    SDL_Rect final_rect;
slouken@3536
   500
slouken@5166
   501
    if (!surface) {
slouken@5147
   502
        return -1;
slouken@5147
   503
    }
slouken@5147
   504
slouken@5297
   505
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@6529
   506
        final_rect.x = (int)(renderer->viewport.x + dstrect->x);
slouken@6529
   507
        final_rect.y = (int)(renderer->viewport.y + dstrect->y);
slouken@6528
   508
    } else {
slouken@6528
   509
        final_rect.x = (int)dstrect->x;
slouken@6528
   510
        final_rect.y = (int)dstrect->y;
slouken@6528
   511
    }
slouken@6528
   512
    final_rect.w = (int)dstrect->w;
slouken@6528
   513
    final_rect.h = (int)dstrect->h;
slouken@5297
   514
ken@5296
   515
    if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
ken@5296
   516
        return SDL_BlitSurface(src, srcrect, surface, &final_rect);
ken@5296
   517
    } else {
ken@5296
   518
        return SDL_BlitScaled(src, srcrect, surface, &final_rect);
ken@5296
   519
    }
slouken@1895
   520
}
slouken@1895
   521
slouken@3427
   522
static int
gabomdq@6320
   523
GetScaleQuality(void)
gabomdq@6320
   524
{
gabomdq@6320
   525
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
gabomdq@6320
   526
gabomdq@6320
   527
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
gabomdq@6320
   528
        return 0;
gabomdq@6320
   529
    } else {
gabomdq@6320
   530
        return 1;
gabomdq@6320
   531
    }
gabomdq@6320
   532
}
gabomdq@6320
   533
gabomdq@6320
   534
static int
gabomdq@6320
   535
SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   536
                const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
   537
                const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
gabomdq@6320
   538
{
gabomdq@6320
   539
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
gabomdq@6320
   540
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
slouken@6528
   541
    SDL_Rect final_rect, tmp_rect;
gabomdq@6320
   542
    SDL_Surface *surface_rotated, *surface_scaled;
gabomdq@6320
   543
    Uint32 colorkey;
gabomdq@6320
   544
    int retval, dstwidth, dstheight, abscenterx, abscentery;
gabomdq@6320
   545
    double cangle, sangle, px, py, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y;
gabomdq@6320
   546
gabomdq@6320
   547
    if (!surface) {
gabomdq@6320
   548
        return -1;
gabomdq@6320
   549
    }
gabomdq@6320
   550
gabomdq@6320
   551
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@6529
   552
        final_rect.x = (int)(renderer->viewport.x + dstrect->x);
slouken@6529
   553
        final_rect.y = (int)(renderer->viewport.y + dstrect->y);
slouken@6528
   554
    } else {
slouken@6528
   555
        final_rect.x = (int)dstrect->x;
slouken@6528
   556
        final_rect.y = (int)dstrect->y;
gabomdq@6320
   557
    }
slouken@6528
   558
    final_rect.w = (int)dstrect->w;
slouken@6528
   559
    final_rect.h = (int)dstrect->h;
gabomdq@6320
   560
gabomdq@6320
   561
    surface_scaled = SDL_CreateRGBSurface(SDL_SWSURFACE, final_rect.w, final_rect.h, src->format->BitsPerPixel,
gabomdq@6320
   562
                                          src->format->Rmask, src->format->Gmask,
gabomdq@6320
   563
                                          src->format->Bmask, src->format->Amask );
gabomdq@6320
   564
    if (surface_scaled) {
slouken@6528
   565
        SDL_GetColorKey(src, &colorkey);
slouken@6528
   566
        SDL_SetColorKey(surface_scaled, SDL_TRUE, colorkey);
slouken@6528
   567
        tmp_rect = final_rect;
slouken@6528
   568
        tmp_rect.x = 0;
slouken@6528
   569
        tmp_rect.y = 0;
slouken@6528
   570
gabomdq@6320
   571
        retval = SDL_BlitScaled(src, srcrect, surface_scaled, &tmp_rect);
gabomdq@6320
   572
        if (!retval) {
gabomdq@6320
   573
            _rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, -angle, &dstwidth, &dstheight, &cangle, &sangle);
gabomdq@6320
   574
            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
   575
            if(surface_rotated) {
gabomdq@6320
   576
                /* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
slouken@6528
   577
                abscenterx = final_rect.x + (int)center->x;
slouken@6528
   578
                abscentery = final_rect.y + (int)center->y;
gabomdq@6320
   579
                /* Compensate the angle inversion to match the behaviour of the other backends */
gabomdq@6320
   580
                sangle = -sangle;
gabomdq@6320
   581
gabomdq@6320
   582
                /* Top Left */
gabomdq@6320
   583
                px = final_rect.x - abscenterx;
gabomdq@6320
   584
                py = final_rect.y - abscentery;
gabomdq@6320
   585
                p1x = px * cangle - py * sangle + abscenterx;
gabomdq@6320
   586
                p1y = px * sangle + py * cangle + abscentery;
gabomdq@6320
   587
gabomdq@6320
   588
                /* Top Right */
gabomdq@6320
   589
                px = final_rect.x + final_rect.w - abscenterx;
gabomdq@6320
   590
                py = final_rect.y - abscentery;
gabomdq@6320
   591
                p2x = px * cangle - py * sangle + abscenterx;
gabomdq@6320
   592
                p2y = px * sangle + py * cangle + abscentery;
gabomdq@6320
   593
gabomdq@6320
   594
                /* Bottom Left */
gabomdq@6320
   595
                px = final_rect.x - abscenterx;
gabomdq@6320
   596
                py = final_rect.y + final_rect.h - abscentery;
gabomdq@6320
   597
                p3x = px * cangle - py * sangle + abscenterx;
gabomdq@6320
   598
                p3y = px * sangle + py * cangle + abscentery;
gabomdq@6320
   599
gabomdq@6320
   600
                /* Bottom Right */
gabomdq@6320
   601
                px = final_rect.x + final_rect.w - abscenterx;
gabomdq@6320
   602
                py = final_rect.y + final_rect.h - abscentery;
gabomdq@6320
   603
                p4x = px * cangle - py * sangle + abscenterx;
gabomdq@6320
   604
                p4y = px * sangle + py * cangle + abscentery;
gabomdq@6320
   605
gabomdq@6320
   606
                tmp_rect.x = (int)MIN(MIN(p1x, p2x), MIN(p3x, p4x));
gabomdq@6320
   607
                tmp_rect.y = (int)MIN(MIN(p1y, p2y), MIN(p3y, p4y));
gabomdq@6320
   608
                tmp_rect.w = dstwidth;
gabomdq@6320
   609
                tmp_rect.h = dstheight;
gabomdq@6320
   610
gabomdq@6320
   611
                retval = SDL_BlitSurface(surface_rotated, NULL, surface, &tmp_rect);
gabomdq@6320
   612
                SDL_FreeSurface(surface_scaled);
gabomdq@6320
   613
                SDL_FreeSurface(surface_rotated);
gabomdq@6320
   614
                return retval;
gabomdq@6320
   615
            }
gabomdq@6320
   616
        }
gabomdq@6320
   617
        return retval;
gabomdq@6320
   618
    }
gabomdq@6320
   619
gabomdq@6320
   620
    return -1;
gabomdq@6320
   621
}
gabomdq@6320
   622
gabomdq@6320
   623
static int
slouken@3427
   624
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   625
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   626
{
slouken@5166
   627
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   628
    Uint32 src_format;
slouken@5166
   629
    void *src_pixels;
slouken@5297
   630
    SDL_Rect final_rect;
slouken@3427
   631
slouken@5166
   632
    if (!surface) {
slouken@5147
   633
        return -1;
slouken@5147
   634
    }
slouken@5147
   635
slouken@5297
   636
    if (renderer->viewport.x || renderer->viewport.y) {
slouken@5297
   637
        final_rect.x = renderer->viewport.x + rect->x;
slouken@5297
   638
        final_rect.y = renderer->viewport.y + rect->y;
slouken@5297
   639
        final_rect.w = rect->w;
slouken@5297
   640
        final_rect.h = rect->h;
slouken@5297
   641
        rect = &final_rect;
slouken@5297
   642
    }
slouken@5297
   643
slouken@5166
   644
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
slouken@5166
   645
        rect->y < 0 || rect->y+rect->h > surface->h) {
icculus@7037
   646
        return SDL_SetError("Tried to read outside of surface bounds");
slouken@3427
   647
    }
slouken@3427
   648
slouken@5297
   649
    src_format = surface->format->format;
slouken@5166
   650
    src_pixels = (void*)((Uint8 *) surface->pixels +
slouken@5166
   651
                    rect->y * surface->pitch +
slouken@5166
   652
                    rect->x * surface->format->BytesPerPixel);
slouken@5166
   653
slouken@5166
   654
    return SDL_ConvertPixels(rect->w, rect->h,
slouken@5166
   655
                             src_format, src_pixels, surface->pitch,
slouken@5166
   656
                             format, pixels, pitch);
slouken@3427
   657
}
slouken@3427
   658
slouken@1895
   659
static void
slouken@1918
   660
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   661
{
slouken@5166
   662
    SDL_Window *window = renderer->window;
slouken@1895
   663
slouken@5166
   664
    if (window) {
slouken@5166
   665
        SDL_UpdateWindowSurface(window);
slouken@5147
   666
    }
slouken@1895
   667
}
slouken@1895
   668
slouken@1895
   669
static void
slouken@1918
   670
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   671
{
slouken@5156
   672
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   673
slouken@5156
   674
    SDL_FreeSurface(surface);
slouken@1895
   675
}
slouken@1895
   676
slouken@1895
   677
static void
slouken@1918
   678
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   679
{
slouken@1918
   680
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   681
slouken@1895
   682
    if (data) {
slouken@1895
   683
        SDL_free(data);
slouken@1895
   684
    }
slouken@1895
   685
    SDL_free(renderer);
slouken@1895
   686
}
slouken@1895
   687
slouken@5226
   688
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   689
slouken@1895
   690
/* vi: set ts=4 sw=4 expandtab: */