src/render/software/SDL_render_sw.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Jul 2014 09:04:55 -0700
changeset 9008 f061a86fbb08
parent 8907 e4c67eb79af8
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed bug 2640 - Unable to SDL_SetRenderTarget to original surface for software renderer without a window

Damian Kaczmarek

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