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