src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 11 Aug 2007 20:54:31 +0000
changeset 2222 926294b2bb4e
parent 1986 f4c65e3bfaed
child 2230 9b7d29d2432b
permissions -rw-r--r--
Emphasized the separation between SDL_Surface and SDL_Texture
- SDL_Surface is a system memory representation of pixel data
- SDL_Texture is a video memory representation of pixel data

The concept of SDL_Surface with SDL_HWSURFACE is no longer used.

Separated SDL_Texture types by usage rather than memory type
- SDL_TEXTUREACCESS_STATIC is for rarely changed pixel data,
can be placed in video memory.
- SDL_TEXTUREACCESS_STREAMING is for frequently changing pixel
data, usually placed in system memory or AGP memory.

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