src/render/opengl/SDL_render_gl.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5228 811beeb698f9
child 5230 8efa43b915be
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
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@1926
   300
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   301
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   302
    } else {
slouken@1927
   303
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   304
    }
slouken@2833
   305
    data->updateSize = SDL_TRUE;
slouken@1918
   306
slouken@1918
   307
    return renderer;
slouken@1918
   308
}
slouken@1918
   309
slouken@5147
   310
static SDL_GLContext SDL_CurrentContext = NULL;
slouken@5147
   311
slouken@1923
   312
static int
slouken@1923
   313
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   314
{
slouken@1923
   315
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
   316
    SDL_Window *window = renderer->window;
slouken@1923
   317
slouken@5147
   318
    if (SDL_CurrentContext != data->context) {
slouken@5147
   319
        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
slouken@5147
   320
            return -1;
slouken@5147
   321
        }
slouken@5147
   322
        SDL_CurrentContext = data->context;
slouken@1970
   323
    }
slouken@2833
   324
    if (data->updateSize) {
slouken@5154
   325
        int w, h;
slouken@5154
   326
slouken@5154
   327
        SDL_GetWindowSize(window, &w, &h);
slouken@2836
   328
        data->glMatrixMode(GL_PROJECTION);
slouken@2836
   329
        data->glLoadIdentity();
slouken@2836
   330
        data->glMatrixMode(GL_MODELVIEW);
slouken@2836
   331
        data->glLoadIdentity();
slouken@5154
   332
        data->glViewport(0, 0, w, h);
slouken@5154
   333
        data->glOrtho(0.0, (GLdouble) w, (GLdouble) h, 0.0, 0.0, 1.0);
slouken@2833
   334
        data->updateSize = SDL_FALSE;
slouken@2833
   335
    }
slouken@1970
   336
    return 0;
slouken@1970
   337
}
slouken@1970
   338
slouken@5147
   339
static void
slouken@5147
   340
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   341
{
slouken@1970
   342
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1970
   343
slouken@5147
   344
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
slouken@5147
   345
        /* Rebind the context to the window area and update matrices */
slouken@5147
   346
        SDL_CurrentContext = NULL;
slouken@5147
   347
        data->updateSize = SDL_TRUE;
slouken@5147
   348
    }
slouken@1923
   349
}
slouken@1923
   350
slouken@1922
   351
static __inline__ int
slouken@1922
   352
power_of_2(int input)
slouken@1922
   353
{
slouken@1922
   354
    int value = 1;
slouken@1922
   355
slouken@1922
   356
    while (value < input) {
slouken@1922
   357
        value <<= 1;
slouken@1922
   358
    }
slouken@1922
   359
    return value;
slouken@1922
   360
}
slouken@1922
   361
slouken@3433
   362
static __inline__ SDL_bool
slouken@3433
   363
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
slouken@3433
   364
               GLint* internalFormat, GLenum* format, GLenum* type)
slouken@3433
   365
{
slouken@3433
   366
    switch (pixel_format) {
slouken@3433
   367
    case SDL_PIXELFORMAT_ARGB8888:
slouken@5156
   368
        *internalFormat = GL_RGBA8;
slouken@3433
   369
        *format = GL_BGRA;
slouken@3433
   370
        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@3433
   371
        break;
slouken@3433
   372
    default:
slouken@3433
   373
        return SDL_FALSE;
slouken@3433
   374
    }
slouken@3433
   375
    return SDL_TRUE;
slouken@3433
   376
}
icculus@2835
   377
slouken@1918
   378
static int
slouken@1918
   379
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   380
{
slouken@1918
   381
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   382
    GL_TextureData *data;
slouken@1920
   383
    GLint internalFormat;
slouken@1920
   384
    GLenum format, type;
slouken@1922
   385
    int texture_w, texture_h;
slouken@1924
   386
    GLenum result;
slouken@1918
   387
slouken@5147
   388
    GL_ActivateRenderer(renderer);
slouken@5147
   389
slouken@3433
   390
    if (!convert_format(renderdata, texture->format, &internalFormat,
slouken@3433
   391
                        &format, &type)) {
slouken@4990
   392
        SDL_SetError("Texture format %s not supported by OpenGL",
slouken@4990
   393
                     SDL_GetPixelFormatName(texture->format));
slouken@1920
   394
        return -1;
slouken@1920
   395
    }
slouken@1920
   396
slouken@1920
   397
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   398
    if (!data) {
slouken@1918
   399
        SDL_OutOfMemory();
slouken@1918
   400
        return -1;
slouken@1918
   401
    }
slouken@1918
   402
slouken@2222
   403
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5156
   404
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2222
   405
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@2222
   406
        if (!data->pixels) {
slouken@2222
   407
            SDL_OutOfMemory();
slouken@2222
   408
            SDL_free(data);
slouken@2222
   409
            return -1;
slouken@2222
   410
        }
slouken@2222
   411
    }
slouken@2222
   412
slouken@1918
   413
    texture->driverdata = data;
slouken@1918
   414
slouken@1927
   415
    renderdata->glGetError();
slouken@1927
   416
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   417
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   418
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   419
        texture_w = texture->w;
slouken@1926
   420
        texture_h = texture->h;
icculus@2835
   421
        data->texw = (GLfloat) texture_w;
icculus@2835
   422
        data->texh = (GLfloat) texture_h;
slouken@1926
   423
    } else {
slouken@1926
   424
        data->type = GL_TEXTURE_2D;
slouken@1926
   425
        texture_w = power_of_2(texture->w);
slouken@1926
   426
        texture_h = power_of_2(texture->h);
icculus@2835
   427
        data->texw = (GLfloat) (texture->w) / texture_w;
slouken@1926
   428
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   429
    }
icculus@2835
   430
slouken@1920
   431
    data->format = format;
slouken@1920
   432
    data->formattype = type;
slouken@2884
   433
    renderdata->glEnable(data->type);
slouken@1927
   434
    renderdata->glBindTexture(data->type, data->texture);
slouken@2230
   435
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
slouken@5138
   436
                                GL_LINEAR);
slouken@2230
   437
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
slouken@5138
   438
                                GL_LINEAR);
slouken@2230
   439
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
slouken@2230
   440
                                GL_CLAMP_TO_EDGE);
slouken@2230
   441
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
slouken@2230
   442
                                GL_CLAMP_TO_EDGE);
slouken@2840
   443
#ifdef __MACOSX__
slouken@2230
   444
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
slouken@2230
   445
#define GL_TEXTURE_STORAGE_HINT_APPLE       0x85BC
slouken@2230
   446
#endif
slouken@2230
   447
#ifndef STORAGE_CACHED_APPLE
slouken@2230
   448
#define STORAGE_CACHED_APPLE                0x85BE
slouken@2230
   449
#endif
slouken@2230
   450
#ifndef STORAGE_SHARED_APPLE
slouken@2230
   451
#define STORAGE_SHARED_APPLE                0x85BF
slouken@2230
   452
#endif
slouken@2230
   453
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@2230
   454
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   455
                                    GL_STORAGE_SHARED_APPLE);
slouken@2230
   456
    } else {
slouken@2230
   457
        renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
slouken@2230
   458
                                    GL_STORAGE_CACHED_APPLE);
slouken@2230
   459
    }
bob@2295
   460
    if (texture->access == SDL_TEXTUREACCESS_STREAMING
bob@2295
   461
        && texture->format == SDL_PIXELFORMAT_ARGB8888) {
slouken@2230
   462
        renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
slouken@2230
   463
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   464
                                 texture_h, 0, format, type, data->pixels);
slouken@5156
   465
    }
slouken@5156
   466
    else
slouken@2809
   467
#endif
slouken@2230
   468
    {
slouken@2230
   469
        renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2230
   470
                                 texture_h, 0, format, type, NULL);
slouken@2230
   471
    }
slouken@3041
   472
    renderdata->glDisable(data->type);
slouken@1927
   473
    result = renderdata->glGetError();
slouken@1924
   474
    if (result != GL_NO_ERROR) {
slouken@1924
   475
        GL_SetError("glTexImage2D()", result);
slouken@1924
   476
        return -1;
slouken@1924
   477
    }
slouken@1918
   478
    return 0;
slouken@1918
   479
}
slouken@1918
   480
slouken@1918
   481
static int
slouken@1918
   482
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   483
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   484
{
slouken@1927
   485
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   486
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   487
    GLenum result;
slouken@1918
   488
slouken@5147
   489
    GL_ActivateRenderer(renderer);
slouken@5147
   490
slouken@1927
   491
    renderdata->glGetError();
slouken@5227
   492
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@5227
   493
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@5227
   494
                              (pitch / SDL_BYTESPERPIXEL(texture->format)));
slouken@2884
   495
    renderdata->glEnable(data->type);
slouken@1927
   496
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   497
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   498
                                rect->h, data->format, data->formattype,
slouken@1927
   499
                                pixels);
slouken@3041
   500
    renderdata->glDisable(data->type);
slouken@1927
   501
    result = renderdata->glGetError();
slouken@1924
   502
    if (result != GL_NO_ERROR) {
slouken@1924
   503
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   504
        return -1;
slouken@1924
   505
    }
slouken@1918
   506
    return 0;
slouken@1918
   507
}
slouken@1918
   508
slouken@1918
   509
static int
slouken@1918
   510
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   511
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1918
   512
{
slouken@1918
   513
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   514
slouken@5227
   515
    data->locked_rect = *rect;
slouken@5227
   516
    *pixels = 
slouken@1920
   517
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5156
   518
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   519
    *pitch = data->pitch;
slouken@1918
   520
    return 0;
slouken@1918
   521
}
slouken@1918
   522
slouken@1918
   523
static void
slouken@1918
   524
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   525
{
slouken@5156
   526
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@5227
   527
    const SDL_Rect *rect;
slouken@5227
   528
    void *pixels;
slouken@1918
   529
slouken@5227
   530
    rect = &data->locked_rect;
slouken@5227
   531
    pixels = 
slouken@5227
   532
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@5227
   533
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@5227
   534
    GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@1918
   535
}
slouken@1918
   536
slouken@2936
   537
static void
slouken@5224
   538
GL_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@5224
   539
{
slouken@5224
   540
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@5224
   541
slouken@5224
   542
    GL_ActivateRenderer(renderer);
slouken@5224
   543
slouken@5224
   544
    if (rect) {
slouken@5224
   545
        int w, h;
slouken@5224
   546
slouken@5224
   547
        SDL_GetWindowSize(renderer->window, &w, &h);
slouken@5224
   548
        data->glScissor(rect->x, (h-(rect->y+rect->h)), rect->w, rect->h);
slouken@5224
   549
        data->glEnable(GL_SCISSOR_TEST);
slouken@5224
   550
    } else {
slouken@5224
   551
        data->glDisable(GL_SCISSOR_TEST);
slouken@5224
   552
    }
slouken@5224
   553
}
slouken@5224
   554
slouken@5224
   555
static void
slouken@5140
   556
GL_SetBlendMode(GL_RenderData * data, int blendMode)
slouken@2936
   557
{
slouken@2936
   558
    if (blendMode != data->blendMode) {
slouken@2936
   559
        switch (blendMode) {
slouken@2936
   560
        case SDL_BLENDMODE_NONE:
slouken@2936
   561
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
   562
            data->glDisable(GL_BLEND);
slouken@2936
   563
            break;
slouken@2936
   564
        case SDL_BLENDMODE_BLEND:
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_MINUS_SRC_ALPHA);
slouken@2936
   568
            break;
slouken@2936
   569
        case SDL_BLENDMODE_ADD:
slouken@2936
   570
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@2936
   571
            data->glEnable(GL_BLEND);
slouken@2936
   572
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@2936
   573
            break;
slouken@5184
   574
        case SDL_BLENDMODE_MOD:
slouken@5184
   575
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@5184
   576
            data->glEnable(GL_BLEND);
slouken@5184
   577
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@5184
   578
            break;
slouken@2936
   579
        }
slouken@2936
   580
        data->blendMode = blendMode;
slouken@2936
   581
    }
slouken@2936
   582
}
slouken@2936
   583
slouken@1918
   584
static int
slouken@3596
   585
GL_RenderClear(SDL_Renderer * renderer)
slouken@3596
   586
{
slouken@3596
   587
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3596
   588
slouken@5147
   589
    GL_ActivateRenderer(renderer);
slouken@5147
   590
slouken@3596
   591
    data->glClearColor((GLfloat) renderer->r * inv255f,
slouken@3596
   592
                       (GLfloat) renderer->g * inv255f,
slouken@3596
   593
                       (GLfloat) renderer->b * inv255f,
slouken@3596
   594
                       (GLfloat) renderer->a * inv255f);
slouken@3596
   595
slouken@3596
   596
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@3596
   597
slouken@3596
   598
    return 0;
slouken@3596
   599
}
slouken@3596
   600
slouken@3596
   601
static int
slouken@3596
   602
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   603
                    int count)
slouken@2884
   604
{
slouken@2884
   605
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   606
    int i;
slouken@2884
   607
slouken@5147
   608
    GL_ActivateRenderer(renderer);
slouken@5147
   609
slouken@5140
   610
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   611
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2884
   612
slouken@2884
   613
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2884
   614
                    (GLfloat) renderer->g * inv255f,
slouken@2884
   615
                    (GLfloat) renderer->b * inv255f,
slouken@2884
   616
                    (GLfloat) renderer->a * inv255f);
slouken@2884
   617
slouken@2901
   618
    data->glBegin(GL_POINTS);
slouken@3536
   619
    for (i = 0; i < count; ++i) {
slouken@3536
   620
        data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   621
    }
slouken@2901
   622
    data->glEnd();
slouken@2901
   623
slouken@2901
   624
    return 0;
slouken@2901
   625
}
slouken@2901
   626
slouken@2901
   627
static int
slouken@3596
   628
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   629
                   int count)
slouken@2901
   630
{
slouken@2901
   631
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   632
    int i;
slouken@2901
   633
slouken@5147
   634
    GL_ActivateRenderer(renderer);
slouken@5147
   635
slouken@5140
   636
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   637
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2901
   638
slouken@2901
   639
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2901
   640
                    (GLfloat) renderer->g * inv255f,
slouken@2901
   641
                    (GLfloat) renderer->b * inv255f,
slouken@2901
   642
                    (GLfloat) renderer->a * inv255f);
slouken@2901
   643
slouken@3536
   644
    if (count > 2 && 
slouken@3536
   645
        points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
slouken@3536
   646
        data->glBegin(GL_LINE_LOOP);
slouken@3536
   647
        /* GL_LINE_LOOP takes care of the final segment */
slouken@3536
   648
        --count;
slouken@3536
   649
        for (i = 0; i < count; ++i) {
slouken@3536
   650
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   651
        }
slouken@3536
   652
        data->glEnd();
slouken@3536
   653
    } else {
slouken@5086
   654
#if defined(__APPLE__) || defined(__WIN32__)
aschiffler@4910
   655
#else
slouken@4905
   656
        int x1, y1, x2, y2;
aschiffler@4910
   657
#endif
slouken@4905
   658
slouken@3536
   659
        data->glBegin(GL_LINE_STRIP);
slouken@3536
   660
        for (i = 0; i < count; ++i) {
slouken@3536
   661
            data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
slouken@3536
   662
        }
slouken@3536
   663
        data->glEnd();
slouken@3474
   664
slouken@3536
   665
        /* The line is half open, so we need one more point to complete it.
slouken@3536
   666
         * http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
slouken@3536
   667
         * If we have to, we can use vertical line and horizontal line textures
slouken@3536
   668
         * for vertical and horizontal lines, and then create custom textures
slouken@3536
   669
         * for diagonal lines and software render those.  It's terrible, but at
slouken@3536
   670
         * least it would be pixel perfect.
slouken@3536
   671
         */
slouken@3536
   672
        data->glBegin(GL_POINTS);
slouken@5086
   673
#if defined(__APPLE__) || defined(__WIN32__)
slouken@3536
   674
        /* Mac OS X and Windows seem to always leave the second point open */
slouken@3536
   675
        data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
slouken@3474
   676
#else
slouken@3536
   677
        /* Linux seems to leave the right-most or bottom-most point open */
slouken@4905
   678
        x1 = points[0].x;
slouken@4905
   679
        y1 = points[0].y;
slouken@4905
   680
        x2 = points[count-1].x;
slouken@4905
   681
        y2 = points[count-1].y;
slouken@3536
   682
slouken@3536
   683
        if (x1 > x2) {
slouken@3536
   684
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   685
        } else if (x2 > x1) {
slouken@3536
   686
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   687
        } else if (y1 > y2) {
slouken@3536
   688
            data->glVertex2f(0.5f + x1, 0.5f + y1);
slouken@3536
   689
        } else if (y2 > y1) {
slouken@3536
   690
            data->glVertex2f(0.5f + x2, 0.5f + y2);
slouken@3536
   691
        }
slouken@3536
   692
#endif
slouken@3536
   693
        data->glEnd();
slouken@3474
   694
    }
slouken@3455
   695
slouken@1918
   696
    return 0;
slouken@1918
   697
}
slouken@1918
   698
slouken@1918
   699
static int
slouken@3596
   700
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
slouken@2925
   701
{
slouken@2925
   702
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3536
   703
    int i;
slouken@2925
   704
slouken@5147
   705
    GL_ActivateRenderer(renderer);
slouken@5147
   706
slouken@5140
   707
    GL_SetBlendMode(data, renderer->blendMode);
slouken@5228
   708
    GL_SelectShader(data->shaders, SHADER_SOLID);
slouken@2936
   709
slouken@2925
   710
    data->glColor4f((GLfloat) renderer->r * inv255f,
slouken@2925
   711
                    (GLfloat) renderer->g * inv255f,
slouken@2925
   712
                    (GLfloat) renderer->b * inv255f,
slouken@2925
   713
                    (GLfloat) renderer->a * inv255f);
slouken@2936
   714
slouken@3536
   715
    for (i = 0; i < count; ++i) {
slouken@3536
   716
        const SDL_Rect *rect = rects[i];
slouken@3536
   717
slouken@3536
   718
        data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
slouken@3536
   719
    }
slouken@2925
   720
slouken@2925
   721
    return 0;
slouken@2925
   722
}
slouken@2925
   723
slouken@2925
   724
static int
slouken@1918
   725
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   726
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1918
   727
{
slouken@1918
   728
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   729
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   730
    int minx, miny, maxx, maxy;
slouken@1918
   731
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   732
slouken@5147
   733
    GL_ActivateRenderer(renderer);
slouken@5147
   734
slouken@1918
   735
    minx = dstrect->x;
slouken@1918
   736
    miny = dstrect->y;
slouken@1918
   737
    maxx = dstrect->x + dstrect->w;
slouken@1918
   738
    maxy = dstrect->y + dstrect->h;
slouken@1918
   739
slouken@1918
   740
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   741
    minu *= texturedata->texw;
slouken@1918
   742
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   743
    maxu *= texturedata->texw;
slouken@1918
   744
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   745
    minv *= texturedata->texh;
slouken@1918
   746
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   747
    maxv *= texturedata->texh;
slouken@1918
   748
slouken@2884
   749
    data->glEnable(texturedata->type);
slouken@1927
   750
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   751
slouken@1985
   752
    if (texture->modMode) {
slouken@1985
   753
        data->glColor4f((GLfloat) texture->r * inv255f,
slouken@1985
   754
                        (GLfloat) texture->g * inv255f,
slouken@1985
   755
                        (GLfloat) texture->b * inv255f,
slouken@1985
   756
                        (GLfloat) texture->a * inv255f);
slouken@1985
   757
    } else {
slouken@1985
   758
        data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
slouken@1985
   759
    }
slouken@1985
   760
slouken@5140
   761
    GL_SetBlendMode(data, texture->blendMode);
slouken@5228
   762
    GL_SelectShader(data->shaders, SHADER_RGB);
slouken@1918
   763
slouken@1927
   764
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
   765
    data->glTexCoord2f(minu, minv);
slouken@3472
   766
    data->glVertex2f((GLfloat) minx, (GLfloat) miny);
slouken@1927
   767
    data->glTexCoord2f(maxu, minv);
slouken@3472
   768
    data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
slouken@1927
   769
    data->glTexCoord2f(minu, maxv);
slouken@3472
   770
    data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
slouken@1927
   771
    data->glTexCoord2f(maxu, maxv);
slouken@3472
   772
    data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
slouken@1927
   773
    data->glEnd();
slouken@1918
   774
slouken@2884
   775
    data->glDisable(texturedata->type);
slouken@2884
   776
slouken@1918
   777
    return 0;
slouken@1918
   778
}
slouken@1918
   779
slouken@3431
   780
static int
slouken@3431
   781
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   782
                    Uint32 pixel_format, void * pixels, int pitch)
slouken@3431
   783
{
slouken@3433
   784
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@3685
   785
    SDL_Window *window = renderer->window;
slouken@3433
   786
    GLint internalFormat;
slouken@3433
   787
    GLenum format, type;
slouken@3435
   788
    Uint8 *src, *dst, *tmp;
slouken@5154
   789
    int w, h, length, rows;
slouken@3433
   790
slouken@5147
   791
    GL_ActivateRenderer(renderer);
slouken@5147
   792
slouken@3433
   793
    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
slouken@3435
   794
        /* FIXME: Do a temp copy to a format that is supported */
slouken@3433
   795
        SDL_SetError("Unsupported pixel format");
slouken@3433
   796
        return -1;
slouken@3433
   797
    }
slouken@3433
   798
slouken@5154
   799
    SDL_GetWindowSize(window, &w, &h);
slouken@5154
   800
slouken@3446
   801
    data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
slouken@3446
   802
    data->glPixelStorei(GL_PACK_ROW_LENGTH,
slouken@5156
   803
                        (pitch / SDL_BYTESPERPIXEL(pixel_format)));
slouken@3433
   804
slouken@5154
   805
    data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
slouken@3435
   806
                       format, type, pixels);
slouken@3435
   807
slouken@3435
   808
    /* Flip the rows to be top-down */
slouken@5156
   809
    length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
slouken@3435
   810
    src = (Uint8*)pixels + (rect->h-1)*pitch;
slouken@3435
   811
    dst = (Uint8*)pixels;
slouken@3435
   812
    tmp = SDL_stack_alloc(Uint8, length);
slouken@3435
   813
    rows = rect->h / 2;
slouken@3435
   814
    while (rows--) {
slouken@3435
   815
        SDL_memcpy(tmp, dst, length);
slouken@3435
   816
        SDL_memcpy(dst, src, length);
slouken@3435
   817
        SDL_memcpy(src, tmp, length);
slouken@3447
   818
        dst += pitch;
slouken@3447
   819
        src -= pitch;
slouken@3435
   820
    }
slouken@3435
   821
    SDL_stack_free(tmp);
slouken@3440
   822
slouken@3440
   823
    return 0;
slouken@3431
   824
}
slouken@3431
   825
slouken@1918
   826
static void
slouken@1918
   827
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   828
{
slouken@5147
   829
    GL_ActivateRenderer(renderer);
slouken@5147
   830
slouken@1918
   831
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   832
}
slouken@1918
   833
slouken@1918
   834
static void
slouken@1918
   835
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   836
{
slouken@1927
   837
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   838
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   839
slouken@5147
   840
    GL_ActivateRenderer(renderer);
slouken@5147
   841
slouken@1918
   842
    if (!data) {
slouken@1918
   843
        return;
slouken@1918
   844
    }
slouken@1918
   845
    if (data->texture) {
slouken@1927
   846
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
   847
    }
slouken@1920
   848
    if (data->pixels) {
slouken@1920
   849
        SDL_free(data->pixels);
slouken@1920
   850
    }
slouken@1918
   851
    SDL_free(data);
slouken@1918
   852
    texture->driverdata = NULL;
slouken@1918
   853
}
slouken@1918
   854
slouken@1975
   855
static void
slouken@1918
   856
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   857
{
slouken@1918
   858
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   859
slouken@1918
   860
    if (data) {
slouken@1920
   861
        if (data->context) {
bob@2328
   862
            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1920
   863
            SDL_GL_DeleteContext(data->context);
slouken@1918
   864
        }
slouken@1918
   865
        SDL_free(data);
slouken@1918
   866
    }
slouken@1918
   867
    SDL_free(renderer);
slouken@1918
   868
}
slouken@1918
   869
slouken@5226
   870
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@1918
   871
slouken@1918
   872
/* vi: set ts=4 sw=4 expandtab: */