src/render/opengl/SDL_render_gl.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 20:23:52 -0800
changeset 5230 8efa43b915be
parent 5229 c015d3e63631
child 5233 ce4f91138031
permissions -rw-r--r--
Fixed the shaders (needed to use texture2DRect) - thanks Ryan!
slouken@1918
     1
/*
slouken@1918
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@5226
    24
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
slouken@1920
    25
slouken@1920
    26
#include "SDL_opengl.h"
slouken@5154
    27
#include "../SDL_sysrender.h"
slouken@5228
    28
#include "SDL_shaders_gl.h"
slouken@1918
    29
slouken@2246
    30
#ifdef __MACOSX__
slouken@2246
    31
#include <OpenGL/OpenGL.h>
slouken@2246
    32
#endif
slouken@2246
    33
slouken@2778
    34
slouken@1918
    35
/* OpenGL renderer implementation */
slouken@1918
    36
slouken@2230
    37
/* Details on optimizing the texture path on Mac OS X:
slouken@5203
    38
   http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
slouken@2230
    39
*/
slouken@2230
    40
slouken@5154
    41
/* Used to re-create the window with OpenGL capability */
slouken@5154
    42
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
icculus@2835
    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@5147
    47
static void GL_WindowEvent(SDL_Renderer * renderer,
slouken@5147
    48
                           const SDL_WindowEvent *event);
slouken@1918
    49
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    50
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    51
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    52
                            int pitch);
slouken@1918
    53
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
    54
                          const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1918
    55
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5224
    56
static void GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
slouken@3596
    57
static int GL_RenderClear(SDL_Renderer * renderer);
slouken@3596
    58
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3596
    59
                               const SDL_Point * points, int count);
slouken@3596
    60
static int GL_RenderDrawLines(SDL_Renderer * renderer,
slouken@3596
    61
                              const SDL_Point * points, int count);
slouken@3596
    62
static int GL_RenderFillRects(SDL_Renderer * renderer,
slouken@3596
    63
                              const SDL_Rect ** rects, int count);
slouken@1918
    64
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    65
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3431
    66
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    67
                               Uint32 pixel_format, void * pixels, int pitch);
slouken@1918
    68
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    69
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    70
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    71
slouken@1918
    72
slouken@1918
    73
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    74
    GL_CreateRenderer,
slouken@1918
    75
    {
slouken@1918
    76
     "opengl",
slouken@5203
    77
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
slouken@5156
    78
     1,
slouken@5156
    79
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1918
    80
     0,
slouken@1918
    81
     0}
slouken@1918
    82
};
slouken@1918
    83
slouken@1918
    84
typedef struct
slouken@1918
    85
{
slouken@1918
    86
    SDL_GLContext context;
slouken@2833
    87
    SDL_bool updateSize;
slouken@2233
    88
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1927
    89
    int blendMode;
slouken@1927
    90
slouken@1927
    91
    /* OpenGL functions */
slouken@1927
    92
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@5204
    93
#include "SDL_glfuncs.h"
slouken@1927
    94
#undef SDL_PROC
slouken@1974
    95
slouken@2233
    96
    void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
slouken@2233
    97
                                 const GLvoid * pointer);
slouken@5228
    98
slouken@5228
    99
    /* Multitexture support */
slouken@5228
   100
    SDL_bool GL_ARB_multitexture_supported;
slouken@5228
   101
    PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
slouken@5228
   102
    int num_texture_units;
slouken@5228
   103
slouken@5228
   104
    /* Shader support */
slouken@5228
   105
    GL_ShaderContext *shaders;
slouken@5228
   106
slouken@1918
   107
} GL_RenderData;
slouken@1918
   108
slouken@1918
   109
typedef struct
slouken@1918
   110
{
slouken@1918
   111
    GLuint texture;
slouken@1920
   112
    GLenum type;
slouken@1918
   113
    GLfloat texw;
slouken@1918
   114
    GLfloat texh;
slouken@1920
   115
    GLenum format;
slouken@1920
   116
    GLenum formattype;
slouken@1918
   117
    void *pixels;
slouken@1918
   118
    int pitch;
slouken@5227
   119
    SDL_Rect locked_rect;
slouken@1918
   120
} GL_TextureData;
slouken@1918
   121
slouken@1918
   122
slouken@1924
   123
static void
slouken@1924
   124
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   125
{
slouken@1924
   126
    const char *error;
slouken@1924
   127
slouken@1924
   128
    switch (result) {
slouken@1924
   129
    case GL_NO_ERROR:
slouken@1924
   130
        error = "GL_NO_ERROR";
slouken@1924
   131
        break;
slouken@1924
   132
    case GL_INVALID_ENUM:
slouken@1924
   133
        error = "GL_INVALID_ENUM";
slouken@1924
   134
        break;
slouken@1924
   135
    case GL_INVALID_VALUE:
slouken@1924
   136
        error = "GL_INVALID_VALUE";
slouken@1924
   137
        break;
slouken@1924
   138
    case GL_INVALID_OPERATION:
slouken@1924
   139
        error = "GL_INVALID_OPERATION";
slouken@1924
   140
        break;
slouken@1924
   141
    case GL_STACK_OVERFLOW:
slouken@1924
   142
        error = "GL_STACK_OVERFLOW";
slouken@1924
   143
        break;
slouken@1924
   144
    case GL_STACK_UNDERFLOW:
slouken@1924
   145
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   146
        break;
slouken@1924
   147
    case GL_OUT_OF_MEMORY:
slouken@1924
   148
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   149
        break;
slouken@1924
   150
    case GL_TABLE_TOO_LARGE:
slouken@1924
   151
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   152
        break;
slouken@1924
   153
    default:
slouken@1924
   154
        error = "UNKNOWN";
slouken@1924
   155
        break;
slouken@1924
   156
    }
slouken@1924
   157
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   158
}
slouken@1924
   159
slouken@1927
   160
static int
slouken@1927
   161
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   162
{
slouken@1927
   163
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   164
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   165
#else
slouken@1927
   166
#define SDL_PROC(ret,func,params) \
slouken@1927
   167
    do { \
slouken@1927
   168
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   169
        if ( ! data->func ) { \
slouken@1927
   170
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   171
            return -1; \
slouken@1927
   172
        } \
slouken@1927
   173
    } while ( 0 );
slouken@1927
   174
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   175
slouken@5204
   176
#include "SDL_glfuncs.h"
slouken@1927
   177
#undef SDL_PROC
slouken@1927
   178
    return 0;
slouken@1927
   179
}
slouken@1927
   180
slouken@1918
   181
SDL_Renderer *
slouken@1918
   182
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   183
{
slouken@1918
   184
    SDL_Renderer *renderer;
slouken@1918
   185
    GL_RenderData *data;
slouken@1952
   186
    GLint value;
slouken@5154
   187
    Uint32 window_flags;
slouken@1918
   188
slouken@5154
   189
    window_flags = SDL_GetWindowFlags(window);
slouken@5154
   190
    if (!(window_flags & SDL_WINDOW_OPENGL)) {
slouken@5154
   191
        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
slouken@1924
   192
            return NULL;
slouken@1924
   193
        }
slouken@1918
   194
    }
slouken@1918
   195
slouken@1920
   196
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   197
    if (!renderer) {
slouken@1918
   198
        SDL_OutOfMemory();
slouken@1918
   199
        return NULL;
slouken@1918
   200
    }
slouken@1918
   201
slouken@1920
   202
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   203
    if (!data) {
slouken@1918
   204
        GL_DestroyRenderer(renderer);
slouken@1918
   205
        SDL_OutOfMemory();
slouken@1918
   206
        return NULL;
slouken@1918
   207
    }
slouken@1918
   208
slouken@5147
   209
    renderer->WindowEvent = GL_WindowEvent;
slouken@1918
   210
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   211
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   212
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   213
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@5224
   214
    renderer->SetClipRect = GL_SetClipRect;
slouken@3596
   215
    renderer->RenderClear = GL_RenderClear;
slouken@3596
   216
    renderer->RenderDrawPoints = GL_RenderDrawPoints;
slouken@3596
   217
    renderer->RenderDrawLines = GL_RenderDrawLines;
slouken@3596
   218
    renderer->RenderFillRects = GL_RenderFillRects;
slouken@1918
   219
    renderer->RenderCopy = GL_RenderCopy;
slouken@3431
   220
    renderer->RenderReadPixels = GL_RenderReadPixels;
slouken@1918
   221
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   222
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   223
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   224
    renderer->info = GL_RenderDriver.info;
slouken@1918
   225
    renderer->driverdata = data;
slouken@1918
   226
slouken@5142
   227
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1918
   228
slouken@3685
   229
    data->context = SDL_GL_CreateContext(window);
slouken@1918
   230
    if (!data->context) {
slouken@1918
   231
        GL_DestroyRenderer(renderer);
slouken@1918
   232
        return NULL;
slouken@1918
   233
    }
slouken@3685
   234
    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@1918
   235
        GL_DestroyRenderer(renderer);
slouken@1918
   236
        return NULL;
slouken@1918
   237
    }
slouken@5204
   238
slouken@5204
   239
    if (GL_LoadFunctions(data) < 0) {
slouken@5204
   240
        GL_DestroyRenderer(renderer);
slouken@5204
   241
        return NULL;
slouken@5204
   242
    }
slouken@5204
   243
slouken@2246
   244
#ifdef __MACOSX__
slouken@2246
   245
    /* Enable multi-threaded rendering */
slouken@2246
   246
    /* Disabled until Ryan finishes his VBO/PBO code...
bob@2295
   247
       CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
bob@2295
   248
     */
slouken@2246
   249
#endif
slouken@2246
   250
slouken@1965
   251
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1918
   252
        SDL_GL_SetSwapInterval(1);
slouken@1918
   253
    } else {
slouken@1918
   254
        SDL_GL_SetSwapInterval(0);
slouken@1918
   255
    }
slouken@1918
   256
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1965
   257
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1918
   258
    }
slouken@1918
   259
slouken@1952
   260
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   261
    renderer->info.max_texture_width = value;
slouken@1952
   262
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
slouken@1952
   263
    renderer->info.max_texture_height = value;
slouken@1920
   264
slouken@1926
   265
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   266
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   267
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   268
    }
slouken@2233
   269
    if (SDL_GL_ExtensionSupported("GL_APPLE_texture_range")) {
slouken@2233
   270
        data->glTextureRangeAPPLE =
slouken@2233
   271
            (void (*)(GLenum, GLsizei, const GLvoid *))
slouken@2233
   272
            SDL_GL_GetProcAddress("glTextureRangeAPPLE");
slouken@2233
   273
    }
slouken@1920
   274
slouken@5228
   275
    /* Check for multitexture support */
slouken@5228
   276
    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
slouken@5228
   277
        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
slouken@5228
   278
        if (data->glActiveTextureARB) {
slouken@5228
   279
            data->GL_ARB_multitexture_supported = SDL_TRUE;
slouken@5228
   280
            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
slouken@5228
   281
        }
slouken@5228
   282
    }
slouken@5228
   283
slouken@5228
   284
    /* Check for shader support */
slouken@5229
   285
    data->shaders = GL_CreateShaderContext();
slouken@5228
   286
slouken@5228
   287
#if 0
slouken@5228
   288
    /* We support YV12 textures using 3 textures and a shader */
slouken@5228
   289
    if (data->shaders && data->num_texture_units >= 3) {
slouken@5228
   290
        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@5228
   291
    }
slouken@5228
   292
#endif
slouken@5228
   293
slouken@1918
   294
    /* Set up parameters for rendering */
slouken@1927
   295
    data->blendMode = -1;
slouken@1927
   296
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   297
    data->glDisable(GL_CULL_FACE);
slouken@3262
   298
    /* This ended up causing video discrepancies between OpenGL and Direct3D */
slouken@3262
   299
    /*data->glEnable(GL_LINE_SMOOTH);*/
slouken@2833
   300
    data->updateSize = SDL_TRUE;
slouken@1918
   301
slouken@1918
   302
    return renderer;
slouken@1918
   303
}
slouken@1918
   304
slouken@5147
   305
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5147
   306
slouken@1923
   307
static int
slouken@1923
   308
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   309
{
slouken@1923
   310
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
   311
    SDL_Window *window = renderer->window;
slouken@1923
   312
slouken@5147
   313
    if (SDL_CurrentContext != data->context) {
slouken@5147
   314
        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@5147
   315
            return -1;
slouken@5147
   316
        }
slouken@5147
   317
        SDL_CurrentContext = data->context;
slouken@1970
   318
    }
slouken@2833
   319
    if (data->updateSize) {
slouken@5154
   320
        int w, h;
slouken@5154
   321
slouken@5154
   322
        SDL_GetWindowSize(window, &w, &h);
slouken@2836
   323
        data->glMatrixMode(GL_PROJECTION);
slouken@2836
   324
        data->glLoadIdentity();
slouken@2836
   325
        data->glMatrixMode(GL_MODELVIEW);
slouken@2836
   326
        data->glLoadIdentity();
slouken@5154
   327
        data->glViewport(0, 0, w, h);
slouken@5154
   328
        data->glOrtho(0.0, (GLdouble) w, (GLdouble) h, 0.0, 0.0, 1.0);
slouken@2833
   329
        data->updateSize = SDL_FALSE;
slouken@2833
   330
    }
slouken@1970
   331
    return 0;
slouken@1970
   332
}
slouken@1970
   333
slouken@5147
   334
static void
slouken@5147
   335
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   336
{
slouken@1970
   337
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   338
slouken@5147
   339
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
slouken@5147
   340
        /* Rebind the context to the window area and update matrices */
slouken@5147
   341
        SDL_CurrentContext = NULL;
slouken@5147
   342
        data->updateSize = SDL_TRUE;
slouken@5147
   343
    }
slouken@1923
   344
}
slouken@1923
   345
slouken@1922
   346
static __inline__ int
slouken@1922
   347
power_of_2(int input)
slouken@1922
   348
{
slouken@1922
   349
    int value = 1;
slouken@1922
   350
slouken@1922
   351
    while (value < input) {
slouken@1922
   352
        value <<= 1;
slouken@1922
   353
    }
slouken@1922
   354
    return value;
slouken@1922
   355
}
slouken@1922
   356
slouken@3433
   357
static __inline__ SDL_bool
slouken@3433
   358
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   359
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   360
{
slouken@3433
   361
    switch (pixel_format) {
slouken@3433
   362
    case SDL_PIXELFORMAT_ARGB8888:
slouken@3433
   363
        *internalFormat = GL_RGBA8;
slouken@3433
   364
        *format = GL_BGRA;
slouken@5156
   365
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   366
        break;
slouken@3433
   367
    default:
slouken@3433
   368
        return SDL_FALSE;
slouken@3433
   369
    }
slouken@3433
   370
    return SDL_TRUE;
slouken@3433
   371
}
icculus@2835
   372
slouken@1918
   373
static int
slouken@1918
   374
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   375
{
slouken@1918
   376
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   377
    GL_TextureData *data;
slouken@1920
   378
    GLint internalFormat;
slouken@1920
   379
    GLenum format, type;
slouken@1922
   380
    int texture_w, texture_h;
slouken@1924
   381
    GLenum result;
slouken@1918
   382
slouken@5147
   383
    GL_ActivateRenderer(renderer);
slouken@5147
   384
slouken@3433
   385
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   386
                        &format, &type)) {
slouken@4990
   387
        SDL_SetError("Texture format %s not supported by OpenGL",
slouken@4990
   388
                     SDL_GetPixelFormatName(texture->format));
slouken@1920
   389
        return -1;
slouken@1920
   390
    }
slouken@1920
   391
slouken@1920
   392
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   393
    if (!data) {
slouken@1918
   394
        SDL_OutOfMemory();
slouken@1918
   395
        return -1;
slouken@1918
   396
    }
slouken@1918
   397
slouken@2222
   398
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   399
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2222
   400
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@2222
   401
        if (!data->pixels) {
slouken@2222
   402
            SDL_OutOfMemory();
slouken@2222
   403
            SDL_free(data);
slouken@2222
   404
            return -1;
slouken@2222
   405
        }
slouken@2222
   406
    }
slouken@2222
   407
slouken@1918
   408
    texture->driverdata = data;
slouken@1918
   409
slouken@1927
   410
    renderdata->glGetError();
slouken@1927
   411
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   412
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   413
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   414
        texture_w = texture->w;
slouken@1926
   415
        texture_h = texture->h;
icculus@2835
   416
        data->texw = (GLfloat) texture_w;
icculus@2835
   417
        data->texh = (GLfloat) texture_h;
slouken@1926
   418
    } else {
slouken@1926
   419
        data->type = GL_TEXTURE_2D;
slouken@1926
   420
        texture_w = power_of_2(texture->w);
slouken@1926
   421
        texture_h = power_of_2(texture->h);
icculus@2835
   422
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   423
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   424
    }
icculus@2835
   425
slouken@1920
   426
    data->format = format;
slouken@1920
   427
    data->formattype = type;
slouken@2884
   428
    renderdata->glEnable(data->type);
slouken@1927
   429
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   430
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5138
   431
                                GL_LINEAR);
slouken@2230
   432
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5138
   433
                                GL_LINEAR);
slouken@2230
   434
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   435
                                GL_CLAMP_TO_EDGE);
slouken@2230
   436
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   437
                                GL_CLAMP_TO_EDGE);
slouken@2840
   438
#ifdef __MACOSX__
slouken@2230
   439
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   440
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   441
#endif
slouken@2230
   442
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   443
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   444
#endif
slouken@2230
   445
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   446
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   447
#endif
slouken@2230
   448
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   449
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   450
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   451
    } else {
slouken@2230
   452
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   453
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   454
    }
bob@2295
   455
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
bob@2295
   456
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
slouken@2230
   457
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   458
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   459
                                 texture_h, 0, format, type, data->pixels);
slouken@5156
   460
    }
slouken@5156
   461
    else
slouken@2809
   462
#endif
slouken@2230
   463
    {
slouken@2230
   464
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   465
                                 texture_h, 0, format, type, NULL);
slouken@2230
   466
    }
slouken@3041
   467
    renderdata->glDisable(data->type);
slouken@1927
   468
    result = renderdata->glGetError();
slouken@1924
   469
    if (result != GL_NO_ERROR) {
slouken@1924
   470
        GL_SetError("glTexImage2D()", result);
slouken@1924
   471
        return -1;
slouken@1924
   472
    }
slouken@1918
   473
    return 0;
slouken@1918
   474
}
slouken@1918
   475
slouken@1918
   476
static int
slouken@1918
   477
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   478
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   479
{
slouken@1927
   480
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   481
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   482
    GLenum result;
slouken@1918
   483
slouken@5147
   484
    GL_ActivateRenderer(renderer);
slouken@5147
   485
slouken@1927
   486
    renderdata->glGetError();
slouken@5227
   487
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5227
   488
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5227
   489
                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@2884
   490
    renderdata->glEnable(data->type);
slouken@1927
   491
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   492
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   493
                                rect->h, data->format, data->formattype,
slouken@1927
   494
                                pixels);
slouken@3041
   495
    renderdata->glDisable(data->type);
slouken@1927
   496
    result = renderdata->glGetError();
slouken@1924
   497
    if (result != GL_NO_ERROR) {
slouken@1924
   498
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   499
        return -1;
slouken@1924
   500
    }
slouken@1918
   501
    return 0;
slouken@1918
   502
}
slouken@1918
   503
slouken@1918
   504
static int
slouken@1918
   505
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   506
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1918
   507
{
slouken@1918
   508
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   509
slouken@5227
   510
    data->locked_rect = *rect;
slouken@5227
   511
    *pixels = 
slouken@1920
   512
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5156
   513
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   514
    *pitch = data->pitch;
slouken@1918
   515
    return 0;
slouken@1918
   516
}
slouken@1918
   517
slouken@1918
   518
static void
slouken@1918
   519
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   520
{
slouken@5156
   521
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@5227
   522
    const SDL_Rect *rect;
slouken@5227
   523
    void *pixels;
slouken@1918
   524
slouken@5227
   525
    rect = &data->locked_rect;
slouken@5227
   526
    pixels = 
slouken@5227
   527
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5227
   528
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5227
   529
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@1918
   530
}
slouken@1918
   531
slouken@2936
   532
static void
slouken@5224
   533
GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5224
   534
{
slouken@5224
   535
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5224
   536
slouken@5224
   537
    GL_ActivateRenderer(renderer);
slouken@5224
   538
slouken@5224
   539
    if (rect) {
slouken@5224
   540
        int w, h;
slouken@5224
   541
slouken@5224
   542
        SDL_GetWindowSize(renderer->window, &w, &h);
slouken@5224
   543
        data->glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
slouken@5224
   544
        data->glEnable(GL_SCISSOR_TEST);
slouken@5224
   545
    } else {
slouken@5224
   546
        data->glDisable(GL_SCISSOR_TEST);
slouken@5224
   547
    }
slouken@5224
   548
}
slouken@5224
   549
slouken@5224
   550
static void
slouken@5140
   551
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
   552
{
slouken@2936
   553
    if (blendMode != data->blendMode) {
slouken@2936
   554
        switch (blendMode) {
slouken@2936
   555
        case SDL_BLENDMODE_NONE:
slouken@2936
   556
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
   557
            data->glDisable(GL_BLEND);
slouken@2936
   558
            break;
slouken@2936
   559
        case SDL_BLENDMODE_BLEND:
slouken@2936
   560
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   561
            data->glEnable(GL_BLEND);
slouken@2936
   562
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2936
   563
            break;
slouken@2936
   564
        case SDL_BLENDMODE_ADD:
slouken@2936
   565
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   566
            data->glEnable(GL_BLEND);
slouken@2936
   567
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@2936
   568
            break;
slouken@5184
   569
        case SDL_BLENDMODE_MOD:
slouken@5184
   570
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@5184
   571
            data->glEnable(GL_BLEND);
slouken@5184
   572
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@5184
   573
            break;
slouken@2936
   574
        }
slouken@2936
   575
        data->blendMode = blendMode;
slouken@2936
   576
    }
slouken@2936
   577
}
slouken@2936
   578
slouken@1918
   579
static int
slouken@3596
   580
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
   581
{
slouken@3596
   582
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
   583
slouken@5147
   584
    GL_ActivateRenderer(renderer);
slouken@5147
   585
slouken@3596
   586
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
   587
                       (GLfloat) renderer->g * inv255f,
slouken@3596
   588
                       (GLfloat) renderer->b * inv255f,
slouken@3596
   589
                       (GLfloat) renderer->a * inv255f);
slouken@3596
   590
slouken@3596
   591
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
   592
slouken@3596
   593
    return 0;
slouken@3596
   594
}
slouken@3596
   595
slouken@3596
   596
static int
slouken@3596
   597
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   598
                    int count)
slouken@2884
   599
{
slouken@2884
   600
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   601
    int i;
slouken@2884
   602
slouken@5147
   603
    GL_ActivateRenderer(renderer);
slouken@5147
   604
slouken@5140
   605
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   606
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2884
   607
slouken@2884
   608
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2884
   609
                    (GLfloat) renderer->g * inv255f,
slouken@2884
   610
                    (GLfloat) renderer->b * inv255f,
slouken@2884
   611
                    (GLfloat) renderer->a * inv255f);
slouken@2884
   612
slouken@2901
   613
    data->glBegin(GL_POINTS);
slouken@3536
   614
    for (i = 0; i < count; ++i) {
slouken@3536
   615
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   616
    }
slouken@2901
   617
    data->glEnd();
slouken@2901
   618
slouken@2901
   619
    return 0;
slouken@2901
   620
}
slouken@2901
   621
slouken@2901
   622
static int
slouken@3596
   623
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   624
                   int count)
slouken@2901
   625
{
slouken@2901
   626
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   627
    int i;
slouken@2901
   628
slouken@5147
   629
    GL_ActivateRenderer(renderer);
slouken@5147
   630
slouken@5140
   631
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   632
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2901
   633
slouken@2901
   634
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2901
   635
                    (GLfloat) renderer->g * inv255f,
slouken@2901
   636
                    (GLfloat) renderer->b * inv255f,
slouken@2901
   637
                    (GLfloat) renderer->a * inv255f);
slouken@2901
   638
slouken@3536
   639
    if (count > 2 && 
slouken@3536
   640
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@3536
   641
        data->glBegin(GL_LINE_LOOP);
slouken@3536
   642
        /* GL_LINE_LOOP takes care of the final segment */
slouken@3536
   643
        --count;
slouken@3536
   644
        for (i = 0; i < count; ++i) {
slouken@3536
   645
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   646
        }
slouken@3536
   647
        data->glEnd();
slouken@3536
   648
    } else {
slouken@5086
   649
#if defined(__APPLE__) || defined(__WIN32__)
aschiffler@4910
   650
#else
slouken@4905
   651
        int x1, y1, x2, y2;
aschiffler@4910
   652
#endif
slouken@4905
   653
slouken@3536
   654
        data->glBegin(GL_LINE_STRIP);
slouken@3536
   655
        for (i = 0; i < count; ++i) {
slouken@3536
   656
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   657
        }
slouken@3536
   658
        data->glEnd();
slouken@3474
   659
slouken@3536
   660
        /* The line is half open, so we need one more point to complete it.
slouken@3536
   661
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@3536
   662
         * If we have to, we can use vertical line and horizontal line textures
slouken@3536
   663
         * for vertical and horizontal lines, and then create custom textures
slouken@3536
   664
         * for diagonal lines and software render those.  It's terrible, but at
slouken@3536
   665
         * least it would be pixel perfect.
slouken@3536
   666
         */
slouken@3536
   667
        data->glBegin(GL_POINTS);
slouken@5086
   668
#if defined(__APPLE__) || defined(__WIN32__)
slouken@3536
   669
        /* Mac OS X and Windows seem to always leave the second point open */
slouken@3536
   670
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@3474
   671
#else
slouken@3536
   672
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@4905
   673
        x1 = points[0].x;
slouken@4905
   674
        y1 = points[0].y;
slouken@4905
   675
        x2 = points[count-1].x;
slouken@4905
   676
        y2 = points[count-1].y;
slouken@3536
   677
slouken@3536
   678
        if (x1 > x2) {
slouken@3536
   679
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   680
        } else if (x2 > x1) {
slouken@3536
   681
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   682
        } else if (y1 > y2) {
slouken@3536
   683
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   684
        } else if (y2 > y1) {
slouken@3536
   685
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   686
        }
slouken@3536
   687
#endif
slouken@3536
   688
        data->glEnd();
slouken@3474
   689
    }
slouken@3455
   690
slouken@1918
   691
    return 0;
slouken@1918
   692
}
slouken@1918
   693
slouken@1918
   694
static int
slouken@3596
   695
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
slouken@2925
   696
{
slouken@2925
   697
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   698
    int i;
slouken@2925
   699
slouken@5147
   700
    GL_ActivateRenderer(renderer);
slouken@5147
   701
slouken@5140
   702
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   703
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2936
   704
slouken@2925
   705
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2925
   706
                    (GLfloat) renderer->g * inv255f,
slouken@2925
   707
                    (GLfloat) renderer->b * inv255f,
slouken@2925
   708
                    (GLfloat) renderer->a * inv255f);
slouken@2936
   709
slouken@3536
   710
    for (i = 0; i < count; ++i) {
slouken@3536
   711
        const SDL_Rect *rect = rects[i];
slouken@3536
   712
slouken@3536
   713
        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
   714
    }
slouken@2925
   715
slouken@2925
   716
    return 0;
slouken@2925
   717
}
slouken@2925
   718
slouken@2925
   719
static int
slouken@1918
   720
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   721
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
   722
{
slouken@1918
   723
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   724
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   725
    int minx, miny, maxx, maxy;
slouken@1918
   726
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   727
slouken@5147
   728
    GL_ActivateRenderer(renderer);
slouken@5147
   729
slouken@1918
   730
    minx = dstrect->x;
slouken@1918
   731
    miny = dstrect->y;
slouken@1918
   732
    maxx = dstrect->x + dstrect->w;
slouken@1918
   733
    maxy = dstrect->y + dstrect->h;
slouken@1918
   734
slouken@1918
   735
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   736
    minu *= texturedata->texw;
slouken@1918
   737
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   738
    maxu *= texturedata->texw;
slouken@1918
   739
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   740
    minv *= texturedata->texh;
slouken@1918
   741
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   742
    maxv *= texturedata->texh;
slouken@1918
   743
slouken@2884
   744
    data->glEnable(texturedata->type);
slouken@1927
   745
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   746
slouken@1985
   747
    if (texture->modMode) {
slouken@1985
   748
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
   749
                        (GLfloat) texture->g * inv255f,
slouken@1985
   750
                        (GLfloat) texture->b * inv255f,
slouken@1985
   751
                        (GLfloat) texture->a * inv255f);
slouken@1985
   752
    } else {
slouken@1985
   753
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
   754
    }
slouken@1985
   755
slouken@5140
   756
    GL_SetBlendMode(data, texture->blendMode);
slouken@5228
   757
    GL_SelectShader(data->shaders, SHADER_RGB);
slouken@1918
   758
slouken@1927
   759
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
   760
    data->glTexCoord2f(minu, minv);
slouken@3472
   761
    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
slouken@1927
   762
    data->glTexCoord2f(maxu, minv);
slouken@3472
   763
    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
slouken@1927
   764
    data->glTexCoord2f(minu, maxv);
slouken@3472
   765
    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
slouken@1927
   766
    data->glTexCoord2f(maxu, maxv);
slouken@3472
   767
    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
slouken@1927
   768
    data->glEnd();
slouken@1918
   769
slouken@2884
   770
    data->glDisable(texturedata->type);
slouken@2884
   771
slouken@1918
   772
    return 0;
slouken@1918
   773
}
slouken@1918
   774
slouken@3431
   775
static int
slouken@3431
   776
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   777
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
   778
{
slouken@3433
   779
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
   780
    SDL_Window *window = renderer->window;
slouken@3433
   781
    GLint internalFormat;
slouken@3433
   782
    GLenum format, type;
slouken@3435
   783
    Uint8 *src, *dst, *tmp;
slouken@5154
   784
    int w, h, length, rows;
slouken@3433
   785
slouken@5147
   786
    GL_ActivateRenderer(renderer);
slouken@5147
   787
slouken@3433
   788
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
slouken@3435
   789
        /* FIXME: Do a temp copy to a format that is supported */
slouken@3433
   790
        SDL_SetError("Unsupported pixel format");
slouken@3433
   791
        return -1;
slouken@3433
   792
    }
slouken@3433
   793
slouken@5154
   794
    SDL_GetWindowSize(window, &w, &h);
slouken@5154
   795
slouken@3446
   796
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
   797
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@5156
   798
                        (pitch / SDL_BYTESPERPIXEL(pixel_format)));
slouken@3433
   799
slouken@5154
   800
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@3435
   801
                       format, type, pixels);
slouken@3435
   802
slouken@3435
   803
    /* Flip the rows to be top-down */
slouken@5156
   804
    length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
slouken@3435
   805
    src = (Uint8*)pixels + (rect->h-1)*pitch;
slouken@3435
   806
    dst = (Uint8*)pixels;
slouken@3435
   807
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
   808
    rows = rect->h / 2;
slouken@3435
   809
    while (rows--) {
slouken@3435
   810
        SDL_memcpy(tmp, dst, length);
slouken@3435
   811
        SDL_memcpy(dst, src, length);
slouken@3435
   812
        SDL_memcpy(src, tmp, length);
slouken@3447
   813
        dst += pitch;
slouken@3447
   814
        src -= pitch;
slouken@3435
   815
    }
slouken@3435
   816
    SDL_stack_free(tmp);
slouken@3440
   817
slouken@3440
   818
    return 0;
slouken@3431
   819
}
slouken@3431
   820
slouken@1918
   821
static void
slouken@1918
   822
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   823
{
slouken@5147
   824
    GL_ActivateRenderer(renderer);
slouken@5147
   825
slouken@1918
   826
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   827
}
slouken@1918
   828
slouken@1918
   829
static void
slouken@1918
   830
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   831
{
slouken@1927
   832
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   833
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   834
slouken@5147
   835
    GL_ActivateRenderer(renderer);
slouken@5147
   836
slouken@1918
   837
    if (!data) {
slouken@1918
   838
        return;
slouken@1918
   839
    }
slouken@1918
   840
    if (data->texture) {
slouken@1927
   841
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
   842
    }
slouken@1920
   843
    if (data->pixels) {
slouken@1920
   844
        SDL_free(data->pixels);
slouken@1920
   845
    }
slouken@1918
   846
    SDL_free(data);
slouken@1918
   847
    texture->driverdata = NULL;
slouken@1918
   848
}
slouken@1918
   849
slouken@1975
   850
static void
slouken@1918
   851
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   852
{
slouken@1918
   853
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   854
slouken@1918
   855
    if (data) {
slouken@1920
   856
        if (data->context) {
bob@2328
   857
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
   858
            SDL_GL_DeleteContext(data->context);
slouken@1918
   859
        }
slouken@1918
   860
        SDL_free(data);
slouken@1918
   861
    }
slouken@1918
   862
    SDL_free(renderer);
slouken@1918
   863
}
slouken@1918
   864
slouken@5226
   865
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@1918
   866
slouken@1918
   867
/* vi: set ts=4 sw=4 expandtab: */