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