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