src/render/software/SDL_render_sw.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 7859 af63b63e7aac
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

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