src/video/SDL_renderer_gl.c
author Bob Pendleton <bob@pendleton.com>
Fri, 14 Mar 2008 18:17:49 +0000
changeset 2328 91e601d9df8b
parent 2295 dbc6d1893869
child 2778 38dfc890ee6b
permissions -rw-r--r--
re: bug#563. checking in some commented out trace code and a fix so that the in testalpha.c the background only flashes when alpha == 255. The problem that is being
fixed is caused by clearing SDL_COPY_RLE_COLORKEY without setting SDL_COPY_RLE_DESIRED in SDL_UnRELSurface.
slouken@1918
     1
/*
slouken@1918
     2
    SDL - Simple DirectMedia Layer
slouken@1918
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1918
     4
slouken@1918
     5
    This library is free software; you can redistribute it and/or
slouken@1918
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1918
     7
    License as published by the Free Software Foundation; either
slouken@1918
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1918
     9
slouken@1918
    10
    This library is distributed in the hope that it will be useful,
slouken@1918
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1918
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1918
    13
    Lesser General Public License for more details.
slouken@1918
    14
slouken@1918
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1918
    16
    License along with this library; if not, write to the Free Software
slouken@1918
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1918
    18
slouken@1918
    19
    Sam Lantinga
slouken@1918
    20
    slouken@libsdl.org
slouken@1918
    21
*/
slouken@1918
    22
#include "SDL_config.h"
slouken@1918
    23
slouken@1952
    24
#if SDL_VIDEO_RENDER_OGL
slouken@1920
    25
slouken@1920
    26
#include "SDL_video.h"
slouken@1920
    27
#include "SDL_opengl.h"
slouken@1920
    28
#include "SDL_sysvideo.h"
slouken@1920
    29
#include "SDL_pixels_c.h"
slouken@1920
    30
#include "SDL_rect_c.h"
slouken@1920
    31
#include "SDL_yuv_sw_c.h"
slouken@1918
    32
slouken@2246
    33
#ifdef __MACOSX__
slouken@2246
    34
#include <OpenGL/OpenGL.h>
slouken@2246
    35
#endif
slouken@2246
    36
slouken@1918
    37
/* OpenGL renderer implementation */
slouken@1918
    38
slouken@2230
    39
/* Details on optimizing the texture path on Mac OS X:
slouken@2230
    40
   http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/chapter_10_section_2.html
slouken@2230
    41
*/
slouken@2230
    42
slouken@1985
    43
static const float inv255f = 1.0f / 255.0f;
slouken@1985
    44
slouken@1918
    45
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1923
    46
static int GL_ActivateRenderer(SDL_Renderer * renderer);
slouken@1970
    47
static int GL_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1918
    48
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@2222
    49
static int GL_QueryTexturePixels(SDL_Renderer * renderer,
slouken@2222
    50
                                 SDL_Texture * texture, void **pixels,
slouken@2222
    51
                                 int *pitch);
slouken@1918
    52
static int GL_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    53
                                SDL_Texture * texture,
slouken@1918
    54
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    55
                                int ncolors);
slouken@1918
    56
static int GL_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    57
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    58
                                int firstcolor, int ncolors);
slouken@1985
    59
static int GL_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    60
                                 SDL_Texture * texture);
slouken@1985
    61
static int GL_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    62
                                 SDL_Texture * texture);
slouken@1985
    63
static int GL_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@1985
    64
                                  SDL_Texture * texture);
slouken@1985
    65
static int GL_SetTextureScaleMode(SDL_Renderer * renderer,
slouken@1985
    66
                                  SDL_Texture * texture);
slouken@1918
    67
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    68
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    69
                            int pitch);
slouken@1918
    70
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    71
                          const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1985
    72
                          int *pitch);
slouken@1918
    73
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    74
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    75
                            int numrects, const SDL_Rect * rects);
slouken@1985
    76
static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
slouken@1985
    77
                         Uint8 a, const SDL_Rect * rect);
slouken@1918
    78
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    79
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@1918
    80
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    81
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    82
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    83
slouken@1918
    84
slouken@1918
    85
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    86
    GL_CreateRenderer,
slouken@1918
    87
    {
slouken@1918
    88
     "opengl",
slouken@1974
    89
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
slouken@1974
    90
      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
slouken@1985
    91
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
    92
      SDL_TEXTUREMODULATE_ALPHA),
slouken@1965
    93
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1965
    94
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
slouken@1965
    95
      SDL_TEXTUREBLENDMODE_MOD),
slouken@1965
    96
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
slouken@1965
    97
      SDL_TEXTURESCALEMODE_SLOW),
slouken@1924
    98
     16,
slouken@1918
    99
     {
slouken@1965
   100
      SDL_PIXELFORMAT_INDEX1LSB,
slouken@1965
   101
      SDL_PIXELFORMAT_INDEX1MSB,
slouken@1965
   102
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
   103
      SDL_PIXELFORMAT_RGB332,
slouken@1965
   104
      SDL_PIXELFORMAT_RGB444,
slouken@1965
   105
      SDL_PIXELFORMAT_RGB555,
slouken@1965
   106
      SDL_PIXELFORMAT_ARGB4444,
slouken@1965
   107
      SDL_PIXELFORMAT_ARGB1555,
slouken@1965
   108
      SDL_PIXELFORMAT_RGB565,
slouken@1965
   109
      SDL_PIXELFORMAT_RGB24,
slouken@1965
   110
      SDL_PIXELFORMAT_BGR24,
slouken@1965
   111
      SDL_PIXELFORMAT_RGB888,
slouken@1965
   112
      SDL_PIXELFORMAT_BGR888,
slouken@1965
   113
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
   114
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
   115
      SDL_PIXELFORMAT_ARGB2101010},
slouken@1918
   116
     0,
slouken@1918
   117
     0}
slouken@1918
   118
};
slouken@1918
   119
slouken@1918
   120
typedef struct
slouken@1918
   121
{
slouken@1918
   122
    SDL_GLContext context;
slouken@1970
   123
    SDL_bool updateSize;
slouken@2233
   124
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1974
   125
    SDL_bool GL_EXT_paletted_texture_supported;
slouken@1927
   126
    int blendMode;
slouken@1927
   127
    int scaleMode;
slouken@1927
   128
slouken@1927
   129
    /* OpenGL functions */
slouken@1927
   130
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@1927
   131
#include "SDL_glfuncs.h"
slouken@1927
   132
#undef SDL_PROC
slouken@1974
   133
slouken@1974
   134
    PFNGLCOLORTABLEEXTPROC glColorTableEXT;
slouken@2233
   135
    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
slouken@2233
   136
                                 const GLvoid * pointer);
slouken@1918
   137
} GL_RenderData;
slouken@1918
   138
slouken@1918
   139
typedef struct
slouken@1918
   140
{
slouken@1918
   141
    GLuint texture;
slouken@1920
   142
    GLenum type;
slouken@1918
   143
    GLfloat texw;
slouken@1918
   144
    GLfloat texh;
slouken@1920
   145
    GLenum format;
slouken@1920
   146
    GLenum formattype;
slouken@1974
   147
    Uint8 *palette;
slouken@1918
   148
    void *pixels;
slouken@1918
   149
    int pitch;
slouken@1920
   150
    SDL_DirtyRectList dirty;
slouken@1918
   151
} GL_TextureData;
slouken@1918
   152
slouken@1918
   153
slouken@1924
   154
static void
slouken@1924
   155
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   156
{
slouken@1924
   157
    const char *error;
slouken@1924
   158
slouken@1924
   159
    switch (result) {
slouken@1924
   160
    case GL_NO_ERROR:
slouken@1924
   161
        error = "GL_NO_ERROR";
slouken@1924
   162
        break;
slouken@1924
   163
    case GL_INVALID_ENUM:
slouken@1924
   164
        error = "GL_INVALID_ENUM";
slouken@1924
   165
        break;
slouken@1924
   166
    case GL_INVALID_VALUE:
slouken@1924
   167
        error = "GL_INVALID_VALUE";
slouken@1924
   168
        break;
slouken@1924
   169
    case GL_INVALID_OPERATION:
slouken@1924
   170
        error = "GL_INVALID_OPERATION";
slouken@1924
   171
        break;
slouken@1924
   172
    case GL_STACK_OVERFLOW:
slouken@1924
   173
        error = "GL_STACK_OVERFLOW";
slouken@1924
   174
        break;
slouken@1924
   175
    case GL_STACK_UNDERFLOW:
slouken@1924
   176
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   177
        break;
slouken@1924
   178
    case GL_OUT_OF_MEMORY:
slouken@1924
   179
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   180
        break;
slouken@1924
   181
    case GL_TABLE_TOO_LARGE:
slouken@1924
   182
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   183
        break;
slouken@1924
   184
    default:
slouken@1924
   185
        error = "UNKNOWN";
slouken@1924
   186
        break;
slouken@1924
   187
    }
slouken@1924
   188
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   189
}
slouken@1924
   190
slouken@1927
   191
static int
slouken@1927
   192
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   193
{
slouken@1927
   194
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
slouken@1927
   195
#define __SDL_NOGETPROCADDR__
slouken@1927
   196
#elif defined(__MINT__)
slouken@1927
   197
#define __SDL_NOGETPROCADDR__
slouken@1927
   198
#endif
slouken@1927
   199
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   200
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   201
#else
slouken@1927
   202
#define SDL_PROC(ret,func,params) \
slouken@1927
   203
    do { \
slouken@1927
   204
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   205
        if ( ! data->func ) { \
slouken@1927
   206
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   207
            return -1; \
slouken@1927
   208
        } \
slouken@1927
   209
    } while ( 0 );
slouken@1927
   210
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   211
slouken@1927
   212
#include "SDL_glfuncs.h"
slouken@1927
   213
#undef SDL_PROC
slouken@1927
   214
    return 0;
slouken@1927
   215
}
slouken@1927
   216
slouken@1918
   217
void
slouken@1918
   218
GL_AddRenderDriver(_THIS)
slouken@1918
   219
{
slouken@1920
   220
    if (_this->GL_CreateContext) {
slouken@1918
   221
        SDL_AddRenderDriver(0, &GL_RenderDriver);
slouken@1918
   222
    }
slouken@1918
   223
}
slouken@1918
   224
slouken@1918
   225
SDL_Renderer *
slouken@1918
   226
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   227
{
slouken@1918
   228
    SDL_Renderer *renderer;
slouken@1918
   229
    GL_RenderData *data;
slouken@1952
   230
    GLint value;
slouken@1974
   231
    int doublebuffer;
slouken@1918
   232
slouken@1974
   233
    /* Render directly to the window, unless we're compositing */
slouken@1974
   234
#ifndef __MACOSX__
slouken@1974
   235
    if (flags & SDL_RENDERER_SINGLEBUFFER) {
slouken@1974
   236
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
slouken@1974
   237
    }
slouken@1974
   238
#endif
slouken@1918
   239
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1928
   240
        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   241
            return NULL;
slouken@1924
   242
        }
slouken@1918
   243
    }
slouken@1918
   244
slouken@1920
   245
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   246
    if (!renderer) {
slouken@1918
   247
        SDL_OutOfMemory();
slouken@1918
   248
        return NULL;
slouken@1918
   249
    }
slouken@1918
   250
slouken@1920
   251
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   252
    if (!data) {
slouken@1918
   253
        GL_DestroyRenderer(renderer);
slouken@1918
   254
        SDL_OutOfMemory();
slouken@1918
   255
        return NULL;
slouken@1918
   256
    }
slouken@1918
   257
slouken@1923
   258
    renderer->ActivateRenderer = GL_ActivateRenderer;
slouken@1970
   259
    renderer->DisplayModeChanged = GL_DisplayModeChanged;
slouken@1918
   260
    renderer->CreateTexture = GL_CreateTexture;
slouken@2222
   261
    renderer->QueryTexturePixels = GL_QueryTexturePixels;
slouken@1918
   262
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   263
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1985
   264
    renderer->SetTextureColorMod = GL_SetTextureColorMod;
slouken@1985
   265
    renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
slouken@1985
   266
    renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
slouken@1985
   267
    renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
slouken@1918
   268
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   269
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   270
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   271
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@1918
   272
    renderer->RenderFill = GL_RenderFill;
slouken@1918
   273
    renderer->RenderCopy = GL_RenderCopy;
slouken@1918
   274
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   275
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   276
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   277
    renderer->info = GL_RenderDriver.info;
slouken@1918
   278
    renderer->window = window->id;
slouken@1918
   279
    renderer->driverdata = data;
slouken@1918
   280
slouken@1918
   281
    renderer->info.flags =
slouken@1965
   282
        (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
slouken@1918
   283
slouken@1927
   284
    if (GL_LoadFunctions(data) < 0) {
slouken@1927
   285
        GL_DestroyRenderer(renderer);
slouken@1927
   286
        return NULL;
slouken@1927
   287
    }
slouken@1927
   288
slouken@1918
   289
    data->context = SDL_GL_CreateContext(window->id);
slouken@1918
   290
    if (!data->context) {
slouken@1918
   291
        GL_DestroyRenderer(renderer);
slouken@1918
   292
        return NULL;
slouken@1918
   293
    }
slouken@1918
   294
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1918
   295
        GL_DestroyRenderer(renderer);
slouken@1918
   296
        return NULL;
slouken@1918
   297
    }
slouken@2246
   298
#ifdef __MACOSX__
slouken@2246
   299
    /* Enable multi-threaded rendering */
slouken@2246
   300
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   301
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   302
     */
slouken@2246
   303
#endif
slouken@2246
   304
slouken@1965
   305
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   306
        SDL_GL_SetSwapInterval(1);
slouken@1918
   307
    } else {
slouken@1918
   308
        SDL_GL_SetSwapInterval(0);
slouken@1918
   309
    }
slouken@1918
   310
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   311
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   312
    }
slouken@1918
   313
slouken@1974
   314
    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
slouken@1974
   315
        if (!doublebuffer) {
slouken@1974
   316
            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
slouken@1974
   317
        }
slouken@1974
   318
    }
slouken@1974
   319
slouken@1952
   320
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   321
    renderer->info.max_texture_width = value;
slouken@1952
   322
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   323
    renderer->info.max_texture_height = value;
slouken@1920
   324
slouken@1926
   325
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   326
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   327
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   328
    }
slouken@1974
   329
    if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) {
slouken@1974
   330
        data->GL_EXT_paletted_texture_supported = SDL_TRUE;
slouken@1974
   331
        data->glColorTableEXT =
slouken@1974
   332
            (PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT");
slouken@1974
   333
    } else {
slouken@1974
   334
        /* Don't advertise support for 8-bit indexed texture format */
slouken@1974
   335
        Uint32 i, j;
slouken@1974
   336
        SDL_RendererInfo *info = &renderer->info;
slouken@1974
   337
        for (i = 0, j = 0; i < info->num_texture_formats; ++i) {
slouken@1974
   338
            if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   339
                info->texture_formats[j++] = info->texture_formats[i];
slouken@1974
   340
            }
slouken@1974
   341
        }
slouken@1974
   342
        --info->num_texture_formats;
slouken@1974
   343
    }
slouken@2233
   344
    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
slouken@2233
   345
        data->glTextureRangeAPPLE =
slouken@2233
   346
            (void (*)(GLenum, GLsizei, const GLvoid *))
slouken@2233
   347
            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
slouken@2233
   348
    }
slouken@1920
   349
slouken@1918
   350
    /* Set up parameters for rendering */
slouken@1927
   351
    data->blendMode = -1;
slouken@1927
   352
    data->scaleMode = -1;
slouken@1927
   353
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   354
    data->glDisable(GL_CULL_FACE);
slouken@1926
   355
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   356
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   357
    } else {
slouken@1927
   358
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   359
    }
slouken@1970
   360
    data->updateSize = SDL_TRUE;
slouken@1918
   361
slouken@1918
   362
    return renderer;
slouken@1918
   363
}
slouken@1918
   364
slouken@1923
   365
static int
slouken@1923
   366
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   367
{
slouken@1923
   368
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1923
   369
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1923
   370
slouken@1970
   371
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1970
   372
        return -1;
slouken@1970
   373
    }
slouken@1970
   374
    if (data->updateSize) {
slouken@1970
   375
        data->glMatrixMode(GL_PROJECTION);
slouken@1970
   376
        data->glLoadIdentity();
slouken@1970
   377
        data->glMatrixMode(GL_MODELVIEW);
slouken@1970
   378
        data->glLoadIdentity();
slouken@1970
   379
        data->glViewport(0, 0, window->w, window->h);
slouken@1970
   380
        data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0,
slouken@1970
   381
                      0.0, 1.0);
slouken@1970
   382
        data->updateSize = SDL_FALSE;
slouken@1970
   383
    }
slouken@1970
   384
    return 0;
slouken@1970
   385
}
slouken@1970
   386
slouken@1970
   387
static int
slouken@1970
   388
GL_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   389
{
slouken@1970
   390
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   391
slouken@1970
   392
    data->updateSize = SDL_TRUE;
slouken@1970
   393
    return 0;
slouken@1923
   394
}
slouken@1923
   395
slouken@1922
   396
static __inline__ int
slouken@1922
   397
power_of_2(int input)
slouken@1922
   398
{
slouken@1922
   399
    int value = 1;
slouken@1922
   400
slouken@1922
   401
    while (value < input) {
slouken@1922
   402
        value <<= 1;
slouken@1922
   403
    }
slouken@1922
   404
    return value;
slouken@1922
   405
}
slouken@1922
   406
slouken@1918
   407
static int
slouken@1918
   408
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   409
{
slouken@1918
   410
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   411
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   412
    GL_TextureData *data;
slouken@1920
   413
    GLint internalFormat;
slouken@1920
   414
    GLenum format, type;
slouken@1922
   415
    int texture_w, texture_h;
slouken@1924
   416
    GLenum result;
slouken@1918
   417
slouken@1920
   418
    switch (texture->format) {
slouken@1965
   419
    case SDL_PIXELFORMAT_INDEX1LSB:
slouken@1965
   420
    case SDL_PIXELFORMAT_INDEX1MSB:
slouken@1920
   421
        internalFormat = GL_RGB;
slouken@1920
   422
        format = GL_COLOR_INDEX;
slouken@1920
   423
        type = GL_BITMAP;
slouken@1920
   424
        break;
slouken@1965
   425
    case SDL_PIXELFORMAT_INDEX8:
slouken@1974
   426
        if (!renderdata->GL_EXT_paletted_texture_supported) {
slouken@1974
   427
            SDL_SetError("Unsupported texture format");
slouken@1974
   428
            return -1;
slouken@1974
   429
        }
slouken@1974
   430
        internalFormat = GL_COLOR_INDEX8_EXT;
slouken@1920
   431
        format = GL_COLOR_INDEX;
slouken@1920
   432
        type = GL_UNSIGNED_BYTE;
slouken@1920
   433
        break;
slouken@1965
   434
    case SDL_PIXELFORMAT_RGB332:
slouken@1920
   435
        internalFormat = GL_R3_G3_B2;
slouken@1920
   436
        format = GL_RGB;
slouken@1920
   437
        type = GL_UNSIGNED_BYTE_3_3_2;
slouken@1920
   438
        break;
slouken@1965
   439
    case SDL_PIXELFORMAT_RGB444:
slouken@1920
   440
        internalFormat = GL_RGB4;
slouken@1920
   441
        format = GL_RGB;
slouken@1920
   442
        type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@1920
   443
        break;
slouken@1965
   444
    case SDL_PIXELFORMAT_RGB555:
slouken@1920
   445
        internalFormat = GL_RGB5;
slouken@1920
   446
        format = GL_RGB;
slouken@1920
   447
        type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@1920
   448
        break;
slouken@1965
   449
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1920
   450
        internalFormat = GL_RGBA4;
slouken@1920
   451
        format = GL_BGRA;
slouken@1920
   452
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@1920
   453
        break;
slouken@1965
   454
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1920
   455
        internalFormat = GL_RGB5_A1;
slouken@1920
   456
        format = GL_BGRA;
slouken@1920
   457
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@1920
   458
        break;
slouken@1965
   459
    case SDL_PIXELFORMAT_RGB565:
slouken@1920
   460
        internalFormat = GL_RGB8;
slouken@1920
   461
        format = GL_RGB;
slouken@1920
   462
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1920
   463
        break;
slouken@1965
   464
    case SDL_PIXELFORMAT_RGB24:
slouken@1920
   465
        internalFormat = GL_RGB8;
slouken@1920
   466
        format = GL_RGB;
slouken@1920
   467
        type = GL_UNSIGNED_BYTE;
slouken@1920
   468
        break;
slouken@1965
   469
    case SDL_PIXELFORMAT_RGB888:
slouken@1920
   470
        internalFormat = GL_RGB8;
slouken@1924
   471
        format = GL_BGRA;
slouken@1924
   472
        type = GL_UNSIGNED_BYTE;
slouken@1920
   473
        break;
slouken@1965
   474
    case SDL_PIXELFORMAT_BGR24:
slouken@1920
   475
        internalFormat = GL_RGB8;
slouken@1920
   476
        format = GL_BGR;
slouken@1920
   477
        type = GL_UNSIGNED_BYTE;
slouken@1920
   478
        break;
slouken@1965
   479
    case SDL_PIXELFORMAT_BGR888:
slouken@1920
   480
        internalFormat = GL_RGB8;
slouken@1924
   481
        format = GL_RGBA;
slouken@1924
   482
        type = GL_UNSIGNED_BYTE;
slouken@1920
   483
        break;
slouken@1965
   484
    case SDL_PIXELFORMAT_ARGB8888:
slouken@2230
   485
#ifdef __MACOSX__
slouken@2230
   486
        internalFormat = GL_RGBA;
slouken@2230
   487
        format = GL_BGRA;
slouken@2230
   488
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@2230
   489
#else
slouken@1920
   490
        internalFormat = GL_RGBA8;
slouken@1920
   491
        format = GL_BGRA;
slouken@1924
   492
        type = GL_UNSIGNED_BYTE;
slouken@2230
   493
#endif
slouken@1920
   494
        break;
slouken@1965
   495
    case SDL_PIXELFORMAT_ABGR8888:
slouken@1920
   496
        internalFormat = GL_RGBA8;
slouken@1920
   497
        format = GL_RGBA;
slouken@1924
   498
        type = GL_UNSIGNED_BYTE;
slouken@1920
   499
        break;
slouken@1965
   500
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1920
   501
        internalFormat = GL_RGB10_A2;
slouken@1920
   502
        format = GL_BGRA;
slouken@1920
   503
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@1920
   504
        break;
slouken@1920
   505
    default:
slouken@1920
   506
        SDL_SetError("Unsupported texture format");
slouken@1920
   507
        return -1;
slouken@1920
   508
    }
slouken@1920
   509
slouken@1920
   510
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   511
    if (!data) {
slouken@1918
   512
        SDL_OutOfMemory();
slouken@1918
   513
        return -1;
slouken@1918
   514
    }
slouken@1918
   515
slouken@1974
   516
    if (texture->format == SDL_PIXELFORMAT_INDEX8) {
slouken@1974
   517
        data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8));
slouken@1974
   518
        if (!data->palette) {
slouken@1974
   519
            SDL_OutOfMemory();
slouken@1974
   520
            SDL_free(data);
slouken@1974
   521
            return -1;
slouken@1974
   522
        }
slouken@1974
   523
        SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8));
slouken@1974
   524
    }
slouken@1974
   525
slouken@2222
   526
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2222
   527
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2222
   528
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@2222
   529
        if (!data->pixels) {
slouken@2222
   530
            SDL_OutOfMemory();
slouken@2222
   531
            SDL_free(data);
slouken@2222
   532
            return -1;
slouken@2222
   533
        }
slouken@2222
   534
    }
slouken@2222
   535
slouken@1918
   536
    texture->driverdata = data;
slouken@1918
   537
slouken@1927
   538
    renderdata->glGetError();
slouken@1927
   539
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   540
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   541
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   542
        texture_w = texture->w;
slouken@1926
   543
        texture_h = texture->h;
slouken@1926
   544
        data->texw = (GLfloat) texture->w;
slouken@1926
   545
        data->texh = (GLfloat) texture->h;
slouken@1926
   546
    } else {
slouken@1926
   547
        data->type = GL_TEXTURE_2D;
slouken@1926
   548
        texture_w = power_of_2(texture->w);
slouken@1926
   549
        texture_h = power_of_2(texture->h);
slouken@1926
   550
        data->texw = (GLfloat) texture->w / texture_w;
slouken@1926
   551
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   552
    }
slouken@1920
   553
    data->format = format;
slouken@1920
   554
    data->formattype = type;
slouken@1927
   555
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   556
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@2230
   557
                                GL_NEAREST);
slouken@2230
   558
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@2230
   559
                                GL_NEAREST);
slouken@2230
   560
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   561
                                GL_CLAMP_TO_EDGE);
slouken@2230
   562
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   563
                                GL_CLAMP_TO_EDGE);
slouken@2230
   564
#ifdef __MACOSX__
slouken@2230
   565
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   566
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   567
#endif
slouken@2230
   568
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   569
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   570
#endif
slouken@2230
   571
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   572
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   573
#endif
slouken@2230
   574
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   575
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   576
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   577
    } else {
slouken@2230
   578
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   579
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   580
    }
bob@2295
   581
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
bob@2295
   582
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
slouken@2237
   583
        /*
bob@2295
   584
           if (renderdata->glTextureRangeAPPLE) {
bob@2295
   585
           renderdata->glTextureRangeAPPLE(data->type,
bob@2295
   586
           texture->h * data->pitch,
bob@2295
   587
           data->pixels);
bob@2295
   588
           }
bob@2295
   589
         */
slouken@2230
   590
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   591
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   592
                                 texture_h, 0, format, type, data->pixels);
slouken@2230
   593
    } else
slouken@2230
   594
#endif
slouken@2230
   595
    {
slouken@2230
   596
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   597
                                 texture_h, 0, format, type, NULL);
slouken@2230
   598
    }
slouken@1927
   599
    result = renderdata->glGetError();
slouken@1924
   600
    if (result != GL_NO_ERROR) {
slouken@1924
   601
        GL_SetError("glTexImage2D()", result);
slouken@1924
   602
        return -1;
slouken@1924
   603
    }
slouken@1918
   604
    return 0;
slouken@1918
   605
}
slouken@1918
   606
slouken@1918
   607
static int
slouken@2222
   608
GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2222
   609
                      void **pixels, int *pitch)
slouken@2222
   610
{
slouken@2222
   611
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@2222
   612
slouken@2222
   613
    *pixels = data->pixels;
slouken@2222
   614
    *pitch = data->pitch;
slouken@2222
   615
    return 0;
slouken@2222
   616
}
slouken@2222
   617
slouken@2222
   618
static int
slouken@1918
   619
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   620
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   621
{
slouken@1918
   622
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   623
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   624
    Uint8 *palette;
slouken@1918
   625
slouken@1974
   626
    if (!data->palette) {
slouken@1974
   627
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   628
        return -1;
slouken@1974
   629
    }
slouken@1974
   630
    palette = data->palette + firstcolor * 3;
slouken@1974
   631
    while (ncolors--) {
slouken@1974
   632
        *palette++ = colors->r;
slouken@1974
   633
        *palette++ = colors->g;
slouken@1974
   634
        *palette++ = colors->b;
slouken@1974
   635
        ++colors;
slouken@1974
   636
    }
slouken@1974
   637
    renderdata->glBindTexture(data->type, data->texture);
slouken@1974
   638
    renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
slouken@1974
   639
                                GL_UNSIGNED_BYTE, data->palette);
slouken@1918
   640
    return 0;
slouken@1918
   641
}
slouken@1918
   642
slouken@1918
   643
static int
slouken@1918
   644
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   645
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   646
{
slouken@1974
   647
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   648
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1974
   649
    Uint8 *palette;
slouken@1918
   650
slouken@1974
   651
    if (!data->palette) {
slouken@1974
   652
        SDL_SetError("Texture doesn't have a palette");
slouken@1974
   653
        return -1;
slouken@1974
   654
    }
slouken@1974
   655
    palette = data->palette + firstcolor * 3;
slouken@1974
   656
    while (ncolors--) {
slouken@1974
   657
        colors->r = *palette++;
slouken@1974
   658
        colors->g = *palette++;
slouken@1974
   659
        colors->b = *palette++;
slouken@1974
   660
        colors->unused = SDL_ALPHA_OPAQUE;
slouken@1974
   661
        ++colors;
slouken@1974
   662
    }
slouken@1918
   663
    return 0;
slouken@1918
   664
}
slouken@1918
   665
slouken@1924
   666
static void
slouken@1927
   667
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
slouken@1927
   668
                   int pitch)
slouken@1924
   669
{
slouken@1965
   670
    if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) {
slouken@1927
   671
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
slouken@1965
   672
    } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
slouken@1927
   673
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
slouken@1924
   674
    }
slouken@1927
   675
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@1927
   676
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@1927
   677
                              pitch / SDL_BYTESPERPIXEL(texture->format));
slouken@1924
   678
}
slouken@1924
   679
slouken@1918
   680
static int
slouken@1985
   681
GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   682
{
slouken@1986
   683
    return 0;
slouken@1985
   684
}
slouken@1985
   685
slouken@1985
   686
static int
slouken@1985
   687
GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   688
{
slouken@1986
   689
    return 0;
slouken@1985
   690
}
slouken@1985
   691
slouken@1985
   692
static int
slouken@1985
   693
GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   694
{
slouken@1985
   695
    switch (texture->blendMode) {
slouken@1985
   696
    case SDL_TEXTUREBLENDMODE_NONE:
slouken@1985
   697
    case SDL_TEXTUREBLENDMODE_MASK:
slouken@1985
   698
    case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1985
   699
    case SDL_TEXTUREBLENDMODE_ADD:
slouken@1985
   700
    case SDL_TEXTUREBLENDMODE_MOD:
slouken@1985
   701
        return 0;
slouken@1985
   702
    default:
slouken@1985
   703
        SDL_Unsupported();
slouken@1985
   704
        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
slouken@1985
   705
        return -1;
slouken@1985
   706
    }
slouken@1985
   707
}
slouken@1985
   708
slouken@1985
   709
static int
slouken@1985
   710
GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   711
{
slouken@1985
   712
    switch (texture->scaleMode) {
slouken@1985
   713
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
   714
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
   715
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
   716
        return 0;
slouken@1985
   717
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
   718
        SDL_Unsupported();
slouken@1985
   719
        texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
slouken@1985
   720
        return -1;
slouken@1985
   721
    default:
slouken@1985
   722
        SDL_Unsupported();
slouken@1985
   723
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
   724
        return -1;
slouken@1985
   725
    }
slouken@1985
   726
}
slouken@1985
   727
slouken@1985
   728
static int
slouken@1918
   729
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   730
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   731
{
slouken@1927
   732
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   733
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   734
    GLenum result;
slouken@1918
   735
slouken@1927
   736
    renderdata->glGetError();
slouken@1927
   737
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@1927
   738
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   739
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   740
                                rect->h, data->format, data->formattype,
slouken@1927
   741
                                pixels);
slouken@1927
   742
    result = renderdata->glGetError();
slouken@1924
   743
    if (result != GL_NO_ERROR) {
slouken@1924
   744
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   745
        return -1;
slouken@1924
   746
    }
slouken@1918
   747
    return 0;
slouken@1918
   748
}
slouken@1918
   749
slouken@1918
   750
static int
slouken@1918
   751
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   752
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   753
               int *pitch)
slouken@1918
   754
{
slouken@1918
   755
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   756
slouken@1920
   757
    if (markDirty) {
slouken@1920
   758
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
   759
    }
slouken@1918
   760
slouken@1920
   761
    *pixels =
slouken@1920
   762
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@1920
   763
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   764
    *pitch = data->pitch;
slouken@1918
   765
    return 0;
slouken@1918
   766
}
slouken@1918
   767
slouken@1918
   768
static void
slouken@1918
   769
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   770
{
slouken@1918
   771
}
slouken@1918
   772
slouken@1918
   773
static void
slouken@1918
   774
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
   775
                const SDL_Rect * rects)
slouken@1918
   776
{
slouken@1918
   777
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   778
    int i;
slouken@1918
   779
slouken@1918
   780
    for (i = 0; i < numrects; ++i) {
slouken@1920
   781
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
   782
    }
slouken@1918
   783
}
slouken@1918
   784
slouken@1918
   785
static int
slouken@1985
   786
GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@1985
   787
              const SDL_Rect * rect)
slouken@1918
   788
{
slouken@1918
   789
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   790
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   791
slouken@1985
   792
    data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
slouken@1985
   793
                       (GLclampf) b * inv255f, (GLclampf) a * inv255f);
slouken@1927
   794
    data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
slouken@1927
   795
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@1927
   796
    data->glViewport(0, 0, window->w, window->h);
slouken@1918
   797
    return 0;
slouken@1918
   798
}
slouken@1918
   799
slouken@1918
   800
static int
slouken@1918
   801
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   802
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
   803
{
slouken@1918
   804
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   805
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   806
    int minx, miny, maxx, maxy;
slouken@1918
   807
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   808
slouken@2275
   809
    if (texturedata->dirty.list) {
slouken@1920
   810
        SDL_DirtyRect *dirty;
slouken@1920
   811
        void *pixels;
slouken@1920
   812
        int bpp = SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   813
        int pitch = texturedata->pitch;
slouken@1920
   814
slouken@1927
   815
        SetupTextureUpdate(data, texture, pitch);
slouken@1927
   816
        data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
   817
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
   818
            SDL_Rect *rect = &dirty->rect;
slouken@1920
   819
            pixels =
slouken@1920
   820
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
   821
                          rect->x * bpp);
slouken@1927
   822
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
slouken@1927
   823
                                  rect->w, rect->h, texturedata->format,
slouken@1927
   824
                                  texturedata->formattype, pixels);
slouken@1920
   825
        }
slouken@1920
   826
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
   827
    }
slouken@1920
   828
slouken@1918
   829
    minx = dstrect->x;
slouken@1918
   830
    miny = dstrect->y;
slouken@1918
   831
    maxx = dstrect->x + dstrect->w;
slouken@1918
   832
    maxy = dstrect->y + dstrect->h;
slouken@1918
   833
slouken@1918
   834
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   835
    minu *= texturedata->texw;
slouken@1918
   836
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   837
    maxu *= texturedata->texw;
slouken@1918
   838
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   839
    minv *= texturedata->texh;
slouken@1918
   840
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   841
    maxv *= texturedata->texh;
slouken@1918
   842
slouken@1927
   843
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   844
slouken@1985
   845
    if (texture->modMode) {
slouken@1985
   846
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
   847
                        (GLfloat) texture->g * inv255f,
slouken@1985
   848
                        (GLfloat) texture->b * inv255f,
slouken@1985
   849
                        (GLfloat) texture->a * inv255f);
slouken@1985
   850
    } else {
slouken@1985
   851
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
   852
    }
slouken@1985
   853
slouken@1985
   854
    if (texture->blendMode != data->blendMode) {
slouken@1985
   855
        switch (texture->blendMode) {
slouken@1965
   856
        case SDL_TEXTUREBLENDMODE_NONE:
slouken@1927
   857
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@1927
   858
            data->glDisable(GL_BLEND);
slouken@1927
   859
            break;
slouken@1965
   860
        case SDL_TEXTUREBLENDMODE_MASK:
slouken@1965
   861
        case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1927
   862
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   863
            data->glEnable(GL_BLEND);
slouken@1927
   864
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1927
   865
            break;
slouken@1965
   866
        case SDL_TEXTUREBLENDMODE_ADD:
slouken@1927
   867
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   868
            data->glEnable(GL_BLEND);
slouken@1927
   869
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@1927
   870
            break;
slouken@1965
   871
        case SDL_TEXTUREBLENDMODE_MOD:
slouken@1927
   872
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   873
            data->glEnable(GL_BLEND);
slouken@1927
   874
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@1927
   875
            break;
slouken@1927
   876
        }
slouken@1985
   877
        data->blendMode = texture->blendMode;
slouken@1918
   878
    }
slouken@1918
   879
slouken@1985
   880
    if (texture->scaleMode != data->scaleMode) {
slouken@1985
   881
        switch (texture->scaleMode) {
slouken@1965
   882
        case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
   883
        case SDL_TEXTURESCALEMODE_FAST:
slouken@1927
   884
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
   885
                                  GL_NEAREST);
slouken@1927
   886
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
   887
                                  GL_NEAREST);
slouken@1927
   888
            break;
slouken@1965
   889
        case SDL_TEXTURESCALEMODE_SLOW:
slouken@1965
   890
        case SDL_TEXTURESCALEMODE_BEST:
slouken@1927
   891
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
   892
                                  GL_LINEAR);
slouken@1927
   893
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
   894
                                  GL_LINEAR);
slouken@1927
   895
            break;
slouken@1927
   896
        }
slouken@1985
   897
        data->scaleMode = texture->scaleMode;
slouken@1918
   898
    }
slouken@1918
   899
slouken@1927
   900
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
   901
    data->glTexCoord2f(minu, minv);
slouken@1927
   902
    data->glVertex2i(minx, miny);
slouken@1927
   903
    data->glTexCoord2f(maxu, minv);
slouken@1927
   904
    data->glVertex2i(maxx, miny);
slouken@1927
   905
    data->glTexCoord2f(minu, maxv);
slouken@1927
   906
    data->glVertex2i(minx, maxy);
slouken@1927
   907
    data->glTexCoord2f(maxu, maxv);
slouken@1927
   908
    data->glVertex2i(maxx, maxy);
slouken@1927
   909
    data->glEnd();
slouken@1918
   910
slouken@1918
   911
    return 0;
slouken@1918
   912
}
slouken@1918
   913
slouken@1918
   914
static void
slouken@1918
   915
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   916
{
slouken@1918
   917
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   918
}
slouken@1918
   919
slouken@1918
   920
static void
slouken@1918
   921
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   922
{
slouken@1927
   923
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   924
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   925
slouken@1918
   926
    if (!data) {
slouken@1918
   927
        return;
slouken@1918
   928
    }
slouken@1918
   929
    if (data->texture) {
slouken@1927
   930
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
   931
    }
slouken@1974
   932
    if (data->palette) {
slouken@1974
   933
        SDL_free(data->palette);
slouken@1974
   934
    }
slouken@1920
   935
    if (data->pixels) {
slouken@1920
   936
        SDL_free(data->pixels);
slouken@1920
   937
    }
slouken@1920
   938
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
   939
    SDL_free(data);
slouken@1918
   940
    texture->driverdata = NULL;
slouken@1918
   941
}
slouken@1918
   942
slouken@1975
   943
static void
slouken@1918
   944
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   945
{
slouken@1918
   946
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   947
slouken@1918
   948
    if (data) {
slouken@1920
   949
        if (data->context) {
bob@2328
   950
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
   951
            SDL_GL_DeleteContext(data->context);
slouken@1918
   952
        }
slouken@1918
   953
        SDL_free(data);
slouken@1918
   954
    }
slouken@1918
   955
    SDL_free(renderer);
slouken@1918
   956
}
slouken@1918
   957
slouken@1952
   958
#endif /* SDL_VIDEO_RENDER_OGL */
slouken@1918
   959
slouken@1918
   960
/* vi: set ts=4 sw=4 expandtab: */