src/render/software/SDL_render_sw.c
author Dimitris Zenios <dimitris.zenios@gmail.com>
Fri, 10 Jun 2011 12:13:06 +0300
changeset 5567 441c3036440c
parent 5535 96594ac5fd1a
child 6044 35448a5ea044
permissions -rw-r--r--
Fixes a bug in SDL_UpdateTexture when using SDL_RENDERER_SOFTWARE and texture access is SDL_TEXTUREACCESS_STATIC
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
     2
  Simple DirectMedia Layer
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
     7
  arising from the use of this software.
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    11
  freely, subject to the following restrictions:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5337
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
5226
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
    23
#if !SDL_RENDER_DISABLED
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
    24
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
    25
#include "../SDL_sysrender.h"
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
5163
d72793305335 Making the API simpler, moved the surface drawing functions to the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
    27
#include "SDL_draw.h"
d72793305335 Making the API simpler, moved the surface drawing functions to the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
    28
#include "SDL_blendfillrect.h"
d72793305335 Making the API simpler, moved the surface drawing functions to the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
    29
#include "SDL_blendline.h"
d72793305335 Making the API simpler, moved the surface drawing functions to the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
    30
#include "SDL_blendpoint.h"
d72793305335 Making the API simpler, moved the surface drawing functions to the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
    31
#include "SDL_drawline.h"
d72793305335 Making the API simpler, moved the surface drawing functions to the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
    32
#include "SDL_drawpoint.h"
d72793305335 Making the API simpler, moved the surface drawing functions to the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5157
diff changeset
    33
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
/* SDL surface based renderer implementation */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    37
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
    38
static void SW_WindowEvent(SDL_Renderer * renderer,
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
    39
                           const SDL_WindowEvent *event);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    40
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
    41
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
    42
                                 SDL_Texture * texture);
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
    43
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
    44
                                 SDL_Texture * texture);
5141
31e7f523ab3d Making the API simpler, texture color and alpha modulation are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
    45
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
31e7f523ab3d Making the API simpler, texture color and alpha modulation are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
    46
                                  SDL_Texture * texture);
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
    47
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
    48
                            const SDL_Rect * rect, const void *pixels,
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
    49
                            int pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    50
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    51
                          const SDL_Rect * rect, void **pixels, int *pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    52
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
    53
static int SW_UpdateViewport(SDL_Renderer * renderer);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
    54
static int SW_RenderClear(SDL_Renderer * renderer);
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    55
static int SW_RenderDrawPoints(SDL_Renderer * renderer,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    56
                               const SDL_Point * points, int count);
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    57
static int SW_RenderDrawLines(SDL_Renderer * renderer,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    58
                              const SDL_Point * points, int count);
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
    59
static int SW_RenderFillRects(SDL_Renderer * renderer,
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
    60
                              const SDL_Rect * rects, int count);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    61
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
    62
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
3427
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
    63
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3427
diff changeset
    64
                               Uint32 format, void * pixels, int pitch);
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    65
static void SW_RenderPresent(SDL_Renderer * renderer);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    66
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    67
static void SW_DestroyRenderer(SDL_Renderer * renderer);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    70
SDL_RenderDriver SW_RenderDriver = {
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    71
    SW_CreateRenderer,
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
    {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
     "software",
5337
d5d312a82d40 Allow the application to explicitly request a software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5332
diff changeset
    74
     SDL_RENDERER_SOFTWARE,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    75
     8,
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
     {
1965
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1930
diff changeset
    77
      SDL_PIXELFORMAT_RGB555,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1930
diff changeset
    78
      SDL_PIXELFORMAT_RGB565,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1930
diff changeset
    79
      SDL_PIXELFORMAT_RGB888,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1930
diff changeset
    80
      SDL_PIXELFORMAT_BGR888,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1930
diff changeset
    81
      SDL_PIXELFORMAT_ARGB8888,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1930
diff changeset
    82
      SDL_PIXELFORMAT_RGBA8888,
a788656ca29a SDL constants are all uppercase.
Sam Lantinga <slouken@libsdl.org>
parents: 1930
diff changeset
    83
      SDL_PIXELFORMAT_ABGR8888,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    84
      SDL_PIXELFORMAT_BGRA8888
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
    85
     },
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
     0,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
     0}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
};
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
typedef struct
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
{
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
    92
    SDL_Surface *surface;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
    93
} SW_RenderData;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
1907
06c27a737b7a Streamlined the API a bit and optimized the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 1897
diff changeset
    95
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
    96
static SDL_Surface *
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
    97
SW_ActivateRenderer(SDL_Renderer * renderer)
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
    98
{
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
    99
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   100
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   101
    if (!data->surface) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   102
        data->surface = SDL_GetWindowSurface(renderer->window);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   103
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   104
        SW_UpdateViewport(renderer);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   105
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   106
    return data->surface;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   107
}
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   108
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
SDL_Renderer *
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   110
SW_CreateRendererForSurface(SDL_Surface * surface)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
    SDL_Renderer *renderer;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   113
    SW_RenderData *data;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   115
    if (!surface) {
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   116
        SDL_SetError("Can't create renderer for NULL surface");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
        return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
    if (!renderer) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
        SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
        return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1918
diff changeset
   126
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
    if (!data) {
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   128
        SW_DestroyRenderer(renderer);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
        SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
        return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
    }
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   132
    data->surface = surface;
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   133
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   134
    renderer->WindowEvent = SW_WindowEvent;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   135
    renderer->CreateTexture = SW_CreateTexture;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   136
    renderer->SetTextureColorMod = SW_SetTextureColorMod;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   137
    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   138
    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   139
    renderer->UpdateTexture = SW_UpdateTexture;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   140
    renderer->LockTexture = SW_LockTexture;
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   141
    renderer->UnlockTexture = SW_UnlockTexture;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   142
    renderer->UpdateViewport = SW_UpdateViewport;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   143
    renderer->DestroyTexture = SW_DestroyTexture;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   144
    renderer->RenderClear = SW_RenderClear;
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   145
    renderer->RenderDrawPoints = SW_RenderDrawPoints;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   146
    renderer->RenderDrawLines = SW_RenderDrawLines;
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   147
    renderer->RenderFillRects = SW_RenderFillRects;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   148
    renderer->RenderCopy = SW_RenderCopy;
3427
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   149
    renderer->RenderReadPixels = SW_RenderReadPixels;
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   150
    renderer->RenderPresent = SW_RenderPresent;
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   151
    renderer->DestroyRenderer = SW_DestroyRenderer;
5154
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 5149
diff changeset
   152
    renderer->info = SW_RenderDriver.info;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
    renderer->driverdata = data;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   155
    SW_ActivateRenderer(renderer);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   156
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
    return renderer;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   160
SDL_Renderer *
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   161
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   162
{
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   163
    SDL_Surface *surface;
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   164
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   165
    surface = SDL_GetWindowSurface(window);
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   166
    if (!surface) {
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   167
        return NULL;
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   168
    }
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   169
    return SW_CreateRendererForSurface(surface);
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   170
}
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   171
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   172
static void
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   173
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   174
{
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   175
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   176
5276
8e421890cdb8 Fixed bug #1117
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   177
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   178
        data->surface = NULL;
1970
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   179
    }
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   180
}
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   181
db3ba6c0d0df Allow the render context to do necessary work when the video mode changes.
Sam Lantinga <slouken@libsdl.org>
parents: 1965
diff changeset
   182
static int
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   183
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
{
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   185
    int bpp;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   186
    Uint32 Rmask, Gmask, Bmask, Amask;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   187
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   188
    if (!SDL_PixelFormatEnumToMasks
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   189
        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   190
        SDL_SetError("Unknown texture format");
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   191
        return -1;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   192
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   194
    texture->driverdata =
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   195
        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   196
                             Bmask, Amask);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   197
    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   198
                           texture->b);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   199
    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   200
    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   202
    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   203
        SDL_SetSurfaceRLE(texture->driverdata, 1);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
    if (!texture->driverdata) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
    return 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
static int
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   213
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   214
{
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2262
diff changeset
   215
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2262
diff changeset
   216
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2262
diff changeset
   217
                                  texture->b);
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   218
}
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   219
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   220
static int
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   221
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   222
{
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2262
diff changeset
   223
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2262
diff changeset
   224
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   225
}
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   226
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   227
static int
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   228
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   229
{
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2262
diff changeset
   230
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2262
diff changeset
   231
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   232
}
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   233
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   234
static int
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   235
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   236
                 const SDL_Rect * rect, const void *pixels, int pitch)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
{
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   238
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   239
    Uint8 *src, *dst;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   240
    int row;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   241
    size_t length;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
5567
441c3036440c Fixes a bug in SDL_UpdateTexture when using SDL_RENDERER_SOFTWARE and texture access is SDL_TEXTUREACCESS_STATIC
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 5535
diff changeset
   243
    if(SDL_MUSTLOCK(surface))
441c3036440c Fixes a bug in SDL_UpdateTexture when using SDL_RENDERER_SOFTWARE and texture access is SDL_TEXTUREACCESS_STATIC
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 5535
diff changeset
   244
        SDL_LockSurface(surface);
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   245
    src = (Uint8 *) pixels;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   246
    dst = (Uint8 *) surface->pixels +
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   247
                        rect->y * surface->pitch +
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   248
                        rect->x * surface->format->BytesPerPixel;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   249
    length = rect->w * surface->format->BytesPerPixel;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   250
    for (row = 0; row < rect->h; ++row) {
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   251
        SDL_memcpy(dst, src, length);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   252
        src += pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   253
        dst += surface->pitch;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
    }
5567
441c3036440c Fixes a bug in SDL_UpdateTexture when using SDL_RENDERER_SOFTWARE and texture access is SDL_TEXTUREACCESS_STATIC
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 5535
diff changeset
   255
    if(SDL_MUSTLOCK(surface))
441c3036440c Fixes a bug in SDL_UpdateTexture when using SDL_RENDERER_SOFTWARE and texture access is SDL_TEXTUREACCESS_STATIC
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 5535
diff changeset
   256
        SDL_UnlockSurface(surface);
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   257
    return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
static int
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   261
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   262
               const SDL_Rect * rect, void **pixels, int *pitch)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
{
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   264
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   266
    *pixels =
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   267
        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   268
                  rect->x * surface->format->BytesPerPixel);
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   269
    *pitch = surface->pitch;
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   270
    return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   271
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   273
static void
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   274
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   275
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   276
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   278
static int
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   279
SW_UpdateViewport(SDL_Renderer * renderer)
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   280
{
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   281
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   282
    SDL_Surface *surface = data->surface;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   283
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   284
    if (!surface) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   285
        /* We'll update the viewport after we recreate the surface */
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   286
        return 0;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   287
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   288
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   289
    if (!renderer->viewport.w && !renderer->viewport.h) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   290
        /* There may be no window, so update the viewport directly */
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   291
        renderer->viewport.w = surface->w;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   292
        renderer->viewport.h = surface->h;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   293
    }
5298
7b1cac2c2230 Whoops, need to actually clip to the viewport in the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
   294
    SDL_SetClipRect(data->surface, &renderer->viewport);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   295
    return 0;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   296
}
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   298
static int
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   299
SW_RenderClear(SDL_Renderer * renderer)
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
   300
{
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
   301
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   302
    Uint32 color;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   303
    SDL_Rect clip_rect;
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
   304
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
   305
    if (!surface) {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   306
        return -1;
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
   307
    }
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   308
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   309
    color = SDL_MapRGBA(surface->format,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   310
                        renderer->r, renderer->g, renderer->b, renderer->a);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   311
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   312
    /* By definition the clear ignores the clip rect */
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   313
    clip_rect = surface->clip_rect;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   314
    SDL_SetClipRect(surface, NULL);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   315
    SDL_FillRect(surface, NULL, color);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   316
    SDL_SetClipRect(surface, &clip_rect);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   317
    return 0;
5224
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
   318
}
2178ffe17222 Added function SDL_RenderSetClipRect()
Sam Lantinga <slouken@libsdl.org>
parents: 5195
diff changeset
   319
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   320
static int
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   321
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   322
                    int count)
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   323
{
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   324
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   325
    SDL_Point *temp = NULL;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   326
    int status;
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   327
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   328
    if (!surface) {
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   329
        return -1;
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   330
    }
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   331
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   332
    if (renderer->viewport.x || renderer->viewport.y) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   333
        int i;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   334
        int x = renderer->viewport.x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   335
        int y = renderer->viewport.y;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   336
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   337
        temp = SDL_stack_alloc(SDL_Point, count);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   338
        for (i = 0; i < count; ++i) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   339
            temp[i].x = x + points[i].x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   340
            temp[i].y = y + points[i].x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   341
        }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   342
        points = temp;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   343
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   344
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   345
    /* Draw the points! */
5140
e743b9c3f6d6 Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents: 5138
diff changeset
   346
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   347
        Uint32 color = SDL_MapRGBA(surface->format,
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   348
                                   renderer->r, renderer->g, renderer->b,
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   349
                                   renderer->a);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   350
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   351
        status = SDL_DrawPoints(surface, points, count, color);
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   352
    } else {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   353
        status = SDL_BlendPoints(surface, points, count,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   354
                                renderer->blendMode,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   355
                                renderer->r, renderer->g, renderer->b,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   356
                                renderer->a);
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   357
    }
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   358
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   359
    if (temp) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   360
        SDL_stack_free(temp);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   361
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   362
    return status;
2901
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   363
}
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   364
133601e3b255 Added RenderPiont() API
Sam Lantinga <slouken@libsdl.org>
parents: 2891
diff changeset
   365
static int
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   366
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
   367
                   int count)
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   368
{
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   369
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   370
    SDL_Point *temp = NULL;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   371
    int status;
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   372
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   373
    if (!surface) {
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   374
        return -1;
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   375
    }
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   376
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   377
    if (renderer->viewport.x || renderer->viewport.y) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   378
        int i;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   379
        int x = renderer->viewport.x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   380
        int y = renderer->viewport.y;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   381
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   382
        temp = SDL_stack_alloc(SDL_Point, count);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   383
        for (i = 0; i < count; ++i) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   384
            temp[i].x = x + points[i].x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   385
            temp[i].y = y + points[i].y;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   386
        }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   387
        points = temp;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   388
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   389
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   390
    /* Draw the lines! */
5140
e743b9c3f6d6 Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents: 5138
diff changeset
   391
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   392
        Uint32 color = SDL_MapRGBA(surface->format,
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   393
                                   renderer->r, renderer->g, renderer->b,
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   394
                                   renderer->a);
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3435
diff changeset
   395
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   396
        status = SDL_DrawLines(surface, points, count, color);
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   397
    } else {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   398
        status = SDL_BlendLines(surface, points, count,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   399
                                renderer->blendMode,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   400
                                renderer->r, renderer->g, renderer->b,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   401
                                renderer->a);
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   402
    }
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   403
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   404
    if (temp) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   405
        SDL_stack_free(temp);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   406
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   407
    return status;
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   408
}
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   409
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents: 2884
diff changeset
   410
static int
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   411
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   412
{
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   413
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   414
    SDL_Rect *temp = NULL;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   415
    int status;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   416
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   417
    if (!surface) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   418
        return -1;
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   419
    }
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   420
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   421
    if (renderer->viewport.x || renderer->viewport.y) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   422
        int i;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   423
        int x = renderer->viewport.x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   424
        int y = renderer->viewport.y;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   425
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   426
        temp = SDL_stack_alloc(SDL_Rect, count);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   427
        for (i = 0; i < count; ++i) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   428
            temp[i].x = x + rects[i].x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   429
            temp[i].y = y + rects[i].y;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   430
            temp[i].w = rects[i].w;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   431
            temp[i].h = rects[i].h;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   432
        }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   433
        rects = temp;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   434
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   435
5140
e743b9c3f6d6 Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents: 5138
diff changeset
   436
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   437
        Uint32 color = SDL_MapRGBA(surface->format,
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   438
                                   renderer->r, renderer->g, renderer->b,
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   439
                                   renderer->a);
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   440
        status = SDL_FillRects(surface, rects, count, color);
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   441
    } else {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   442
        status = SDL_BlendFillRects(surface, rects, count,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   443
                                    renderer->blendMode,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   444
                                    renderer->r, renderer->g, renderer->b,
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   445
                                    renderer->a);
1907
06c27a737b7a Streamlined the API a bit and optimized the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 1897
diff changeset
   446
    }
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   447
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   448
    if (temp) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   449
        SDL_stack_free(temp);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   450
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   451
    return status;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   452
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   453
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   454
static int
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   455
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
1985
8055185ae4ed Added source color and alpha modulation support.
Sam Lantinga <slouken@libsdl.org>
parents: 1972
diff changeset
   456
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   457
{
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   458
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   459
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   460
    SDL_Rect final_rect = *dstrect;
1907
06c27a737b7a Streamlined the API a bit and optimized the software renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 1897
diff changeset
   461
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   462
    if (!surface) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   463
        return -1;
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   464
    }
5296
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   465
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   466
    if (renderer->viewport.x || renderer->viewport.y) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   467
        final_rect.x += renderer->viewport.x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   468
        final_rect.y += renderer->viewport.y;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   469
    }
5296
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   470
    if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   471
        return SDL_BlitSurface(src, srcrect, surface, &final_rect);
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   472
    } else {
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   473
        return SDL_BlitScaled(src, srcrect, surface, &final_rect);
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   474
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   475
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   476
3427
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   477
static int
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   478
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
3435
9f62f47d989b You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3427
diff changeset
   479
                    Uint32 format, void * pixels, int pitch)
3427
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   480
{
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   481
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   482
    Uint32 src_format;
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   483
    void *src_pixels;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   484
    SDL_Rect final_rect;
3427
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   485
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   486
    if (!surface) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   487
        return -1;
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   488
    }
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   489
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   490
    if (renderer->viewport.x || renderer->viewport.y) {
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   491
        final_rect.x = renderer->viewport.x + rect->x;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   492
        final_rect.y = renderer->viewport.y + rect->y;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   493
        final_rect.w = rect->w;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   494
        final_rect.h = rect->h;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   495
        rect = &final_rect;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   496
    }
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   497
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   498
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   499
        rect->y < 0 || rect->y+rect->h > surface->h) {
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   500
        SDL_SetError("Tried to read outside of surface bounds");
3427
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   501
        return -1;
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   502
    }
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   503
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   504
    src_format = surface->format->format;
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   505
    src_pixels = (void*)((Uint8 *) surface->pixels +
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   506
                    rect->y * surface->pitch +
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   507
                    rect->x * surface->format->BytesPerPixel);
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   508
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   509
    return SDL_ConvertPixels(rect->w, rect->h,
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   510
                             src_format, src_pixels, surface->pitch,
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   511
                             format, pixels, pitch);
3427
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   512
}
36cf454ba065 Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
Sam Lantinga <slouken@libsdl.org>
parents: 3053
diff changeset
   513
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   514
static void
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   515
SW_RenderPresent(SDL_Renderer * renderer)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   516
{
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   517
    SDL_Window *window = renderer->window;
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5142
diff changeset
   518
5166
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   519
    if (window) {
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   520
        SDL_UpdateWindowSurface(window);
4d39eeaad00b Added a way to get a framebuffer interface for a window, and also a way to create a software renderer for an arbitrary surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5163
diff changeset
   521
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   522
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   523
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   524
static void
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   525
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   526
{
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   527
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   528
5156
307ccc9c135e Made it possible to create a texture of any format, even if not supported by the renderer.
Sam Lantinga <slouken@libsdl.org>
parents: 5154
diff changeset
   529
    SDL_FreeSurface(surface);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   530
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   531
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   532
static void
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   533
SW_DestroyRenderer(SDL_Renderer * renderer)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   534
{
1918
092bd3a019c5 Starting on the OpenGL renderer...
Sam Lantinga <slouken@libsdl.org>
parents: 1907
diff changeset
   535
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   536
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   537
    if (data) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   538
        SDL_free(data);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   539
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   540
    SDL_free(renderer);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   541
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   542
5226
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
   543
#endif /* !SDL_RENDER_DISABLED */
710d00cb3a6a Made it possible to disable the rendering subsystem with configure --disable-render
Sam Lantinga <slouken@libsdl.org>
parents: 5224
diff changeset
   544
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   545
/* vi: set ts=4 sw=4 expandtab: */