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