Renamed files for consistency
authorSam Lantinga <slouken@libsdl.org>
Sat, 05 Feb 2011 12:01:11 -0800
changeset 5195bb45ecd958d8
parent 5194 69f47f2c1856
child 5196 06837416f969
Renamed files for consistency
src/render/SDL_render.c
src/render/direct3d/SDL_d3drender.c
src/render/direct3d/SDL_render_d3d.c
src/render/opengl/SDL_render_gl.c
src/render/opengl/SDL_renderer_gl.c
src/render/opengles/SDL_render_gles.c
src/render/opengles/SDL_renderer_gles.c
src/render/software/SDL_render_sw.c
src/render/software/SDL_render_sw_c.h
src/render/software/SDL_renderer_sw.c
src/render/software/SDL_renderer_sw_c.h
     1.1 --- a/src/render/SDL_render.c	Sat Feb 05 11:54:46 2011 -0800
     1.2 +++ b/src/render/SDL_render.c	Sat Feb 05 12:01:11 2011 -0800
     1.3 @@ -27,7 +27,7 @@
     1.4  #include "SDL_render.h"
     1.5  #include "SDL_sysrender.h"
     1.6  #include "../video/SDL_pixels_c.h"
     1.7 -#include "software/SDL_renderer_sw_c.h"
     1.8 +#include "software/SDL_render_sw_c.h"
     1.9  
    1.10  
    1.11  #define CHECK_RENDERER_MAGIC(renderer, retval) \
     2.1 --- a/src/render/direct3d/SDL_d3drender.c	Sat Feb 05 11:54:46 2011 -0800
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,1048 +0,0 @@
     2.4 -/*
     2.5 -    SDL - Simple DirectMedia Layer
     2.6 -    Copyright (C) 1997-2010 Sam Lantinga
     2.7 -
     2.8 -    This library is free software; you can redistribute it and/or
     2.9 -    modify it under the terms of the GNU Lesser General Public
    2.10 -    License as published by the Free Software Foundation; either
    2.11 -    version 2.1 of the License, or (at your option) any later version.
    2.12 -
    2.13 -    This library is distributed in the hope that it will be useful,
    2.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 -    Lesser General Public License for more details.
    2.17 -
    2.18 -    You should have received a copy of the GNU Lesser General Public
    2.19 -    License along with this library; if not, write to the Free Software
    2.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.21 -
    2.22 -    Sam Lantinga
    2.23 -    slouken@libsdl.org
    2.24 -*/
    2.25 -#include "SDL_config.h"
    2.26 -
    2.27 -#if SDL_VIDEO_RENDER_D3D
    2.28 -
    2.29 -#include "../../core/windows/SDL_windows.h"
    2.30 -
    2.31 -#include "SDL_loadso.h"
    2.32 -#include "SDL_syswm.h"
    2.33 -#include "../SDL_sysrender.h"
    2.34 -
    2.35 -#if SDL_VIDEO_RENDER_D3D
    2.36 -#define D3D_DEBUG_INFO
    2.37 -#include <d3d9.h>
    2.38 -#endif
    2.39 -
    2.40 -#ifdef ASSEMBLE_SHADER
    2.41 -///////////////////////////////////////////////////////////////////////////
    2.42 -// ID3DXBuffer:
    2.43 -// ------------
    2.44 -// The buffer object is used by D3DX to return arbitrary size data.
    2.45 -//
    2.46 -// GetBufferPointer -
    2.47 -//    Returns a pointer to the beginning of the buffer.
    2.48 -//
    2.49 -// GetBufferSize -
    2.50 -//    Returns the size of the buffer, in bytes.
    2.51 -///////////////////////////////////////////////////////////////////////////
    2.52 -
    2.53 -typedef interface ID3DXBuffer ID3DXBuffer;
    2.54 -typedef interface ID3DXBuffer *LPD3DXBUFFER;
    2.55 -
    2.56 -// {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
    2.57 -DEFINE_GUID(IID_ID3DXBuffer, 
    2.58 -0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
    2.59 -
    2.60 -#undef INTERFACE
    2.61 -#define INTERFACE ID3DXBuffer
    2.62 -
    2.63 -typedef interface ID3DXBuffer {
    2.64 -    const struct ID3DXBufferVtbl FAR* lpVtbl;
    2.65 -} ID3DXBuffer;
    2.66 -typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
    2.67 -const struct ID3DXBufferVtbl
    2.68 -{
    2.69 -    // IUnknown
    2.70 -    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
    2.71 -    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
    2.72 -    STDMETHOD_(ULONG, Release)(THIS) PURE;
    2.73 -
    2.74 -    // ID3DXBuffer
    2.75 -    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
    2.76 -    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
    2.77 -};
    2.78 -
    2.79 -HRESULT WINAPI
    2.80 -    D3DXAssembleShader(
    2.81 -        LPCSTR                          pSrcData,
    2.82 -        UINT                            SrcDataLen,
    2.83 -        CONST LPVOID*                   pDefines,
    2.84 -        LPVOID                          pInclude,
    2.85 -        DWORD                           Flags,
    2.86 -        LPD3DXBUFFER*                   ppShader,
    2.87 -        LPD3DXBUFFER*                   ppErrorMsgs);
    2.88 -
    2.89 -#endif /* ASSEMBLE_SHADER */
    2.90 -
    2.91 -
    2.92 -/* Direct3D renderer implementation */
    2.93 -
    2.94 -static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
    2.95 -static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    2.96 -static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    2.97 -                             const SDL_Rect * rect, const void *pixels,
    2.98 -                             int pitch);
    2.99 -static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   2.100 -                           const SDL_Rect * rect, void **pixels, int *pitch);
   2.101 -static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
   2.102 -static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
   2.103 -                                const SDL_Point * points, int count);
   2.104 -static int D3D_RenderDrawLines(SDL_Renderer * renderer,
   2.105 -                               const SDL_Point * points, int count);
   2.106 -static int D3D_RenderFillRects(SDL_Renderer * renderer,
   2.107 -                               const SDL_Rect ** rects, int count);
   2.108 -static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   2.109 -                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
   2.110 -static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   2.111 -                                Uint32 format, void * pixels, int pitch);
   2.112 -static void D3D_RenderPresent(SDL_Renderer * renderer);
   2.113 -static void D3D_DestroyTexture(SDL_Renderer * renderer,
   2.114 -                               SDL_Texture * texture);
   2.115 -static void D3D_DestroyRenderer(SDL_Renderer * renderer);
   2.116 -
   2.117 -
   2.118 -SDL_RenderDriver D3D_RenderDriver = {
   2.119 -    D3D_CreateRenderer,
   2.120 -    {
   2.121 -     "direct3d",
   2.122 -     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
   2.123 -     1,
   2.124 -     {SDL_PIXELFORMAT_ARGB8888},
   2.125 -     0,
   2.126 -     0}
   2.127 -};
   2.128 -
   2.129 -typedef struct
   2.130 -{
   2.131 -    void* d3dDLL;
   2.132 -    IDirect3D9 *d3d;
   2.133 -    IDirect3DDevice9 *device;
   2.134 -    UINT adapter;
   2.135 -    D3DPRESENT_PARAMETERS pparams;
   2.136 -    SDL_bool beginScene;
   2.137 -} D3D_RenderData;
   2.138 -
   2.139 -typedef struct
   2.140 -{
   2.141 -    IDirect3DTexture9 *texture;
   2.142 -} D3D_TextureData;
   2.143 -
   2.144 -typedef struct
   2.145 -{
   2.146 -    float x, y, z;
   2.147 -    float rhw;
   2.148 -    DWORD color;
   2.149 -    float u, v;
   2.150 -} Vertex;
   2.151 -
   2.152 -static void
   2.153 -D3D_SetError(const char *prefix, HRESULT result)
   2.154 -{
   2.155 -    const char *error;
   2.156 -
   2.157 -    switch (result) {
   2.158 -    case D3DERR_WRONGTEXTUREFORMAT:
   2.159 -        error = "WRONGTEXTUREFORMAT";
   2.160 -        break;
   2.161 -    case D3DERR_UNSUPPORTEDCOLOROPERATION:
   2.162 -        error = "UNSUPPORTEDCOLOROPERATION";
   2.163 -        break;
   2.164 -    case D3DERR_UNSUPPORTEDCOLORARG:
   2.165 -        error = "UNSUPPORTEDCOLORARG";
   2.166 -        break;
   2.167 -    case D3DERR_UNSUPPORTEDALPHAOPERATION:
   2.168 -        error = "UNSUPPORTEDALPHAOPERATION";
   2.169 -        break;
   2.170 -    case D3DERR_UNSUPPORTEDALPHAARG:
   2.171 -        error = "UNSUPPORTEDALPHAARG";
   2.172 -        break;
   2.173 -    case D3DERR_TOOMANYOPERATIONS:
   2.174 -        error = "TOOMANYOPERATIONS";
   2.175 -        break;
   2.176 -    case D3DERR_CONFLICTINGTEXTUREFILTER:
   2.177 -        error = "CONFLICTINGTEXTUREFILTER";
   2.178 -        break;
   2.179 -    case D3DERR_UNSUPPORTEDFACTORVALUE:
   2.180 -        error = "UNSUPPORTEDFACTORVALUE";
   2.181 -        break;
   2.182 -    case D3DERR_CONFLICTINGRENDERSTATE:
   2.183 -        error = "CONFLICTINGRENDERSTATE";
   2.184 -        break;
   2.185 -    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
   2.186 -        error = "UNSUPPORTEDTEXTUREFILTER";
   2.187 -        break;
   2.188 -    case D3DERR_CONFLICTINGTEXTUREPALETTE:
   2.189 -        error = "CONFLICTINGTEXTUREPALETTE";
   2.190 -        break;
   2.191 -    case D3DERR_DRIVERINTERNALERROR:
   2.192 -        error = "DRIVERINTERNALERROR";
   2.193 -        break;
   2.194 -    case D3DERR_NOTFOUND:
   2.195 -        error = "NOTFOUND";
   2.196 -        break;
   2.197 -    case D3DERR_MOREDATA:
   2.198 -        error = "MOREDATA";
   2.199 -        break;
   2.200 -    case D3DERR_DEVICELOST:
   2.201 -        error = "DEVICELOST";
   2.202 -        break;
   2.203 -    case D3DERR_DEVICENOTRESET:
   2.204 -        error = "DEVICENOTRESET";
   2.205 -        break;
   2.206 -    case D3DERR_NOTAVAILABLE:
   2.207 -        error = "NOTAVAILABLE";
   2.208 -        break;
   2.209 -    case D3DERR_OUTOFVIDEOMEMORY:
   2.210 -        error = "OUTOFVIDEOMEMORY";
   2.211 -        break;
   2.212 -    case D3DERR_INVALIDDEVICE:
   2.213 -        error = "INVALIDDEVICE";
   2.214 -        break;
   2.215 -    case D3DERR_INVALIDCALL:
   2.216 -        error = "INVALIDCALL";
   2.217 -        break;
   2.218 -    case D3DERR_DRIVERINVALIDCALL:
   2.219 -        error = "DRIVERINVALIDCALL";
   2.220 -        break;
   2.221 -    case D3DERR_WASSTILLDRAWING:
   2.222 -        error = "WASSTILLDRAWING";
   2.223 -        break;
   2.224 -    default:
   2.225 -        error = "UNKNOWN";
   2.226 -        break;
   2.227 -    }
   2.228 -    SDL_SetError("%s: %s", prefix, error);
   2.229 -}
   2.230 -
   2.231 -static D3DFORMAT
   2.232 -PixelFormatToD3DFMT(Uint32 format)
   2.233 -{
   2.234 -    switch (format) {
   2.235 -    case SDL_PIXELFORMAT_RGB565:
   2.236 -        return D3DFMT_R5G6B5;
   2.237 -    case SDL_PIXELFORMAT_RGB888:
   2.238 -        return D3DFMT_X8R8G8B8;
   2.239 -    case SDL_PIXELFORMAT_ARGB8888:
   2.240 -        return D3DFMT_A8R8G8B8;
   2.241 -    default:
   2.242 -        return D3DFMT_UNKNOWN;
   2.243 -    }
   2.244 -}
   2.245 -
   2.246 -static Uint32
   2.247 -D3DFMTToPixelFormat(D3DFORMAT format)
   2.248 -{
   2.249 -    switch (format) {
   2.250 -    case D3DFMT_R5G6B5:
   2.251 -        return SDL_PIXELFORMAT_RGB565;
   2.252 -    case D3DFMT_X8R8G8B8:
   2.253 -        return SDL_PIXELFORMAT_RGB888;
   2.254 -    case D3DFMT_A8R8G8B8:
   2.255 -        return SDL_PIXELFORMAT_ARGB8888;
   2.256 -    default:
   2.257 -        return SDL_PIXELFORMAT_UNKNOWN;
   2.258 -    }
   2.259 -}
   2.260 -
   2.261 -SDL_Renderer *
   2.262 -D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   2.263 -{
   2.264 -    SDL_Renderer *renderer;
   2.265 -    D3D_RenderData *data;
   2.266 -    SDL_SysWMinfo windowinfo;
   2.267 -    HRESULT result;
   2.268 -    D3DPRESENT_PARAMETERS pparams;
   2.269 -    IDirect3DSwapChain9 *chain;
   2.270 -    D3DCAPS9 caps;
   2.271 -    Uint32 window_flags;
   2.272 -    int w, h;
   2.273 -    SDL_DisplayMode fullscreen_mode;
   2.274 -
   2.275 -    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   2.276 -    if (!renderer) {
   2.277 -        SDL_OutOfMemory();
   2.278 -        return NULL;
   2.279 -    }
   2.280 -
   2.281 -    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
   2.282 -    if (!data) {
   2.283 -        SDL_free(renderer);
   2.284 -        SDL_OutOfMemory();
   2.285 -        return NULL;
   2.286 -    }
   2.287 -
   2.288 -    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
   2.289 -    if (data->d3dDLL) {
   2.290 -        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
   2.291 -
   2.292 -        D3DCreate =
   2.293 -            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
   2.294 -                                                            "Direct3DCreate9");
   2.295 -        if (D3DCreate) {
   2.296 -            data->d3d = D3DCreate(D3D_SDK_VERSION);
   2.297 -        }
   2.298 -        if (!data->d3d) {
   2.299 -            SDL_UnloadObject(data->d3dDLL);
   2.300 -            data->d3dDLL = NULL;
   2.301 -        }
   2.302 -    }
   2.303 -    if (!data->d3d) {
   2.304 -        SDL_free(renderer);
   2.305 -        SDL_free(data);
   2.306 -        SDL_SetError("Unable to create Direct3D interface");
   2.307 -        return NULL;
   2.308 -    }
   2.309 -
   2.310 -    renderer->CreateTexture = D3D_CreateTexture;
   2.311 -    renderer->UpdateTexture = D3D_UpdateTexture;
   2.312 -    renderer->LockTexture = D3D_LockTexture;
   2.313 -    renderer->UnlockTexture = D3D_UnlockTexture;
   2.314 -    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
   2.315 -    renderer->RenderDrawLines = D3D_RenderDrawLines;
   2.316 -    renderer->RenderFillRects = D3D_RenderFillRects;
   2.317 -    renderer->RenderCopy = D3D_RenderCopy;
   2.318 -    renderer->RenderReadPixels = D3D_RenderReadPixels;
   2.319 -    renderer->RenderPresent = D3D_RenderPresent;
   2.320 -    renderer->DestroyTexture = D3D_DestroyTexture;
   2.321 -    renderer->DestroyRenderer = D3D_DestroyRenderer;
   2.322 -    renderer->info = D3D_RenderDriver.info;
   2.323 -    renderer->driverdata = data;
   2.324 -
   2.325 -    renderer->info.flags = SDL_RENDERER_ACCELERATED;
   2.326 -
   2.327 -    SDL_VERSION(&windowinfo.version);
   2.328 -    SDL_GetWindowWMInfo(window, &windowinfo);
   2.329 -
   2.330 -    window_flags = SDL_GetWindowFlags(window);
   2.331 -    SDL_GetWindowSize(window, &w, &h);
   2.332 -    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
   2.333 -
   2.334 -    SDL_zero(pparams);
   2.335 -    pparams.hDeviceWindow = windowinfo.info.win.window;
   2.336 -    pparams.BackBufferWidth = w;
   2.337 -    pparams.BackBufferHeight = h;
   2.338 -    if (window_flags & SDL_WINDOW_FULLSCREEN) {
   2.339 -        pparams.BackBufferFormat =
   2.340 -            PixelFormatToD3DFMT(fullscreen_mode.format);
   2.341 -    } else {
   2.342 -        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   2.343 -    }
   2.344 -    pparams.BackBufferCount = 1;
   2.345 -    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   2.346 -
   2.347 -    if (window_flags & SDL_WINDOW_FULLSCREEN) {
   2.348 -        pparams.Windowed = FALSE;
   2.349 -        pparams.FullScreen_RefreshRateInHz =
   2.350 -            fullscreen_mode.refresh_rate;
   2.351 -    } else {
   2.352 -        pparams.Windowed = TRUE;
   2.353 -        pparams.FullScreen_RefreshRateInHz = 0;
   2.354 -    }
   2.355 -    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   2.356 -        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
   2.357 -    } else {
   2.358 -        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   2.359 -    }
   2.360 -
   2.361 -    /* FIXME: Which adapter? */
   2.362 -    data->adapter = D3DADAPTER_DEFAULT;
   2.363 -    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
   2.364 -
   2.365 -    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
   2.366 -                                     D3DDEVTYPE_HAL,
   2.367 -                                     pparams.hDeviceWindow,
   2.368 -                                     (caps.
   2.369 -                                      DevCaps &
   2.370 -                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
   2.371 -                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
   2.372 -                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   2.373 -                                     &pparams, &data->device);
   2.374 -    if (FAILED(result)) {
   2.375 -        D3D_DestroyRenderer(renderer);
   2.376 -        D3D_SetError("CreateDevice()", result);
   2.377 -        return NULL;
   2.378 -    }
   2.379 -    data->beginScene = SDL_TRUE;
   2.380 -
   2.381 -    /* Get presentation parameters to fill info */
   2.382 -    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
   2.383 -    if (FAILED(result)) {
   2.384 -        D3D_DestroyRenderer(renderer);
   2.385 -        D3D_SetError("GetSwapChain()", result);
   2.386 -        return NULL;
   2.387 -    }
   2.388 -    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
   2.389 -    if (FAILED(result)) {
   2.390 -        IDirect3DSwapChain9_Release(chain);
   2.391 -        D3D_DestroyRenderer(renderer);
   2.392 -        D3D_SetError("GetPresentParameters()", result);
   2.393 -        return NULL;
   2.394 -    }
   2.395 -    IDirect3DSwapChain9_Release(chain);
   2.396 -    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
   2.397 -        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   2.398 -    }
   2.399 -    data->pparams = pparams;
   2.400 -
   2.401 -    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
   2.402 -    renderer->info.max_texture_width = caps.MaxTextureWidth;
   2.403 -    renderer->info.max_texture_height = caps.MaxTextureHeight;
   2.404 -
   2.405 -    /* Set up parameters for rendering */
   2.406 -    IDirect3DDevice9_SetVertexShader(data->device, NULL);
   2.407 -    IDirect3DDevice9_SetFVF(data->device,
   2.408 -                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
   2.409 -    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
   2.410 -    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   2.411 -                                    D3DCULL_NONE);
   2.412 -    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   2.413 -    /* Enable color modulation by diffuse color */
   2.414 -    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
   2.415 -                                          D3DTOP_MODULATE);
   2.416 -    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
   2.417 -                                          D3DTA_TEXTURE);
   2.418 -    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
   2.419 -                                          D3DTA_DIFFUSE);
   2.420 -    /* Enable alpha modulation by diffuse alpha */
   2.421 -    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
   2.422 -                                          D3DTOP_MODULATE);
   2.423 -    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
   2.424 -                                          D3DTA_TEXTURE);
   2.425 -    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
   2.426 -                                          D3DTA_DIFFUSE);
   2.427 -    /* Disable second texture stage, since we're done */
   2.428 -    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
   2.429 -                                          D3DTOP_DISABLE);
   2.430 -    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
   2.431 -                                          D3DTOP_DISABLE);
   2.432 -
   2.433 -    return renderer;
   2.434 -}
   2.435 -
   2.436 -static int
   2.437 -D3D_Reset(SDL_Renderer * renderer)
   2.438 -{
   2.439 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   2.440 -    HRESULT result;
   2.441 -
   2.442 -    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
   2.443 -    if (FAILED(result)) {
   2.444 -        if (result == D3DERR_DEVICELOST) {
   2.445 -            /* Don't worry about it, we'll reset later... */
   2.446 -            return 0;
   2.447 -        } else {
   2.448 -            D3D_SetError("Reset()", result);
   2.449 -            return -1;
   2.450 -        }
   2.451 -    }
   2.452 -    IDirect3DDevice9_SetVertexShader(data->device, NULL);
   2.453 -    IDirect3DDevice9_SetFVF(data->device,
   2.454 -                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
   2.455 -    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   2.456 -                                    D3DCULL_NONE);
   2.457 -    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   2.458 -    return 0;
   2.459 -}
   2.460 -
   2.461 -/* FIXME: This needs to be called... when? */
   2.462 -#if 0
   2.463 -static int
   2.464 -D3D_DisplayModeChanged(SDL_Renderer * renderer)
   2.465 -{
   2.466 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   2.467 -    SDL_Window *window = renderer->window;
   2.468 -    SDL_VideoDisplay *display = window->display;
   2.469 -
   2.470 -    data->pparams.BackBufferWidth = window->w;
   2.471 -    data->pparams.BackBufferHeight = window->h;
   2.472 -    if (window->flags & SDL_WINDOW_FULLSCREEN) {
   2.473 -        data->pparams.BackBufferFormat =
   2.474 -            PixelFormatToD3DFMT(window->fullscreen_mode.format);
   2.475 -    } else {
   2.476 -        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   2.477 -    }
   2.478 -    return D3D_Reset(renderer);
   2.479 -}
   2.480 -#endif
   2.481 -
   2.482 -static int
   2.483 -D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   2.484 -{
   2.485 -    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   2.486 -    SDL_Window *window = renderer->window;
   2.487 -    D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
   2.488 -    D3D_TextureData *data;
   2.489 -    D3DPOOL pool;
   2.490 -    DWORD usage;
   2.491 -    HRESULT result;
   2.492 -
   2.493 -    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
   2.494 -    if (!data) {
   2.495 -        SDL_OutOfMemory();
   2.496 -        return -1;
   2.497 -    }
   2.498 -
   2.499 -    texture->driverdata = data;
   2.500 -
   2.501 -#ifdef USE_DYNAMIC_TEXTURE
   2.502 -    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   2.503 -        pool = D3DPOOL_DEFAULT;
   2.504 -        usage = D3DUSAGE_DYNAMIC;
   2.505 -    } else
   2.506 -#endif
   2.507 -    {
   2.508 -        pool = D3DPOOL_MANAGED;
   2.509 -        usage = 0;
   2.510 -    }
   2.511 -
   2.512 -    result =
   2.513 -        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   2.514 -                                       texture->h, 1, usage,
   2.515 -                                       PixelFormatToD3DFMT(texture->format),
   2.516 -                                       pool, &data->texture, NULL);
   2.517 -    if (FAILED(result)) {
   2.518 -        D3D_SetError("CreateTexture()", result);
   2.519 -        return -1;
   2.520 -    }
   2.521 -
   2.522 -    return 0;
   2.523 -}
   2.524 -
   2.525 -static int
   2.526 -D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   2.527 -                  const SDL_Rect * rect, const void *pixels, int pitch)
   2.528 -{
   2.529 -    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   2.530 -    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   2.531 -    RECT d3drect;
   2.532 -    D3DLOCKED_RECT locked;
   2.533 -    const Uint8 *src;
   2.534 -    Uint8 *dst;
   2.535 -    int row, length;
   2.536 -    HRESULT result;
   2.537 -
   2.538 -#ifdef USE_DYNAMIC_TEXTURE
   2.539 -    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
   2.540 -        rect->x == 0 && rect->y == 0 &&
   2.541 -        rect->w == texture->w && rect->h == texture->h) {
   2.542 -        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
   2.543 -    } else
   2.544 -#endif
   2.545 -    {
   2.546 -        d3drect.left = rect->x;
   2.547 -        d3drect.right = rect->x + rect->w;
   2.548 -        d3drect.top = rect->y;
   2.549 -        d3drect.bottom = rect->y + rect->h;
   2.550 -        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   2.551 -    }
   2.552 -
   2.553 -    if (FAILED(result)) {
   2.554 -        D3D_SetError("LockRect()", result);
   2.555 -        return -1;
   2.556 -    }
   2.557 -
   2.558 -    src = pixels;
   2.559 -    dst = locked.pBits;
   2.560 -    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   2.561 -    if (length == pitch && length == locked.Pitch) {
   2.562 -        SDL_memcpy(dst, src, length*rect->h);
   2.563 -    } else {
   2.564 -        for (row = 0; row < rect->h; ++row) {
   2.565 -            SDL_memcpy(dst, src, length);
   2.566 -            src += pitch;
   2.567 -            dst += locked.Pitch;
   2.568 -        }
   2.569 -    }
   2.570 -    IDirect3DTexture9_UnlockRect(data->texture, 0);
   2.571 -
   2.572 -    return 0;
   2.573 -}
   2.574 -
   2.575 -static int
   2.576 -D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   2.577 -                const SDL_Rect * rect, void **pixels, int *pitch)
   2.578 -{
   2.579 -    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   2.580 -    RECT d3drect;
   2.581 -    D3DLOCKED_RECT locked;
   2.582 -    HRESULT result;
   2.583 -
   2.584 -    d3drect.left = rect->x;
   2.585 -    d3drect.right = rect->x + rect->w;
   2.586 -    d3drect.top = rect->y;
   2.587 -    d3drect.bottom = rect->y + rect->h;
   2.588 -
   2.589 -    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   2.590 -    if (FAILED(result)) {
   2.591 -        D3D_SetError("LockRect()", result);
   2.592 -        return -1;
   2.593 -    }
   2.594 -    *pixels = locked.pBits;
   2.595 -    *pitch = locked.Pitch;
   2.596 -    return 0;
   2.597 -}
   2.598 -
   2.599 -static void
   2.600 -D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   2.601 -{
   2.602 -    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   2.603 -
   2.604 -    IDirect3DTexture9_UnlockRect(data->texture, 0);
   2.605 -}
   2.606 -
   2.607 -static void
   2.608 -D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
   2.609 -{
   2.610 -    switch (blendMode) {
   2.611 -    case SDL_BLENDMODE_NONE:
   2.612 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   2.613 -                                        FALSE);
   2.614 -        break;
   2.615 -    case SDL_BLENDMODE_BLEND:
   2.616 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   2.617 -                                        TRUE);
   2.618 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   2.619 -                                        D3DBLEND_SRCALPHA);
   2.620 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   2.621 -                                        D3DBLEND_INVSRCALPHA);
   2.622 -        break;
   2.623 -    case SDL_BLENDMODE_ADD:
   2.624 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   2.625 -                                        TRUE);
   2.626 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   2.627 -                                        D3DBLEND_SRCALPHA);
   2.628 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   2.629 -                                        D3DBLEND_ONE);
   2.630 -        break;
   2.631 -    case SDL_BLENDMODE_MOD:
   2.632 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   2.633 -                                        TRUE);
   2.634 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   2.635 -                                        D3DBLEND_ZERO);
   2.636 -        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   2.637 -                                        D3DBLEND_SRCCOLOR);
   2.638 -        break;
   2.639 -    }
   2.640 -}
   2.641 -
   2.642 -static int
   2.643 -D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   2.644 -                     int count)
   2.645 -{
   2.646 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   2.647 -    DWORD color;
   2.648 -    Vertex *vertices;
   2.649 -    int i;
   2.650 -    HRESULT result;
   2.651 -
   2.652 -    if (data->beginScene) {
   2.653 -        IDirect3DDevice9_BeginScene(data->device);
   2.654 -        data->beginScene = SDL_FALSE;
   2.655 -    }
   2.656 -
   2.657 -    D3D_SetBlendMode(data, renderer->blendMode);
   2.658 -
   2.659 -    result =
   2.660 -        IDirect3DDevice9_SetTexture(data->device, 0,
   2.661 -                                    (IDirect3DBaseTexture9 *) 0);
   2.662 -    if (FAILED(result)) {
   2.663 -        D3D_SetError("SetTexture()", result);
   2.664 -        return -1;
   2.665 -    }
   2.666 -
   2.667 -    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   2.668 -
   2.669 -    vertices = SDL_stack_alloc(Vertex, count);
   2.670 -    for (i = 0; i < count; ++i) {
   2.671 -        vertices[i].x = (float) points[i].x;
   2.672 -        vertices[i].y = (float) points[i].y;
   2.673 -        vertices[i].z = 0.0f;
   2.674 -        vertices[i].rhw = 1.0f;
   2.675 -        vertices[i].color = color;
   2.676 -        vertices[i].u = 0.0f;
   2.677 -        vertices[i].v = 0.0f;
   2.678 -    }
   2.679 -    result =
   2.680 -        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
   2.681 -                                         vertices, sizeof(*vertices));
   2.682 -    SDL_stack_free(vertices);
   2.683 -    if (FAILED(result)) {
   2.684 -        D3D_SetError("DrawPrimitiveUP()", result);
   2.685 -        return -1;
   2.686 -    }
   2.687 -    return 0;
   2.688 -}
   2.689 -
   2.690 -static int
   2.691 -D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   2.692 -                    int count)
   2.693 -{
   2.694 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   2.695 -    DWORD color;
   2.696 -    Vertex *vertices;
   2.697 -    int i;
   2.698 -    HRESULT result;
   2.699 -
   2.700 -    if (data->beginScene) {
   2.701 -        IDirect3DDevice9_BeginScene(data->device);
   2.702 -        data->beginScene = SDL_FALSE;
   2.703 -    }
   2.704 -
   2.705 -    D3D_SetBlendMode(data, renderer->blendMode);
   2.706 -
   2.707 -    result =
   2.708 -        IDirect3DDevice9_SetTexture(data->device, 0,
   2.709 -                                    (IDirect3DBaseTexture9 *) 0);
   2.710 -    if (FAILED(result)) {
   2.711 -        D3D_SetError("SetTexture()", result);
   2.712 -        return -1;
   2.713 -    }
   2.714 -
   2.715 -    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   2.716 -
   2.717 -    vertices = SDL_stack_alloc(Vertex, count);
   2.718 -    for (i = 0; i < count; ++i) {
   2.719 -        vertices[i].x = (float) points[i].x;
   2.720 -        vertices[i].y = (float) points[i].y;
   2.721 -        vertices[i].z = 0.0f;
   2.722 -        vertices[i].rhw = 1.0f;
   2.723 -        vertices[i].color = color;
   2.724 -        vertices[i].u = 0.0f;
   2.725 -        vertices[i].v = 0.0f;
   2.726 -    }
   2.727 -    result =
   2.728 -        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
   2.729 -                                         vertices, sizeof(*vertices));
   2.730 -
   2.731 -    /* DirectX 9 has the same line rasterization semantics as GDI,
   2.732 -       so we need to close the endpoint of the line */
   2.733 -    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
   2.734 -        vertices[0].x = (float) points[count-1].x;
   2.735 -        vertices[0].y = (float) points[count-1].y;
   2.736 -        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
   2.737 -    }
   2.738 -
   2.739 -    SDL_stack_free(vertices);
   2.740 -    if (FAILED(result)) {
   2.741 -        D3D_SetError("DrawPrimitiveUP()", result);
   2.742 -        return -1;
   2.743 -    }
   2.744 -    return 0;
   2.745 -}
   2.746 -
   2.747 -static int
   2.748 -D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   2.749 -                    int count)
   2.750 -{
   2.751 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   2.752 -    DWORD color;
   2.753 -    int i;
   2.754 -    float minx, miny, maxx, maxy;
   2.755 -    Vertex vertices[4];
   2.756 -    HRESULT result;
   2.757 -
   2.758 -    if (data->beginScene) {
   2.759 -        IDirect3DDevice9_BeginScene(data->device);
   2.760 -        data->beginScene = SDL_FALSE;
   2.761 -    }
   2.762 -
   2.763 -    D3D_SetBlendMode(data, renderer->blendMode);
   2.764 -
   2.765 -    result =
   2.766 -        IDirect3DDevice9_SetTexture(data->device, 0,
   2.767 -                                    (IDirect3DBaseTexture9 *) 0);
   2.768 -    if (FAILED(result)) {
   2.769 -        D3D_SetError("SetTexture()", result);
   2.770 -        return -1;
   2.771 -    }
   2.772 -
   2.773 -    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   2.774 -
   2.775 -    for (i = 0; i < count; ++i) {
   2.776 -        const SDL_Rect *rect = rects[i];
   2.777 -
   2.778 -        minx = (float) rect->x;
   2.779 -        miny = (float) rect->y;
   2.780 -        maxx = (float) rect->x + rect->w;
   2.781 -        maxy = (float) rect->y + rect->h;
   2.782 -
   2.783 -        vertices[0].x = minx;
   2.784 -        vertices[0].y = miny;
   2.785 -        vertices[0].z = 0.0f;
   2.786 -        vertices[0].rhw = 1.0f;
   2.787 -        vertices[0].color = color;
   2.788 -        vertices[0].u = 0.0f;
   2.789 -        vertices[0].v = 0.0f;
   2.790 -
   2.791 -        vertices[1].x = maxx;
   2.792 -        vertices[1].y = miny;
   2.793 -        vertices[1].z = 0.0f;
   2.794 -        vertices[1].rhw = 1.0f;
   2.795 -        vertices[1].color = color;
   2.796 -        vertices[1].u = 0.0f;
   2.797 -        vertices[1].v = 0.0f;
   2.798 -
   2.799 -        vertices[2].x = maxx;
   2.800 -        vertices[2].y = maxy;
   2.801 -        vertices[2].z = 0.0f;
   2.802 -        vertices[2].rhw = 1.0f;
   2.803 -        vertices[2].color = color;
   2.804 -        vertices[2].u = 0.0f;
   2.805 -        vertices[2].v = 0.0f;
   2.806 -
   2.807 -        vertices[3].x = minx;
   2.808 -        vertices[3].y = maxy;
   2.809 -        vertices[3].z = 0.0f;
   2.810 -        vertices[3].rhw = 1.0f;
   2.811 -        vertices[3].color = color;
   2.812 -        vertices[3].u = 0.0f;
   2.813 -        vertices[3].v = 0.0f;
   2.814 -
   2.815 -        result =
   2.816 -            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
   2.817 -                                             2, vertices, sizeof(*vertices));
   2.818 -        if (FAILED(result)) {
   2.819 -            D3D_SetError("DrawPrimitiveUP()", result);
   2.820 -            return -1;
   2.821 -        }
   2.822 -    }
   2.823 -    return 0;
   2.824 -}
   2.825 -
   2.826 -static int
   2.827 -D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   2.828 -               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   2.829 -{
   2.830 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   2.831 -    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   2.832 -    LPDIRECT3DPIXELSHADER9 shader = NULL;
   2.833 -    float minx, miny, maxx, maxy;
   2.834 -    float minu, maxu, minv, maxv;
   2.835 -    DWORD color;
   2.836 -    Vertex vertices[4];
   2.837 -    HRESULT result;
   2.838 -
   2.839 -    if (data->beginScene) {
   2.840 -        IDirect3DDevice9_BeginScene(data->device);
   2.841 -        data->beginScene = SDL_FALSE;
   2.842 -    }
   2.843 -
   2.844 -    minx = (float) dstrect->x - 0.5f;
   2.845 -    miny = (float) dstrect->y - 0.5f;
   2.846 -    maxx = (float) dstrect->x + dstrect->w - 0.5f;
   2.847 -    maxy = (float) dstrect->y + dstrect->h - 0.5f;
   2.848 -
   2.849 -    minu = (float) srcrect->x / texture->w;
   2.850 -    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   2.851 -    minv = (float) srcrect->y / texture->h;
   2.852 -    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   2.853 -
   2.854 -    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
   2.855 -
   2.856 -    vertices[0].x = minx;
   2.857 -    vertices[0].y = miny;
   2.858 -    vertices[0].z = 0.0f;
   2.859 -    vertices[0].rhw = 1.0f;
   2.860 -    vertices[0].color = color;
   2.861 -    vertices[0].u = minu;
   2.862 -    vertices[0].v = minv;
   2.863 -
   2.864 -    vertices[1].x = maxx;
   2.865 -    vertices[1].y = miny;
   2.866 -    vertices[1].z = 0.0f;
   2.867 -    vertices[1].rhw = 1.0f;
   2.868 -    vertices[1].color = color;
   2.869 -    vertices[1].u = maxu;
   2.870 -    vertices[1].v = minv;
   2.871 -
   2.872 -    vertices[2].x = maxx;
   2.873 -    vertices[2].y = maxy;
   2.874 -    vertices[2].z = 0.0f;
   2.875 -    vertices[2].rhw = 1.0f;
   2.876 -    vertices[2].color = color;
   2.877 -    vertices[2].u = maxu;
   2.878 -    vertices[2].v = maxv;
   2.879 -
   2.880 -    vertices[3].x = minx;
   2.881 -    vertices[3].y = maxy;
   2.882 -    vertices[3].z = 0.0f;
   2.883 -    vertices[3].rhw = 1.0f;
   2.884 -    vertices[3].color = color;
   2.885 -    vertices[3].u = minu;
   2.886 -    vertices[3].v = maxv;
   2.887 -
   2.888 -    D3D_SetBlendMode(data, texture->blendMode);
   2.889 -
   2.890 -    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   2.891 -                                     D3DTEXF_LINEAR);
   2.892 -    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   2.893 -                                     D3DTEXF_LINEAR);
   2.894 -
   2.895 -    result =
   2.896 -        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
   2.897 -                                    texturedata->texture);
   2.898 -    if (FAILED(result)) {
   2.899 -        D3D_SetError("SetTexture()", result);
   2.900 -        return -1;
   2.901 -    }
   2.902 -    if (shader) {
   2.903 -        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
   2.904 -        if (FAILED(result)) {
   2.905 -            D3D_SetError("SetShader()", result);
   2.906 -            return -1;
   2.907 -        }
   2.908 -    }
   2.909 -    result =
   2.910 -        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   2.911 -                                         vertices, sizeof(*vertices));
   2.912 -    if (FAILED(result)) {
   2.913 -        D3D_SetError("DrawPrimitiveUP()", result);
   2.914 -        return -1;
   2.915 -    }
   2.916 -    if (shader) {
   2.917 -        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
   2.918 -        if (FAILED(result)) {
   2.919 -            D3D_SetError("SetShader()", result);
   2.920 -            return -1;
   2.921 -        }
   2.922 -    }
   2.923 -    return 0;
   2.924 -}
   2.925 -
   2.926 -static int
   2.927 -D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   2.928 -                     Uint32 format, void * pixels, int pitch)
   2.929 -{
   2.930 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   2.931 -    D3DSURFACE_DESC desc;
   2.932 -    LPDIRECT3DSURFACE9 backBuffer;
   2.933 -    LPDIRECT3DSURFACE9 surface;
   2.934 -    RECT d3drect;
   2.935 -    D3DLOCKED_RECT locked;
   2.936 -    HRESULT result;
   2.937 -
   2.938 -    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
   2.939 -    if (FAILED(result)) {
   2.940 -        D3D_SetError("GetBackBuffer()", result);
   2.941 -        return -1;
   2.942 -    }
   2.943 -
   2.944 -    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
   2.945 -    if (FAILED(result)) {
   2.946 -        D3D_SetError("GetDesc()", result);
   2.947 -        IDirect3DSurface9_Release(backBuffer);
   2.948 -        return -1;
   2.949 -    }
   2.950 -
   2.951 -    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
   2.952 -    if (FAILED(result)) {
   2.953 -        D3D_SetError("CreateOffscreenPlainSurface()", result);
   2.954 -        IDirect3DSurface9_Release(backBuffer);
   2.955 -        return -1;
   2.956 -    }
   2.957 -
   2.958 -    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
   2.959 -    if (FAILED(result)) {
   2.960 -        D3D_SetError("GetRenderTargetData()", result);
   2.961 -        IDirect3DSurface9_Release(surface);
   2.962 -        IDirect3DSurface9_Release(backBuffer);
   2.963 -        return -1;
   2.964 -    }
   2.965 -
   2.966 -    d3drect.left = rect->x;
   2.967 -    d3drect.right = rect->x + rect->w;
   2.968 -    d3drect.top = rect->y;
   2.969 -    d3drect.bottom = rect->y + rect->h;
   2.970 -
   2.971 -    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
   2.972 -    if (FAILED(result)) {
   2.973 -        D3D_SetError("LockRect()", result);
   2.974 -        IDirect3DSurface9_Release(surface);
   2.975 -        IDirect3DSurface9_Release(backBuffer);
   2.976 -        return -1;
   2.977 -    }
   2.978 -
   2.979 -    SDL_ConvertPixels(rect->w, rect->h,
   2.980 -                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
   2.981 -                      format, pixels, pitch);
   2.982 -
   2.983 -    IDirect3DSurface9_UnlockRect(surface);
   2.984 -
   2.985 -    IDirect3DSurface9_Release(surface);
   2.986 -    IDirect3DSurface9_Release(backBuffer);
   2.987 -
   2.988 -    return 0;
   2.989 -}
   2.990 -
   2.991 -static void
   2.992 -D3D_RenderPresent(SDL_Renderer * renderer)
   2.993 -{
   2.994 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   2.995 -    HRESULT result;
   2.996 -
   2.997 -    if (!data->beginScene) {
   2.998 -        IDirect3DDevice9_EndScene(data->device);
   2.999 -        data->beginScene = SDL_TRUE;
  2.1000 -    }
  2.1001 -
  2.1002 -    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
  2.1003 -    if (result == D3DERR_DEVICELOST) {
  2.1004 -        /* We'll reset later */
  2.1005 -        return;
  2.1006 -    }
  2.1007 -    if (result == D3DERR_DEVICENOTRESET) {
  2.1008 -        D3D_Reset(renderer);
  2.1009 -    }
  2.1010 -    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
  2.1011 -    if (FAILED(result)) {
  2.1012 -        D3D_SetError("Present()", result);
  2.1013 -    }
  2.1014 -}
  2.1015 -
  2.1016 -static void
  2.1017 -D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  2.1018 -{
  2.1019 -    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
  2.1020 -
  2.1021 -    if (!data) {
  2.1022 -        return;
  2.1023 -    }
  2.1024 -    if (data->texture) {
  2.1025 -        IDirect3DTexture9_Release(data->texture);
  2.1026 -    }
  2.1027 -    SDL_free(data);
  2.1028 -    texture->driverdata = NULL;
  2.1029 -}
  2.1030 -
  2.1031 -static void
  2.1032 -D3D_DestroyRenderer(SDL_Renderer * renderer)
  2.1033 -{
  2.1034 -    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
  2.1035 -
  2.1036 -    if (data) {
  2.1037 -        if (data->device) {
  2.1038 -            IDirect3DDevice9_Release(data->device);
  2.1039 -        }
  2.1040 -        if (data->d3d) {
  2.1041 -            IDirect3D9_Release(data->d3d);
  2.1042 -            SDL_UnloadObject(data->d3dDLL);
  2.1043 -        }
  2.1044 -        SDL_free(data);
  2.1045 -    }
  2.1046 -    SDL_free(renderer);
  2.1047 -}
  2.1048 -
  2.1049 -#endif /* SDL_VIDEO_RENDER_D3D */
  2.1050 -
  2.1051 -/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Sat Feb 05 12:01:11 2011 -0800
     3.3 @@ -0,0 +1,1048 @@
     3.4 +/*
     3.5 +    SDL - Simple DirectMedia Layer
     3.6 +    Copyright (C) 1997-2010 Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Lesser General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2.1 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Lesser General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Lesser General Public
    3.19 +    License along with this library; if not, write to the Free Software
    3.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    slouken@libsdl.org
    3.24 +*/
    3.25 +#include "SDL_config.h"
    3.26 +
    3.27 +#if SDL_VIDEO_RENDER_D3D
    3.28 +
    3.29 +#include "../../core/windows/SDL_windows.h"
    3.30 +
    3.31 +#include "SDL_loadso.h"
    3.32 +#include "SDL_syswm.h"
    3.33 +#include "../SDL_sysrender.h"
    3.34 +
    3.35 +#if SDL_VIDEO_RENDER_D3D
    3.36 +#define D3D_DEBUG_INFO
    3.37 +#include <d3d9.h>
    3.38 +#endif
    3.39 +
    3.40 +#ifdef ASSEMBLE_SHADER
    3.41 +///////////////////////////////////////////////////////////////////////////
    3.42 +// ID3DXBuffer:
    3.43 +// ------------
    3.44 +// The buffer object is used by D3DX to return arbitrary size data.
    3.45 +//
    3.46 +// GetBufferPointer -
    3.47 +//    Returns a pointer to the beginning of the buffer.
    3.48 +//
    3.49 +// GetBufferSize -
    3.50 +//    Returns the size of the buffer, in bytes.
    3.51 +///////////////////////////////////////////////////////////////////////////
    3.52 +
    3.53 +typedef interface ID3DXBuffer ID3DXBuffer;
    3.54 +typedef interface ID3DXBuffer *LPD3DXBUFFER;
    3.55 +
    3.56 +// {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
    3.57 +DEFINE_GUID(IID_ID3DXBuffer, 
    3.58 +0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
    3.59 +
    3.60 +#undef INTERFACE
    3.61 +#define INTERFACE ID3DXBuffer
    3.62 +
    3.63 +typedef interface ID3DXBuffer {
    3.64 +    const struct ID3DXBufferVtbl FAR* lpVtbl;
    3.65 +} ID3DXBuffer;
    3.66 +typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
    3.67 +const struct ID3DXBufferVtbl
    3.68 +{
    3.69 +    // IUnknown
    3.70 +    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
    3.71 +    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
    3.72 +    STDMETHOD_(ULONG, Release)(THIS) PURE;
    3.73 +
    3.74 +    // ID3DXBuffer
    3.75 +    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
    3.76 +    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
    3.77 +};
    3.78 +
    3.79 +HRESULT WINAPI
    3.80 +    D3DXAssembleShader(
    3.81 +        LPCSTR                          pSrcData,
    3.82 +        UINT                            SrcDataLen,
    3.83 +        CONST LPVOID*                   pDefines,
    3.84 +        LPVOID                          pInclude,
    3.85 +        DWORD                           Flags,
    3.86 +        LPD3DXBUFFER*                   ppShader,
    3.87 +        LPD3DXBUFFER*                   ppErrorMsgs);
    3.88 +
    3.89 +#endif /* ASSEMBLE_SHADER */
    3.90 +
    3.91 +
    3.92 +/* Direct3D renderer implementation */
    3.93 +
    3.94 +static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
    3.95 +static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    3.96 +static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    3.97 +                             const SDL_Rect * rect, const void *pixels,
    3.98 +                             int pitch);
    3.99 +static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   3.100 +                           const SDL_Rect * rect, void **pixels, int *pitch);
   3.101 +static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
   3.102 +static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
   3.103 +                                const SDL_Point * points, int count);
   3.104 +static int D3D_RenderDrawLines(SDL_Renderer * renderer,
   3.105 +                               const SDL_Point * points, int count);
   3.106 +static int D3D_RenderFillRects(SDL_Renderer * renderer,
   3.107 +                               const SDL_Rect ** rects, int count);
   3.108 +static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   3.109 +                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
   3.110 +static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   3.111 +                                Uint32 format, void * pixels, int pitch);
   3.112 +static void D3D_RenderPresent(SDL_Renderer * renderer);
   3.113 +static void D3D_DestroyTexture(SDL_Renderer * renderer,
   3.114 +                               SDL_Texture * texture);
   3.115 +static void D3D_DestroyRenderer(SDL_Renderer * renderer);
   3.116 +
   3.117 +
   3.118 +SDL_RenderDriver D3D_RenderDriver = {
   3.119 +    D3D_CreateRenderer,
   3.120 +    {
   3.121 +     "direct3d",
   3.122 +     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
   3.123 +     1,
   3.124 +     {SDL_PIXELFORMAT_ARGB8888},
   3.125 +     0,
   3.126 +     0}
   3.127 +};
   3.128 +
   3.129 +typedef struct
   3.130 +{
   3.131 +    void* d3dDLL;
   3.132 +    IDirect3D9 *d3d;
   3.133 +    IDirect3DDevice9 *device;
   3.134 +    UINT adapter;
   3.135 +    D3DPRESENT_PARAMETERS pparams;
   3.136 +    SDL_bool beginScene;
   3.137 +} D3D_RenderData;
   3.138 +
   3.139 +typedef struct
   3.140 +{
   3.141 +    IDirect3DTexture9 *texture;
   3.142 +} D3D_TextureData;
   3.143 +
   3.144 +typedef struct
   3.145 +{
   3.146 +    float x, y, z;
   3.147 +    float rhw;
   3.148 +    DWORD color;
   3.149 +    float u, v;
   3.150 +} Vertex;
   3.151 +
   3.152 +static void
   3.153 +D3D_SetError(const char *prefix, HRESULT result)
   3.154 +{
   3.155 +    const char *error;
   3.156 +
   3.157 +    switch (result) {
   3.158 +    case D3DERR_WRONGTEXTUREFORMAT:
   3.159 +        error = "WRONGTEXTUREFORMAT";
   3.160 +        break;
   3.161 +    case D3DERR_UNSUPPORTEDCOLOROPERATION:
   3.162 +        error = "UNSUPPORTEDCOLOROPERATION";
   3.163 +        break;
   3.164 +    case D3DERR_UNSUPPORTEDCOLORARG:
   3.165 +        error = "UNSUPPORTEDCOLORARG";
   3.166 +        break;
   3.167 +    case D3DERR_UNSUPPORTEDALPHAOPERATION:
   3.168 +        error = "UNSUPPORTEDALPHAOPERATION";
   3.169 +        break;
   3.170 +    case D3DERR_UNSUPPORTEDALPHAARG:
   3.171 +        error = "UNSUPPORTEDALPHAARG";
   3.172 +        break;
   3.173 +    case D3DERR_TOOMANYOPERATIONS:
   3.174 +        error = "TOOMANYOPERATIONS";
   3.175 +        break;
   3.176 +    case D3DERR_CONFLICTINGTEXTUREFILTER:
   3.177 +        error = "CONFLICTINGTEXTUREFILTER";
   3.178 +        break;
   3.179 +    case D3DERR_UNSUPPORTEDFACTORVALUE:
   3.180 +        error = "UNSUPPORTEDFACTORVALUE";
   3.181 +        break;
   3.182 +    case D3DERR_CONFLICTINGRENDERSTATE:
   3.183 +        error = "CONFLICTINGRENDERSTATE";
   3.184 +        break;
   3.185 +    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
   3.186 +        error = "UNSUPPORTEDTEXTUREFILTER";
   3.187 +        break;
   3.188 +    case D3DERR_CONFLICTINGTEXTUREPALETTE:
   3.189 +        error = "CONFLICTINGTEXTUREPALETTE";
   3.190 +        break;
   3.191 +    case D3DERR_DRIVERINTERNALERROR:
   3.192 +        error = "DRIVERINTERNALERROR";
   3.193 +        break;
   3.194 +    case D3DERR_NOTFOUND:
   3.195 +        error = "NOTFOUND";
   3.196 +        break;
   3.197 +    case D3DERR_MOREDATA:
   3.198 +        error = "MOREDATA";
   3.199 +        break;
   3.200 +    case D3DERR_DEVICELOST:
   3.201 +        error = "DEVICELOST";
   3.202 +        break;
   3.203 +    case D3DERR_DEVICENOTRESET:
   3.204 +        error = "DEVICENOTRESET";
   3.205 +        break;
   3.206 +    case D3DERR_NOTAVAILABLE:
   3.207 +        error = "NOTAVAILABLE";
   3.208 +        break;
   3.209 +    case D3DERR_OUTOFVIDEOMEMORY:
   3.210 +        error = "OUTOFVIDEOMEMORY";
   3.211 +        break;
   3.212 +    case D3DERR_INVALIDDEVICE:
   3.213 +        error = "INVALIDDEVICE";
   3.214 +        break;
   3.215 +    case D3DERR_INVALIDCALL:
   3.216 +        error = "INVALIDCALL";
   3.217 +        break;
   3.218 +    case D3DERR_DRIVERINVALIDCALL:
   3.219 +        error = "DRIVERINVALIDCALL";
   3.220 +        break;
   3.221 +    case D3DERR_WASSTILLDRAWING:
   3.222 +        error = "WASSTILLDRAWING";
   3.223 +        break;
   3.224 +    default:
   3.225 +        error = "UNKNOWN";
   3.226 +        break;
   3.227 +    }
   3.228 +    SDL_SetError("%s: %s", prefix, error);
   3.229 +}
   3.230 +
   3.231 +static D3DFORMAT
   3.232 +PixelFormatToD3DFMT(Uint32 format)
   3.233 +{
   3.234 +    switch (format) {
   3.235 +    case SDL_PIXELFORMAT_RGB565:
   3.236 +        return D3DFMT_R5G6B5;
   3.237 +    case SDL_PIXELFORMAT_RGB888:
   3.238 +        return D3DFMT_X8R8G8B8;
   3.239 +    case SDL_PIXELFORMAT_ARGB8888:
   3.240 +        return D3DFMT_A8R8G8B8;
   3.241 +    default:
   3.242 +        return D3DFMT_UNKNOWN;
   3.243 +    }
   3.244 +}
   3.245 +
   3.246 +static Uint32
   3.247 +D3DFMTToPixelFormat(D3DFORMAT format)
   3.248 +{
   3.249 +    switch (format) {
   3.250 +    case D3DFMT_R5G6B5:
   3.251 +        return SDL_PIXELFORMAT_RGB565;
   3.252 +    case D3DFMT_X8R8G8B8:
   3.253 +        return SDL_PIXELFORMAT_RGB888;
   3.254 +    case D3DFMT_A8R8G8B8:
   3.255 +        return SDL_PIXELFORMAT_ARGB8888;
   3.256 +    default:
   3.257 +        return SDL_PIXELFORMAT_UNKNOWN;
   3.258 +    }
   3.259 +}
   3.260 +
   3.261 +SDL_Renderer *
   3.262 +D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   3.263 +{
   3.264 +    SDL_Renderer *renderer;
   3.265 +    D3D_RenderData *data;
   3.266 +    SDL_SysWMinfo windowinfo;
   3.267 +    HRESULT result;
   3.268 +    D3DPRESENT_PARAMETERS pparams;
   3.269 +    IDirect3DSwapChain9 *chain;
   3.270 +    D3DCAPS9 caps;
   3.271 +    Uint32 window_flags;
   3.272 +    int w, h;
   3.273 +    SDL_DisplayMode fullscreen_mode;
   3.274 +
   3.275 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   3.276 +    if (!renderer) {
   3.277 +        SDL_OutOfMemory();
   3.278 +        return NULL;
   3.279 +    }
   3.280 +
   3.281 +    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
   3.282 +    if (!data) {
   3.283 +        SDL_free(renderer);
   3.284 +        SDL_OutOfMemory();
   3.285 +        return NULL;
   3.286 +    }
   3.287 +
   3.288 +    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
   3.289 +    if (data->d3dDLL) {
   3.290 +        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
   3.291 +
   3.292 +        D3DCreate =
   3.293 +            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
   3.294 +                                                            "Direct3DCreate9");
   3.295 +        if (D3DCreate) {
   3.296 +            data->d3d = D3DCreate(D3D_SDK_VERSION);
   3.297 +        }
   3.298 +        if (!data->d3d) {
   3.299 +            SDL_UnloadObject(data->d3dDLL);
   3.300 +            data->d3dDLL = NULL;
   3.301 +        }
   3.302 +    }
   3.303 +    if (!data->d3d) {
   3.304 +        SDL_free(renderer);
   3.305 +        SDL_free(data);
   3.306 +        SDL_SetError("Unable to create Direct3D interface");
   3.307 +        return NULL;
   3.308 +    }
   3.309 +
   3.310 +    renderer->CreateTexture = D3D_CreateTexture;
   3.311 +    renderer->UpdateTexture = D3D_UpdateTexture;
   3.312 +    renderer->LockTexture = D3D_LockTexture;
   3.313 +    renderer->UnlockTexture = D3D_UnlockTexture;
   3.314 +    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
   3.315 +    renderer->RenderDrawLines = D3D_RenderDrawLines;
   3.316 +    renderer->RenderFillRects = D3D_RenderFillRects;
   3.317 +    renderer->RenderCopy = D3D_RenderCopy;
   3.318 +    renderer->RenderReadPixels = D3D_RenderReadPixels;
   3.319 +    renderer->RenderPresent = D3D_RenderPresent;
   3.320 +    renderer->DestroyTexture = D3D_DestroyTexture;
   3.321 +    renderer->DestroyRenderer = D3D_DestroyRenderer;
   3.322 +    renderer->info = D3D_RenderDriver.info;
   3.323 +    renderer->driverdata = data;
   3.324 +
   3.325 +    renderer->info.flags = SDL_RENDERER_ACCELERATED;
   3.326 +
   3.327 +    SDL_VERSION(&windowinfo.version);
   3.328 +    SDL_GetWindowWMInfo(window, &windowinfo);
   3.329 +
   3.330 +    window_flags = SDL_GetWindowFlags(window);
   3.331 +    SDL_GetWindowSize(window, &w, &h);
   3.332 +    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
   3.333 +
   3.334 +    SDL_zero(pparams);
   3.335 +    pparams.hDeviceWindow = windowinfo.info.win.window;
   3.336 +    pparams.BackBufferWidth = w;
   3.337 +    pparams.BackBufferHeight = h;
   3.338 +    if (window_flags & SDL_WINDOW_FULLSCREEN) {
   3.339 +        pparams.BackBufferFormat =
   3.340 +            PixelFormatToD3DFMT(fullscreen_mode.format);
   3.341 +    } else {
   3.342 +        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   3.343 +    }
   3.344 +    pparams.BackBufferCount = 1;
   3.345 +    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   3.346 +
   3.347 +    if (window_flags & SDL_WINDOW_FULLSCREEN) {
   3.348 +        pparams.Windowed = FALSE;
   3.349 +        pparams.FullScreen_RefreshRateInHz =
   3.350 +            fullscreen_mode.refresh_rate;
   3.351 +    } else {
   3.352 +        pparams.Windowed = TRUE;
   3.353 +        pparams.FullScreen_RefreshRateInHz = 0;
   3.354 +    }
   3.355 +    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   3.356 +        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
   3.357 +    } else {
   3.358 +        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   3.359 +    }
   3.360 +
   3.361 +    /* FIXME: Which adapter? */
   3.362 +    data->adapter = D3DADAPTER_DEFAULT;
   3.363 +    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
   3.364 +
   3.365 +    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
   3.366 +                                     D3DDEVTYPE_HAL,
   3.367 +                                     pparams.hDeviceWindow,
   3.368 +                                     (caps.
   3.369 +                                      DevCaps &
   3.370 +                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
   3.371 +                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
   3.372 +                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   3.373 +                                     &pparams, &data->device);
   3.374 +    if (FAILED(result)) {
   3.375 +        D3D_DestroyRenderer(renderer);
   3.376 +        D3D_SetError("CreateDevice()", result);
   3.377 +        return NULL;
   3.378 +    }
   3.379 +    data->beginScene = SDL_TRUE;
   3.380 +
   3.381 +    /* Get presentation parameters to fill info */
   3.382 +    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
   3.383 +    if (FAILED(result)) {
   3.384 +        D3D_DestroyRenderer(renderer);
   3.385 +        D3D_SetError("GetSwapChain()", result);
   3.386 +        return NULL;
   3.387 +    }
   3.388 +    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
   3.389 +    if (FAILED(result)) {
   3.390 +        IDirect3DSwapChain9_Release(chain);
   3.391 +        D3D_DestroyRenderer(renderer);
   3.392 +        D3D_SetError("GetPresentParameters()", result);
   3.393 +        return NULL;
   3.394 +    }
   3.395 +    IDirect3DSwapChain9_Release(chain);
   3.396 +    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
   3.397 +        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   3.398 +    }
   3.399 +    data->pparams = pparams;
   3.400 +
   3.401 +    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
   3.402 +    renderer->info.max_texture_width = caps.MaxTextureWidth;
   3.403 +    renderer->info.max_texture_height = caps.MaxTextureHeight;
   3.404 +
   3.405 +    /* Set up parameters for rendering */
   3.406 +    IDirect3DDevice9_SetVertexShader(data->device, NULL);
   3.407 +    IDirect3DDevice9_SetFVF(data->device,
   3.408 +                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
   3.409 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
   3.410 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   3.411 +                                    D3DCULL_NONE);
   3.412 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   3.413 +    /* Enable color modulation by diffuse color */
   3.414 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
   3.415 +                                          D3DTOP_MODULATE);
   3.416 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
   3.417 +                                          D3DTA_TEXTURE);
   3.418 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
   3.419 +                                          D3DTA_DIFFUSE);
   3.420 +    /* Enable alpha modulation by diffuse alpha */
   3.421 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
   3.422 +                                          D3DTOP_MODULATE);
   3.423 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
   3.424 +                                          D3DTA_TEXTURE);
   3.425 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
   3.426 +                                          D3DTA_DIFFUSE);
   3.427 +    /* Disable second texture stage, since we're done */
   3.428 +    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
   3.429 +                                          D3DTOP_DISABLE);
   3.430 +    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
   3.431 +                                          D3DTOP_DISABLE);
   3.432 +
   3.433 +    return renderer;
   3.434 +}
   3.435 +
   3.436 +static int
   3.437 +D3D_Reset(SDL_Renderer * renderer)
   3.438 +{
   3.439 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   3.440 +    HRESULT result;
   3.441 +
   3.442 +    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
   3.443 +    if (FAILED(result)) {
   3.444 +        if (result == D3DERR_DEVICELOST) {
   3.445 +            /* Don't worry about it, we'll reset later... */
   3.446 +            return 0;
   3.447 +        } else {
   3.448 +            D3D_SetError("Reset()", result);
   3.449 +            return -1;
   3.450 +        }
   3.451 +    }
   3.452 +    IDirect3DDevice9_SetVertexShader(data->device, NULL);
   3.453 +    IDirect3DDevice9_SetFVF(data->device,
   3.454 +                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
   3.455 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   3.456 +                                    D3DCULL_NONE);
   3.457 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   3.458 +    return 0;
   3.459 +}
   3.460 +
   3.461 +/* FIXME: This needs to be called... when? */
   3.462 +#if 0
   3.463 +static int
   3.464 +D3D_DisplayModeChanged(SDL_Renderer * renderer)
   3.465 +{
   3.466 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   3.467 +    SDL_Window *window = renderer->window;
   3.468 +    SDL_VideoDisplay *display = window->display;
   3.469 +
   3.470 +    data->pparams.BackBufferWidth = window->w;
   3.471 +    data->pparams.BackBufferHeight = window->h;
   3.472 +    if (window->flags & SDL_WINDOW_FULLSCREEN) {
   3.473 +        data->pparams.BackBufferFormat =
   3.474 +            PixelFormatToD3DFMT(window->fullscreen_mode.format);
   3.475 +    } else {
   3.476 +        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   3.477 +    }
   3.478 +    return D3D_Reset(renderer);
   3.479 +}
   3.480 +#endif
   3.481 +
   3.482 +static int
   3.483 +D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   3.484 +{
   3.485 +    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   3.486 +    SDL_Window *window = renderer->window;
   3.487 +    D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
   3.488 +    D3D_TextureData *data;
   3.489 +    D3DPOOL pool;
   3.490 +    DWORD usage;
   3.491 +    HRESULT result;
   3.492 +
   3.493 +    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
   3.494 +    if (!data) {
   3.495 +        SDL_OutOfMemory();
   3.496 +        return -1;
   3.497 +    }
   3.498 +
   3.499 +    texture->driverdata = data;
   3.500 +
   3.501 +#ifdef USE_DYNAMIC_TEXTURE
   3.502 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   3.503 +        pool = D3DPOOL_DEFAULT;
   3.504 +        usage = D3DUSAGE_DYNAMIC;
   3.505 +    } else
   3.506 +#endif
   3.507 +    {
   3.508 +        pool = D3DPOOL_MANAGED;
   3.509 +        usage = 0;
   3.510 +    }
   3.511 +
   3.512 +    result =
   3.513 +        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   3.514 +                                       texture->h, 1, usage,
   3.515 +                                       PixelFormatToD3DFMT(texture->format),
   3.516 +                                       pool, &data->texture, NULL);
   3.517 +    if (FAILED(result)) {
   3.518 +        D3D_SetError("CreateTexture()", result);
   3.519 +        return -1;
   3.520 +    }
   3.521 +
   3.522 +    return 0;
   3.523 +}
   3.524 +
   3.525 +static int
   3.526 +D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   3.527 +                  const SDL_Rect * rect, const void *pixels, int pitch)
   3.528 +{
   3.529 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   3.530 +    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   3.531 +    RECT d3drect;
   3.532 +    D3DLOCKED_RECT locked;
   3.533 +    const Uint8 *src;
   3.534 +    Uint8 *dst;
   3.535 +    int row, length;
   3.536 +    HRESULT result;
   3.537 +
   3.538 +#ifdef USE_DYNAMIC_TEXTURE
   3.539 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
   3.540 +        rect->x == 0 && rect->y == 0 &&
   3.541 +        rect->w == texture->w && rect->h == texture->h) {
   3.542 +        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
   3.543 +    } else
   3.544 +#endif
   3.545 +    {
   3.546 +        d3drect.left = rect->x;
   3.547 +        d3drect.right = rect->x + rect->w;
   3.548 +        d3drect.top = rect->y;
   3.549 +        d3drect.bottom = rect->y + rect->h;
   3.550 +        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   3.551 +    }
   3.552 +
   3.553 +    if (FAILED(result)) {
   3.554 +        D3D_SetError("LockRect()", result);
   3.555 +        return -1;
   3.556 +    }
   3.557 +
   3.558 +    src = pixels;
   3.559 +    dst = locked.pBits;
   3.560 +    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   3.561 +    if (length == pitch && length == locked.Pitch) {
   3.562 +        SDL_memcpy(dst, src, length*rect->h);
   3.563 +    } else {
   3.564 +        for (row = 0; row < rect->h; ++row) {
   3.565 +            SDL_memcpy(dst, src, length);
   3.566 +            src += pitch;
   3.567 +            dst += locked.Pitch;
   3.568 +        }
   3.569 +    }
   3.570 +    IDirect3DTexture9_UnlockRect(data->texture, 0);
   3.571 +
   3.572 +    return 0;
   3.573 +}
   3.574 +
   3.575 +static int
   3.576 +D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   3.577 +                const SDL_Rect * rect, void **pixels, int *pitch)
   3.578 +{
   3.579 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   3.580 +    RECT d3drect;
   3.581 +    D3DLOCKED_RECT locked;
   3.582 +    HRESULT result;
   3.583 +
   3.584 +    d3drect.left = rect->x;
   3.585 +    d3drect.right = rect->x + rect->w;
   3.586 +    d3drect.top = rect->y;
   3.587 +    d3drect.bottom = rect->y + rect->h;
   3.588 +
   3.589 +    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   3.590 +    if (FAILED(result)) {
   3.591 +        D3D_SetError("LockRect()", result);
   3.592 +        return -1;
   3.593 +    }
   3.594 +    *pixels = locked.pBits;
   3.595 +    *pitch = locked.Pitch;
   3.596 +    return 0;
   3.597 +}
   3.598 +
   3.599 +static void
   3.600 +D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   3.601 +{
   3.602 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   3.603 +
   3.604 +    IDirect3DTexture9_UnlockRect(data->texture, 0);
   3.605 +}
   3.606 +
   3.607 +static void
   3.608 +D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
   3.609 +{
   3.610 +    switch (blendMode) {
   3.611 +    case SDL_BLENDMODE_NONE:
   3.612 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   3.613 +                                        FALSE);
   3.614 +        break;
   3.615 +    case SDL_BLENDMODE_BLEND:
   3.616 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   3.617 +                                        TRUE);
   3.618 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   3.619 +                                        D3DBLEND_SRCALPHA);
   3.620 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   3.621 +                                        D3DBLEND_INVSRCALPHA);
   3.622 +        break;
   3.623 +    case SDL_BLENDMODE_ADD:
   3.624 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   3.625 +                                        TRUE);
   3.626 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   3.627 +                                        D3DBLEND_SRCALPHA);
   3.628 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   3.629 +                                        D3DBLEND_ONE);
   3.630 +        break;
   3.631 +    case SDL_BLENDMODE_MOD:
   3.632 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   3.633 +                                        TRUE);
   3.634 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   3.635 +                                        D3DBLEND_ZERO);
   3.636 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   3.637 +                                        D3DBLEND_SRCCOLOR);
   3.638 +        break;
   3.639 +    }
   3.640 +}
   3.641 +
   3.642 +static int
   3.643 +D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   3.644 +                     int count)
   3.645 +{
   3.646 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   3.647 +    DWORD color;
   3.648 +    Vertex *vertices;
   3.649 +    int i;
   3.650 +    HRESULT result;
   3.651 +
   3.652 +    if (data->beginScene) {
   3.653 +        IDirect3DDevice9_BeginScene(data->device);
   3.654 +        data->beginScene = SDL_FALSE;
   3.655 +    }
   3.656 +
   3.657 +    D3D_SetBlendMode(data, renderer->blendMode);
   3.658 +
   3.659 +    result =
   3.660 +        IDirect3DDevice9_SetTexture(data->device, 0,
   3.661 +                                    (IDirect3DBaseTexture9 *) 0);
   3.662 +    if (FAILED(result)) {
   3.663 +        D3D_SetError("SetTexture()", result);
   3.664 +        return -1;
   3.665 +    }
   3.666 +
   3.667 +    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   3.668 +
   3.669 +    vertices = SDL_stack_alloc(Vertex, count);
   3.670 +    for (i = 0; i < count; ++i) {
   3.671 +        vertices[i].x = (float) points[i].x;
   3.672 +        vertices[i].y = (float) points[i].y;
   3.673 +        vertices[i].z = 0.0f;
   3.674 +        vertices[i].rhw = 1.0f;
   3.675 +        vertices[i].color = color;
   3.676 +        vertices[i].u = 0.0f;
   3.677 +        vertices[i].v = 0.0f;
   3.678 +    }
   3.679 +    result =
   3.680 +        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
   3.681 +                                         vertices, sizeof(*vertices));
   3.682 +    SDL_stack_free(vertices);
   3.683 +    if (FAILED(result)) {
   3.684 +        D3D_SetError("DrawPrimitiveUP()", result);
   3.685 +        return -1;
   3.686 +    }
   3.687 +    return 0;
   3.688 +}
   3.689 +
   3.690 +static int
   3.691 +D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   3.692 +                    int count)
   3.693 +{
   3.694 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   3.695 +    DWORD color;
   3.696 +    Vertex *vertices;
   3.697 +    int i;
   3.698 +    HRESULT result;
   3.699 +
   3.700 +    if (data->beginScene) {
   3.701 +        IDirect3DDevice9_BeginScene(data->device);
   3.702 +        data->beginScene = SDL_FALSE;
   3.703 +    }
   3.704 +
   3.705 +    D3D_SetBlendMode(data, renderer->blendMode);
   3.706 +
   3.707 +    result =
   3.708 +        IDirect3DDevice9_SetTexture(data->device, 0,
   3.709 +                                    (IDirect3DBaseTexture9 *) 0);
   3.710 +    if (FAILED(result)) {
   3.711 +        D3D_SetError("SetTexture()", result);
   3.712 +        return -1;
   3.713 +    }
   3.714 +
   3.715 +    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   3.716 +
   3.717 +    vertices = SDL_stack_alloc(Vertex, count);
   3.718 +    for (i = 0; i < count; ++i) {
   3.719 +        vertices[i].x = (float) points[i].x;
   3.720 +        vertices[i].y = (float) points[i].y;
   3.721 +        vertices[i].z = 0.0f;
   3.722 +        vertices[i].rhw = 1.0f;
   3.723 +        vertices[i].color = color;
   3.724 +        vertices[i].u = 0.0f;
   3.725 +        vertices[i].v = 0.0f;
   3.726 +    }
   3.727 +    result =
   3.728 +        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
   3.729 +                                         vertices, sizeof(*vertices));
   3.730 +
   3.731 +    /* DirectX 9 has the same line rasterization semantics as GDI,
   3.732 +       so we need to close the endpoint of the line */
   3.733 +    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
   3.734 +        vertices[0].x = (float) points[count-1].x;
   3.735 +        vertices[0].y = (float) points[count-1].y;
   3.736 +        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
   3.737 +    }
   3.738 +
   3.739 +    SDL_stack_free(vertices);
   3.740 +    if (FAILED(result)) {
   3.741 +        D3D_SetError("DrawPrimitiveUP()", result);
   3.742 +        return -1;
   3.743 +    }
   3.744 +    return 0;
   3.745 +}
   3.746 +
   3.747 +static int
   3.748 +D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   3.749 +                    int count)
   3.750 +{
   3.751 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   3.752 +    DWORD color;
   3.753 +    int i;
   3.754 +    float minx, miny, maxx, maxy;
   3.755 +    Vertex vertices[4];
   3.756 +    HRESULT result;
   3.757 +
   3.758 +    if (data->beginScene) {
   3.759 +        IDirect3DDevice9_BeginScene(data->device);
   3.760 +        data->beginScene = SDL_FALSE;
   3.761 +    }
   3.762 +
   3.763 +    D3D_SetBlendMode(data, renderer->blendMode);
   3.764 +
   3.765 +    result =
   3.766 +        IDirect3DDevice9_SetTexture(data->device, 0,
   3.767 +                                    (IDirect3DBaseTexture9 *) 0);
   3.768 +    if (FAILED(result)) {
   3.769 +        D3D_SetError("SetTexture()", result);
   3.770 +        return -1;
   3.771 +    }
   3.772 +
   3.773 +    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   3.774 +
   3.775 +    for (i = 0; i < count; ++i) {
   3.776 +        const SDL_Rect *rect = rects[i];
   3.777 +
   3.778 +        minx = (float) rect->x;
   3.779 +        miny = (float) rect->y;
   3.780 +        maxx = (float) rect->x + rect->w;
   3.781 +        maxy = (float) rect->y + rect->h;
   3.782 +
   3.783 +        vertices[0].x = minx;
   3.784 +        vertices[0].y = miny;
   3.785 +        vertices[0].z = 0.0f;
   3.786 +        vertices[0].rhw = 1.0f;
   3.787 +        vertices[0].color = color;
   3.788 +        vertices[0].u = 0.0f;
   3.789 +        vertices[0].v = 0.0f;
   3.790 +
   3.791 +        vertices[1].x = maxx;
   3.792 +        vertices[1].y = miny;
   3.793 +        vertices[1].z = 0.0f;
   3.794 +        vertices[1].rhw = 1.0f;
   3.795 +        vertices[1].color = color;
   3.796 +        vertices[1].u = 0.0f;
   3.797 +        vertices[1].v = 0.0f;
   3.798 +
   3.799 +        vertices[2].x = maxx;
   3.800 +        vertices[2].y = maxy;
   3.801 +        vertices[2].z = 0.0f;
   3.802 +        vertices[2].rhw = 1.0f;
   3.803 +        vertices[2].color = color;
   3.804 +        vertices[2].u = 0.0f;
   3.805 +        vertices[2].v = 0.0f;
   3.806 +
   3.807 +        vertices[3].x = minx;
   3.808 +        vertices[3].y = maxy;
   3.809 +        vertices[3].z = 0.0f;
   3.810 +        vertices[3].rhw = 1.0f;
   3.811 +        vertices[3].color = color;
   3.812 +        vertices[3].u = 0.0f;
   3.813 +        vertices[3].v = 0.0f;
   3.814 +
   3.815 +        result =
   3.816 +            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
   3.817 +                                             2, vertices, sizeof(*vertices));
   3.818 +        if (FAILED(result)) {
   3.819 +            D3D_SetError("DrawPrimitiveUP()", result);
   3.820 +            return -1;
   3.821 +        }
   3.822 +    }
   3.823 +    return 0;
   3.824 +}
   3.825 +
   3.826 +static int
   3.827 +D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   3.828 +               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   3.829 +{
   3.830 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   3.831 +    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   3.832 +    LPDIRECT3DPIXELSHADER9 shader = NULL;
   3.833 +    float minx, miny, maxx, maxy;
   3.834 +    float minu, maxu, minv, maxv;
   3.835 +    DWORD color;
   3.836 +    Vertex vertices[4];
   3.837 +    HRESULT result;
   3.838 +
   3.839 +    if (data->beginScene) {
   3.840 +        IDirect3DDevice9_BeginScene(data->device);
   3.841 +        data->beginScene = SDL_FALSE;
   3.842 +    }
   3.843 +
   3.844 +    minx = (float) dstrect->x - 0.5f;
   3.845 +    miny = (float) dstrect->y - 0.5f;
   3.846 +    maxx = (float) dstrect->x + dstrect->w - 0.5f;
   3.847 +    maxy = (float) dstrect->y + dstrect->h - 0.5f;
   3.848 +
   3.849 +    minu = (float) srcrect->x / texture->w;
   3.850 +    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   3.851 +    minv = (float) srcrect->y / texture->h;
   3.852 +    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   3.853 +
   3.854 +    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
   3.855 +
   3.856 +    vertices[0].x = minx;
   3.857 +    vertices[0].y = miny;
   3.858 +    vertices[0].z = 0.0f;
   3.859 +    vertices[0].rhw = 1.0f;
   3.860 +    vertices[0].color = color;
   3.861 +    vertices[0].u = minu;
   3.862 +    vertices[0].v = minv;
   3.863 +
   3.864 +    vertices[1].x = maxx;
   3.865 +    vertices[1].y = miny;
   3.866 +    vertices[1].z = 0.0f;
   3.867 +    vertices[1].rhw = 1.0f;
   3.868 +    vertices[1].color = color;
   3.869 +    vertices[1].u = maxu;
   3.870 +    vertices[1].v = minv;
   3.871 +
   3.872 +    vertices[2].x = maxx;
   3.873 +    vertices[2].y = maxy;
   3.874 +    vertices[2].z = 0.0f;
   3.875 +    vertices[2].rhw = 1.0f;
   3.876 +    vertices[2].color = color;
   3.877 +    vertices[2].u = maxu;
   3.878 +    vertices[2].v = maxv;
   3.879 +
   3.880 +    vertices[3].x = minx;
   3.881 +    vertices[3].y = maxy;
   3.882 +    vertices[3].z = 0.0f;
   3.883 +    vertices[3].rhw = 1.0f;
   3.884 +    vertices[3].color = color;
   3.885 +    vertices[3].u = minu;
   3.886 +    vertices[3].v = maxv;
   3.887 +
   3.888 +    D3D_SetBlendMode(data, texture->blendMode);
   3.889 +
   3.890 +    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   3.891 +                                     D3DTEXF_LINEAR);
   3.892 +    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   3.893 +                                     D3DTEXF_LINEAR);
   3.894 +
   3.895 +    result =
   3.896 +        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
   3.897 +                                    texturedata->texture);
   3.898 +    if (FAILED(result)) {
   3.899 +        D3D_SetError("SetTexture()", result);
   3.900 +        return -1;
   3.901 +    }
   3.902 +    if (shader) {
   3.903 +        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
   3.904 +        if (FAILED(result)) {
   3.905 +            D3D_SetError("SetShader()", result);
   3.906 +            return -1;
   3.907 +        }
   3.908 +    }
   3.909 +    result =
   3.910 +        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   3.911 +                                         vertices, sizeof(*vertices));
   3.912 +    if (FAILED(result)) {
   3.913 +        D3D_SetError("DrawPrimitiveUP()", result);
   3.914 +        return -1;
   3.915 +    }
   3.916 +    if (shader) {
   3.917 +        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
   3.918 +        if (FAILED(result)) {
   3.919 +            D3D_SetError("SetShader()", result);
   3.920 +            return -1;
   3.921 +        }
   3.922 +    }
   3.923 +    return 0;
   3.924 +}
   3.925 +
   3.926 +static int
   3.927 +D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   3.928 +                     Uint32 format, void * pixels, int pitch)
   3.929 +{
   3.930 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   3.931 +    D3DSURFACE_DESC desc;
   3.932 +    LPDIRECT3DSURFACE9 backBuffer;
   3.933 +    LPDIRECT3DSURFACE9 surface;
   3.934 +    RECT d3drect;
   3.935 +    D3DLOCKED_RECT locked;
   3.936 +    HRESULT result;
   3.937 +
   3.938 +    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
   3.939 +    if (FAILED(result)) {
   3.940 +        D3D_SetError("GetBackBuffer()", result);
   3.941 +        return -1;
   3.942 +    }
   3.943 +
   3.944 +    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
   3.945 +    if (FAILED(result)) {
   3.946 +        D3D_SetError("GetDesc()", result);
   3.947 +        IDirect3DSurface9_Release(backBuffer);
   3.948 +        return -1;
   3.949 +    }
   3.950 +
   3.951 +    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
   3.952 +    if (FAILED(result)) {
   3.953 +        D3D_SetError("CreateOffscreenPlainSurface()", result);
   3.954 +        IDirect3DSurface9_Release(backBuffer);
   3.955 +        return -1;
   3.956 +    }
   3.957 +
   3.958 +    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
   3.959 +    if (FAILED(result)) {
   3.960 +        D3D_SetError("GetRenderTargetData()", result);
   3.961 +        IDirect3DSurface9_Release(surface);
   3.962 +        IDirect3DSurface9_Release(backBuffer);
   3.963 +        return -1;
   3.964 +    }
   3.965 +
   3.966 +    d3drect.left = rect->x;
   3.967 +    d3drect.right = rect->x + rect->w;
   3.968 +    d3drect.top = rect->y;
   3.969 +    d3drect.bottom = rect->y + rect->h;
   3.970 +
   3.971 +    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
   3.972 +    if (FAILED(result)) {
   3.973 +        D3D_SetError("LockRect()", result);
   3.974 +        IDirect3DSurface9_Release(surface);
   3.975 +        IDirect3DSurface9_Release(backBuffer);
   3.976 +        return -1;
   3.977 +    }
   3.978 +
   3.979 +    SDL_ConvertPixels(rect->w, rect->h,
   3.980 +                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
   3.981 +                      format, pixels, pitch);
   3.982 +
   3.983 +    IDirect3DSurface9_UnlockRect(surface);
   3.984 +
   3.985 +    IDirect3DSurface9_Release(surface);
   3.986 +    IDirect3DSurface9_Release(backBuffer);
   3.987 +
   3.988 +    return 0;
   3.989 +}
   3.990 +
   3.991 +static void
   3.992 +D3D_RenderPresent(SDL_Renderer * renderer)
   3.993 +{
   3.994 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   3.995 +    HRESULT result;
   3.996 +
   3.997 +    if (!data->beginScene) {
   3.998 +        IDirect3DDevice9_EndScene(data->device);
   3.999 +        data->beginScene = SDL_TRUE;
  3.1000 +    }
  3.1001 +
  3.1002 +    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
  3.1003 +    if (result == D3DERR_DEVICELOST) {
  3.1004 +        /* We'll reset later */
  3.1005 +        return;
  3.1006 +    }
  3.1007 +    if (result == D3DERR_DEVICENOTRESET) {
  3.1008 +        D3D_Reset(renderer);
  3.1009 +    }
  3.1010 +    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
  3.1011 +    if (FAILED(result)) {
  3.1012 +        D3D_SetError("Present()", result);
  3.1013 +    }
  3.1014 +}
  3.1015 +
  3.1016 +static void
  3.1017 +D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  3.1018 +{
  3.1019 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
  3.1020 +
  3.1021 +    if (!data) {
  3.1022 +        return;
  3.1023 +    }
  3.1024 +    if (data->texture) {
  3.1025 +        IDirect3DTexture9_Release(data->texture);
  3.1026 +    }
  3.1027 +    SDL_free(data);
  3.1028 +    texture->driverdata = NULL;
  3.1029 +}
  3.1030 +
  3.1031 +static void
  3.1032 +D3D_DestroyRenderer(SDL_Renderer * renderer)
  3.1033 +{
  3.1034 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
  3.1035 +
  3.1036 +    if (data) {
  3.1037 +        if (data->device) {
  3.1038 +            IDirect3DDevice9_Release(data->device);
  3.1039 +        }
  3.1040 +        if (data->d3d) {
  3.1041 +            IDirect3D9_Release(data->d3d);
  3.1042 +            SDL_UnloadObject(data->d3dDLL);
  3.1043 +        }
  3.1044 +        SDL_free(data);
  3.1045 +    }
  3.1046 +    SDL_free(renderer);
  3.1047 +}
  3.1048 +
  3.1049 +#endif /* SDL_VIDEO_RENDER_D3D */
  3.1050 +
  3.1051 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/render/opengl/SDL_render_gl.c	Sat Feb 05 12:01:11 2011 -0800
     4.3 @@ -0,0 +1,826 @@
     4.4 +/*
     4.5 +    SDL - Simple DirectMedia Layer
     4.6 +    Copyright (C) 1997-2010 Sam Lantinga
     4.7 +
     4.8 +    This library is free software; you can redistribute it and/or
     4.9 +    modify it under the terms of the GNU Lesser General Public
    4.10 +    License as published by the Free Software Foundation; either
    4.11 +    version 2.1 of the License, or (at your option) any later version.
    4.12 +
    4.13 +    This library is distributed in the hope that it will be useful,
    4.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.16 +    Lesser General Public License for more details.
    4.17 +
    4.18 +    You should have received a copy of the GNU Lesser General Public
    4.19 +    License along with this library; if not, write to the Free Software
    4.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    4.21 +
    4.22 +    Sam Lantinga
    4.23 +    slouken@libsdl.org
    4.24 +*/
    4.25 +#include "SDL_config.h"
    4.26 +
    4.27 +#if SDL_VIDEO_RENDER_OGL
    4.28 +
    4.29 +#include "SDL_opengl.h"
    4.30 +#include "../SDL_sysrender.h"
    4.31 +
    4.32 +#ifdef __MACOSX__
    4.33 +#include <OpenGL/OpenGL.h>
    4.34 +#endif
    4.35 +
    4.36 +
    4.37 +/* OpenGL renderer implementation */
    4.38 +
    4.39 +/* Details on optimizing the texture path on Mac OS X:
    4.40 +   http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/chapter_10_section_2.html
    4.41 +*/
    4.42 +
    4.43 +/* Used to re-create the window with OpenGL capability */
    4.44 +extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
    4.45 +
    4.46 +static const float inv255f = 1.0f / 255.0f;
    4.47 +
    4.48 +static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
    4.49 +static void GL_WindowEvent(SDL_Renderer * renderer,
    4.50 +                           const SDL_WindowEvent *event);
    4.51 +static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    4.52 +static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    4.53 +                            const SDL_Rect * rect, const void *pixels,
    4.54 +                            int pitch);
    4.55 +static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    4.56 +                          const SDL_Rect * rect, void **pixels, int *pitch);
    4.57 +static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    4.58 +static int GL_RenderClear(SDL_Renderer * renderer);
    4.59 +static int GL_RenderDrawPoints(SDL_Renderer * renderer,
    4.60 +                               const SDL_Point * points, int count);
    4.61 +static int GL_RenderDrawLines(SDL_Renderer * renderer,
    4.62 +                              const SDL_Point * points, int count);
    4.63 +static int GL_RenderFillRects(SDL_Renderer * renderer,
    4.64 +                              const SDL_Rect ** rects, int count);
    4.65 +static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    4.66 +                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
    4.67 +static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    4.68 +                               Uint32 pixel_format, void * pixels, int pitch);
    4.69 +static void GL_RenderPresent(SDL_Renderer * renderer);
    4.70 +static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    4.71 +static void GL_DestroyRenderer(SDL_Renderer * renderer);
    4.72 +
    4.73 +
    4.74 +SDL_RenderDriver GL_RenderDriver = {
    4.75 +    GL_CreateRenderer,
    4.76 +    {
    4.77 +     "opengl",
    4.78 +     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
    4.79 +     1,
    4.80 +     {SDL_PIXELFORMAT_ARGB8888},
    4.81 +     0,
    4.82 +     0}
    4.83 +};
    4.84 +
    4.85 +typedef struct
    4.86 +{
    4.87 +    SDL_GLContext context;
    4.88 +    SDL_bool updateSize;
    4.89 +    SDL_bool GL_ARB_texture_rectangle_supported;
    4.90 +    int blendMode;
    4.91 +
    4.92 +    /* OpenGL functions */
    4.93 +#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
    4.94 +#include "../../video/SDL_glfuncs.h"
    4.95 +#undef SDL_PROC
    4.96 +
    4.97 +    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
    4.98 +                                 const GLvoid * pointer);
    4.99 +} GL_RenderData;
   4.100 +
   4.101 +typedef struct
   4.102 +{
   4.103 +    GLuint texture;
   4.104 +    GLenum type;
   4.105 +    GLfloat texw;
   4.106 +    GLfloat texh;
   4.107 +    GLenum format;
   4.108 +    GLenum formattype;
   4.109 +    void *pixels;
   4.110 +    int pitch;
   4.111 +} GL_TextureData;
   4.112 +
   4.113 +
   4.114 +static void
   4.115 +GL_SetError(const char *prefix, GLenum result)
   4.116 +{
   4.117 +    const char *error;
   4.118 +
   4.119 +    switch (result) {
   4.120 +    case GL_NO_ERROR:
   4.121 +        error = "GL_NO_ERROR";
   4.122 +        break;
   4.123 +    case GL_INVALID_ENUM:
   4.124 +        error = "GL_INVALID_ENUM";
   4.125 +        break;
   4.126 +    case GL_INVALID_VALUE:
   4.127 +        error = "GL_INVALID_VALUE";
   4.128 +        break;
   4.129 +    case GL_INVALID_OPERATION:
   4.130 +        error = "GL_INVALID_OPERATION";
   4.131 +        break;
   4.132 +    case GL_STACK_OVERFLOW:
   4.133 +        error = "GL_STACK_OVERFLOW";
   4.134 +        break;
   4.135 +    case GL_STACK_UNDERFLOW:
   4.136 +        error = "GL_STACK_UNDERFLOW";
   4.137 +        break;
   4.138 +    case GL_OUT_OF_MEMORY:
   4.139 +        error = "GL_OUT_OF_MEMORY";
   4.140 +        break;
   4.141 +    case GL_TABLE_TOO_LARGE:
   4.142 +        error = "GL_TABLE_TOO_LARGE";
   4.143 +        break;
   4.144 +    default:
   4.145 +        error = "UNKNOWN";
   4.146 +        break;
   4.147 +    }
   4.148 +    SDL_SetError("%s: %s", prefix, error);
   4.149 +}
   4.150 +
   4.151 +static int
   4.152 +GL_LoadFunctions(GL_RenderData * data)
   4.153 +{
   4.154 +#ifdef __SDL_NOGETPROCADDR__
   4.155 +#define SDL_PROC(ret,func,params) data->func=func;
   4.156 +#else
   4.157 +#define SDL_PROC(ret,func,params) \
   4.158 +    do { \
   4.159 +        data->func = SDL_GL_GetProcAddress(#func); \
   4.160 +        if ( ! data->func ) { \
   4.161 +            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
   4.162 +            return -1; \
   4.163 +        } \
   4.164 +    } while ( 0 );
   4.165 +#endif /* __SDL_NOGETPROCADDR__ */
   4.166 +
   4.167 +#include "../../video/SDL_glfuncs.h"
   4.168 +#undef SDL_PROC
   4.169 +    return 0;
   4.170 +}
   4.171 +
   4.172 +SDL_Renderer *
   4.173 +GL_CreateRenderer(SDL_Window * window, Uint32 flags)
   4.174 +{
   4.175 +    SDL_Renderer *renderer;
   4.176 +    GL_RenderData *data;
   4.177 +    GLint value;
   4.178 +    Uint32 window_flags;
   4.179 +
   4.180 +    window_flags = SDL_GetWindowFlags(window);
   4.181 +    if (!(window_flags & SDL_WINDOW_OPENGL)) {
   4.182 +        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
   4.183 +            return NULL;
   4.184 +        }
   4.185 +    }
   4.186 +
   4.187 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   4.188 +    if (!renderer) {
   4.189 +        SDL_OutOfMemory();
   4.190 +        return NULL;
   4.191 +    }
   4.192 +
   4.193 +    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
   4.194 +    if (!data) {
   4.195 +        GL_DestroyRenderer(renderer);
   4.196 +        SDL_OutOfMemory();
   4.197 +        return NULL;
   4.198 +    }
   4.199 +
   4.200 +    renderer->WindowEvent = GL_WindowEvent;
   4.201 +    renderer->CreateTexture = GL_CreateTexture;
   4.202 +    renderer->UpdateTexture = GL_UpdateTexture;
   4.203 +    renderer->LockTexture = GL_LockTexture;
   4.204 +    renderer->UnlockTexture = GL_UnlockTexture;
   4.205 +    renderer->RenderClear = GL_RenderClear;
   4.206 +    renderer->RenderDrawPoints = GL_RenderDrawPoints;
   4.207 +    renderer->RenderDrawLines = GL_RenderDrawLines;
   4.208 +    renderer->RenderFillRects = GL_RenderFillRects;
   4.209 +    renderer->RenderCopy = GL_RenderCopy;
   4.210 +    renderer->RenderReadPixels = GL_RenderReadPixels;
   4.211 +    renderer->RenderPresent = GL_RenderPresent;
   4.212 +    renderer->DestroyTexture = GL_DestroyTexture;
   4.213 +    renderer->DestroyRenderer = GL_DestroyRenderer;
   4.214 +    renderer->info = GL_RenderDriver.info;
   4.215 +    renderer->driverdata = data;
   4.216 +
   4.217 +    renderer->info.flags = SDL_RENDERER_ACCELERATED;
   4.218 +
   4.219 +    if (GL_LoadFunctions(data) < 0) {
   4.220 +        GL_DestroyRenderer(renderer);
   4.221 +        return NULL;
   4.222 +    }
   4.223 +
   4.224 +    data->context = SDL_GL_CreateContext(window);
   4.225 +    if (!data->context) {
   4.226 +        GL_DestroyRenderer(renderer);
   4.227 +        return NULL;
   4.228 +    }
   4.229 +    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   4.230 +        GL_DestroyRenderer(renderer);
   4.231 +        return NULL;
   4.232 +    }
   4.233 +#ifdef __MACOSX__
   4.234 +    /* Enable multi-threaded rendering */
   4.235 +    /* Disabled until Ryan finishes his VBO/PBO code...
   4.236 +       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
   4.237 +     */
   4.238 +#endif
   4.239 +
   4.240 +    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   4.241 +        SDL_GL_SetSwapInterval(1);
   4.242 +    } else {
   4.243 +        SDL_GL_SetSwapInterval(0);
   4.244 +    }
   4.245 +    if (SDL_GL_GetSwapInterval() > 0) {
   4.246 +        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   4.247 +    }
   4.248 +
   4.249 +    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   4.250 +    renderer->info.max_texture_width = value;
   4.251 +    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   4.252 +    renderer->info.max_texture_height = value;
   4.253 +
   4.254 +    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
   4.255 +        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
   4.256 +        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
   4.257 +    }
   4.258 +    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
   4.259 +        data->glTextureRangeAPPLE =
   4.260 +            (void (*)(GLenum, GLsizei, const GLvoid *))
   4.261 +            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
   4.262 +    }
   4.263 +
   4.264 +    /* Set up parameters for rendering */
   4.265 +    data->blendMode = -1;
   4.266 +    data->glDisable(GL_DEPTH_TEST);
   4.267 +    data->glDisable(GL_CULL_FACE);
   4.268 +    /* This ended up causing video discrepancies between OpenGL and Direct3D */
   4.269 +    /*data->glEnable(GL_LINE_SMOOTH);*/
   4.270 +    if (data->GL_ARB_texture_rectangle_supported) {
   4.271 +        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
   4.272 +    } else {
   4.273 +        data->glEnable(GL_TEXTURE_2D);
   4.274 +    }
   4.275 +    data->updateSize = SDL_TRUE;
   4.276 +
   4.277 +    return renderer;
   4.278 +}
   4.279 +
   4.280 +static SDL_GLContext SDL_CurrentContext = NULL;
   4.281 +
   4.282 +static int
   4.283 +GL_ActivateRenderer(SDL_Renderer * renderer)
   4.284 +{
   4.285 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.286 +    SDL_Window *window = renderer->window;
   4.287 +
   4.288 +    if (SDL_CurrentContext != data->context) {
   4.289 +        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   4.290 +            return -1;
   4.291 +        }
   4.292 +        SDL_CurrentContext = data->context;
   4.293 +    }
   4.294 +    if (data->updateSize) {
   4.295 +        int w, h;
   4.296 +
   4.297 +        SDL_GetWindowSize(window, &w, &h);
   4.298 +        data->glMatrixMode(GL_PROJECTION);
   4.299 +        data->glLoadIdentity();
   4.300 +        data->glMatrixMode(GL_MODELVIEW);
   4.301 +        data->glLoadIdentity();
   4.302 +        data->glViewport(0, 0, w, h);
   4.303 +        data->glOrtho(0.0, (GLdouble) w, (GLdouble) h, 0.0, 0.0, 1.0);
   4.304 +        data->updateSize = SDL_FALSE;
   4.305 +    }
   4.306 +    return 0;
   4.307 +}
   4.308 +
   4.309 +static void
   4.310 +GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   4.311 +{
   4.312 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.313 +
   4.314 +    if (event->event == SDL_WINDOWEVENT_RESIZED) {
   4.315 +        /* Rebind the context to the window area and update matrices */
   4.316 +        SDL_CurrentContext = NULL;
   4.317 +        data->updateSize = SDL_TRUE;
   4.318 +    }
   4.319 +}
   4.320 +
   4.321 +static __inline__ int
   4.322 +power_of_2(int input)
   4.323 +{
   4.324 +    int value = 1;
   4.325 +
   4.326 +    while (value < input) {
   4.327 +        value <<= 1;
   4.328 +    }
   4.329 +    return value;
   4.330 +}
   4.331 +
   4.332 +static __inline__ SDL_bool
   4.333 +convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
   4.334 +               GLint* internalFormat, GLenum* format, GLenum* type)
   4.335 +{
   4.336 +    switch (pixel_format) {
   4.337 +    case SDL_PIXELFORMAT_RGB888:
   4.338 +    case SDL_PIXELFORMAT_ARGB8888:
   4.339 +        *internalFormat = GL_RGBA8;
   4.340 +        *format = GL_BGRA;
   4.341 +        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
   4.342 +        break;
   4.343 +    default:
   4.344 +        return SDL_FALSE;
   4.345 +    }
   4.346 +    return SDL_TRUE;
   4.347 +}
   4.348 +
   4.349 +static int
   4.350 +GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   4.351 +{
   4.352 +    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   4.353 +    GL_TextureData *data;
   4.354 +    GLint internalFormat;
   4.355 +    GLenum format, type;
   4.356 +    int texture_w, texture_h;
   4.357 +    GLenum result;
   4.358 +
   4.359 +    GL_ActivateRenderer(renderer);
   4.360 +
   4.361 +    if (!convert_format(renderdata, texture->format, &internalFormat,
   4.362 +                        &format, &type)) {
   4.363 +        SDL_SetError("Texture format %s not supported by OpenGL",
   4.364 +                     SDL_GetPixelFormatName(texture->format));
   4.365 +        return -1;
   4.366 +    }
   4.367 +
   4.368 +    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
   4.369 +    if (!data) {
   4.370 +        SDL_OutOfMemory();
   4.371 +        return -1;
   4.372 +    }
   4.373 +
   4.374 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   4.375 +        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   4.376 +        data->pixels = SDL_malloc(texture->h * data->pitch);
   4.377 +        if (!data->pixels) {
   4.378 +            SDL_OutOfMemory();
   4.379 +            SDL_free(data);
   4.380 +            return -1;
   4.381 +        }
   4.382 +    }
   4.383 +
   4.384 +    texture->driverdata = data;
   4.385 +
   4.386 +    renderdata->glGetError();
   4.387 +    renderdata->glGenTextures(1, &data->texture);
   4.388 +    if (renderdata->GL_ARB_texture_rectangle_supported) {
   4.389 +        data->type = GL_TEXTURE_RECTANGLE_ARB;
   4.390 +        texture_w = texture->w;
   4.391 +        texture_h = texture->h;
   4.392 +        data->texw = (GLfloat) texture_w;
   4.393 +        data->texh = (GLfloat) texture_h;
   4.394 +    } else {
   4.395 +        data->type = GL_TEXTURE_2D;
   4.396 +        texture_w = power_of_2(texture->w);
   4.397 +        texture_h = power_of_2(texture->h);
   4.398 +        data->texw = (GLfloat) (texture->w) / texture_w;
   4.399 +        data->texh = (GLfloat) texture->h / texture_h;
   4.400 +    }
   4.401 +
   4.402 +    data->format = format;
   4.403 +    data->formattype = type;
   4.404 +    renderdata->glEnable(data->type);
   4.405 +    renderdata->glBindTexture(data->type, data->texture);
   4.406 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   4.407 +                                GL_LINEAR);
   4.408 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   4.409 +                                GL_LINEAR);
   4.410 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   4.411 +                                GL_CLAMP_TO_EDGE);
   4.412 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   4.413 +                                GL_CLAMP_TO_EDGE);
   4.414 +#ifdef __MACOSX__
   4.415 +#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
   4.416 +#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
   4.417 +#endif
   4.418 +#ifndef STORAGE_CACHED_APPLE
   4.419 +#define STORAGE_CACHED_APPLE                0x85BE
   4.420 +#endif
   4.421 +#ifndef STORAGE_SHARED_APPLE
   4.422 +#define STORAGE_SHARED_APPLE                0x85BF
   4.423 +#endif
   4.424 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   4.425 +        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
   4.426 +                                    GL_STORAGE_SHARED_APPLE);
   4.427 +    } else {
   4.428 +        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
   4.429 +                                    GL_STORAGE_CACHED_APPLE);
   4.430 +    }
   4.431 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING
   4.432 +        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
   4.433 +        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
   4.434 +        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   4.435 +                                 texture_h, 0, format, type, data->pixels);
   4.436 +    }
   4.437 +    else
   4.438 +#endif
   4.439 +    {
   4.440 +        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   4.441 +                                 texture_h, 0, format, type, NULL);
   4.442 +    }
   4.443 +    renderdata->glDisable(data->type);
   4.444 +    result = renderdata->glGetError();
   4.445 +    if (result != GL_NO_ERROR) {
   4.446 +        GL_SetError("glTexImage2D()", result);
   4.447 +        return -1;
   4.448 +    }
   4.449 +    return 0;
   4.450 +}
   4.451 +
   4.452 +static void
   4.453 +SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
   4.454 +                   int pitch)
   4.455 +{
   4.456 +    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   4.457 +    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
   4.458 +                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
   4.459 +}
   4.460 +
   4.461 +static int
   4.462 +GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   4.463 +                 const SDL_Rect * rect, const void *pixels, int pitch)
   4.464 +{
   4.465 +    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   4.466 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   4.467 +    GLenum result;
   4.468 +
   4.469 +    GL_ActivateRenderer(renderer);
   4.470 +
   4.471 +    renderdata->glGetError();
   4.472 +    SetupTextureUpdate(renderdata, texture, pitch);
   4.473 +    renderdata->glEnable(data->type);
   4.474 +    renderdata->glBindTexture(data->type, data->texture);
   4.475 +    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   4.476 +                                rect->h, data->format, data->formattype,
   4.477 +                                pixels);
   4.478 +    renderdata->glDisable(data->type);
   4.479 +    result = renderdata->glGetError();
   4.480 +    if (result != GL_NO_ERROR) {
   4.481 +        GL_SetError("glTexSubImage2D()", result);
   4.482 +        return -1;
   4.483 +    }
   4.484 +    return 0;
   4.485 +}
   4.486 +
   4.487 +static int
   4.488 +GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   4.489 +               const SDL_Rect * rect, void **pixels, int *pitch)
   4.490 +{
   4.491 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   4.492 +
   4.493 +    *pixels =
   4.494 +        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   4.495 +                  rect->x * SDL_BYTESPERPIXEL(texture->format));
   4.496 +    *pitch = data->pitch;
   4.497 +    return 0;
   4.498 +}
   4.499 +
   4.500 +static void
   4.501 +GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   4.502 +{
   4.503 +    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   4.504 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   4.505 +
   4.506 +    GL_ActivateRenderer(renderer);
   4.507 +
   4.508 +    SetupTextureUpdate(renderdata, texture, data->pitch);
   4.509 +    renderdata->glEnable(data->type);
   4.510 +    renderdata->glBindTexture(data->type, data->texture);
   4.511 +    renderdata->glTexSubImage2D(data->type, 0, 0, 0, texture->w, texture->h,
   4.512 +                                data->format, data->formattype, data->pixels);
   4.513 +    renderdata->glDisable(data->type);
   4.514 +}
   4.515 +
   4.516 +static void
   4.517 +GL_SetBlendMode(GL_RenderData * data, int blendMode)
   4.518 +{
   4.519 +    if (blendMode != data->blendMode) {
   4.520 +        switch (blendMode) {
   4.521 +        case SDL_BLENDMODE_NONE:
   4.522 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   4.523 +            data->glDisable(GL_BLEND);
   4.524 +            break;
   4.525 +        case SDL_BLENDMODE_BLEND:
   4.526 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   4.527 +            data->glEnable(GL_BLEND);
   4.528 +            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   4.529 +            break;
   4.530 +        case SDL_BLENDMODE_ADD:
   4.531 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   4.532 +            data->glEnable(GL_BLEND);
   4.533 +            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   4.534 +            break;
   4.535 +        case SDL_BLENDMODE_MOD:
   4.536 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   4.537 +            data->glEnable(GL_BLEND);
   4.538 +            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   4.539 +            break;
   4.540 +        }
   4.541 +        data->blendMode = blendMode;
   4.542 +    }
   4.543 +}
   4.544 +
   4.545 +static int
   4.546 +GL_RenderClear(SDL_Renderer * renderer)
   4.547 +{
   4.548 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.549 +
   4.550 +    GL_ActivateRenderer(renderer);
   4.551 +
   4.552 +    data->glClearColor((GLfloat) renderer->r * inv255f,
   4.553 +                       (GLfloat) renderer->g * inv255f,
   4.554 +                       (GLfloat) renderer->b * inv255f,
   4.555 +                       (GLfloat) renderer->a * inv255f);
   4.556 +
   4.557 +    data->glClear(GL_COLOR_BUFFER_BIT);
   4.558 +
   4.559 +    return 0;
   4.560 +}
   4.561 +
   4.562 +static int
   4.563 +GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   4.564 +                    int count)
   4.565 +{
   4.566 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.567 +    int i;
   4.568 +
   4.569 +    GL_ActivateRenderer(renderer);
   4.570 +
   4.571 +    GL_SetBlendMode(data, renderer->blendMode);
   4.572 +
   4.573 +    data->glColor4f((GLfloat) renderer->r * inv255f,
   4.574 +                    (GLfloat) renderer->g * inv255f,
   4.575 +                    (GLfloat) renderer->b * inv255f,
   4.576 +                    (GLfloat) renderer->a * inv255f);
   4.577 +
   4.578 +    data->glBegin(GL_POINTS);
   4.579 +    for (i = 0; i < count; ++i) {
   4.580 +        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
   4.581 +    }
   4.582 +    data->glEnd();
   4.583 +
   4.584 +    return 0;
   4.585 +}
   4.586 +
   4.587 +static int
   4.588 +GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   4.589 +                   int count)
   4.590 +{
   4.591 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.592 +    int i;
   4.593 +
   4.594 +    GL_ActivateRenderer(renderer);
   4.595 +
   4.596 +    GL_SetBlendMode(data, renderer->blendMode);
   4.597 +
   4.598 +    data->glColor4f((GLfloat) renderer->r * inv255f,
   4.599 +                    (GLfloat) renderer->g * inv255f,
   4.600 +                    (GLfloat) renderer->b * inv255f,
   4.601 +                    (GLfloat) renderer->a * inv255f);
   4.602 +
   4.603 +    if (count > 2 && 
   4.604 +        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
   4.605 +        data->glBegin(GL_LINE_LOOP);
   4.606 +        /* GL_LINE_LOOP takes care of the final segment */
   4.607 +        --count;
   4.608 +        for (i = 0; i < count; ++i) {
   4.609 +            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
   4.610 +        }
   4.611 +        data->glEnd();
   4.612 +    } else {
   4.613 +#if defined(__APPLE__) || defined(__WIN32__)
   4.614 +#else
   4.615 +        int x1, y1, x2, y2;
   4.616 +#endif
   4.617 +
   4.618 +        data->glBegin(GL_LINE_STRIP);
   4.619 +        for (i = 0; i < count; ++i) {
   4.620 +            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
   4.621 +        }
   4.622 +        data->glEnd();
   4.623 +
   4.624 +        /* The line is half open, so we need one more point to complete it.
   4.625 +         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
   4.626 +         * If we have to, we can use vertical line and horizontal line textures
   4.627 +         * for vertical and horizontal lines, and then create custom textures
   4.628 +         * for diagonal lines and software render those.  It's terrible, but at
   4.629 +         * least it would be pixel perfect.
   4.630 +         */
   4.631 +        data->glBegin(GL_POINTS);
   4.632 +#if defined(__APPLE__) || defined(__WIN32__)
   4.633 +        /* Mac OS X and Windows seem to always leave the second point open */
   4.634 +        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
   4.635 +#else
   4.636 +        /* Linux seems to leave the right-most or bottom-most point open */
   4.637 +        x1 = points[0].x;
   4.638 +        y1 = points[0].y;
   4.639 +        x2 = points[count-1].x;
   4.640 +        y2 = points[count-1].y;
   4.641 +
   4.642 +        if (x1 > x2) {
   4.643 +            data->glVertex2f(0.5f + x1, 0.5f + y1);
   4.644 +        } else if (x2 > x1) {
   4.645 +            data->glVertex2f(0.5f + x2, 0.5f + y2);
   4.646 +        } else if (y1 > y2) {
   4.647 +            data->glVertex2f(0.5f + x1, 0.5f + y1);
   4.648 +        } else if (y2 > y1) {
   4.649 +            data->glVertex2f(0.5f + x2, 0.5f + y2);
   4.650 +        }
   4.651 +#endif
   4.652 +        data->glEnd();
   4.653 +    }
   4.654 +
   4.655 +    return 0;
   4.656 +}
   4.657 +
   4.658 +static int
   4.659 +GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
   4.660 +{
   4.661 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.662 +    int i;
   4.663 +
   4.664 +    GL_ActivateRenderer(renderer);
   4.665 +
   4.666 +    GL_SetBlendMode(data, renderer->blendMode);
   4.667 +
   4.668 +    data->glColor4f((GLfloat) renderer->r * inv255f,
   4.669 +                    (GLfloat) renderer->g * inv255f,
   4.670 +                    (GLfloat) renderer->b * inv255f,
   4.671 +                    (GLfloat) renderer->a * inv255f);
   4.672 +
   4.673 +    for (i = 0; i < count; ++i) {
   4.674 +        const SDL_Rect *rect = rects[i];
   4.675 +
   4.676 +        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
   4.677 +    }
   4.678 +
   4.679 +    return 0;
   4.680 +}
   4.681 +
   4.682 +static int
   4.683 +GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   4.684 +              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   4.685 +{
   4.686 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.687 +    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
   4.688 +    int minx, miny, maxx, maxy;
   4.689 +    GLfloat minu, maxu, minv, maxv;
   4.690 +
   4.691 +    GL_ActivateRenderer(renderer);
   4.692 +
   4.693 +    minx = dstrect->x;
   4.694 +    miny = dstrect->y;
   4.695 +    maxx = dstrect->x + dstrect->w;
   4.696 +    maxy = dstrect->y + dstrect->h;
   4.697 +
   4.698 +    minu = (GLfloat) srcrect->x / texture->w;
   4.699 +    minu *= texturedata->texw;
   4.700 +    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   4.701 +    maxu *= texturedata->texw;
   4.702 +    minv = (GLfloat) srcrect->y / texture->h;
   4.703 +    minv *= texturedata->texh;
   4.704 +    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   4.705 +    maxv *= texturedata->texh;
   4.706 +
   4.707 +    data->glEnable(texturedata->type);
   4.708 +    data->glBindTexture(texturedata->type, texturedata->texture);
   4.709 +
   4.710 +    if (texture->modMode) {
   4.711 +        data->glColor4f((GLfloat) texture->r * inv255f,
   4.712 +                        (GLfloat) texture->g * inv255f,
   4.713 +                        (GLfloat) texture->b * inv255f,
   4.714 +                        (GLfloat) texture->a * inv255f);
   4.715 +    } else {
   4.716 +        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   4.717 +    }
   4.718 +
   4.719 +    GL_SetBlendMode(data, texture->blendMode);
   4.720 +
   4.721 +    data->glBegin(GL_TRIANGLE_STRIP);
   4.722 +    data->glTexCoord2f(minu, minv);
   4.723 +    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
   4.724 +    data->glTexCoord2f(maxu, minv);
   4.725 +    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
   4.726 +    data->glTexCoord2f(minu, maxv);
   4.727 +    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
   4.728 +    data->glTexCoord2f(maxu, maxv);
   4.729 +    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
   4.730 +    data->glEnd();
   4.731 +
   4.732 +    data->glDisable(texturedata->type);
   4.733 +
   4.734 +    return 0;
   4.735 +}
   4.736 +
   4.737 +static int
   4.738 +GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   4.739 +                    Uint32 pixel_format, void * pixels, int pitch)
   4.740 +{
   4.741 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.742 +    SDL_Window *window = renderer->window;
   4.743 +    GLint internalFormat;
   4.744 +    GLenum format, type;
   4.745 +    Uint8 *src, *dst, *tmp;
   4.746 +    int w, h, length, rows;
   4.747 +
   4.748 +    GL_ActivateRenderer(renderer);
   4.749 +
   4.750 +    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
   4.751 +        /* FIXME: Do a temp copy to a format that is supported */
   4.752 +        SDL_SetError("Unsupported pixel format");
   4.753 +        return -1;
   4.754 +    }
   4.755 +
   4.756 +    SDL_GetWindowSize(window, &w, &h);
   4.757 +
   4.758 +    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
   4.759 +    data->glPixelStorei(GL_PACK_ROW_LENGTH,
   4.760 +                        (pitch / SDL_BYTESPERPIXEL(pixel_format)));
   4.761 +
   4.762 +    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
   4.763 +                       format, type, pixels);
   4.764 +
   4.765 +    /* Flip the rows to be top-down */
   4.766 +    length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
   4.767 +    src = (Uint8*)pixels + (rect->h-1)*pitch;
   4.768 +    dst = (Uint8*)pixels;
   4.769 +    tmp = SDL_stack_alloc(Uint8, length);
   4.770 +    rows = rect->h / 2;
   4.771 +    while (rows--) {
   4.772 +        SDL_memcpy(tmp, dst, length);
   4.773 +        SDL_memcpy(dst, src, length);
   4.774 +        SDL_memcpy(src, tmp, length);
   4.775 +        dst += pitch;
   4.776 +        src -= pitch;
   4.777 +    }
   4.778 +    SDL_stack_free(tmp);
   4.779 +
   4.780 +    return 0;
   4.781 +}
   4.782 +
   4.783 +static void
   4.784 +GL_RenderPresent(SDL_Renderer * renderer)
   4.785 +{
   4.786 +    GL_ActivateRenderer(renderer);
   4.787 +
   4.788 +    SDL_GL_SwapWindow(renderer->window);
   4.789 +}
   4.790 +
   4.791 +static void
   4.792 +GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   4.793 +{
   4.794 +    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   4.795 +    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   4.796 +
   4.797 +    GL_ActivateRenderer(renderer);
   4.798 +
   4.799 +    if (!data) {
   4.800 +        return;
   4.801 +    }
   4.802 +    if (data->texture) {
   4.803 +        renderdata->glDeleteTextures(1, &data->texture);
   4.804 +    }
   4.805 +    if (data->pixels) {
   4.806 +        SDL_free(data->pixels);
   4.807 +    }
   4.808 +    SDL_free(data);
   4.809 +    texture->driverdata = NULL;
   4.810 +}
   4.811 +
   4.812 +static void
   4.813 +GL_DestroyRenderer(SDL_Renderer * renderer)
   4.814 +{
   4.815 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.816 +
   4.817 +    if (data) {
   4.818 +        if (data->context) {
   4.819 +            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
   4.820 +            SDL_GL_DeleteContext(data->context);
   4.821 +        }
   4.822 +        SDL_free(data);
   4.823 +    }
   4.824 +    SDL_free(renderer);
   4.825 +}
   4.826 +
   4.827 +#endif /* SDL_VIDEO_RENDER_OGL */
   4.828 +
   4.829 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/render/opengl/SDL_renderer_gl.c	Sat Feb 05 11:54:46 2011 -0800
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,826 +0,0 @@
     5.4 -/*
     5.5 -    SDL - Simple DirectMedia Layer
     5.6 -    Copyright (C) 1997-2010 Sam Lantinga
     5.7 -
     5.8 -    This library is free software; you can redistribute it and/or
     5.9 -    modify it under the terms of the GNU Lesser General Public
    5.10 -    License as published by the Free Software Foundation; either
    5.11 -    version 2.1 of the License, or (at your option) any later version.
    5.12 -
    5.13 -    This library is distributed in the hope that it will be useful,
    5.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    5.16 -    Lesser General Public License for more details.
    5.17 -
    5.18 -    You should have received a copy of the GNU Lesser General Public
    5.19 -    License along with this library; if not, write to the Free Software
    5.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    5.21 -
    5.22 -    Sam Lantinga
    5.23 -    slouken@libsdl.org
    5.24 -*/
    5.25 -#include "SDL_config.h"
    5.26 -
    5.27 -#if SDL_VIDEO_RENDER_OGL
    5.28 -
    5.29 -#include "SDL_opengl.h"
    5.30 -#include "../SDL_sysrender.h"
    5.31 -
    5.32 -#ifdef __MACOSX__
    5.33 -#include <OpenGL/OpenGL.h>
    5.34 -#endif
    5.35 -
    5.36 -
    5.37 -/* OpenGL renderer implementation */
    5.38 -
    5.39 -/* Details on optimizing the texture path on Mac OS X:
    5.40 -   http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/chapter_10_section_2.html
    5.41 -*/
    5.42 -
    5.43 -/* Used to re-create the window with OpenGL capability */
    5.44 -extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
    5.45 -
    5.46 -static const float inv255f = 1.0f / 255.0f;
    5.47 -
    5.48 -static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
    5.49 -static void GL_WindowEvent(SDL_Renderer * renderer,
    5.50 -                           const SDL_WindowEvent *event);
    5.51 -static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    5.52 -static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    5.53 -                            const SDL_Rect * rect, const void *pixels,
    5.54 -                            int pitch);
    5.55 -static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    5.56 -                          const SDL_Rect * rect, void **pixels, int *pitch);
    5.57 -static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    5.58 -static int GL_RenderClear(SDL_Renderer * renderer);
    5.59 -static int GL_RenderDrawPoints(SDL_Renderer * renderer,
    5.60 -                               const SDL_Point * points, int count);
    5.61 -static int GL_RenderDrawLines(SDL_Renderer * renderer,
    5.62 -                              const SDL_Point * points, int count);
    5.63 -static int GL_RenderFillRects(SDL_Renderer * renderer,
    5.64 -                              const SDL_Rect ** rects, int count);
    5.65 -static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    5.66 -                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
    5.67 -static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    5.68 -                               Uint32 pixel_format, void * pixels, int pitch);
    5.69 -static void GL_RenderPresent(SDL_Renderer * renderer);
    5.70 -static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    5.71 -static void GL_DestroyRenderer(SDL_Renderer * renderer);
    5.72 -
    5.73 -
    5.74 -SDL_RenderDriver GL_RenderDriver = {
    5.75 -    GL_CreateRenderer,
    5.76 -    {
    5.77 -     "opengl",
    5.78 -     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
    5.79 -     1,
    5.80 -     {SDL_PIXELFORMAT_ARGB8888},
    5.81 -     0,
    5.82 -     0}
    5.83 -};
    5.84 -
    5.85 -typedef struct
    5.86 -{
    5.87 -    SDL_GLContext context;
    5.88 -    SDL_bool updateSize;
    5.89 -    SDL_bool GL_ARB_texture_rectangle_supported;
    5.90 -    int blendMode;
    5.91 -
    5.92 -    /* OpenGL functions */
    5.93 -#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
    5.94 -#include "../../video/SDL_glfuncs.h"
    5.95 -#undef SDL_PROC
    5.96 -
    5.97 -    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
    5.98 -                                 const GLvoid * pointer);
    5.99 -} GL_RenderData;
   5.100 -
   5.101 -typedef struct
   5.102 -{
   5.103 -    GLuint texture;
   5.104 -    GLenum type;
   5.105 -    GLfloat texw;
   5.106 -    GLfloat texh;
   5.107 -    GLenum format;
   5.108 -    GLenum formattype;
   5.109 -    void *pixels;
   5.110 -    int pitch;
   5.111 -} GL_TextureData;
   5.112 -
   5.113 -
   5.114 -static void
   5.115 -GL_SetError(const char *prefix, GLenum result)
   5.116 -{
   5.117 -    const char *error;
   5.118 -
   5.119 -    switch (result) {
   5.120 -    case GL_NO_ERROR:
   5.121 -        error = "GL_NO_ERROR";
   5.122 -        break;
   5.123 -    case GL_INVALID_ENUM:
   5.124 -        error = "GL_INVALID_ENUM";
   5.125 -        break;
   5.126 -    case GL_INVALID_VALUE:
   5.127 -        error = "GL_INVALID_VALUE";
   5.128 -        break;
   5.129 -    case GL_INVALID_OPERATION:
   5.130 -        error = "GL_INVALID_OPERATION";
   5.131 -        break;
   5.132 -    case GL_STACK_OVERFLOW:
   5.133 -        error = "GL_STACK_OVERFLOW";
   5.134 -        break;
   5.135 -    case GL_STACK_UNDERFLOW:
   5.136 -        error = "GL_STACK_UNDERFLOW";
   5.137 -        break;
   5.138 -    case GL_OUT_OF_MEMORY:
   5.139 -        error = "GL_OUT_OF_MEMORY";
   5.140 -        break;
   5.141 -    case GL_TABLE_TOO_LARGE:
   5.142 -        error = "GL_TABLE_TOO_LARGE";
   5.143 -        break;
   5.144 -    default:
   5.145 -        error = "UNKNOWN";
   5.146 -        break;
   5.147 -    }
   5.148 -    SDL_SetError("%s: %s", prefix, error);
   5.149 -}
   5.150 -
   5.151 -static int
   5.152 -GL_LoadFunctions(GL_RenderData * data)
   5.153 -{
   5.154 -#ifdef __SDL_NOGETPROCADDR__
   5.155 -#define SDL_PROC(ret,func,params) data->func=func;
   5.156 -#else
   5.157 -#define SDL_PROC(ret,func,params) \
   5.158 -    do { \
   5.159 -        data->func = SDL_GL_GetProcAddress(#func); \
   5.160 -        if ( ! data->func ) { \
   5.161 -            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
   5.162 -            return -1; \
   5.163 -        } \
   5.164 -    } while ( 0 );
   5.165 -#endif /* __SDL_NOGETPROCADDR__ */
   5.166 -
   5.167 -#include "../../video/SDL_glfuncs.h"
   5.168 -#undef SDL_PROC
   5.169 -    return 0;
   5.170 -}
   5.171 -
   5.172 -SDL_Renderer *
   5.173 -GL_CreateRenderer(SDL_Window * window, Uint32 flags)
   5.174 -{
   5.175 -    SDL_Renderer *renderer;
   5.176 -    GL_RenderData *data;
   5.177 -    GLint value;
   5.178 -    Uint32 window_flags;
   5.179 -
   5.180 -    window_flags = SDL_GetWindowFlags(window);
   5.181 -    if (!(window_flags & SDL_WINDOW_OPENGL)) {
   5.182 -        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
   5.183 -            return NULL;
   5.184 -        }
   5.185 -    }
   5.186 -
   5.187 -    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   5.188 -    if (!renderer) {
   5.189 -        SDL_OutOfMemory();
   5.190 -        return NULL;
   5.191 -    }
   5.192 -
   5.193 -    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
   5.194 -    if (!data) {
   5.195 -        GL_DestroyRenderer(renderer);
   5.196 -        SDL_OutOfMemory();
   5.197 -        return NULL;
   5.198 -    }
   5.199 -
   5.200 -    renderer->WindowEvent = GL_WindowEvent;
   5.201 -    renderer->CreateTexture = GL_CreateTexture;
   5.202 -    renderer->UpdateTexture = GL_UpdateTexture;
   5.203 -    renderer->LockTexture = GL_LockTexture;
   5.204 -    renderer->UnlockTexture = GL_UnlockTexture;
   5.205 -    renderer->RenderClear = GL_RenderClear;
   5.206 -    renderer->RenderDrawPoints = GL_RenderDrawPoints;
   5.207 -    renderer->RenderDrawLines = GL_RenderDrawLines;
   5.208 -    renderer->RenderFillRects = GL_RenderFillRects;
   5.209 -    renderer->RenderCopy = GL_RenderCopy;
   5.210 -    renderer->RenderReadPixels = GL_RenderReadPixels;
   5.211 -    renderer->RenderPresent = GL_RenderPresent;
   5.212 -    renderer->DestroyTexture = GL_DestroyTexture;
   5.213 -    renderer->DestroyRenderer = GL_DestroyRenderer;
   5.214 -    renderer->info = GL_RenderDriver.info;
   5.215 -    renderer->driverdata = data;
   5.216 -
   5.217 -    renderer->info.flags = SDL_RENDERER_ACCELERATED;
   5.218 -
   5.219 -    if (GL_LoadFunctions(data) < 0) {
   5.220 -        GL_DestroyRenderer(renderer);
   5.221 -        return NULL;
   5.222 -    }
   5.223 -
   5.224 -    data->context = SDL_GL_CreateContext(window);
   5.225 -    if (!data->context) {
   5.226 -        GL_DestroyRenderer(renderer);
   5.227 -        return NULL;
   5.228 -    }
   5.229 -    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   5.230 -        GL_DestroyRenderer(renderer);
   5.231 -        return NULL;
   5.232 -    }
   5.233 -#ifdef __MACOSX__
   5.234 -    /* Enable multi-threaded rendering */
   5.235 -    /* Disabled until Ryan finishes his VBO/PBO code...
   5.236 -       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
   5.237 -     */
   5.238 -#endif
   5.239 -
   5.240 -    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   5.241 -        SDL_GL_SetSwapInterval(1);
   5.242 -    } else {
   5.243 -        SDL_GL_SetSwapInterval(0);
   5.244 -    }
   5.245 -    if (SDL_GL_GetSwapInterval() > 0) {
   5.246 -        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   5.247 -    }
   5.248 -
   5.249 -    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   5.250 -    renderer->info.max_texture_width = value;
   5.251 -    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   5.252 -    renderer->info.max_texture_height = value;
   5.253 -
   5.254 -    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
   5.255 -        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
   5.256 -        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
   5.257 -    }
   5.258 -    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
   5.259 -        data->glTextureRangeAPPLE =
   5.260 -            (void (*)(GLenum, GLsizei, const GLvoid *))
   5.261 -            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
   5.262 -    }
   5.263 -
   5.264 -    /* Set up parameters for rendering */
   5.265 -    data->blendMode = -1;
   5.266 -    data->glDisable(GL_DEPTH_TEST);
   5.267 -    data->glDisable(GL_CULL_FACE);
   5.268 -    /* This ended up causing video discrepancies between OpenGL and Direct3D */
   5.269 -    /*data->glEnable(GL_LINE_SMOOTH);*/
   5.270 -    if (data->GL_ARB_texture_rectangle_supported) {
   5.271 -        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
   5.272 -    } else {
   5.273 -        data->glEnable(GL_TEXTURE_2D);
   5.274 -    }
   5.275 -    data->updateSize = SDL_TRUE;
   5.276 -
   5.277 -    return renderer;
   5.278 -}
   5.279 -
   5.280 -static SDL_GLContext SDL_CurrentContext = NULL;
   5.281 -
   5.282 -static int
   5.283 -GL_ActivateRenderer(SDL_Renderer * renderer)
   5.284 -{
   5.285 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.286 -    SDL_Window *window = renderer->window;
   5.287 -
   5.288 -    if (SDL_CurrentContext != data->context) {
   5.289 -        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   5.290 -            return -1;
   5.291 -        }
   5.292 -        SDL_CurrentContext = data->context;
   5.293 -    }
   5.294 -    if (data->updateSize) {
   5.295 -        int w, h;
   5.296 -
   5.297 -        SDL_GetWindowSize(window, &w, &h);
   5.298 -        data->glMatrixMode(GL_PROJECTION);
   5.299 -        data->glLoadIdentity();
   5.300 -        data->glMatrixMode(GL_MODELVIEW);
   5.301 -        data->glLoadIdentity();
   5.302 -        data->glViewport(0, 0, w, h);
   5.303 -        data->glOrtho(0.0, (GLdouble) w, (GLdouble) h, 0.0, 0.0, 1.0);
   5.304 -        data->updateSize = SDL_FALSE;
   5.305 -    }
   5.306 -    return 0;
   5.307 -}
   5.308 -
   5.309 -static void
   5.310 -GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   5.311 -{
   5.312 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.313 -
   5.314 -    if (event->event == SDL_WINDOWEVENT_RESIZED) {
   5.315 -        /* Rebind the context to the window area and update matrices */
   5.316 -        SDL_CurrentContext = NULL;
   5.317 -        data->updateSize = SDL_TRUE;
   5.318 -    }
   5.319 -}
   5.320 -
   5.321 -static __inline__ int
   5.322 -power_of_2(int input)
   5.323 -{
   5.324 -    int value = 1;
   5.325 -
   5.326 -    while (value < input) {
   5.327 -        value <<= 1;
   5.328 -    }
   5.329 -    return value;
   5.330 -}
   5.331 -
   5.332 -static __inline__ SDL_bool
   5.333 -convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
   5.334 -               GLint* internalFormat, GLenum* format, GLenum* type)
   5.335 -{
   5.336 -    switch (pixel_format) {
   5.337 -    case SDL_PIXELFORMAT_RGB888:
   5.338 -    case SDL_PIXELFORMAT_ARGB8888:
   5.339 -        *internalFormat = GL_RGBA8;
   5.340 -        *format = GL_BGRA;
   5.341 -        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
   5.342 -        break;
   5.343 -    default:
   5.344 -        return SDL_FALSE;
   5.345 -    }
   5.346 -    return SDL_TRUE;
   5.347 -}
   5.348 -
   5.349 -static int
   5.350 -GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   5.351 -{
   5.352 -    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   5.353 -    GL_TextureData *data;
   5.354 -    GLint internalFormat;
   5.355 -    GLenum format, type;
   5.356 -    int texture_w, texture_h;
   5.357 -    GLenum result;
   5.358 -
   5.359 -    GL_ActivateRenderer(renderer);
   5.360 -
   5.361 -    if (!convert_format(renderdata, texture->format, &internalFormat,
   5.362 -                        &format, &type)) {
   5.363 -        SDL_SetError("Texture format %s not supported by OpenGL",
   5.364 -                     SDL_GetPixelFormatName(texture->format));
   5.365 -        return -1;
   5.366 -    }
   5.367 -
   5.368 -    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
   5.369 -    if (!data) {
   5.370 -        SDL_OutOfMemory();
   5.371 -        return -1;
   5.372 -    }
   5.373 -
   5.374 -    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   5.375 -        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   5.376 -        data->pixels = SDL_malloc(texture->h * data->pitch);
   5.377 -        if (!data->pixels) {
   5.378 -            SDL_OutOfMemory();
   5.379 -            SDL_free(data);
   5.380 -            return -1;
   5.381 -        }
   5.382 -    }
   5.383 -
   5.384 -    texture->driverdata = data;
   5.385 -
   5.386 -    renderdata->glGetError();
   5.387 -    renderdata->glGenTextures(1, &data->texture);
   5.388 -    if (renderdata->GL_ARB_texture_rectangle_supported) {
   5.389 -        data->type = GL_TEXTURE_RECTANGLE_ARB;
   5.390 -        texture_w = texture->w;
   5.391 -        texture_h = texture->h;
   5.392 -        data->texw = (GLfloat) texture_w;
   5.393 -        data->texh = (GLfloat) texture_h;
   5.394 -    } else {
   5.395 -        data->type = GL_TEXTURE_2D;
   5.396 -        texture_w = power_of_2(texture->w);
   5.397 -        texture_h = power_of_2(texture->h);
   5.398 -        data->texw = (GLfloat) (texture->w) / texture_w;
   5.399 -        data->texh = (GLfloat) texture->h / texture_h;
   5.400 -    }
   5.401 -
   5.402 -    data->format = format;
   5.403 -    data->formattype = type;
   5.404 -    renderdata->glEnable(data->type);
   5.405 -    renderdata->glBindTexture(data->type, data->texture);
   5.406 -    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   5.407 -                                GL_LINEAR);
   5.408 -    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   5.409 -                                GL_LINEAR);
   5.410 -    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   5.411 -                                GL_CLAMP_TO_EDGE);
   5.412 -    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   5.413 -                                GL_CLAMP_TO_EDGE);
   5.414 -#ifdef __MACOSX__
   5.415 -#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
   5.416 -#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
   5.417 -#endif
   5.418 -#ifndef STORAGE_CACHED_APPLE
   5.419 -#define STORAGE_CACHED_APPLE                0x85BE
   5.420 -#endif
   5.421 -#ifndef STORAGE_SHARED_APPLE
   5.422 -#define STORAGE_SHARED_APPLE                0x85BF
   5.423 -#endif
   5.424 -    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   5.425 -        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
   5.426 -                                    GL_STORAGE_SHARED_APPLE);
   5.427 -    } else {
   5.428 -        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
   5.429 -                                    GL_STORAGE_CACHED_APPLE);
   5.430 -    }
   5.431 -    if (texture->access == SDL_TEXTUREACCESS_STREAMING
   5.432 -        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
   5.433 -        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
   5.434 -        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   5.435 -                                 texture_h, 0, format, type, data->pixels);
   5.436 -    }
   5.437 -    else
   5.438 -#endif
   5.439 -    {
   5.440 -        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   5.441 -                                 texture_h, 0, format, type, NULL);
   5.442 -    }
   5.443 -    renderdata->glDisable(data->type);
   5.444 -    result = renderdata->glGetError();
   5.445 -    if (result != GL_NO_ERROR) {
   5.446 -        GL_SetError("glTexImage2D()", result);
   5.447 -        return -1;
   5.448 -    }
   5.449 -    return 0;
   5.450 -}
   5.451 -
   5.452 -static void
   5.453 -SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
   5.454 -                   int pitch)
   5.455 -{
   5.456 -    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   5.457 -    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
   5.458 -                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
   5.459 -}
   5.460 -
   5.461 -static int
   5.462 -GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   5.463 -                 const SDL_Rect * rect, const void *pixels, int pitch)
   5.464 -{
   5.465 -    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   5.466 -    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   5.467 -    GLenum result;
   5.468 -
   5.469 -    GL_ActivateRenderer(renderer);
   5.470 -
   5.471 -    renderdata->glGetError();
   5.472 -    SetupTextureUpdate(renderdata, texture, pitch);
   5.473 -    renderdata->glEnable(data->type);
   5.474 -    renderdata->glBindTexture(data->type, data->texture);
   5.475 -    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   5.476 -                                rect->h, data->format, data->formattype,
   5.477 -                                pixels);
   5.478 -    renderdata->glDisable(data->type);
   5.479 -    result = renderdata->glGetError();
   5.480 -    if (result != GL_NO_ERROR) {
   5.481 -        GL_SetError("glTexSubImage2D()", result);
   5.482 -        return -1;
   5.483 -    }
   5.484 -    return 0;
   5.485 -}
   5.486 -
   5.487 -static int
   5.488 -GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   5.489 -               const SDL_Rect * rect, void **pixels, int *pitch)
   5.490 -{
   5.491 -    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   5.492 -
   5.493 -    *pixels =
   5.494 -        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   5.495 -                  rect->x * SDL_BYTESPERPIXEL(texture->format));
   5.496 -    *pitch = data->pitch;
   5.497 -    return 0;
   5.498 -}
   5.499 -
   5.500 -static void
   5.501 -GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   5.502 -{
   5.503 -    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   5.504 -    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   5.505 -
   5.506 -    GL_ActivateRenderer(renderer);
   5.507 -
   5.508 -    SetupTextureUpdate(renderdata, texture, data->pitch);
   5.509 -    renderdata->glEnable(data->type);
   5.510 -    renderdata->glBindTexture(data->type, data->texture);
   5.511 -    renderdata->glTexSubImage2D(data->type, 0, 0, 0, texture->w, texture->h,
   5.512 -                                data->format, data->formattype, data->pixels);
   5.513 -    renderdata->glDisable(data->type);
   5.514 -}
   5.515 -
   5.516 -static void
   5.517 -GL_SetBlendMode(GL_RenderData * data, int blendMode)
   5.518 -{
   5.519 -    if (blendMode != data->blendMode) {
   5.520 -        switch (blendMode) {
   5.521 -        case SDL_BLENDMODE_NONE:
   5.522 -            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   5.523 -            data->glDisable(GL_BLEND);
   5.524 -            break;
   5.525 -        case SDL_BLENDMODE_BLEND:
   5.526 -            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   5.527 -            data->glEnable(GL_BLEND);
   5.528 -            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   5.529 -            break;
   5.530 -        case SDL_BLENDMODE_ADD:
   5.531 -            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   5.532 -            data->glEnable(GL_BLEND);
   5.533 -            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   5.534 -            break;
   5.535 -        case SDL_BLENDMODE_MOD:
   5.536 -            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   5.537 -            data->glEnable(GL_BLEND);
   5.538 -            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   5.539 -            break;
   5.540 -        }
   5.541 -        data->blendMode = blendMode;
   5.542 -    }
   5.543 -}
   5.544 -
   5.545 -static int
   5.546 -GL_RenderClear(SDL_Renderer * renderer)
   5.547 -{
   5.548 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.549 -
   5.550 -    GL_ActivateRenderer(renderer);
   5.551 -
   5.552 -    data->glClearColor((GLfloat) renderer->r * inv255f,
   5.553 -                       (GLfloat) renderer->g * inv255f,
   5.554 -                       (GLfloat) renderer->b * inv255f,
   5.555 -                       (GLfloat) renderer->a * inv255f);
   5.556 -
   5.557 -    data->glClear(GL_COLOR_BUFFER_BIT);
   5.558 -
   5.559 -    return 0;
   5.560 -}
   5.561 -
   5.562 -static int
   5.563 -GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   5.564 -                    int count)
   5.565 -{
   5.566 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.567 -    int i;
   5.568 -
   5.569 -    GL_ActivateRenderer(renderer);
   5.570 -
   5.571 -    GL_SetBlendMode(data, renderer->blendMode);
   5.572 -
   5.573 -    data->glColor4f((GLfloat) renderer->r * inv255f,
   5.574 -                    (GLfloat) renderer->g * inv255f,
   5.575 -                    (GLfloat) renderer->b * inv255f,
   5.576 -                    (GLfloat) renderer->a * inv255f);
   5.577 -
   5.578 -    data->glBegin(GL_POINTS);
   5.579 -    for (i = 0; i < count; ++i) {
   5.580 -        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
   5.581 -    }
   5.582 -    data->glEnd();
   5.583 -
   5.584 -    return 0;
   5.585 -}
   5.586 -
   5.587 -static int
   5.588 -GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   5.589 -                   int count)
   5.590 -{
   5.591 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.592 -    int i;
   5.593 -
   5.594 -    GL_ActivateRenderer(renderer);
   5.595 -
   5.596 -    GL_SetBlendMode(data, renderer->blendMode);
   5.597 -
   5.598 -    data->glColor4f((GLfloat) renderer->r * inv255f,
   5.599 -                    (GLfloat) renderer->g * inv255f,
   5.600 -                    (GLfloat) renderer->b * inv255f,
   5.601 -                    (GLfloat) renderer->a * inv255f);
   5.602 -
   5.603 -    if (count > 2 && 
   5.604 -        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
   5.605 -        data->glBegin(GL_LINE_LOOP);
   5.606 -        /* GL_LINE_LOOP takes care of the final segment */
   5.607 -        --count;
   5.608 -        for (i = 0; i < count; ++i) {
   5.609 -            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
   5.610 -        }
   5.611 -        data->glEnd();
   5.612 -    } else {
   5.613 -#if defined(__APPLE__) || defined(__WIN32__)
   5.614 -#else
   5.615 -        int x1, y1, x2, y2;
   5.616 -#endif
   5.617 -
   5.618 -        data->glBegin(GL_LINE_STRIP);
   5.619 -        for (i = 0; i < count; ++i) {
   5.620 -            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
   5.621 -        }
   5.622 -        data->glEnd();
   5.623 -
   5.624 -        /* The line is half open, so we need one more point to complete it.
   5.625 -         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
   5.626 -         * If we have to, we can use vertical line and horizontal line textures
   5.627 -         * for vertical and horizontal lines, and then create custom textures
   5.628 -         * for diagonal lines and software render those.  It's terrible, but at
   5.629 -         * least it would be pixel perfect.
   5.630 -         */
   5.631 -        data->glBegin(GL_POINTS);
   5.632 -#if defined(__APPLE__) || defined(__WIN32__)
   5.633 -        /* Mac OS X and Windows seem to always leave the second point open */
   5.634 -        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
   5.635 -#else
   5.636 -        /* Linux seems to leave the right-most or bottom-most point open */
   5.637 -        x1 = points[0].x;
   5.638 -        y1 = points[0].y;
   5.639 -        x2 = points[count-1].x;
   5.640 -        y2 = points[count-1].y;
   5.641 -
   5.642 -        if (x1 > x2) {
   5.643 -            data->glVertex2f(0.5f + x1, 0.5f + y1);
   5.644 -        } else if (x2 > x1) {
   5.645 -            data->glVertex2f(0.5f + x2, 0.5f + y2);
   5.646 -        } else if (y1 > y2) {
   5.647 -            data->glVertex2f(0.5f + x1, 0.5f + y1);
   5.648 -        } else if (y2 > y1) {
   5.649 -            data->glVertex2f(0.5f + x2, 0.5f + y2);
   5.650 -        }
   5.651 -#endif
   5.652 -        data->glEnd();
   5.653 -    }
   5.654 -
   5.655 -    return 0;
   5.656 -}
   5.657 -
   5.658 -static int
   5.659 -GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
   5.660 -{
   5.661 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.662 -    int i;
   5.663 -
   5.664 -    GL_ActivateRenderer(renderer);
   5.665 -
   5.666 -    GL_SetBlendMode(data, renderer->blendMode);
   5.667 -
   5.668 -    data->glColor4f((GLfloat) renderer->r * inv255f,
   5.669 -                    (GLfloat) renderer->g * inv255f,
   5.670 -                    (GLfloat) renderer->b * inv255f,
   5.671 -                    (GLfloat) renderer->a * inv255f);
   5.672 -
   5.673 -    for (i = 0; i < count; ++i) {
   5.674 -        const SDL_Rect *rect = rects[i];
   5.675 -
   5.676 -        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
   5.677 -    }
   5.678 -
   5.679 -    return 0;
   5.680 -}
   5.681 -
   5.682 -static int
   5.683 -GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   5.684 -              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   5.685 -{
   5.686 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.687 -    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
   5.688 -    int minx, miny, maxx, maxy;
   5.689 -    GLfloat minu, maxu, minv, maxv;
   5.690 -
   5.691 -    GL_ActivateRenderer(renderer);
   5.692 -
   5.693 -    minx = dstrect->x;
   5.694 -    miny = dstrect->y;
   5.695 -    maxx = dstrect->x + dstrect->w;
   5.696 -    maxy = dstrect->y + dstrect->h;
   5.697 -
   5.698 -    minu = (GLfloat) srcrect->x / texture->w;
   5.699 -    minu *= texturedata->texw;
   5.700 -    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   5.701 -    maxu *= texturedata->texw;
   5.702 -    minv = (GLfloat) srcrect->y / texture->h;
   5.703 -    minv *= texturedata->texh;
   5.704 -    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   5.705 -    maxv *= texturedata->texh;
   5.706 -
   5.707 -    data->glEnable(texturedata->type);
   5.708 -    data->glBindTexture(texturedata->type, texturedata->texture);
   5.709 -
   5.710 -    if (texture->modMode) {
   5.711 -        data->glColor4f((GLfloat) texture->r * inv255f,
   5.712 -                        (GLfloat) texture->g * inv255f,
   5.713 -                        (GLfloat) texture->b * inv255f,
   5.714 -                        (GLfloat) texture->a * inv255f);
   5.715 -    } else {
   5.716 -        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   5.717 -    }
   5.718 -
   5.719 -    GL_SetBlendMode(data, texture->blendMode);
   5.720 -
   5.721 -    data->glBegin(GL_TRIANGLE_STRIP);
   5.722 -    data->glTexCoord2f(minu, minv);
   5.723 -    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
   5.724 -    data->glTexCoord2f(maxu, minv);
   5.725 -    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
   5.726 -    data->glTexCoord2f(minu, maxv);
   5.727 -    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
   5.728 -    data->glTexCoord2f(maxu, maxv);
   5.729 -    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
   5.730 -    data->glEnd();
   5.731 -
   5.732 -    data->glDisable(texturedata->type);
   5.733 -
   5.734 -    return 0;
   5.735 -}
   5.736 -
   5.737 -static int
   5.738 -GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   5.739 -                    Uint32 pixel_format, void * pixels, int pitch)
   5.740 -{
   5.741 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.742 -    SDL_Window *window = renderer->window;
   5.743 -    GLint internalFormat;
   5.744 -    GLenum format, type;
   5.745 -    Uint8 *src, *dst, *tmp;
   5.746 -    int w, h, length, rows;
   5.747 -
   5.748 -    GL_ActivateRenderer(renderer);
   5.749 -
   5.750 -    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
   5.751 -        /* FIXME: Do a temp copy to a format that is supported */
   5.752 -        SDL_SetError("Unsupported pixel format");
   5.753 -        return -1;
   5.754 -    }
   5.755 -
   5.756 -    SDL_GetWindowSize(window, &w, &h);
   5.757 -
   5.758 -    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
   5.759 -    data->glPixelStorei(GL_PACK_ROW_LENGTH,
   5.760 -                        (pitch / SDL_BYTESPERPIXEL(pixel_format)));
   5.761 -
   5.762 -    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
   5.763 -                       format, type, pixels);
   5.764 -
   5.765 -    /* Flip the rows to be top-down */
   5.766 -    length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
   5.767 -    src = (Uint8*)pixels + (rect->h-1)*pitch;
   5.768 -    dst = (Uint8*)pixels;
   5.769 -    tmp = SDL_stack_alloc(Uint8, length);
   5.770 -    rows = rect->h / 2;
   5.771 -    while (rows--) {
   5.772 -        SDL_memcpy(tmp, dst, length);
   5.773 -        SDL_memcpy(dst, src, length);
   5.774 -        SDL_memcpy(src, tmp, length);
   5.775 -        dst += pitch;
   5.776 -        src -= pitch;
   5.777 -    }
   5.778 -    SDL_stack_free(tmp);
   5.779 -
   5.780 -    return 0;
   5.781 -}
   5.782 -
   5.783 -static void
   5.784 -GL_RenderPresent(SDL_Renderer * renderer)
   5.785 -{
   5.786 -    GL_ActivateRenderer(renderer);
   5.787 -
   5.788 -    SDL_GL_SwapWindow(renderer->window);
   5.789 -}
   5.790 -
   5.791 -static void
   5.792 -GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   5.793 -{
   5.794 -    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   5.795 -    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   5.796 -
   5.797 -    GL_ActivateRenderer(renderer);
   5.798 -
   5.799 -    if (!data) {
   5.800 -        return;
   5.801 -    }
   5.802 -    if (data->texture) {
   5.803 -        renderdata->glDeleteTextures(1, &data->texture);
   5.804 -    }
   5.805 -    if (data->pixels) {
   5.806 -        SDL_free(data->pixels);
   5.807 -    }
   5.808 -    SDL_free(data);
   5.809 -    texture->driverdata = NULL;
   5.810 -}
   5.811 -
   5.812 -static void
   5.813 -GL_DestroyRenderer(SDL_Renderer * renderer)
   5.814 -{
   5.815 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   5.816 -
   5.817 -    if (data) {
   5.818 -        if (data->context) {
   5.819 -            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
   5.820 -            SDL_GL_DeleteContext(data->context);
   5.821 -        }
   5.822 -        SDL_free(data);
   5.823 -    }
   5.824 -    SDL_free(renderer);
   5.825 -}
   5.826 -
   5.827 -#endif /* SDL_VIDEO_RENDER_OGL */
   5.828 -
   5.829 -/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/render/opengles/SDL_render_gles.c	Sat Feb 05 12:01:11 2011 -0800
     6.3 @@ -0,0 +1,748 @@
     6.4 +/*
     6.5 +    SDL - Simple DirectMedia Layer
     6.6 +    Copyright (C) 1997-2010 Sam Lantinga
     6.7 +
     6.8 +    This library is free software; you can redistribute it and/or
     6.9 +    modify it under the terms of the GNU Lesser General Public
    6.10 +    License as published by the Free Software Foundation; either
    6.11 +    version 2.1 of the License, or (at your option) any later version.
    6.12 +
    6.13 +    This library is distributed in the hope that it will be useful,
    6.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    6.16 +    Lesser General Public License for more details.
    6.17 +
    6.18 +    You should have received a copy of the GNU Lesser General Public
    6.19 +    License along with this library; if not, write to the Free Software
    6.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    6.21 +
    6.22 +    Sam Lantinga
    6.23 +    slouken@libsdl.org
    6.24 +*/
    6.25 +#include "SDL_config.h"
    6.26 +
    6.27 +#if SDL_VIDEO_RENDER_OGL_ES
    6.28 +
    6.29 +#include "SDL_opengles.h"
    6.30 +#include "../SDL_sysrender.h"
    6.31 +
    6.32 +#if defined(SDL_VIDEO_DRIVER_PANDORA)
    6.33 +
    6.34 +/* Empty function stub to get OpenGL ES 1.x support without  */
    6.35 +/* OpenGL ES extension GL_OES_draw_texture supported         */
    6.36 +GL_API void GL_APIENTRY
    6.37 +glDrawTexiOES(GLint x, GLint y, GLint z, GLint width, GLint height)
    6.38 +{
    6.39 +    return;
    6.40 +}
    6.41 +
    6.42 +#endif /* PANDORA */
    6.43 +
    6.44 +/* OpenGL ES 1.1 renderer implementation, based on the OpenGL renderer */
    6.45 +
    6.46 +/* Used to re-create the window with OpenGL capability */
    6.47 +extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
    6.48 +
    6.49 +static const float inv255f = 1.0f / 255.0f;
    6.50 +
    6.51 +static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags);
    6.52 +static void GLES_WindowEvent(SDL_Renderer * renderer,
    6.53 +                             const SDL_WindowEvent *event);
    6.54 +static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    6.55 +static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    6.56 +                              const SDL_Rect * rect, const void *pixels,
    6.57 +                              int pitch);
    6.58 +static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    6.59 +                            const SDL_Rect * rect, void **pixels, int *pitch);
    6.60 +static void GLES_UnlockTexture(SDL_Renderer * renderer,
    6.61 +                               SDL_Texture * texture);
    6.62 +static int GLES_RenderDrawPoints(SDL_Renderer * renderer,
    6.63 +                                 const SDL_Point * points, int count);
    6.64 +static int GLES_RenderDrawLines(SDL_Renderer * renderer,
    6.65 +                                const SDL_Point * points, int count);
    6.66 +static int GLES_RenderFillRects(SDL_Renderer * renderer,
    6.67 +                                const SDL_Rect ** rects, int count);
    6.68 +static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    6.69 +                           const SDL_Rect * srcrect,
    6.70 +                           const SDL_Rect * dstrect);
    6.71 +static void GLES_RenderPresent(SDL_Renderer * renderer);
    6.72 +static void GLES_DestroyTexture(SDL_Renderer * renderer,
    6.73 +                                SDL_Texture * texture);
    6.74 +static void GLES_DestroyRenderer(SDL_Renderer * renderer);
    6.75 +
    6.76 +
    6.77 +SDL_RenderDriver GL_ES_RenderDriver = {
    6.78 +    GLES_CreateRenderer,
    6.79 +    {
    6.80 +     "opengl_es",
    6.81 +     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
    6.82 +     1,
    6.83 +     {SDL_PIXELFORMAT_ABGR8888},
    6.84 +     0,
    6.85 +     0}
    6.86 +};
    6.87 +
    6.88 +typedef struct
    6.89 +{
    6.90 +    SDL_GLContext context;
    6.91 +    SDL_bool updateSize;
    6.92 +    int blendMode;
    6.93 +
    6.94 +#ifndef APIENTRY
    6.95 +#define APIENTRY
    6.96 +#endif
    6.97 +
    6.98 +    SDL_bool useDrawTexture;
    6.99 +    SDL_bool GL_OES_draw_texture_supported;
   6.100 +
   6.101 +    /* OpenGL ES functions */
   6.102 +#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
   6.103 +#include "../../video/SDL_glesfuncs.h"
   6.104 +#undef SDL_PROC
   6.105 +
   6.106 +} GLES_RenderData;
   6.107 +
   6.108 +typedef struct
   6.109 +{
   6.110 +    GLuint texture;
   6.111 +    GLenum type;
   6.112 +    GLfloat texw;
   6.113 +    GLfloat texh;
   6.114 +    GLenum format;
   6.115 +    GLenum formattype;
   6.116 +    void *pixels;
   6.117 +    int pitch;
   6.118 +} GLES_TextureData;
   6.119 +
   6.120 +static void
   6.121 +GLES_SetError(const char *prefix, GLenum result)
   6.122 +{
   6.123 +    const char *error;
   6.124 +
   6.125 +    switch (result) {
   6.126 +    case GL_NO_ERROR:
   6.127 +        error = "GL_NO_ERROR";
   6.128 +        break;
   6.129 +    case GL_INVALID_ENUM:
   6.130 +        error = "GL_INVALID_ENUM";
   6.131 +        break;
   6.132 +    case GL_INVALID_VALUE:
   6.133 +        error = "GL_INVALID_VALUE";
   6.134 +        break;
   6.135 +    case GL_INVALID_OPERATION:
   6.136 +        error = "GL_INVALID_OPERATION";
   6.137 +        break;
   6.138 +    case GL_STACK_OVERFLOW:
   6.139 +        error = "GL_STACK_OVERFLOW";
   6.140 +        break;
   6.141 +    case GL_STACK_UNDERFLOW:
   6.142 +        error = "GL_STACK_UNDERFLOW";
   6.143 +        break;
   6.144 +    case GL_OUT_OF_MEMORY:
   6.145 +        error = "GL_OUT_OF_MEMORY";
   6.146 +        break;
   6.147 +    default:
   6.148 +        error = "UNKNOWN";
   6.149 +        break;
   6.150 +    }
   6.151 +    SDL_SetError("%s: %s", prefix, error);
   6.152 +}
   6.153 +
   6.154 +static int
   6.155 +GLES_LoadFunctions(GLES_RenderData * data)
   6.156 +{
   6.157 +
   6.158 +#define SDL_PROC(ret,func,params) \
   6.159 +    data->func = func;
   6.160 +#include "../../video/SDL_glesfuncs.h"
   6.161 +#undef SDL_PROC
   6.162 +
   6.163 +    return 0;
   6.164 +}
   6.165 +
   6.166 +SDL_Renderer *
   6.167 +GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
   6.168 +{
   6.169 +
   6.170 +    SDL_Renderer *renderer;
   6.171 +    GLES_RenderData *data;
   6.172 +    GLint value;
   6.173 +    Uint32 window_flags;
   6.174 +
   6.175 +    window_flags = SDL_GetWindowFlags(window);
   6.176 +    if (!(window_flags & SDL_WINDOW_OPENGL)) {
   6.177 +        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
   6.178 +            return NULL;
   6.179 +        }
   6.180 +    }
   6.181 +
   6.182 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   6.183 +    if (!renderer) {
   6.184 +        SDL_OutOfMemory();
   6.185 +        return NULL;
   6.186 +    }
   6.187 +
   6.188 +    data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
   6.189 +    if (!data) {
   6.190 +        GLES_DestroyRenderer(renderer);
   6.191 +        SDL_OutOfMemory();
   6.192 +        return NULL;
   6.193 +    }
   6.194 +
   6.195 +    renderer->WindowEvent = GLES_WindowEvent;
   6.196 +    renderer->CreateTexture = GLES_CreateTexture;
   6.197 +    renderer->UpdateTexture = GLES_UpdateTexture;
   6.198 +    renderer->LockTexture = GLES_LockTexture;
   6.199 +    renderer->UnlockTexture = GLES_UnlockTexture;
   6.200 +    renderer->RenderDrawPoints = GLES_RenderDrawPoints;
   6.201 +    renderer->RenderDrawLines = GLES_RenderDrawLines;
   6.202 +    renderer->RenderFillRects = GLES_RenderFillRects;
   6.203 +    renderer->RenderCopy = GLES_RenderCopy;
   6.204 +    renderer->RenderPresent = GLES_RenderPresent;
   6.205 +    renderer->DestroyTexture = GLES_DestroyTexture;
   6.206 +    renderer->DestroyRenderer = GLES_DestroyRenderer;
   6.207 +    renderer->info = GL_ES_RenderDriver.info;
   6.208 +    renderer->driverdata = data;
   6.209 +
   6.210 +    renderer->info.flags = SDL_RENDERER_ACCELERATED;
   6.211 +
   6.212 +    if (GLES_LoadFunctions(data) < 0) {
   6.213 +        GLES_DestroyRenderer(renderer);
   6.214 +        return NULL;
   6.215 +    }
   6.216 +
   6.217 +    data->context = SDL_GL_CreateContext(window);
   6.218 +    if (!data->context) {
   6.219 +        GLES_DestroyRenderer(renderer);
   6.220 +        return NULL;
   6.221 +    }
   6.222 +    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   6.223 +        GLES_DestroyRenderer(renderer);
   6.224 +        return NULL;
   6.225 +    }
   6.226 +
   6.227 +    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   6.228 +        SDL_GL_SetSwapInterval(1);
   6.229 +    } else {
   6.230 +        SDL_GL_SetSwapInterval(0);
   6.231 +    }
   6.232 +    if (SDL_GL_GetSwapInterval() > 0) {
   6.233 +        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   6.234 +    }
   6.235 +
   6.236 +#if SDL_VIDEO_DRIVER_PANDORA
   6.237 +    data->GL_OES_draw_texture_supported = SDL_FALSE;
   6.238 +    data->useDrawTexture = SDL_FALSE;
   6.239 +#else
   6.240 +    if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
   6.241 +        data->GL_OES_draw_texture_supported = SDL_TRUE;
   6.242 +        data->useDrawTexture = SDL_TRUE;
   6.243 +    } else {
   6.244 +        data->GL_OES_draw_texture_supported = SDL_FALSE;
   6.245 +        data->useDrawTexture = SDL_FALSE;
   6.246 +    }
   6.247 +#endif
   6.248 +
   6.249 +    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   6.250 +    renderer->info.max_texture_width = value;
   6.251 +    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   6.252 +    renderer->info.max_texture_height = value;
   6.253 +
   6.254 +    /* Set up parameters for rendering */
   6.255 +    data->blendMode = -1;
   6.256 +    data->glDisable(GL_DEPTH_TEST);
   6.257 +    data->glDisable(GL_CULL_FACE);
   6.258 +    data->updateSize = SDL_TRUE;
   6.259 +
   6.260 +    data->glEnableClientState(GL_VERTEX_ARRAY);
   6.261 +    data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   6.262 +    
   6.263 +    return renderer;
   6.264 +}
   6.265 +
   6.266 +static SDL_GLContext SDL_CurrentContext = NULL;
   6.267 +
   6.268 +static int
   6.269 +GLES_ActivateRenderer(SDL_Renderer * renderer)
   6.270 +{
   6.271 +
   6.272 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   6.273 +    SDL_Window *window = renderer->window;
   6.274 +
   6.275 +    if (SDL_CurrentContext != data->context) {
   6.276 +        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   6.277 +            return -1;
   6.278 +        }
   6.279 +        SDL_CurrentContext = data->context;
   6.280 +    }
   6.281 +    if (data->updateSize) {
   6.282 +        int w, h;
   6.283 +
   6.284 +        SDL_GetWindowSize(window, &w, &h);
   6.285 +        data->glMatrixMode(GL_PROJECTION);
   6.286 +        data->glLoadIdentity();
   6.287 +        data->glMatrixMode(GL_MODELVIEW);
   6.288 +        data->glLoadIdentity();
   6.289 +        data->glViewport(0, 0, w, h);
   6.290 +        data->glOrthof(0.0, (GLfloat) w, (GLfloat) h, 0.0, 0.0, 1.0);
   6.291 +        data->updateSize = SDL_FALSE;
   6.292 +    }
   6.293 +    return 0;
   6.294 +}
   6.295 +
   6.296 +static void
   6.297 +GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   6.298 +{
   6.299 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   6.300 +
   6.301 +    if (event->event == SDL_WINDOWEVENT_RESIZED) {
   6.302 +        /* Rebind the context to the window area and update matrices */
   6.303 +        SDL_CurrentContext = NULL;
   6.304 +        data->updateSize = SDL_TRUE;
   6.305 +    }
   6.306 +}
   6.307 +
   6.308 +static __inline__ int
   6.309 +power_of_2(int input)
   6.310 +{
   6.311 +    int value = 1;
   6.312 +
   6.313 +    while (value < input) {
   6.314 +        value <<= 1;
   6.315 +    }
   6.316 +    return value;
   6.317 +}
   6.318 +
   6.319 +static int
   6.320 +GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   6.321 +{
   6.322 +    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   6.323 +    GLES_TextureData *data;
   6.324 +    GLint internalFormat;
   6.325 +    GLenum format, type;
   6.326 +    int texture_w, texture_h;
   6.327 +    GLenum result;
   6.328 +
   6.329 +    GLES_ActivateRenderer(renderer);
   6.330 +
   6.331 +    switch (texture->format) {
   6.332 +    case SDL_PIXELFORMAT_ABGR8888:
   6.333 +        internalFormat = GL_RGBA;
   6.334 +        format = GL_RGBA;
   6.335 +        type = GL_UNSIGNED_BYTE;
   6.336 +        break;
   6.337 +    default:
   6.338 +        SDL_SetError("Texture format %s not supported by OpenGL ES",
   6.339 +                     SDL_GetPixelFormatName(texture->format));
   6.340 +        return -1;
   6.341 +    }
   6.342 +
   6.343 +    data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
   6.344 +    if (!data) {
   6.345 +        SDL_OutOfMemory();
   6.346 +        return -1;
   6.347 +    }
   6.348 +
   6.349 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   6.350 +        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   6.351 +        data->pixels = SDL_malloc(texture->h * data->pitch);
   6.352 +        if (!data->pixels) {
   6.353 +            SDL_OutOfMemory();
   6.354 +            SDL_free(data);
   6.355 +            return -1;
   6.356 +        }
   6.357 +    }
   6.358 +
   6.359 +    texture->driverdata = data;
   6.360 +
   6.361 +    renderdata->glGetError();
   6.362 +    renderdata->glEnable(GL_TEXTURE_2D);
   6.363 +    renderdata->glGenTextures(1, &data->texture);
   6.364 +
   6.365 +    data->type = GL_TEXTURE_2D;
   6.366 +    /* no NPOV textures allowed in OpenGL ES (yet) */
   6.367 +    texture_w = power_of_2(texture->w);
   6.368 +    texture_h = power_of_2(texture->h);
   6.369 +    data->texw = (GLfloat) texture->w / texture_w;
   6.370 +    data->texh = (GLfloat) texture->h / texture_h;
   6.371 +
   6.372 +    data->format = format;
   6.373 +    data->formattype = type;
   6.374 +    renderdata->glBindTexture(data->type, data->texture);
   6.375 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   6.376 +                                GL_LINEAR);
   6.377 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   6.378 +                                GL_LINEAR);
   6.379 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   6.380 +                                GL_CLAMP_TO_EDGE);
   6.381 +    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   6.382 +                                GL_CLAMP_TO_EDGE);
   6.383 +
   6.384 +    renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   6.385 +                             texture_h, 0, format, type, NULL);
   6.386 +    renderdata->glDisable(GL_TEXTURE_2D);
   6.387 +
   6.388 +    result = renderdata->glGetError();
   6.389 +    if (result != GL_NO_ERROR) {
   6.390 +        GLES_SetError("glTexImage2D()", result);
   6.391 +        return -1;
   6.392 +    }
   6.393 +    return 0;
   6.394 +}
   6.395 +
   6.396 +static void
   6.397 +SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture,
   6.398 +                   int pitch)
   6.399 +{
   6.400 +    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   6.401 +}
   6.402 +
   6.403 +static int
   6.404 +GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   6.405 +                   const SDL_Rect * rect, const void *pixels, int pitch)
   6.406 +{
   6.407 +    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   6.408 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   6.409 +    GLenum result;
   6.410 +    int bpp = SDL_BYTESPERPIXEL(texture->format);
   6.411 +    void * temp_buffer;
   6.412 +    void * temp_ptr;
   6.413 +    int i;
   6.414 +
   6.415 +    GLES_ActivateRenderer(renderer);
   6.416 +
   6.417 +    renderdata->glGetError();
   6.418 +    SetupTextureUpdate(renderdata, texture, pitch);
   6.419 +    renderdata->glEnable(data->type);
   6.420 +    renderdata->glBindTexture(data->type, data->texture);
   6.421 +
   6.422 +    if( rect->w * bpp == pitch ) {
   6.423 +         temp_buffer = (void *)pixels; /* No need to reformat */
   6.424 +    } else {
   6.425 +         /* Reformatting of mem area required */
   6.426 +         temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   6.427 +         temp_ptr = temp_buffer;
   6.428 +         for (i = 0; i < rect->h; i++) {
   6.429 +             SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   6.430 +             temp_ptr += rect->w * bpp;
   6.431 +             pixels += pitch;
   6.432 +         }
   6.433 +    }
   6.434 +
   6.435 +    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   6.436 +                                rect->h, data->format, data->formattype,
   6.437 +                                temp_buffer);
   6.438 +
   6.439 +    if( temp_buffer != pixels ) {
   6.440 +        SDL_free(temp_buffer);
   6.441 +    }
   6.442 +
   6.443 +    renderdata->glDisable(data->type);
   6.444 +    result = renderdata->glGetError();
   6.445 +    if (result != GL_NO_ERROR) {
   6.446 +        GLES_SetError("glTexSubImage2D()", result);
   6.447 +        return -1;
   6.448 +    }
   6.449 +    return 0;
   6.450 +}
   6.451 +
   6.452 +static int
   6.453 +GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   6.454 +                 const SDL_Rect * rect, void **pixels, int *pitch)
   6.455 +{
   6.456 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   6.457 +
   6.458 +    *pixels =
   6.459 +        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   6.460 +                  rect->x * SDL_BYTESPERPIXEL(texture->format));
   6.461 +    *pitch = data->pitch;
   6.462 +    return 0;
   6.463 +}
   6.464 +
   6.465 +static void
   6.466 +GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   6.467 +{
   6.468 +    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   6.469 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   6.470 +
   6.471 +    GLES_ActivateRenderer(renderer);
   6.472 +
   6.473 +    SetupTextureUpdate(renderdata, texture, data->pitch);
   6.474 +    renderdata->glEnable(data->type);
   6.475 +    renderdata->glBindTexture(data->type, data->texture);
   6.476 +    renderdata->glTexSubImage2D(data->type, 0, 0, 0, texture->w,
   6.477 +                                texture->h, data->format, data->formattype,
   6.478 +                                data->pixels);
   6.479 +    renderdata->glDisable(data->type);
   6.480 +}
   6.481 +
   6.482 +static void
   6.483 +GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
   6.484 +{
   6.485 +    if (blendMode != data->blendMode) {
   6.486 +        switch (blendMode) {
   6.487 +        case SDL_BLENDMODE_NONE:
   6.488 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   6.489 +            data->glDisable(GL_BLEND);
   6.490 +            break;
   6.491 +        case SDL_BLENDMODE_BLEND:
   6.492 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   6.493 +            data->glEnable(GL_BLEND);
   6.494 +            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   6.495 +            break;
   6.496 +        case SDL_BLENDMODE_ADD:
   6.497 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   6.498 +            data->glEnable(GL_BLEND);
   6.499 +            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   6.500 +            break;
   6.501 +        case SDL_BLENDMODE_MOD:
   6.502 +            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   6.503 +            data->glEnable(GL_BLEND);
   6.504 +            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   6.505 +            break;
   6.506 +        }
   6.507 +        data->blendMode = blendMode;
   6.508 +    }
   6.509 +}
   6.510 +
   6.511 +static int
   6.512 +GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   6.513 +                      int count)
   6.514 +{
   6.515 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   6.516 +    int i;
   6.517 +    GLshort *vertices;
   6.518 +
   6.519 +    GLES_ActivateRenderer(renderer);
   6.520 +
   6.521 +    GLES_SetBlendMode(data, renderer->blendMode);
   6.522 +
   6.523 +    data->glColor4f((GLfloat) renderer->r * inv255f,
   6.524 +                    (GLfloat) renderer->g * inv255f,
   6.525 +                    (GLfloat) renderer->b * inv255f,
   6.526 +                    (GLfloat) renderer->a * inv255f);
   6.527 +
   6.528 +    vertices = SDL_stack_alloc(GLshort, count*2);
   6.529 +    for (i = 0; i < count; ++i) {
   6.530 +        vertices[2*i+0] = (GLshort)points[i].x;
   6.531 +        vertices[2*i+1] = (GLshort)points[i].y;
   6.532 +    }
   6.533 +    data->glVertexPointer(2, GL_SHORT, 0, vertices);
   6.534 +    data->glDrawArrays(GL_POINTS, 0, count);
   6.535 +    SDL_stack_free(vertices);
   6.536 +
   6.537 +    return 0;
   6.538 +}
   6.539 +
   6.540 +static int
   6.541 +GLES_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   6.542 +                     int count)
   6.543 +{
   6.544 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   6.545 +    int i;
   6.546 +    GLshort *vertices;
   6.547 +
   6.548 +    GLES_ActivateRenderer(renderer);
   6.549 +
   6.550 +    GLES_SetBlendMode(data, renderer->blendMode);
   6.551 +
   6.552 +    data->glColor4f((GLfloat) renderer->r * inv255f,
   6.553 +                    (GLfloat) renderer->g * inv255f,
   6.554 +                    (GLfloat) renderer->b * inv255f,
   6.555 +                    (GLfloat) renderer->a * inv255f);
   6.556 +
   6.557 +    vertices = SDL_stack_alloc(GLshort, count*2);
   6.558 +    for (i = 0; i < count; ++i) {
   6.559 +        vertices[2*i+0] = (GLshort)points[i].x;
   6.560 +        vertices[2*i+1] = (GLshort)points[i].y;
   6.561 +    }
   6.562 +    data->glVertexPointer(2, GL_SHORT, 0, vertices);
   6.563 +    if (count > 2 && 
   6.564 +        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
   6.565 +        /* GL_LINE_LOOP takes care of the final segment */
   6.566 +        --count;
   6.567 +        data->glDrawArrays(GL_LINE_LOOP, 0, count);
   6.568 +    } else {
   6.569 +        data->glDrawArrays(GL_LINE_STRIP, 0, count);
   6.570 +    }
   6.571 +    SDL_stack_free(vertices);
   6.572 +
   6.573 +    return 0;
   6.574 +}
   6.575 +
   6.576 +static int
   6.577 +GLES_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   6.578 +                     int count)
   6.579 +{
   6.580 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   6.581 +    int i;
   6.582 +
   6.583 +    GLES_ActivateRenderer(renderer);
   6.584 +
   6.585 +    GLES_SetBlendMode(data, renderer->blendMode);
   6.586 +
   6.587 +    data->glColor4f((GLfloat) renderer->r * inv255f,
   6.588 +                    (GLfloat) renderer->g * inv255f,
   6.589 +                    (GLfloat) renderer->b * inv255f,
   6.590 +                    (GLfloat) renderer->a * inv255f);
   6.591 +
   6.592 +    for (i = 0; i < count; ++i) {
   6.593 +        const SDL_Rect *rect = rects[i];
   6.594 +        GLshort minx = rect->x;
   6.595 +        GLshort maxx = rect->x + rect->w;
   6.596 +        GLshort miny = rect->y;
   6.597 +        GLshort maxy = rect->y + rect->h;
   6.598 +        GLshort vertices[8];
   6.599 +        vertices[0] = minx;
   6.600 +        vertices[1] = miny;
   6.601 +        vertices[2] = maxx;
   6.602 +        vertices[3] = miny;
   6.603 +        vertices[4] = minx;
   6.604 +        vertices[5] = maxy;
   6.605 +        vertices[6] = maxx;
   6.606 +        vertices[7] = maxy;
   6.607 +
   6.608 +        data->glVertexPointer(2, GL_SHORT, 0, vertices);
   6.609 +        data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   6.610 +    }
   6.611 +
   6.612 +    return 0;
   6.613 +}
   6.614 +
   6.615 +static int
   6.616 +GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   6.617 +                const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   6.618 +{
   6.619 +
   6.620 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   6.621 +    GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
   6.622 +    int minx, miny, maxx, maxy;
   6.623 +    GLfloat minu, maxu, minv, maxv;
   6.624 +    int i;
   6.625 +    void *temp_buffer;          /* used for reformatting dirty rect pixels */
   6.626 +    void *temp_ptr;
   6.627 +
   6.628 +    GLES_ActivateRenderer(renderer);
   6.629 +
   6.630 +    data->glEnable(GL_TEXTURE_2D);
   6.631 +
   6.632 +    data->glBindTexture(texturedata->type, texturedata->texture);
   6.633 +
   6.634 +    if (texture->modMode) {
   6.635 +        data->glColor4f((GLfloat) texture->r * inv255f,
   6.636 +                        (GLfloat) texture->g * inv255f,
   6.637 +                        (GLfloat) texture->b * inv255f,
   6.638 +                        (GLfloat) texture->a * inv255f);
   6.639 +    } else {
   6.640 +        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   6.641 +    }
   6.642 +
   6.643 +    GLES_SetBlendMode(data, texture->blendMode);
   6.644 +
   6.645 +    if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   6.646 +        /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   6.647 +        GLint cropRect[4];
   6.648 +        int w, h;
   6.649 +        SDL_Window *window = renderer->window;
   6.650 +
   6.651 +        SDL_GetWindowSize(window, &w, &h);
   6.652 +        cropRect[0] = srcrect->x;
   6.653 +        cropRect[1] = srcrect->y + srcrect->h;
   6.654 +        cropRect[2] = srcrect->w;
   6.655 +        cropRect[3] = -srcrect->h;
   6.656 +        data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
   6.657 +                               cropRect);
   6.658 +        data->glDrawTexiOES(dstrect->x, h - dstrect->y - dstrect->h, 0,
   6.659 +                            dstrect->w, dstrect->h);
   6.660 +    } else {
   6.661 +
   6.662 +        minx = dstrect->x;
   6.663 +        miny = dstrect->y;
   6.664 +        maxx = dstrect->x + dstrect->w;
   6.665 +        maxy = dstrect->y + dstrect->h;
   6.666 +
   6.667 +        minu = (GLfloat) srcrect->x / texture->w;
   6.668 +        minu *= texturedata->texw;
   6.669 +        maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   6.670 +        maxu *= texturedata->texw;
   6.671 +        minv = (GLfloat) srcrect->y / texture->h;
   6.672 +        minv *= texturedata->texh;
   6.673 +        maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   6.674 +        maxv *= texturedata->texh;
   6.675 +
   6.676 +        GLshort vertices[8];
   6.677 +        GLfloat texCoords[8];
   6.678 +
   6.679 +        vertices[0] = minx;
   6.680 +        vertices[1] = miny;
   6.681 +        vertices[2] = maxx;
   6.682 +        vertices[3] = miny;
   6.683 +        vertices[4] = minx;
   6.684 +        vertices[5] = maxy;
   6.685 +        vertices[6] = maxx;
   6.686 +        vertices[7] = maxy;
   6.687 +
   6.688 +        texCoords[0] = minu;
   6.689 +        texCoords[1] = minv;
   6.690 +        texCoords[2] = maxu;
   6.691 +        texCoords[3] = minv;
   6.692 +        texCoords[4] = minu;
   6.693 +        texCoords[5] = maxv;
   6.694 +        texCoords[6] = maxu;
   6.695 +        texCoords[7] = maxv;
   6.696 +
   6.697 +        data->glVertexPointer(2, GL_SHORT, 0, vertices);
   6.698 +        data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   6.699 +        data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   6.700 +    }
   6.701 +
   6.702 +    data->glDisable(GL_TEXTURE_2D);
   6.703 +
   6.704 +    return 0;
   6.705 +}
   6.706 +
   6.707 +static void
   6.708 +GLES_RenderPresent(SDL_Renderer * renderer)
   6.709 +{
   6.710 +    GLES_ActivateRenderer(renderer);
   6.711 +
   6.712 +    SDL_GL_SwapWindow(renderer->window);
   6.713 +}
   6.714 +
   6.715 +static void
   6.716 +GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   6.717 +{
   6.718 +    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   6.719 +
   6.720 +    GLES_ActivateRenderer(renderer);
   6.721 +
   6.722 +    if (!data) {
   6.723 +        return;
   6.724 +    }
   6.725 +    if (data->texture) {
   6.726 +        glDeleteTextures(1, &data->texture);
   6.727 +    }
   6.728 +    if (data->pixels) {
   6.729 +        SDL_free(data->pixels);
   6.730 +    }
   6.731 +    SDL_free(data);
   6.732 +    texture->driverdata = NULL;
   6.733 +}
   6.734 +
   6.735 +static void
   6.736 +GLES_DestroyRenderer(SDL_Renderer * renderer)
   6.737 +{
   6.738 +    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   6.739 +
   6.740 +    if (data) {
   6.741 +        if (data->context) {
   6.742 +            SDL_GL_DeleteContext(data->context);
   6.743 +        }
   6.744 +        SDL_free(data);
   6.745 +    }
   6.746 +    SDL_free(renderer);
   6.747 +}
   6.748 +
   6.749 +#endif /* SDL_VIDEO_RENDER_OGL_ES */
   6.750 +
   6.751 +/* vi: set ts=4 sw=4 expandtab: */
     7.1 --- a/src/render/opengles/SDL_renderer_gles.c	Sat Feb 05 11:54:46 2011 -0800
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,748 +0,0 @@
     7.4 -/*
     7.5 -    SDL - Simple DirectMedia Layer
     7.6 -    Copyright (C) 1997-2010 Sam Lantinga
     7.7 -
     7.8 -    This library is free software; you can redistribute it and/or
     7.9 -    modify it under the terms of the GNU Lesser General Public
    7.10 -    License as published by the Free Software Foundation; either
    7.11 -    version 2.1 of the License, or (at your option) any later version.
    7.12 -
    7.13 -    This library is distributed in the hope that it will be useful,
    7.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    7.16 -    Lesser General Public License for more details.
    7.17 -
    7.18 -    You should have received a copy of the GNU Lesser General Public
    7.19 -    License along with this library; if not, write to the Free Software
    7.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    7.21 -
    7.22 -    Sam Lantinga
    7.23 -    slouken@libsdl.org
    7.24 -*/
    7.25 -#include "SDL_config.h"
    7.26 -
    7.27 -#if SDL_VIDEO_RENDER_OGL_ES
    7.28 -
    7.29 -#include "SDL_opengles.h"
    7.30 -#include "../SDL_sysrender.h"
    7.31 -
    7.32 -#if defined(SDL_VIDEO_DRIVER_PANDORA)
    7.33 -
    7.34 -/* Empty function stub to get OpenGL ES 1.x support without  */
    7.35 -/* OpenGL ES extension GL_OES_draw_texture supported         */
    7.36 -GL_API void GL_APIENTRY
    7.37 -glDrawTexiOES(GLint x, GLint y, GLint z, GLint width, GLint height)
    7.38 -{
    7.39 -    return;
    7.40 -}
    7.41 -
    7.42 -#endif /* PANDORA */
    7.43 -
    7.44 -/* OpenGL ES 1.1 renderer implementation, based on the OpenGL renderer */
    7.45 -
    7.46 -/* Used to re-create the window with OpenGL capability */
    7.47 -extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
    7.48 -
    7.49 -static const float inv255f = 1.0f / 255.0f;
    7.50 -
    7.51 -static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags);
    7.52 -static void GLES_WindowEvent(SDL_Renderer * renderer,
    7.53 -                             const SDL_WindowEvent *event);
    7.54 -static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    7.55 -static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    7.56 -                              const SDL_Rect * rect, const void *pixels,
    7.57 -                              int pitch);
    7.58 -static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    7.59 -                            const SDL_Rect * rect, void **pixels, int *pitch);
    7.60 -static void GLES_UnlockTexture(SDL_Renderer * renderer,
    7.61 -                               SDL_Texture * texture);
    7.62 -static int GLES_RenderDrawPoints(SDL_Renderer * renderer,
    7.63 -                                 const SDL_Point * points, int count);
    7.64 -static int GLES_RenderDrawLines(SDL_Renderer * renderer,
    7.65 -                                const SDL_Point * points, int count);
    7.66 -static int GLES_RenderFillRects(SDL_Renderer * renderer,
    7.67 -                                const SDL_Rect ** rects, int count);
    7.68 -static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    7.69 -                           const SDL_Rect * srcrect,
    7.70 -                           const SDL_Rect * dstrect);
    7.71 -static void GLES_RenderPresent(SDL_Renderer * renderer);
    7.72 -static void GLES_DestroyTexture(SDL_Renderer * renderer,
    7.73 -                                SDL_Texture * texture);
    7.74 -static void GLES_DestroyRenderer(SDL_Renderer * renderer);
    7.75 -
    7.76 -
    7.77 -SDL_RenderDriver GL_ES_RenderDriver = {
    7.78 -    GLES_CreateRenderer,
    7.79 -    {
    7.80 -     "opengl_es",
    7.81 -     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
    7.82 -     1,
    7.83 -     {SDL_PIXELFORMAT_ABGR8888},
    7.84 -     0,
    7.85 -     0}
    7.86 -};
    7.87 -
    7.88 -typedef struct
    7.89 -{
    7.90 -    SDL_GLContext context;
    7.91 -    SDL_bool updateSize;
    7.92 -    int blendMode;
    7.93 -
    7.94 -#ifndef APIENTRY
    7.95 -#define APIENTRY
    7.96 -#endif
    7.97 -
    7.98 -    SDL_bool useDrawTexture;
    7.99 -    SDL_bool GL_OES_draw_texture_supported;
   7.100 -
   7.101 -    /* OpenGL ES functions */
   7.102 -#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
   7.103 -#include "../../video/SDL_glesfuncs.h"
   7.104 -#undef SDL_PROC
   7.105 -
   7.106 -} GLES_RenderData;
   7.107 -
   7.108 -typedef struct
   7.109 -{
   7.110 -    GLuint texture;
   7.111 -    GLenum type;
   7.112 -    GLfloat texw;
   7.113 -    GLfloat texh;
   7.114 -    GLenum format;
   7.115 -    GLenum formattype;
   7.116 -    void *pixels;
   7.117 -    int pitch;
   7.118 -} GLES_TextureData;
   7.119 -
   7.120 -static void
   7.121 -GLES_SetError(const char *prefix, GLenum result)
   7.122 -{
   7.123 -    const char *error;
   7.124 -
   7.125 -    switch (result) {
   7.126 -    case GL_NO_ERROR:
   7.127 -        error = "GL_NO_ERROR";
   7.128 -        break;
   7.129 -    case GL_INVALID_ENUM:
   7.130 -        error = "GL_INVALID_ENUM";
   7.131 -        break;
   7.132 -    case GL_INVALID_VALUE:
   7.133 -        error = "GL_INVALID_VALUE";
   7.134 -        break;
   7.135 -    case GL_INVALID_OPERATION:
   7.136 -        error = "GL_INVALID_OPERATION";
   7.137 -        break;
   7.138 -    case GL_STACK_OVERFLOW:
   7.139 -        error = "GL_STACK_OVERFLOW";
   7.140 -        break;
   7.141 -    case GL_STACK_UNDERFLOW:
   7.142 -        error = "GL_STACK_UNDERFLOW";
   7.143 -        break;
   7.144 -    case GL_OUT_OF_MEMORY:
   7.145 -        error = "GL_OUT_OF_MEMORY";
   7.146 -        break;
   7.147 -    default:
   7.148 -        error = "UNKNOWN";
   7.149 -        break;
   7.150 -    }
   7.151 -    SDL_SetError("%s: %s", prefix, error);
   7.152 -}
   7.153 -
   7.154 -static int
   7.155 -GLES_LoadFunctions(GLES_RenderData * data)
   7.156 -{
   7.157 -
   7.158 -#define SDL_PROC(ret,func,params) \
   7.159 -    data->func = func;
   7.160 -#include "../../video/SDL_glesfuncs.h"
   7.161 -#undef SDL_PROC
   7.162 -
   7.163 -    return 0;
   7.164 -}
   7.165 -
   7.166 -SDL_Renderer *
   7.167 -GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
   7.168 -{
   7.169 -
   7.170 -    SDL_Renderer *renderer;
   7.171 -    GLES_RenderData *data;
   7.172 -    GLint value;
   7.173 -    Uint32 window_flags;
   7.174 -
   7.175 -    window_flags = SDL_GetWindowFlags(window);
   7.176 -    if (!(window_flags & SDL_WINDOW_OPENGL)) {
   7.177 -        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
   7.178 -            return NULL;
   7.179 -        }
   7.180 -    }
   7.181 -
   7.182 -    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   7.183 -    if (!renderer) {
   7.184 -        SDL_OutOfMemory();
   7.185 -        return NULL;
   7.186 -    }
   7.187 -
   7.188 -    data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
   7.189 -    if (!data) {
   7.190 -        GLES_DestroyRenderer(renderer);
   7.191 -        SDL_OutOfMemory();
   7.192 -        return NULL;
   7.193 -    }
   7.194 -
   7.195 -    renderer->WindowEvent = GLES_WindowEvent;
   7.196 -    renderer->CreateTexture = GLES_CreateTexture;
   7.197 -    renderer->UpdateTexture = GLES_UpdateTexture;
   7.198 -    renderer->LockTexture = GLES_LockTexture;
   7.199 -    renderer->UnlockTexture = GLES_UnlockTexture;
   7.200 -    renderer->RenderDrawPoints = GLES_RenderDrawPoints;
   7.201 -    renderer->RenderDrawLines = GLES_RenderDrawLines;
   7.202 -    renderer->RenderFillRects = GLES_RenderFillRects;
   7.203 -    renderer->RenderCopy = GLES_RenderCopy;
   7.204 -    renderer->RenderPresent = GLES_RenderPresent;
   7.205 -    renderer->DestroyTexture = GLES_DestroyTexture;
   7.206 -    renderer->DestroyRenderer = GLES_DestroyRenderer;
   7.207 -    renderer->info = GL_ES_RenderDriver.info;
   7.208 -    renderer->driverdata = data;
   7.209 -
   7.210 -    renderer->info.flags = SDL_RENDERER_ACCELERATED;
   7.211 -
   7.212 -    if (GLES_LoadFunctions(data) < 0) {
   7.213 -        GLES_DestroyRenderer(renderer);
   7.214 -        return NULL;
   7.215 -    }
   7.216 -
   7.217 -    data->context = SDL_GL_CreateContext(window);
   7.218 -    if (!data->context) {
   7.219 -        GLES_DestroyRenderer(renderer);
   7.220 -        return NULL;
   7.221 -    }
   7.222 -    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   7.223 -        GLES_DestroyRenderer(renderer);
   7.224 -        return NULL;
   7.225 -    }
   7.226 -
   7.227 -    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   7.228 -        SDL_GL_SetSwapInterval(1);
   7.229 -    } else {
   7.230 -        SDL_GL_SetSwapInterval(0);
   7.231 -    }
   7.232 -    if (SDL_GL_GetSwapInterval() > 0) {
   7.233 -        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   7.234 -    }
   7.235 -
   7.236 -#if SDL_VIDEO_DRIVER_PANDORA
   7.237 -    data->GL_OES_draw_texture_supported = SDL_FALSE;
   7.238 -    data->useDrawTexture = SDL_FALSE;
   7.239 -#else
   7.240 -    if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
   7.241 -        data->GL_OES_draw_texture_supported = SDL_TRUE;
   7.242 -        data->useDrawTexture = SDL_TRUE;
   7.243 -    } else {
   7.244 -        data->GL_OES_draw_texture_supported = SDL_FALSE;
   7.245 -        data->useDrawTexture = SDL_FALSE;
   7.246 -    }
   7.247 -#endif
   7.248 -
   7.249 -    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   7.250 -    renderer->info.max_texture_width = value;
   7.251 -    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   7.252 -    renderer->info.max_texture_height = value;
   7.253 -
   7.254 -    /* Set up parameters for rendering */
   7.255 -    data->blendMode = -1;
   7.256 -    data->glDisable(GL_DEPTH_TEST);
   7.257 -    data->glDisable(GL_CULL_FACE);
   7.258 -    data->updateSize = SDL_TRUE;
   7.259 -
   7.260 -    data->glEnableClientState(GL_VERTEX_ARRAY);
   7.261 -    data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   7.262 -    
   7.263 -    return renderer;
   7.264 -}
   7.265 -
   7.266 -static SDL_GLContext SDL_CurrentContext = NULL;
   7.267 -
   7.268 -static int
   7.269 -GLES_ActivateRenderer(SDL_Renderer * renderer)
   7.270 -{
   7.271 -
   7.272 -    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   7.273 -    SDL_Window *window = renderer->window;
   7.274 -
   7.275 -    if (SDL_CurrentContext != data->context) {
   7.276 -        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   7.277 -            return -1;
   7.278 -        }
   7.279 -        SDL_CurrentContext = data->context;
   7.280 -    }
   7.281 -    if (data->updateSize) {
   7.282 -        int w, h;
   7.283 -
   7.284 -        SDL_GetWindowSize(window, &w, &h);
   7.285 -        data->glMatrixMode(GL_PROJECTION);
   7.286 -        data->glLoadIdentity();
   7.287 -        data->glMatrixMode(GL_MODELVIEW);
   7.288 -        data->glLoadIdentity();
   7.289 -        data->glViewport(0, 0, w, h);
   7.290 -        data->glOrthof(0.0, (GLfloat) w, (GLfloat) h, 0.0, 0.0, 1.0);
   7.291 -        data->updateSize = SDL_FALSE;
   7.292 -    }
   7.293 -    return 0;
   7.294 -}
   7.295 -
   7.296 -static void
   7.297 -GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   7.298 -{
   7.299 -    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   7.300 -
   7.301 -    if (event->event == SDL_WINDOWEVENT_RESIZED) {
   7.302 -        /* Rebind the context to the window area and update matrices */
   7.303 -        SDL_CurrentContext = NULL;
   7.304 -        data->updateSize = SDL_TRUE;
   7.305 -    }
   7.306 -}
   7.307 -
   7.308 -static __inline__ int
   7.309 -power_of_2(int input)
   7.310 -{
   7.311 -    int value = 1;
   7.312 -
   7.313 -    while (value < input) {
   7.314 -        value <<= 1;
   7.315 -    }
   7.316 -    return value;
   7.317 -}
   7.318 -
   7.319 -static int
   7.320 -GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   7.321 -{
   7.322 -    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   7.323 -    GLES_TextureData *data;
   7.324 -    GLint internalFormat;
   7.325 -    GLenum format, type;
   7.326 -    int texture_w, texture_h;
   7.327 -    GLenum result;
   7.328 -
   7.329 -    GLES_ActivateRenderer(renderer);
   7.330 -
   7.331 -    switch (texture->format) {
   7.332 -    case SDL_PIXELFORMAT_ABGR8888:
   7.333 -        internalFormat = GL_RGBA;
   7.334 -        format = GL_RGBA;
   7.335 -        type = GL_UNSIGNED_BYTE;
   7.336 -        break;
   7.337 -    default:
   7.338 -        SDL_SetError("Texture format %s not supported by OpenGL ES",
   7.339 -                     SDL_GetPixelFormatName(texture->format));
   7.340 -        return -1;
   7.341 -    }
   7.342 -
   7.343 -    data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
   7.344 -    if (!data) {
   7.345 -        SDL_OutOfMemory();
   7.346 -        return -1;
   7.347 -    }
   7.348 -
   7.349 -    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   7.350 -        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   7.351 -        data->pixels = SDL_malloc(texture->h * data->pitch);
   7.352 -        if (!data->pixels) {
   7.353 -            SDL_OutOfMemory();
   7.354 -            SDL_free(data);
   7.355 -            return -1;
   7.356 -        }
   7.357 -    }
   7.358 -
   7.359 -    texture->driverdata = data;
   7.360 -
   7.361 -    renderdata->glGetError();
   7.362 -    renderdata->glEnable(GL_TEXTURE_2D);
   7.363 -    renderdata->glGenTextures(1, &data->texture);
   7.364 -
   7.365 -    data->type = GL_TEXTURE_2D;
   7.366 -    /* no NPOV textures allowed in OpenGL ES (yet) */
   7.367 -    texture_w = power_of_2(texture->w);
   7.368 -    texture_h = power_of_2(texture->h);
   7.369 -    data->texw = (GLfloat) texture->w / texture_w;
   7.370 -    data->texh = (GLfloat) texture->h / texture_h;
   7.371 -
   7.372 -    data->format = format;
   7.373 -    data->formattype = type;
   7.374 -    renderdata->glBindTexture(data->type, data->texture);
   7.375 -    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   7.376 -                                GL_LINEAR);
   7.377 -    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   7.378 -                                GL_LINEAR);
   7.379 -    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   7.380 -                                GL_CLAMP_TO_EDGE);
   7.381 -    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   7.382 -                                GL_CLAMP_TO_EDGE);
   7.383 -
   7.384 -    renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   7.385 -                             texture_h, 0, format, type, NULL);
   7.386 -    renderdata->glDisable(GL_TEXTURE_2D);
   7.387 -
   7.388 -    result = renderdata->glGetError();
   7.389 -    if (result != GL_NO_ERROR) {
   7.390 -        GLES_SetError("glTexImage2D()", result);
   7.391 -        return -1;
   7.392 -    }
   7.393 -    return 0;
   7.394 -}
   7.395 -
   7.396 -static void
   7.397 -SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture,
   7.398 -                   int pitch)
   7.399 -{
   7.400 -    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   7.401 -}
   7.402 -
   7.403 -static int
   7.404 -GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   7.405 -                   const SDL_Rect * rect, const void *pixels, int pitch)
   7.406 -{
   7.407 -    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   7.408 -    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   7.409 -    GLenum result;
   7.410 -    int bpp = SDL_BYTESPERPIXEL(texture->format);
   7.411 -    void * temp_buffer;
   7.412 -    void * temp_ptr;
   7.413 -    int i;
   7.414 -
   7.415 -    GLES_ActivateRenderer(renderer);
   7.416 -
   7.417 -    renderdata->glGetError();
   7.418 -    SetupTextureUpdate(renderdata, texture, pitch);
   7.419 -    renderdata->glEnable(data->type);
   7.420 -    renderdata->glBindTexture(data->type, data->texture);
   7.421 -
   7.422 -    if( rect->w * bpp == pitch ) {
   7.423 -         temp_buffer = (void *)pixels; /* No need to reformat */
   7.424 -    } else {
   7.425 -         /* Reformatting of mem area required */
   7.426 -         temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   7.427 -         temp_ptr = temp_buffer;
   7.428 -         for (i = 0; i < rect->h; i++) {
   7.429 -             SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   7.430 -             temp_ptr += rect->w * bpp;
   7.431 -             pixels += pitch;
   7.432 -         }
   7.433 -    }
   7.434 -
   7.435 -    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   7.436 -                                rect->h, data->format, data->formattype,
   7.437 -                                temp_buffer);
   7.438 -
   7.439 -    if( temp_buffer != pixels ) {
   7.440 -        SDL_free(temp_buffer);
   7.441 -    }
   7.442 -
   7.443 -    renderdata->glDisable(data->type);
   7.444 -    result = renderdata->glGetError();
   7.445 -    if (result != GL_NO_ERROR) {
   7.446 -        GLES_SetError("glTexSubImage2D()", result);
   7.447 -        return -1;
   7.448 -    }
   7.449 -    return 0;
   7.450 -}
   7.451 -
   7.452 -static int
   7.453 -GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   7.454 -                 const SDL_Rect * rect, void **pixels, int *pitch)
   7.455 -{
   7.456 -    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   7.457 -
   7.458 -    *pixels =
   7.459 -        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   7.460 -                  rect->x * SDL_BYTESPERPIXEL(texture->format));
   7.461 -    *pitch = data->pitch;
   7.462 -    return 0;
   7.463 -}
   7.464 -
   7.465 -static void
   7.466 -GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   7.467 -{
   7.468 -    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   7.469 -    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   7.470 -
   7.471 -    GLES_ActivateRenderer(renderer);
   7.472 -
   7.473 -    SetupTextureUpdate(renderdata, texture, data->pitch);
   7.474 -    renderdata->glEnable(data->type);
   7.475 -    renderdata->glBindTexture(data->type, data->texture);
   7.476 -    renderdata->glTexSubImage2D(data->type, 0, 0, 0, texture->w,
   7.477 -                                texture->h, data->format, data->formattype,
   7.478 -                                data->pixels);
   7.479 -    renderdata->glDisable(data->type);
   7.480 -}
   7.481 -
   7.482 -static void
   7.483 -GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
   7.484 -{
   7.485 -    if (blendMode != data->blendMode) {
   7.486 -        switch (blendMode) {
   7.487 -        case SDL_BLENDMODE_NONE:
   7.488 -            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   7.489 -            data->glDisable(GL_BLEND);
   7.490 -            break;
   7.491 -        case SDL_BLENDMODE_BLEND:
   7.492 -            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   7.493 -            data->glEnable(GL_BLEND);
   7.494 -            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   7.495 -            break;
   7.496 -        case SDL_BLENDMODE_ADD:
   7.497 -            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   7.498 -            data->glEnable(GL_BLEND);
   7.499 -            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   7.500 -            break;
   7.501 -        case SDL_BLENDMODE_MOD:
   7.502 -            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   7.503 -            data->glEnable(GL_BLEND);
   7.504 -            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   7.505 -            break;
   7.506 -        }
   7.507 -        data->blendMode = blendMode;
   7.508 -    }
   7.509 -}
   7.510 -
   7.511 -static int
   7.512 -GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   7.513 -                      int count)
   7.514 -{
   7.515 -    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   7.516 -    int i;
   7.517 -    GLshort *vertices;
   7.518 -
   7.519 -    GLES_ActivateRenderer(renderer);
   7.520 -
   7.521 -    GLES_SetBlendMode(data, renderer->blendMode);
   7.522 -
   7.523 -    data->glColor4f((GLfloat) renderer->r * inv255f,
   7.524 -                    (GLfloat) renderer->g * inv255f,
   7.525 -                    (GLfloat) renderer->b * inv255f,
   7.526 -                    (GLfloat) renderer->a * inv255f);
   7.527 -
   7.528 -    vertices = SDL_stack_alloc(GLshort, count*2);
   7.529 -    for (i = 0; i < count; ++i) {
   7.530 -        vertices[2*i+0] = (GLshort)points[i].x;
   7.531 -        vertices[2*i+1] = (GLshort)points[i].y;
   7.532 -    }
   7.533 -    data->glVertexPointer(2, GL_SHORT, 0, vertices);
   7.534 -    data->glDrawArrays(GL_POINTS, 0, count);
   7.535 -    SDL_stack_free(vertices);
   7.536 -
   7.537 -    return 0;
   7.538 -}
   7.539 -
   7.540 -static int
   7.541 -GLES_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   7.542 -                     int count)
   7.543 -{
   7.544 -    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   7.545 -    int i;
   7.546 -    GLshort *vertices;
   7.547 -
   7.548 -    GLES_ActivateRenderer(renderer);
   7.549 -
   7.550 -    GLES_SetBlendMode(data, renderer->blendMode);
   7.551 -
   7.552 -    data->glColor4f((GLfloat) renderer->r * inv255f,
   7.553 -                    (GLfloat) renderer->g * inv255f,
   7.554 -                    (GLfloat) renderer->b * inv255f,
   7.555 -                    (GLfloat) renderer->a * inv255f);
   7.556 -
   7.557 -    vertices = SDL_stack_alloc(GLshort, count*2);
   7.558 -    for (i = 0; i < count; ++i) {
   7.559 -        vertices[2*i+0] = (GLshort)points[i].x;
   7.560 -        vertices[2*i+1] = (GLshort)points[i].y;
   7.561 -    }
   7.562 -    data->glVertexPointer(2, GL_SHORT, 0, vertices);
   7.563 -    if (count > 2 && 
   7.564 -        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
   7.565 -        /* GL_LINE_LOOP takes care of the final segment */
   7.566 -        --count;
   7.567 -        data->glDrawArrays(GL_LINE_LOOP, 0, count);
   7.568 -    } else {
   7.569 -        data->glDrawArrays(GL_LINE_STRIP, 0, count);
   7.570 -    }
   7.571 -    SDL_stack_free(vertices);
   7.572 -
   7.573 -    return 0;
   7.574 -}
   7.575 -
   7.576 -static int
   7.577 -GLES_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   7.578 -                     int count)
   7.579 -{
   7.580 -    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   7.581 -    int i;
   7.582 -
   7.583 -    GLES_ActivateRenderer(renderer);
   7.584 -
   7.585 -    GLES_SetBlendMode(data, renderer->blendMode);
   7.586 -
   7.587 -    data->glColor4f((GLfloat) renderer->r * inv255f,
   7.588 -                    (GLfloat) renderer->g * inv255f,
   7.589 -                    (GLfloat) renderer->b * inv255f,
   7.590 -                    (GLfloat) renderer->a * inv255f);
   7.591 -
   7.592 -    for (i = 0; i < count; ++i) {
   7.593 -        const SDL_Rect *rect = rects[i];
   7.594 -        GLshort minx = rect->x;
   7.595 -        GLshort maxx = rect->x + rect->w;
   7.596 -        GLshort miny = rect->y;
   7.597 -        GLshort maxy = rect->y + rect->h;
   7.598 -        GLshort vertices[8];
   7.599 -        vertices[0] = minx;
   7.600 -        vertices[1] = miny;
   7.601 -        vertices[2] = maxx;
   7.602 -        vertices[3] = miny;
   7.603 -        vertices[4] = minx;
   7.604 -        vertices[5] = maxy;
   7.605 -        vertices[6] = maxx;
   7.606 -        vertices[7] = maxy;
   7.607 -
   7.608 -        data->glVertexPointer(2, GL_SHORT, 0, vertices);
   7.609 -        data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   7.610 -    }
   7.611 -
   7.612 -    return 0;
   7.613 -}
   7.614 -
   7.615 -static int
   7.616 -GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   7.617 -                const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   7.618 -{
   7.619 -
   7.620 -    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   7.621 -    GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
   7.622 -    int minx, miny, maxx, maxy;
   7.623 -    GLfloat minu, maxu, minv, maxv;
   7.624 -    int i;
   7.625 -    void *temp_buffer;          /* used for reformatting dirty rect pixels */
   7.626 -    void *temp_ptr;
   7.627 -
   7.628 -    GLES_ActivateRenderer(renderer);
   7.629 -
   7.630 -    data->glEnable(GL_TEXTURE_2D);
   7.631 -
   7.632 -    data->glBindTexture(texturedata->type, texturedata->texture);
   7.633 -
   7.634 -    if (texture->modMode) {
   7.635 -        data->glColor4f((GLfloat) texture->r * inv255f,
   7.636 -                        (GLfloat) texture->g * inv255f,
   7.637 -                        (GLfloat) texture->b * inv255f,
   7.638 -                        (GLfloat) texture->a * inv255f);
   7.639 -    } else {
   7.640 -        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   7.641 -    }
   7.642 -
   7.643 -    GLES_SetBlendMode(data, texture->blendMode);
   7.644 -
   7.645 -    if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   7.646 -        /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   7.647 -        GLint cropRect[4];
   7.648 -        int w, h;
   7.649 -        SDL_Window *window = renderer->window;
   7.650 -
   7.651 -        SDL_GetWindowSize(window, &w, &h);
   7.652 -        cropRect[0] = srcrect->x;
   7.653 -        cropRect[1] = srcrect->y + srcrect->h;
   7.654 -        cropRect[2] = srcrect->w;
   7.655 -        cropRect[3] = -srcrect->h;
   7.656 -        data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
   7.657 -                               cropRect);
   7.658 -        data->glDrawTexiOES(dstrect->x, h - dstrect->y - dstrect->h, 0,
   7.659 -                            dstrect->w, dstrect->h);
   7.660 -    } else {
   7.661 -
   7.662 -        minx = dstrect->x;
   7.663 -        miny = dstrect->y;
   7.664 -        maxx = dstrect->x + dstrect->w;
   7.665 -        maxy = dstrect->y + dstrect->h;
   7.666 -
   7.667 -        minu = (GLfloat) srcrect->x / texture->w;
   7.668 -        minu *= texturedata->texw;
   7.669 -        maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   7.670 -        maxu *= texturedata->texw;
   7.671 -        minv = (GLfloat) srcrect->y / texture->h;
   7.672 -        minv *= texturedata->texh;
   7.673 -        maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   7.674 -        maxv *= texturedata->texh;
   7.675 -
   7.676 -        GLshort vertices[8];
   7.677 -        GLfloat texCoords[8];
   7.678 -
   7.679 -        vertices[0] = minx;
   7.680 -        vertices[1] = miny;
   7.681 -        vertices[2] = maxx;
   7.682 -        vertices[3] = miny;
   7.683 -        vertices[4] = minx;
   7.684 -        vertices[5] = maxy;
   7.685 -        vertices[6] = maxx;
   7.686 -        vertices[7] = maxy;
   7.687 -
   7.688 -        texCoords[0] = minu;
   7.689 -        texCoords[1] = minv;
   7.690 -        texCoords[2] = maxu;
   7.691 -        texCoords[3] = minv;
   7.692 -        texCoords[4] = minu;
   7.693 -        texCoords[5] = maxv;
   7.694 -        texCoords[6] = maxu;
   7.695 -        texCoords[7] = maxv;
   7.696 -
   7.697 -        data->glVertexPointer(2, GL_SHORT, 0, vertices);
   7.698 -        data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   7.699 -        data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   7.700 -    }
   7.701 -
   7.702 -    data->glDisable(GL_TEXTURE_2D);
   7.703 -
   7.704 -    return 0;
   7.705 -}
   7.706 -
   7.707 -static void
   7.708 -GLES_RenderPresent(SDL_Renderer * renderer)
   7.709 -{
   7.710 -    GLES_ActivateRenderer(renderer);
   7.711 -
   7.712 -    SDL_GL_SwapWindow(renderer->window);
   7.713 -}
   7.714 -
   7.715 -static void
   7.716 -GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   7.717 -{
   7.718 -    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   7.719 -
   7.720 -    GLES_ActivateRenderer(renderer);
   7.721 -
   7.722 -    if (!data) {
   7.723 -        return;
   7.724 -    }
   7.725 -    if (data->texture) {
   7.726 -        glDeleteTextures(1, &data->texture);
   7.727 -    }
   7.728 -    if (data->pixels) {
   7.729 -        SDL_free(data->pixels);
   7.730 -    }
   7.731 -    SDL_free(data);
   7.732 -    texture->driverdata = NULL;
   7.733 -}
   7.734 -
   7.735 -static void
   7.736 -GLES_DestroyRenderer(SDL_Renderer * renderer)
   7.737 -{
   7.738 -    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   7.739 -
   7.740 -    if (data) {
   7.741 -        if (data->context) {
   7.742 -            SDL_GL_DeleteContext(data->context);
   7.743 -        }
   7.744 -        SDL_free(data);
   7.745 -    }
   7.746 -    SDL_free(renderer);
   7.747 -}
   7.748 -
   7.749 -#endif /* SDL_VIDEO_RENDER_OGL_ES */
   7.750 -
   7.751 -/* vi: set ts=4 sw=4 expandtab: */
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/render/software/SDL_render_sw.c	Sat Feb 05 12:01:11 2011 -0800
     8.3 @@ -0,0 +1,418 @@
     8.4 +/*
     8.5 +    SDL - Simple DirectMedia Layer
     8.6 +    Copyright (C) 1997-2010 Sam Lantinga
     8.7 +
     8.8 +    This library is free software; you can redistribute it and/or
     8.9 +    modify it under the terms of the GNU Lesser General Public
    8.10 +    License as published by the Free Software Foundation; either
    8.11 +    version 2.1 of the License, or (at your option) any later version.
    8.12 +
    8.13 +    This library is distributed in the hope that it will be useful,
    8.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    8.16 +    Lesser General Public License for more details.
    8.17 +
    8.18 +    You should have received a copy of the GNU Lesser General Public
    8.19 +    License along with this library; if not, write to the Free Software
    8.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    8.21 +
    8.22 +    Sam Lantinga
    8.23 +    slouken@libsdl.org
    8.24 +*/
    8.25 +#include "SDL_config.h"
    8.26 +
    8.27 +#include "../SDL_sysrender.h"
    8.28 +#include "../../video/SDL_pixels_c.h"
    8.29 +
    8.30 +#include "SDL_draw.h"
    8.31 +#include "SDL_blendfillrect.h"
    8.32 +#include "SDL_blendline.h"
    8.33 +#include "SDL_blendpoint.h"
    8.34 +#include "SDL_drawline.h"
    8.35 +#include "SDL_drawpoint.h"
    8.36 +
    8.37 +
    8.38 +/* SDL surface based renderer implementation */
    8.39 +
    8.40 +static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
    8.41 +static void SW_WindowEvent(SDL_Renderer * renderer,
    8.42 +                           const SDL_WindowEvent *event);
    8.43 +static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    8.44 +static int SW_SetTextureColorMod(SDL_Renderer * renderer,
    8.45 +                                 SDL_Texture * texture);
    8.46 +static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
    8.47 +                                 SDL_Texture * texture);
    8.48 +static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
    8.49 +                                  SDL_Texture * texture);
    8.50 +static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    8.51 +                            const SDL_Rect * rect, const void *pixels,
    8.52 +                            int pitch);
    8.53 +static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    8.54 +                          const SDL_Rect * rect, void **pixels, int *pitch);
    8.55 +static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    8.56 +static int SW_RenderDrawPoints(SDL_Renderer * renderer,
    8.57 +                               const SDL_Point * points, int count);
    8.58 +static int SW_RenderDrawLines(SDL_Renderer * renderer,
    8.59 +                              const SDL_Point * points, int count);
    8.60 +static int SW_RenderFillRects(SDL_Renderer * renderer,
    8.61 +                              const SDL_Rect ** rects, int count);
    8.62 +static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    8.63 +                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
    8.64 +static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    8.65 +                               Uint32 format, void * pixels, int pitch);
    8.66 +static void SW_RenderPresent(SDL_Renderer * renderer);
    8.67 +static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    8.68 +static void SW_DestroyRenderer(SDL_Renderer * renderer);
    8.69 +
    8.70 +
    8.71 +SDL_RenderDriver SW_RenderDriver = {
    8.72 +    SW_CreateRenderer,
    8.73 +    {
    8.74 +     "software",
    8.75 +     0,
    8.76 +     8,
    8.77 +     {
    8.78 +      SDL_PIXELFORMAT_RGB555,
    8.79 +      SDL_PIXELFORMAT_RGB565,
    8.80 +      SDL_PIXELFORMAT_RGB888,
    8.81 +      SDL_PIXELFORMAT_BGR888,
    8.82 +      SDL_PIXELFORMAT_ARGB8888,
    8.83 +      SDL_PIXELFORMAT_RGBA8888,
    8.84 +      SDL_PIXELFORMAT_ABGR8888,
    8.85 +      SDL_PIXELFORMAT_BGRA8888
    8.86 +     },
    8.87 +     0,
    8.88 +     0}
    8.89 +};
    8.90 +
    8.91 +typedef struct
    8.92 +{
    8.93 +    SDL_bool updateSize;
    8.94 +    SDL_Surface *surface;
    8.95 +} SW_RenderData;
    8.96 +
    8.97 +
    8.98 +SDL_Renderer *
    8.99 +SW_CreateRendererForSurface(SDL_Surface * surface)
   8.100 +{
   8.101 +    SDL_Renderer *renderer;
   8.102 +    SW_RenderData *data;
   8.103 +
   8.104 +    if (!surface) {
   8.105 +        SDL_SetError("Can't create renderer for NULL surface");
   8.106 +        return NULL;
   8.107 +    }
   8.108 +
   8.109 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   8.110 +    if (!renderer) {
   8.111 +        SDL_OutOfMemory();
   8.112 +        return NULL;
   8.113 +    }
   8.114 +
   8.115 +    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
   8.116 +    if (!data) {
   8.117 +        SW_DestroyRenderer(renderer);
   8.118 +        SDL_OutOfMemory();
   8.119 +        return NULL;
   8.120 +    }
   8.121 +    data->surface = surface;
   8.122 +
   8.123 +    renderer->WindowEvent = SW_WindowEvent;
   8.124 +    renderer->CreateTexture = SW_CreateTexture;
   8.125 +    renderer->SetTextureColorMod = SW_SetTextureColorMod;
   8.126 +    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
   8.127 +    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
   8.128 +    renderer->UpdateTexture = SW_UpdateTexture;
   8.129 +    renderer->LockTexture = SW_LockTexture;
   8.130 +    renderer->UnlockTexture = SW_UnlockTexture;
   8.131 +    renderer->DestroyTexture = SW_DestroyTexture;
   8.132 +    renderer->RenderDrawPoints = SW_RenderDrawPoints;
   8.133 +    renderer->RenderDrawLines = SW_RenderDrawLines;
   8.134 +    renderer->RenderFillRects = SW_RenderFillRects;
   8.135 +    renderer->RenderCopy = SW_RenderCopy;
   8.136 +    renderer->RenderReadPixels = SW_RenderReadPixels;
   8.137 +    renderer->RenderPresent = SW_RenderPresent;
   8.138 +    renderer->DestroyRenderer = SW_DestroyRenderer;
   8.139 +    renderer->info = SW_RenderDriver.info;
   8.140 +    renderer->driverdata = data;
   8.141 +
   8.142 +    return renderer;
   8.143 +}
   8.144 +
   8.145 +SDL_Renderer *
   8.146 +SW_CreateRenderer(SDL_Window * window, Uint32 flags)
   8.147 +{
   8.148 +    SDL_Surface *surface;
   8.149 +
   8.150 +    surface = SDL_GetWindowSurface(window);
   8.151 +    if (!surface) {
   8.152 +        return NULL;
   8.153 +    }
   8.154 +    return SW_CreateRendererForSurface(surface);
   8.155 +}
   8.156 +
   8.157 +static SDL_Surface *
   8.158 +SW_ActivateRenderer(SDL_Renderer * renderer)
   8.159 +{
   8.160 +    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   8.161 +    SDL_Window *window = renderer->window;
   8.162 +
   8.163 +    if (data->updateSize) {
   8.164 +        data->surface = SDL_GetWindowSurface(window);
   8.165 +        data->updateSize = SDL_FALSE;
   8.166 +    }
   8.167 +    return data->surface;
   8.168 +}
   8.169 +
   8.170 +static void
   8.171 +SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   8.172 +{
   8.173 +    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   8.174 +
   8.175 +    if (event->event == SDL_WINDOWEVENT_RESIZED) {
   8.176 +        data->updateSize = SDL_TRUE;
   8.177 +    }
   8.178 +}
   8.179 +
   8.180 +static int
   8.181 +SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   8.182 +{
   8.183 +    int bpp;
   8.184 +    Uint32 Rmask, Gmask, Bmask, Amask;
   8.185 +
   8.186 +    if (!SDL_PixelFormatEnumToMasks
   8.187 +        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   8.188 +        SDL_SetError("Unknown texture format");
   8.189 +        return -1;
   8.190 +    }
   8.191 +
   8.192 +    texture->driverdata =
   8.193 +        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
   8.194 +                             Bmask, Amask);
   8.195 +    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
   8.196 +                           texture->b);
   8.197 +    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
   8.198 +    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
   8.199 +
   8.200 +    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
   8.201 +        SDL_SetSurfaceRLE(texture->driverdata, 1);
   8.202 +    }
   8.203 +
   8.204 +    if (!texture->driverdata) {
   8.205 +        return -1;
   8.206 +    }
   8.207 +    return 0;
   8.208 +}
   8.209 +
   8.210 +static int
   8.211 +SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   8.212 +{
   8.213 +    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   8.214 +    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
   8.215 +                                  texture->b);
   8.216 +}
   8.217 +
   8.218 +static int
   8.219 +SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   8.220 +{
   8.221 +    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   8.222 +    return SDL_SetSurfaceAlphaMod(surface, texture->a);
   8.223 +}
   8.224 +
   8.225 +static int
   8.226 +SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   8.227 +{
   8.228 +    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   8.229 +    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
   8.230 +}
   8.231 +
   8.232 +static int
   8.233 +SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   8.234 +                 const SDL_Rect * rect, const void *pixels, int pitch)
   8.235 +{
   8.236 +    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   8.237 +    Uint8 *src, *dst;
   8.238 +    int row;
   8.239 +    size_t length;
   8.240 +
   8.241 +    src = (Uint8 *) pixels;
   8.242 +    dst = (Uint8 *) surface->pixels +
   8.243 +                        rect->y * surface->pitch +
   8.244 +                        rect->x * surface->format->BytesPerPixel;
   8.245 +    length = rect->w * surface->format->BytesPerPixel;
   8.246 +    for (row = 0; row < rect->h; ++row) {
   8.247 +        SDL_memcpy(dst, src, length);
   8.248 +        src += pitch;
   8.249 +        dst += surface->pitch;
   8.250 +    }
   8.251 +    return 0;
   8.252 +}
   8.253 +
   8.254 +static int
   8.255 +SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   8.256 +               const SDL_Rect * rect, void **pixels, int *pitch)
   8.257 +{
   8.258 +    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   8.259 +
   8.260 +    *pixels =
   8.261 +        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
   8.262 +                  rect->x * surface->format->BytesPerPixel);
   8.263 +    *pitch = surface->pitch;
   8.264 +    return 0;
   8.265 +}
   8.266 +
   8.267 +static void
   8.268 +SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   8.269 +{
   8.270 +}
   8.271 +
   8.272 +static int
   8.273 +SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   8.274 +                    int count)
   8.275 +{
   8.276 +    SDL_Surface *surface = SW_ActivateRenderer(renderer);
   8.277 +
   8.278 +    if (!surface) {
   8.279 +        return -1;
   8.280 +    }
   8.281 +
   8.282 +    /* Draw the points! */
   8.283 +    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
   8.284 +        Uint32 color = SDL_MapRGBA(surface->format,
   8.285 +                                   renderer->r, renderer->g, renderer->b,
   8.286 +                                   renderer->a);
   8.287 +
   8.288 +        return SDL_DrawPoints(surface, points, count, color);
   8.289 +    } else {
   8.290 +        return SDL_BlendPoints(surface, points, count,
   8.291 +                               renderer->blendMode,
   8.292 +                               renderer->r, renderer->g, renderer->b,
   8.293 +                               renderer->a);
   8.294 +    }
   8.295 +}
   8.296 +
   8.297 +static int
   8.298 +SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   8.299 +                   int count)
   8.300 +{
   8.301 +    SDL_Surface *surface = SW_ActivateRenderer(renderer);
   8.302 +
   8.303 +    if (!surface) {
   8.304 +        return -1;
   8.305 +    }
   8.306 +
   8.307 +    /* Draw the lines! */
   8.308 +    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
   8.309 +        Uint32 color = SDL_MapRGBA(surface->format,
   8.310 +                                   renderer->r, renderer->g, renderer->b,
   8.311 +                                   renderer->a);
   8.312 +
   8.313 +        return SDL_DrawLines(surface, points, count, color);
   8.314 +    } else {
   8.315 +        return SDL_BlendLines(surface, points, count,
   8.316 +                              renderer->blendMode,
   8.317 +                              renderer->r, renderer->g, renderer->b,
   8.318 +                              renderer->a);
   8.319 +    }
   8.320 +}
   8.321 +
   8.322 +static int
   8.323 +SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   8.324 +                   int count)
   8.325 +{
   8.326 +    SDL_Surface *surface = SW_ActivateRenderer(renderer);
   8.327 +
   8.328 +    if (!surface) {
   8.329 +        return -1;
   8.330 +    }
   8.331 +
   8.332 +    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
   8.333 +        Uint32 color = SDL_MapRGBA(surface->format,
   8.334 +                                   renderer->r, renderer->g, renderer->b,
   8.335 +                                   renderer->a);
   8.336 +        return SDL_FillRects(surface, rects, count, color);
   8.337 +    } else {
   8.338 +        return SDL_BlendFillRects(surface, rects, count,
   8.339 +                                     renderer->blendMode,
   8.340 +                                     renderer->r, renderer->g, renderer->b,
   8.341 +                                     renderer->a);
   8.342 +    }
   8.343 +}
   8.344 +
   8.345 +static int
   8.346 +SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   8.347 +              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   8.348 +{
   8.349 +    SDL_Surface *surface = SW_ActivateRenderer(renderer);
   8.350 +    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
   8.351 +    SDL_Rect final_rect = *dstrect;
   8.352 +
   8.353 +    if (!surface) {
   8.354 +        return -1;
   8.355 +    }
   8.356 +    return SDL_BlitSurface(src, srcrect, surface, &final_rect);
   8.357 +}
   8.358 +
   8.359 +static int
   8.360 +SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   8.361 +                    Uint32 format, void * pixels, int pitch)
   8.362 +{
   8.363 +    SDL_Surface *surface = SW_ActivateRenderer(renderer);
   8.364 +    Uint32 src_format;
   8.365 +    void *src_pixels;
   8.366 +
   8.367 +    if (!surface) {
   8.368 +        return -1;
   8.369 +    }
   8.370 +
   8.371 +    if (rect->x < 0 || rect->x+rect->w > surface->w ||
   8.372 +        rect->y < 0 || rect->y+rect->h > surface->h) {
   8.373 +        SDL_SetError("Tried to read outside of surface bounds");
   8.374 +        return -1;
   8.375 +    }
   8.376 +
   8.377 +    src_format = SDL_MasksToPixelFormatEnum(
   8.378 +                    surface->format->BitsPerPixel,
   8.379 +                    surface->format->Rmask, surface->format->Gmask,
   8.380 +                    surface->format->Bmask, surface->format->Amask);
   8.381 +
   8.382 +    src_pixels = (void*)((Uint8 *) surface->pixels +
   8.383 +                    rect->y * surface->pitch +
   8.384 +                    rect->x * surface->format->BytesPerPixel);
   8.385 +
   8.386 +    return SDL_ConvertPixels(rect->w, rect->h,
   8.387 +                             src_format, src_pixels, surface->pitch,
   8.388 +                             format, pixels, pitch);
   8.389 +}
   8.390 +
   8.391 +static void
   8.392 +SW_RenderPresent(SDL_Renderer * renderer)
   8.393 +{
   8.394 +    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   8.395 +    SDL_Window *window = renderer->window;
   8.396 +
   8.397 +    if (window) {
   8.398 +        SDL_UpdateWindowSurface(window);
   8.399 +    }
   8.400 +}
   8.401 +
   8.402 +static void
   8.403 +SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   8.404 +{
   8.405 +    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   8.406 +
   8.407 +    SDL_FreeSurface(surface);
   8.408 +}
   8.409 +
   8.410 +static void
   8.411 +SW_DestroyRenderer(SDL_Renderer * renderer)
   8.412 +{
   8.413 +    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   8.414 +
   8.415 +    if (data) {
   8.416 +        SDL_free(data);
   8.417 +    }
   8.418 +    SDL_free(renderer);
   8.419 +}
   8.420 +
   8.421 +/* vi: set ts=4 sw=4 expandtab: */
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/render/software/SDL_render_sw_c.h	Sat Feb 05 12:01:11 2011 -0800
     9.3 @@ -0,0 +1,25 @@
     9.4 +/*
     9.5 +    SDL - Simple DirectMedia Layer
     9.6 +    Copyright (C) 1997-2010 Sam Lantinga
     9.7 +
     9.8 +    This library is free software; you can redistribute it and/or
     9.9 +    modify it under the terms of the GNU Lesser General Public
    9.10 +    License as published by the Free Software Foundation; either
    9.11 +    version 2.1 of the License, or (at your option) any later version.
    9.12 +
    9.13 +    This library is distributed in the hope that it will be useful,
    9.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    9.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    9.16 +    Lesser General Public License for more details.
    9.17 +
    9.18 +    You should have received a copy of the GNU Lesser General Public
    9.19 +    License along with this library; if not, write to the Free Software
    9.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    9.21 +
    9.22 +    Sam Lantinga
    9.23 +    slouken@libsdl.org
    9.24 +*/
    9.25 +
    9.26 +extern SDL_Renderer * SW_CreateRendererForSurface(SDL_Surface * surface);
    9.27 +
    9.28 +/* vi: set ts=4 sw=4 expandtab: */
    10.1 --- a/src/render/software/SDL_renderer_sw.c	Sat Feb 05 11:54:46 2011 -0800
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,418 +0,0 @@
    10.4 -/*
    10.5 -    SDL - Simple DirectMedia Layer
    10.6 -    Copyright (C) 1997-2010 Sam Lantinga
    10.7 -
    10.8 -    This library is free software; you can redistribute it and/or
    10.9 -    modify it under the terms of the GNU Lesser General Public
   10.10 -    License as published by the Free Software Foundation; either
   10.11 -    version 2.1 of the License, or (at your option) any later version.
   10.12 -
   10.13 -    This library is distributed in the hope that it will be useful,
   10.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   10.16 -    Lesser General Public License for more details.
   10.17 -
   10.18 -    You should have received a copy of the GNU Lesser General Public
   10.19 -    License along with this library; if not, write to the Free Software
   10.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   10.21 -
   10.22 -    Sam Lantinga
   10.23 -    slouken@libsdl.org
   10.24 -*/
   10.25 -#include "SDL_config.h"
   10.26 -
   10.27 -#include "../SDL_sysrender.h"
   10.28 -#include "../../video/SDL_pixels_c.h"
   10.29 -
   10.30 -#include "SDL_draw.h"
   10.31 -#include "SDL_blendfillrect.h"
   10.32 -#include "SDL_blendline.h"
   10.33 -#include "SDL_blendpoint.h"
   10.34 -#include "SDL_drawline.h"
   10.35 -#include "SDL_drawpoint.h"
   10.36 -
   10.37 -
   10.38 -/* SDL surface based renderer implementation */
   10.39 -
   10.40 -static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
   10.41 -static void SW_WindowEvent(SDL_Renderer * renderer,
   10.42 -                           const SDL_WindowEvent *event);
   10.43 -static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
   10.44 -static int SW_SetTextureColorMod(SDL_Renderer * renderer,
   10.45 -                                 SDL_Texture * texture);
   10.46 -static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
   10.47 -                                 SDL_Texture * texture);
   10.48 -static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
   10.49 -                                  SDL_Texture * texture);
   10.50 -static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   10.51 -                            const SDL_Rect * rect, const void *pixels,
   10.52 -                            int pitch);
   10.53 -static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   10.54 -                          const SDL_Rect * rect, void **pixels, int *pitch);
   10.55 -static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
   10.56 -static int SW_RenderDrawPoints(SDL_Renderer * renderer,
   10.57 -                               const SDL_Point * points, int count);
   10.58 -static int SW_RenderDrawLines(SDL_Renderer * renderer,
   10.59 -                              const SDL_Point * points, int count);
   10.60 -static int SW_RenderFillRects(SDL_Renderer * renderer,
   10.61 -                              const SDL_Rect ** rects, int count);
   10.62 -static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   10.63 -                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
   10.64 -static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   10.65 -                               Uint32 format, void * pixels, int pitch);
   10.66 -static void SW_RenderPresent(SDL_Renderer * renderer);
   10.67 -static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
   10.68 -static void SW_DestroyRenderer(SDL_Renderer * renderer);
   10.69 -
   10.70 -
   10.71 -SDL_RenderDriver SW_RenderDriver = {
   10.72 -    SW_CreateRenderer,
   10.73 -    {
   10.74 -     "software",
   10.75 -     0,
   10.76 -     8,
   10.77 -     {
   10.78 -      SDL_PIXELFORMAT_RGB555,
   10.79 -      SDL_PIXELFORMAT_RGB565,
   10.80 -      SDL_PIXELFORMAT_RGB888,
   10.81 -      SDL_PIXELFORMAT_BGR888,
   10.82 -      SDL_PIXELFORMAT_ARGB8888,
   10.83 -      SDL_PIXELFORMAT_RGBA8888,
   10.84 -      SDL_PIXELFORMAT_ABGR8888,
   10.85 -      SDL_PIXELFORMAT_BGRA8888
   10.86 -     },
   10.87 -     0,
   10.88 -     0}
   10.89 -};
   10.90 -
   10.91 -typedef struct
   10.92 -{
   10.93 -    SDL_bool updateSize;
   10.94 -    SDL_Surface *surface;
   10.95 -} SW_RenderData;
   10.96 -
   10.97 -
   10.98 -SDL_Renderer *
   10.99 -SW_CreateRendererForSurface(SDL_Surface * surface)
  10.100 -{
  10.101 -    SDL_Renderer *renderer;
  10.102 -    SW_RenderData *data;
  10.103 -
  10.104 -    if (!surface) {
  10.105 -        SDL_SetError("Can't create renderer for NULL surface");
  10.106 -        return NULL;
  10.107 -    }
  10.108 -
  10.109 -    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
  10.110 -    if (!renderer) {
  10.111 -        SDL_OutOfMemory();
  10.112 -        return NULL;
  10.113 -    }
  10.114 -
  10.115 -    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
  10.116 -    if (!data) {
  10.117 -        SW_DestroyRenderer(renderer);
  10.118 -        SDL_OutOfMemory();
  10.119 -        return NULL;
  10.120 -    }
  10.121 -    data->surface = surface;
  10.122 -
  10.123 -    renderer->WindowEvent = SW_WindowEvent;
  10.124 -    renderer->CreateTexture = SW_CreateTexture;
  10.125 -    renderer->SetTextureColorMod = SW_SetTextureColorMod;
  10.126 -    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
  10.127 -    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
  10.128 -    renderer->UpdateTexture = SW_UpdateTexture;
  10.129 -    renderer->LockTexture = SW_LockTexture;
  10.130 -    renderer->UnlockTexture = SW_UnlockTexture;
  10.131 -    renderer->DestroyTexture = SW_DestroyTexture;
  10.132 -    renderer->RenderDrawPoints = SW_RenderDrawPoints;
  10.133 -    renderer->RenderDrawLines = SW_RenderDrawLines;
  10.134 -    renderer->RenderFillRects = SW_RenderFillRects;
  10.135 -    renderer->RenderCopy = SW_RenderCopy;
  10.136 -    renderer->RenderReadPixels = SW_RenderReadPixels;
  10.137 -    renderer->RenderPresent = SW_RenderPresent;
  10.138 -    renderer->DestroyRenderer = SW_DestroyRenderer;
  10.139 -    renderer->info = SW_RenderDriver.info;
  10.140 -    renderer->driverdata = data;
  10.141 -
  10.142 -    return renderer;
  10.143 -}
  10.144 -
  10.145 -SDL_Renderer *
  10.146 -SW_CreateRenderer(SDL_Window * window, Uint32 flags)
  10.147 -{
  10.148 -    SDL_Surface *surface;
  10.149 -
  10.150 -    surface = SDL_GetWindowSurface(window);
  10.151 -    if (!surface) {
  10.152 -        return NULL;
  10.153 -    }
  10.154 -    return SW_CreateRendererForSurface(surface);
  10.155 -}
  10.156 -
  10.157 -static SDL_Surface *
  10.158 -SW_ActivateRenderer(SDL_Renderer * renderer)
  10.159 -{
  10.160 -    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  10.161 -    SDL_Window *window = renderer->window;
  10.162 -
  10.163 -    if (data->updateSize) {
  10.164 -        data->surface = SDL_GetWindowSurface(window);
  10.165 -        data->updateSize = SDL_FALSE;
  10.166 -    }
  10.167 -    return data->surface;
  10.168 -}
  10.169 -
  10.170 -static void
  10.171 -SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
  10.172 -{
  10.173 -    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  10.174 -
  10.175 -    if (event->event == SDL_WINDOWEVENT_RESIZED) {
  10.176 -        data->updateSize = SDL_TRUE;
  10.177 -    }
  10.178 -}
  10.179 -
  10.180 -static int
  10.181 -SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  10.182 -{
  10.183 -    int bpp;
  10.184 -    Uint32 Rmask, Gmask, Bmask, Amask;
  10.185 -
  10.186 -    if (!SDL_PixelFormatEnumToMasks
  10.187 -        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  10.188 -        SDL_SetError("Unknown texture format");
  10.189 -        return -1;
  10.190 -    }
  10.191 -
  10.192 -    texture->driverdata =
  10.193 -        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
  10.194 -                             Bmask, Amask);
  10.195 -    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
  10.196 -                           texture->b);
  10.197 -    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
  10.198 -    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
  10.199 -
  10.200 -    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
  10.201 -        SDL_SetSurfaceRLE(texture->driverdata, 1);
  10.202 -    }
  10.203 -
  10.204 -    if (!texture->driverdata) {
  10.205 -        return -1;
  10.206 -    }
  10.207 -    return 0;
  10.208 -}
  10.209 -
  10.210 -static int
  10.211 -SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
  10.212 -{
  10.213 -    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  10.214 -    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
  10.215 -                                  texture->b);
  10.216 -}
  10.217 -
  10.218 -static int
  10.219 -SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
  10.220 -{
  10.221 -    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  10.222 -    return SDL_SetSurfaceAlphaMod(surface, texture->a);
  10.223 -}
  10.224 -
  10.225 -static int
  10.226 -SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
  10.227 -{
  10.228 -    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  10.229 -    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
  10.230 -}
  10.231 -
  10.232 -static int
  10.233 -SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  10.234 -                 const SDL_Rect * rect, const void *pixels, int pitch)
  10.235 -{
  10.236 -    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  10.237 -    Uint8 *src, *dst;
  10.238 -    int row;
  10.239 -    size_t length;
  10.240 -
  10.241 -    src = (Uint8 *) pixels;
  10.242 -    dst = (Uint8 *) surface->pixels +
  10.243 -                        rect->y * surface->pitch +
  10.244 -                        rect->x * surface->format->BytesPerPixel;
  10.245 -    length = rect->w * surface->format->BytesPerPixel;
  10.246 -    for (row = 0; row < rect->h; ++row) {
  10.247 -        SDL_memcpy(dst, src, length);
  10.248 -        src += pitch;
  10.249 -        dst += surface->pitch;
  10.250 -    }
  10.251 -    return 0;
  10.252 -}
  10.253 -
  10.254 -static int
  10.255 -SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  10.256 -               const SDL_Rect * rect, void **pixels, int *pitch)
  10.257 -{
  10.258 -    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  10.259 -
  10.260 -    *pixels =
  10.261 -        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
  10.262 -                  rect->x * surface->format->BytesPerPixel);
  10.263 -    *pitch = surface->pitch;
  10.264 -    return 0;
  10.265 -}
  10.266 -
  10.267 -static void
  10.268 -SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  10.269 -{
  10.270 -}
  10.271 -
  10.272 -static int
  10.273 -SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
  10.274 -                    int count)
  10.275 -{
  10.276 -    SDL_Surface *surface = SW_ActivateRenderer(renderer);
  10.277 -
  10.278 -    if (!surface) {
  10.279 -        return -1;
  10.280 -    }
  10.281 -
  10.282 -    /* Draw the points! */
  10.283 -    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
  10.284 -        Uint32 color = SDL_MapRGBA(surface->format,
  10.285 -                                   renderer->r, renderer->g, renderer->b,
  10.286 -                                   renderer->a);
  10.287 -
  10.288 -        return SDL_DrawPoints(surface, points, count, color);
  10.289 -    } else {
  10.290 -        return SDL_BlendPoints(surface, points, count,
  10.291 -                               renderer->blendMode,
  10.292 -                               renderer->r, renderer->g, renderer->b,
  10.293 -                               renderer->a);
  10.294 -    }
  10.295 -}
  10.296 -
  10.297 -static int
  10.298 -SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
  10.299 -                   int count)
  10.300 -{
  10.301 -    SDL_Surface *surface = SW_ActivateRenderer(renderer);
  10.302 -
  10.303 -    if (!surface) {
  10.304 -        return -1;
  10.305 -    }
  10.306 -
  10.307 -    /* Draw the lines! */
  10.308 -    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
  10.309 -        Uint32 color = SDL_MapRGBA(surface->format,
  10.310 -                                   renderer->r, renderer->g, renderer->b,
  10.311 -                                   renderer->a);
  10.312 -
  10.313 -        return SDL_DrawLines(surface, points, count, color);
  10.314 -    } else {
  10.315 -        return SDL_BlendLines(surface, points, count,
  10.316 -                              renderer->blendMode,
  10.317 -                              renderer->r, renderer->g, renderer->b,
  10.318 -                              renderer->a);
  10.319 -    }
  10.320 -}
  10.321 -
  10.322 -static int
  10.323 -SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
  10.324 -                   int count)
  10.325 -{
  10.326 -    SDL_Surface *surface = SW_ActivateRenderer(renderer);
  10.327 -
  10.328 -    if (!surface) {
  10.329 -        return -1;
  10.330 -    }
  10.331 -
  10.332 -    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
  10.333 -        Uint32 color = SDL_MapRGBA(surface->format,
  10.334 -                                   renderer->r, renderer->g, renderer->b,
  10.335 -                                   renderer->a);
  10.336 -        return SDL_FillRects(surface, rects, count, color);
  10.337 -    } else {
  10.338 -        return SDL_BlendFillRects(surface, rects, count,
  10.339 -                                     renderer->blendMode,
  10.340 -                                     renderer->r, renderer->g, renderer->b,
  10.341 -                                     renderer->a);
  10.342 -    }
  10.343 -}
  10.344 -
  10.345 -static int
  10.346 -SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  10.347 -              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
  10.348 -{
  10.349 -    SDL_Surface *surface = SW_ActivateRenderer(renderer);
  10.350 -    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
  10.351 -    SDL_Rect final_rect = *dstrect;
  10.352 -
  10.353 -    if (!surface) {
  10.354 -        return -1;
  10.355 -    }
  10.356 -    return SDL_BlitSurface(src, srcrect, surface, &final_rect);
  10.357 -}
  10.358 -
  10.359 -static int
  10.360 -SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  10.361 -                    Uint32 format, void * pixels, int pitch)
  10.362 -{
  10.363 -    SDL_Surface *surface = SW_ActivateRenderer(renderer);
  10.364 -    Uint32 src_format;
  10.365 -    void *src_pixels;
  10.366 -
  10.367 -    if (!surface) {
  10.368 -        return -1;
  10.369 -    }
  10.370 -
  10.371 -    if (rect->x < 0 || rect->x+rect->w > surface->w ||
  10.372 -        rect->y < 0 || rect->y+rect->h > surface->h) {
  10.373 -        SDL_SetError("Tried to read outside of surface bounds");
  10.374 -        return -1;
  10.375 -    }
  10.376 -
  10.377 -    src_format = SDL_MasksToPixelFormatEnum(
  10.378 -                    surface->format->BitsPerPixel,
  10.379 -                    surface->format->Rmask, surface->format->Gmask,
  10.380 -                    surface->format->Bmask, surface->format->Amask);
  10.381 -
  10.382 -    src_pixels = (void*)((Uint8 *) surface->pixels +
  10.383 -                    rect->y * surface->pitch +
  10.384 -                    rect->x * surface->format->BytesPerPixel);
  10.385 -
  10.386 -    return SDL_ConvertPixels(rect->w, rect->h,
  10.387 -                             src_format, src_pixels, surface->pitch,
  10.388 -                             format, pixels, pitch);
  10.389 -}
  10.390 -
  10.391 -static void
  10.392 -SW_RenderPresent(SDL_Renderer * renderer)
  10.393 -{
  10.394 -    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  10.395 -    SDL_Window *window = renderer->window;
  10.396 -
  10.397 -    if (window) {
  10.398 -        SDL_UpdateWindowSurface(window);
  10.399 -    }
  10.400 -}
  10.401 -
  10.402 -static void
  10.403 -SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  10.404 -{
  10.405 -    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
  10.406 -
  10.407 -    SDL_FreeSurface(surface);
  10.408 -}
  10.409 -
  10.410 -static void
  10.411 -SW_DestroyRenderer(SDL_Renderer * renderer)
  10.412 -{
  10.413 -    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
  10.414 -
  10.415 -    if (data) {
  10.416 -        SDL_free(data);
  10.417 -    }
  10.418 -    SDL_free(renderer);
  10.419 -}
  10.420 -
  10.421 -/* vi: set ts=4 sw=4 expandtab: */
    11.1 --- a/src/render/software/SDL_renderer_sw_c.h	Sat Feb 05 11:54:46 2011 -0800
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,25 +0,0 @@
    11.4 -/*
    11.5 -    SDL - Simple DirectMedia Layer
    11.6 -    Copyright (C) 1997-2010 Sam Lantinga
    11.7 -
    11.8 -    This library is free software; you can redistribute it and/or
    11.9 -    modify it under the terms of the GNU Lesser General Public
   11.10 -    License as published by the Free Software Foundation; either
   11.11 -    version 2.1 of the License, or (at your option) any later version.
   11.12 -
   11.13 -    This library is distributed in the hope that it will be useful,
   11.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   11.16 -    Lesser General Public License for more details.
   11.17 -
   11.18 -    You should have received a copy of the GNU Lesser General Public
   11.19 -    License along with this library; if not, write to the Free Software
   11.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   11.21 -
   11.22 -    Sam Lantinga
   11.23 -    slouken@libsdl.org
   11.24 -*/
   11.25 -
   11.26 -extern SDL_Renderer * SW_CreateRendererForSurface(SDL_Surface * surface);
   11.27 -
   11.28 -/* vi: set ts=4 sw=4 expandtab: */