src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Aug 2007 16:53:58 +0000
changeset 2233 fb01ee9716bc
parent 2230 9b7d29d2432b
child 2236 741afd8402cf
permissions -rw-r--r--
Test using glTextureRangeAPPLE
This actually ends up being quite a bit slower on my MacBook, but I'm
checking it in to test on a PPC iMac.

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