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