src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Jul 2006 23:04:41 +0000
changeset 1927 aeb8263d377a
parent 1926 307355678142
child 1928 861bc36f0ab3
permissions -rw-r--r--
OpenGL renderer is feature complete!
Dynamically load GL functions in the OpenGL renderer.
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@1918
    24
#if SDL_VIDEO_OPENGL
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@1918
    35
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1923
    36
static int GL_ActivateRenderer(SDL_Renderer * renderer);
slouken@1918
    37
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    38
static int GL_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    39
                                SDL_Texture * texture,
slouken@1918
    40
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    41
                                int ncolors);
slouken@1918
    42
static int GL_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    43
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    44
                                int firstcolor, int ncolors);
slouken@1918
    45
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    46
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    47
                            int pitch);
slouken@1918
    48
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    49
                          const SDL_Rect * rect, int markDirty,
slouken@1918
    50
                          void **pixels, int *pitch);
slouken@1918
    51
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    52
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    53
                            int numrects, const SDL_Rect * rects);
slouken@1918
    54
static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1918
    55
                         Uint32 color);
slouken@1918
    56
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    57
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
    58
                         int blendMode, int scaleMode);
slouken@1918
    59
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    60
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    61
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    62
slouken@1918
    63
slouken@1918
    64
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    65
    GL_CreateRenderer,
slouken@1918
    66
    {
slouken@1918
    67
     "opengl",
slouken@1918
    68
     (SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync |
slouken@1918
    69
      SDL_Renderer_Accelerated),
slouken@1918
    70
     (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask |
slouken@1918
    71
      SDL_TextureBlendMode_Blend | SDL_TextureBlendMode_Add |
slouken@1918
    72
      SDL_TextureBlendMode_Mod),
slouken@1918
    73
     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast |
slouken@1921
    74
      SDL_TextureScaleMode_Slow),
slouken@1924
    75
     16,
slouken@1918
    76
     {
slouken@1920
    77
      SDL_PixelFormat_Index1LSB,
slouken@1920
    78
      SDL_PixelFormat_Index1MSB,
slouken@1918
    79
      SDL_PixelFormat_Index8,
slouken@1918
    80
      SDL_PixelFormat_RGB332,
slouken@1918
    81
      SDL_PixelFormat_RGB444,
slouken@1918
    82
      SDL_PixelFormat_RGB555,
slouken@1918
    83
      SDL_PixelFormat_ARGB4444,
slouken@1918
    84
      SDL_PixelFormat_ARGB1555,
slouken@1918
    85
      SDL_PixelFormat_RGB565,
slouken@1920
    86
      SDL_PixelFormat_RGB24,
slouken@1920
    87
      SDL_PixelFormat_BGR24,
slouken@1918
    88
      SDL_PixelFormat_RGB888,
slouken@1920
    89
      SDL_PixelFormat_BGR888,
slouken@1918
    90
      SDL_PixelFormat_ARGB8888,
slouken@1920
    91
      SDL_PixelFormat_ABGR8888,
slouken@1924
    92
      SDL_PixelFormat_ARGB2101010},
slouken@1918
    93
     0,
slouken@1918
    94
     0}
slouken@1918
    95
};
slouken@1918
    96
slouken@1918
    97
typedef struct
slouken@1918
    98
{
slouken@1918
    99
    SDL_GLContext context;
slouken@1926
   100
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@1927
   101
    int blendMode;
slouken@1927
   102
    int scaleMode;
slouken@1927
   103
slouken@1927
   104
    /* OpenGL functions */
slouken@1927
   105
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
slouken@1927
   106
#include "SDL_glfuncs.h"
slouken@1927
   107
#undef SDL_PROC
slouken@1918
   108
} GL_RenderData;
slouken@1918
   109
slouken@1918
   110
typedef struct
slouken@1918
   111
{
slouken@1918
   112
    GLuint texture;
slouken@1920
   113
    GLenum type;
slouken@1918
   114
    GLfloat texw;
slouken@1918
   115
    GLfloat texh;
slouken@1920
   116
    GLenum format;
slouken@1920
   117
    GLenum formattype;
slouken@1918
   118
    void *pixels;
slouken@1918
   119
    int pitch;
slouken@1920
   120
    SDL_DirtyRectList dirty;
slouken@1918
   121
} GL_TextureData;
slouken@1918
   122
slouken@1918
   123
slouken@1924
   124
static void
slouken@1924
   125
GL_SetError(const char *prefix, GLenum result)
slouken@1924
   126
{
slouken@1924
   127
    const char *error;
slouken@1924
   128
slouken@1924
   129
    switch (result) {
slouken@1924
   130
    case GL_NO_ERROR:
slouken@1924
   131
        error = "GL_NO_ERROR";
slouken@1924
   132
        break;
slouken@1924
   133
    case GL_INVALID_ENUM:
slouken@1924
   134
        error = "GL_INVALID_ENUM";
slouken@1924
   135
        break;
slouken@1924
   136
    case GL_INVALID_VALUE:
slouken@1924
   137
        error = "GL_INVALID_VALUE";
slouken@1924
   138
        break;
slouken@1924
   139
    case GL_INVALID_OPERATION:
slouken@1924
   140
        error = "GL_INVALID_OPERATION";
slouken@1924
   141
        break;
slouken@1924
   142
    case GL_STACK_OVERFLOW:
slouken@1924
   143
        error = "GL_STACK_OVERFLOW";
slouken@1924
   144
        break;
slouken@1924
   145
    case GL_STACK_UNDERFLOW:
slouken@1924
   146
        error = "GL_STACK_UNDERFLOW";
slouken@1924
   147
        break;
slouken@1924
   148
    case GL_OUT_OF_MEMORY:
slouken@1924
   149
        error = "GL_OUT_OF_MEMORY";
slouken@1924
   150
        break;
slouken@1924
   151
    case GL_TABLE_TOO_LARGE:
slouken@1924
   152
        error = "GL_TABLE_TOO_LARGE";
slouken@1924
   153
        break;
slouken@1924
   154
    default:
slouken@1924
   155
        error = "UNKNOWN";
slouken@1924
   156
        break;
slouken@1924
   157
    }
slouken@1924
   158
    SDL_SetError("%s: %s", prefix, error);
slouken@1924
   159
}
slouken@1924
   160
slouken@1927
   161
static int
slouken@1927
   162
GL_LoadFunctions(GL_RenderData * data)
slouken@1927
   163
{
slouken@1927
   164
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
slouken@1927
   165
#define __SDL_NOGETPROCADDR__
slouken@1927
   166
#elif defined(__MINT__)
slouken@1927
   167
#define __SDL_NOGETPROCADDR__
slouken@1927
   168
#endif
slouken@1927
   169
#ifdef __SDL_NOGETPROCADDR__
slouken@1927
   170
#define SDL_PROC(ret,func,params) data->func=func;
slouken@1927
   171
#else
slouken@1927
   172
#define SDL_PROC(ret,func,params) \
slouken@1927
   173
    do { \
slouken@1927
   174
        data->func = SDL_GL_GetProcAddress(#func); \
slouken@1927
   175
        if ( ! data->func ) { \
slouken@1927
   176
            SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
slouken@1927
   177
            return -1; \
slouken@1927
   178
        } \
slouken@1927
   179
    } while ( 0 );
slouken@1927
   180
#endif /* __SDL_NOGETPROCADDR__ */
slouken@1927
   181
slouken@1927
   182
#include "SDL_glfuncs.h"
slouken@1927
   183
#undef SDL_PROC
slouken@1927
   184
    return 0;
slouken@1927
   185
}
slouken@1927
   186
slouken@1918
   187
void
slouken@1918
   188
GL_AddRenderDriver(_THIS)
slouken@1918
   189
{
slouken@1920
   190
    if (_this->GL_CreateContext) {
slouken@1918
   191
        SDL_AddRenderDriver(0, &GL_RenderDriver);
slouken@1918
   192
    }
slouken@1918
   193
}
slouken@1918
   194
slouken@1918
   195
SDL_Renderer *
slouken@1918
   196
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   197
{
slouken@1918
   198
    SDL_Renderer *renderer;
slouken@1918
   199
    GL_RenderData *data;
slouken@1918
   200
slouken@1918
   201
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1924
   202
        window->flags |= SDL_WINDOW_OPENGL;
slouken@1924
   203
        if (SDL_RecreateWindow(window) < 0) {
slouken@1924
   204
            return NULL;
slouken@1924
   205
        }
slouken@1918
   206
    }
slouken@1918
   207
slouken@1920
   208
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   209
    if (!renderer) {
slouken@1918
   210
        SDL_OutOfMemory();
slouken@1918
   211
        return NULL;
slouken@1918
   212
    }
slouken@1918
   213
slouken@1920
   214
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   215
    if (!data) {
slouken@1918
   216
        GL_DestroyRenderer(renderer);
slouken@1918
   217
        SDL_OutOfMemory();
slouken@1918
   218
        return NULL;
slouken@1918
   219
    }
slouken@1918
   220
slouken@1923
   221
    renderer->ActivateRenderer = GL_ActivateRenderer;
slouken@1918
   222
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   223
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   224
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1918
   225
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   226
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   227
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   228
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@1918
   229
    renderer->RenderFill = GL_RenderFill;
slouken@1918
   230
    renderer->RenderCopy = GL_RenderCopy;
slouken@1918
   231
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   232
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   233
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   234
    renderer->info = GL_RenderDriver.info;
slouken@1918
   235
    renderer->window = window->id;
slouken@1918
   236
    renderer->driverdata = data;
slouken@1918
   237
slouken@1918
   238
    renderer->info.flags =
slouken@1918
   239
        (SDL_Renderer_PresentDiscard | SDL_Renderer_Accelerated);
slouken@1918
   240
slouken@1927
   241
    if (GL_LoadFunctions(data) < 0) {
slouken@1927
   242
        GL_DestroyRenderer(renderer);
slouken@1927
   243
        return NULL;
slouken@1927
   244
    }
slouken@1927
   245
slouken@1918
   246
    data->context = SDL_GL_CreateContext(window->id);
slouken@1918
   247
    if (!data->context) {
slouken@1918
   248
        GL_DestroyRenderer(renderer);
slouken@1918
   249
        return NULL;
slouken@1918
   250
    }
slouken@1918
   251
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1918
   252
        GL_DestroyRenderer(renderer);
slouken@1918
   253
        return NULL;
slouken@1918
   254
    }
slouken@1918
   255
slouken@1918
   256
    if (flags & SDL_Renderer_PresentVSync) {
slouken@1918
   257
        SDL_GL_SetSwapInterval(1);
slouken@1918
   258
    } else {
slouken@1918
   259
        SDL_GL_SetSwapInterval(0);
slouken@1918
   260
    }
slouken@1918
   261
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1918
   262
        renderer->info.flags |= SDL_Renderer_PresentVSync;
slouken@1918
   263
    }
slouken@1918
   264
slouken@1927
   265
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE,
slouken@1927
   266
                        &renderer->info.max_texture_width);
slouken@1927
   267
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE,
slouken@1927
   268
                        &renderer->info.max_texture_height);
slouken@1920
   269
slouken@1926
   270
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@1926
   271
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@1926
   272
        data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@1926
   273
    }
slouken@1920
   274
slouken@1918
   275
    /* Set up parameters for rendering */
slouken@1927
   276
    data->blendMode = -1;
slouken@1927
   277
    data->scaleMode = -1;
slouken@1927
   278
    data->glDisable(GL_DEPTH_TEST);
slouken@1927
   279
    data->glDisable(GL_CULL_FACE);
slouken@1926
   280
    if (data->GL_ARB_texture_rectangle_supported) {
slouken@1927
   281
        data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1926
   282
    } else {
slouken@1927
   283
        data->glEnable(GL_TEXTURE_2D);
slouken@1926
   284
    }
slouken@1927
   285
    data->glMatrixMode(GL_PROJECTION);
slouken@1927
   286
    data->glLoadIdentity();
slouken@1927
   287
    data->glMatrixMode(GL_MODELVIEW);
slouken@1927
   288
    data->glLoadIdentity();
slouken@1927
   289
    data->glViewport(0, 0, window->w, window->h);
slouken@1927
   290
    data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0, 0.0,
slouken@1927
   291
                  1.0);
slouken@1918
   292
slouken@1918
   293
    return renderer;
slouken@1918
   294
}
slouken@1918
   295
slouken@1923
   296
static int
slouken@1923
   297
GL_ActivateRenderer(SDL_Renderer * renderer)
slouken@1923
   298
{
slouken@1923
   299
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1923
   300
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1923
   301
slouken@1923
   302
    return SDL_GL_MakeCurrent(window->id, data->context);
slouken@1923
   303
}
slouken@1923
   304
slouken@1922
   305
static __inline__ int
slouken@1922
   306
power_of_2(int input)
slouken@1922
   307
{
slouken@1922
   308
    int value = 1;
slouken@1922
   309
slouken@1922
   310
    while (value < input) {
slouken@1922
   311
        value <<= 1;
slouken@1922
   312
    }
slouken@1922
   313
    return value;
slouken@1922
   314
}
slouken@1922
   315
slouken@1918
   316
static int
slouken@1918
   317
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   318
{
slouken@1918
   319
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   320
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   321
    GL_TextureData *data;
slouken@1920
   322
    GLint internalFormat;
slouken@1920
   323
    GLenum format, type;
slouken@1922
   324
    int texture_w, texture_h;
slouken@1924
   325
    GLenum result;
slouken@1918
   326
slouken@1920
   327
    switch (texture->format) {
slouken@1920
   328
    case SDL_PixelFormat_Index1LSB:
slouken@1920
   329
    case SDL_PixelFormat_Index1MSB:
slouken@1920
   330
        internalFormat = GL_RGB;
slouken@1920
   331
        format = GL_COLOR_INDEX;
slouken@1920
   332
        type = GL_BITMAP;
slouken@1920
   333
        break;
slouken@1920
   334
    case SDL_PixelFormat_Index8:
slouken@1920
   335
        internalFormat = GL_RGB;
slouken@1920
   336
        format = GL_COLOR_INDEX;
slouken@1920
   337
        type = GL_UNSIGNED_BYTE;
slouken@1920
   338
        break;
slouken@1920
   339
    case SDL_PixelFormat_RGB332:
slouken@1920
   340
        internalFormat = GL_R3_G3_B2;
slouken@1920
   341
        format = GL_RGB;
slouken@1920
   342
        type = GL_UNSIGNED_BYTE_3_3_2;
slouken@1920
   343
        break;
slouken@1920
   344
    case SDL_PixelFormat_RGB444:
slouken@1920
   345
        internalFormat = GL_RGB4;
slouken@1920
   346
        format = GL_RGB;
slouken@1920
   347
        type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@1920
   348
        break;
slouken@1920
   349
    case SDL_PixelFormat_RGB555:
slouken@1920
   350
        internalFormat = GL_RGB5;
slouken@1920
   351
        format = GL_RGB;
slouken@1920
   352
        type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@1920
   353
        break;
slouken@1920
   354
    case SDL_PixelFormat_ARGB4444:
slouken@1920
   355
        internalFormat = GL_RGBA4;
slouken@1920
   356
        format = GL_BGRA;
slouken@1920
   357
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@1920
   358
        break;
slouken@1920
   359
    case SDL_PixelFormat_ARGB1555:
slouken@1920
   360
        internalFormat = GL_RGB5_A1;
slouken@1920
   361
        format = GL_BGRA;
slouken@1920
   362
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@1920
   363
        break;
slouken@1920
   364
    case SDL_PixelFormat_RGB565:
slouken@1920
   365
        internalFormat = GL_RGB8;
slouken@1920
   366
        format = GL_RGB;
slouken@1920
   367
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1920
   368
        break;
slouken@1920
   369
    case SDL_PixelFormat_RGB24:
slouken@1920
   370
        internalFormat = GL_RGB8;
slouken@1920
   371
        format = GL_RGB;
slouken@1920
   372
        type = GL_UNSIGNED_BYTE;
slouken@1920
   373
        break;
slouken@1920
   374
    case SDL_PixelFormat_RGB888:
slouken@1920
   375
        internalFormat = GL_RGB8;
slouken@1924
   376
        format = GL_BGRA;
slouken@1924
   377
        type = GL_UNSIGNED_BYTE;
slouken@1920
   378
        break;
slouken@1920
   379
    case SDL_PixelFormat_BGR24:
slouken@1920
   380
        internalFormat = GL_RGB8;
slouken@1920
   381
        format = GL_BGR;
slouken@1920
   382
        type = GL_UNSIGNED_BYTE;
slouken@1920
   383
        break;
slouken@1920
   384
    case SDL_PixelFormat_BGR888:
slouken@1920
   385
        internalFormat = GL_RGB8;
slouken@1924
   386
        format = GL_RGBA;
slouken@1924
   387
        type = GL_UNSIGNED_BYTE;
slouken@1920
   388
        break;
slouken@1920
   389
    case SDL_PixelFormat_ARGB8888:
slouken@1920
   390
        internalFormat = GL_RGBA8;
slouken@1920
   391
        format = GL_BGRA;
slouken@1924
   392
        type = GL_UNSIGNED_BYTE;
slouken@1920
   393
        break;
slouken@1920
   394
    case SDL_PixelFormat_ABGR8888:
slouken@1920
   395
        internalFormat = GL_RGBA8;
slouken@1920
   396
        format = GL_RGBA;
slouken@1924
   397
        type = GL_UNSIGNED_BYTE;
slouken@1920
   398
        break;
slouken@1920
   399
    case SDL_PixelFormat_ARGB2101010:
slouken@1920
   400
        internalFormat = GL_RGB10_A2;
slouken@1920
   401
        format = GL_BGRA;
slouken@1920
   402
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@1920
   403
        break;
slouken@1920
   404
    default:
slouken@1920
   405
        SDL_SetError("Unsupported texture format");
slouken@1920
   406
        return -1;
slouken@1920
   407
    }
slouken@1920
   408
slouken@1920
   409
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   410
    if (!data) {
slouken@1918
   411
        SDL_OutOfMemory();
slouken@1918
   412
        return -1;
slouken@1918
   413
    }
slouken@1918
   414
slouken@1918
   415
    texture->driverdata = data;
slouken@1918
   416
slouken@1927
   417
    renderdata->glGetError();
slouken@1927
   418
    renderdata->glGenTextures(1, &data->texture);
slouken@1926
   419
    if (renderdata->GL_ARB_texture_rectangle_supported) {
slouken@1926
   420
        data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1926
   421
        texture_w = texture->w;
slouken@1926
   422
        texture_h = texture->h;
slouken@1926
   423
        data->texw = (GLfloat) texture->w;
slouken@1926
   424
        data->texh = (GLfloat) texture->h;
slouken@1926
   425
    } else {
slouken@1926
   426
        data->type = GL_TEXTURE_2D;
slouken@1926
   427
        texture_w = power_of_2(texture->w);
slouken@1926
   428
        texture_h = power_of_2(texture->h);
slouken@1926
   429
        data->texw = (GLfloat) texture->w / texture_w;
slouken@1926
   430
        data->texh = (GLfloat) texture->h / texture_h;
slouken@1926
   431
    }
slouken@1920
   432
    data->format = format;
slouken@1920
   433
    data->formattype = type;
slouken@1927
   434
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   435
    renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@1927
   436
                             texture_h, 0, format, type, NULL);
slouken@1927
   437
    result = renderdata->glGetError();
slouken@1924
   438
    if (result != GL_NO_ERROR) {
slouken@1924
   439
        GL_SetError("glTexImage2D()", result);
slouken@1924
   440
        return -1;
slouken@1924
   441
    }
slouken@1918
   442
    return 0;
slouken@1918
   443
}
slouken@1918
   444
slouken@1918
   445
static int
slouken@1918
   446
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   447
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   448
{
slouken@1918
   449
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   450
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   451
slouken@1918
   452
    return 0;
slouken@1918
   453
}
slouken@1918
   454
slouken@1918
   455
static int
slouken@1918
   456
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   457
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   458
{
slouken@1918
   459
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   460
slouken@1918
   461
    return 0;
slouken@1918
   462
}
slouken@1918
   463
slouken@1924
   464
static void
slouken@1927
   465
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
slouken@1927
   466
                   int pitch)
slouken@1924
   467
{
slouken@1924
   468
    if (texture->format == SDL_PixelFormat_Index1LSB) {
slouken@1927
   469
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
slouken@1924
   470
    } else if (texture->format == SDL_PixelFormat_Index1MSB) {
slouken@1927
   471
        renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
slouken@1924
   472
    }
slouken@1927
   473
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
slouken@1927
   474
    renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@1927
   475
                              pitch / SDL_BYTESPERPIXEL(texture->format));
slouken@1924
   476
}
slouken@1924
   477
slouken@1918
   478
static int
slouken@1918
   479
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   480
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   481
{
slouken@1927
   482
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   483
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1924
   484
    GLenum result;
slouken@1918
   485
slouken@1927
   486
    renderdata->glGetError();
slouken@1927
   487
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@1927
   488
    renderdata->glBindTexture(data->type, data->texture);
slouken@1927
   489
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
slouken@1927
   490
                                rect->h, data->format, data->formattype,
slouken@1927
   491
                                pixels);
slouken@1927
   492
    result = renderdata->glGetError();
slouken@1924
   493
    if (result != GL_NO_ERROR) {
slouken@1924
   494
        GL_SetError("glTexSubImage2D()", result);
slouken@1924
   495
        return -1;
slouken@1924
   496
    }
slouken@1918
   497
    return 0;
slouken@1918
   498
}
slouken@1918
   499
slouken@1918
   500
static int
slouken@1918
   501
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   502
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   503
               int *pitch)
slouken@1918
   504
{
slouken@1918
   505
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   506
slouken@1920
   507
    if (!data->pixels) {
slouken@1920
   508
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   509
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@1920
   510
        if (!data->pixels) {
slouken@1920
   511
            SDL_OutOfMemory();
slouken@1920
   512
            return -1;
slouken@1920
   513
        }
slouken@1918
   514
    }
slouken@1918
   515
slouken@1920
   516
    if (markDirty) {
slouken@1920
   517
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
   518
    }
slouken@1918
   519
slouken@1920
   520
    *pixels =
slouken@1920
   521
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@1920
   522
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   523
    *pitch = data->pitch;
slouken@1918
   524
    return 0;
slouken@1918
   525
}
slouken@1918
   526
slouken@1918
   527
static void
slouken@1918
   528
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   529
{
slouken@1918
   530
}
slouken@1918
   531
slouken@1918
   532
static void
slouken@1918
   533
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
   534
                const SDL_Rect * rects)
slouken@1918
   535
{
slouken@1918
   536
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   537
    int i;
slouken@1918
   538
slouken@1918
   539
    for (i = 0; i < numrects; ++i) {
slouken@1920
   540
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
   541
    }
slouken@1918
   542
}
slouken@1918
   543
slouken@1918
   544
static int
slouken@1918
   545
GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1918
   546
{
slouken@1918
   547
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   548
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   549
    GLclampf r, g, b, a;
slouken@1918
   550
slouken@1918
   551
    a = ((GLclampf) ((color >> 24) & 0xFF)) / 255.0f;
slouken@1918
   552
    r = ((GLclampf) ((color >> 16) & 0xFF)) / 255.0f;
slouken@1918
   553
    g = ((GLclampf) ((color >> 8) & 0xFF)) / 255.0f;
slouken@1918
   554
    b = ((GLclampf) (color & 0xFF)) / 255.0f;
slouken@1918
   555
slouken@1927
   556
    data->glClearColor(r, g, b, a);
slouken@1927
   557
    data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
slouken@1927
   558
    data->glClear(GL_COLOR_BUFFER_BIT);
slouken@1927
   559
    data->glViewport(0, 0, window->w, window->h);
slouken@1918
   560
    return 0;
slouken@1918
   561
}
slouken@1918
   562
slouken@1918
   563
static int
slouken@1918
   564
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   565
              const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
   566
              int blendMode, int scaleMode)
slouken@1918
   567
{
slouken@1918
   568
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   569
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   570
    int minx, miny, maxx, maxy;
slouken@1918
   571
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   572
slouken@1920
   573
    if (texturedata->dirty.count > 0) {
slouken@1920
   574
        SDL_DirtyRect *dirty;
slouken@1920
   575
        void *pixels;
slouken@1920
   576
        int bpp = SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   577
        int pitch = texturedata->pitch;
slouken@1920
   578
slouken@1927
   579
        SetupTextureUpdate(data, texture, pitch);
slouken@1927
   580
        data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
   581
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
   582
            SDL_Rect *rect = &dirty->rect;
slouken@1920
   583
            pixels =
slouken@1920
   584
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
   585
                          rect->x * bpp);
slouken@1927
   586
            data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
slouken@1927
   587
                                  rect->w, rect->h, texturedata->format,
slouken@1927
   588
                                  texturedata->formattype, pixels);
slouken@1920
   589
        }
slouken@1920
   590
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
   591
    }
slouken@1920
   592
slouken@1918
   593
    minx = dstrect->x;
slouken@1918
   594
    miny = dstrect->y;
slouken@1918
   595
    maxx = dstrect->x + dstrect->w;
slouken@1918
   596
    maxy = dstrect->y + dstrect->h;
slouken@1918
   597
slouken@1918
   598
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   599
    minu *= texturedata->texw;
slouken@1918
   600
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   601
    maxu *= texturedata->texw;
slouken@1918
   602
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   603
    minv *= texturedata->texh;
slouken@1918
   604
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   605
    maxv *= texturedata->texh;
slouken@1918
   606
slouken@1927
   607
    data->glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   608
slouken@1927
   609
    if (blendMode != data->blendMode) {
slouken@1927
   610
        switch (blendMode) {
slouken@1927
   611
        case SDL_TextureBlendMode_None:
slouken@1927
   612
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@1927
   613
            data->glDisable(GL_BLEND);
slouken@1927
   614
            break;
slouken@1927
   615
        case SDL_TextureBlendMode_Mask:
slouken@1927
   616
        case SDL_TextureBlendMode_Blend:
slouken@1927
   617
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   618
            data->glEnable(GL_BLEND);
slouken@1927
   619
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1927
   620
            break;
slouken@1927
   621
        case SDL_TextureBlendMode_Add:
slouken@1927
   622
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   623
            data->glEnable(GL_BLEND);
slouken@1927
   624
            data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@1927
   625
            break;
slouken@1927
   626
        case SDL_TextureBlendMode_Mod:
slouken@1927
   627
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1927
   628
            data->glEnable(GL_BLEND);
slouken@1927
   629
            data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@1927
   630
            break;
slouken@1927
   631
        }
slouken@1927
   632
        data->blendMode = blendMode;
slouken@1918
   633
    }
slouken@1918
   634
slouken@1927
   635
    if (scaleMode != data->scaleMode) {
slouken@1927
   636
        switch (scaleMode) {
slouken@1927
   637
        case SDL_TextureScaleMode_None:
slouken@1927
   638
        case SDL_TextureScaleMode_Fast:
slouken@1927
   639
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
   640
                                  GL_NEAREST);
slouken@1927
   641
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
   642
                                  GL_NEAREST);
slouken@1927
   643
            break;
slouken@1927
   644
        case SDL_TextureScaleMode_Slow:
slouken@1927
   645
        case SDL_TextureScaleMode_Best:
slouken@1927
   646
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
slouken@1927
   647
                                  GL_LINEAR);
slouken@1927
   648
            data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
slouken@1927
   649
                                  GL_LINEAR);
slouken@1927
   650
            break;
slouken@1927
   651
        }
slouken@1927
   652
        data->scaleMode = scaleMode;
slouken@1918
   653
    }
slouken@1918
   654
slouken@1927
   655
    data->glBegin(GL_TRIANGLE_STRIP);
slouken@1927
   656
    data->glTexCoord2f(minu, minv);
slouken@1927
   657
    data->glVertex2i(minx, miny);
slouken@1927
   658
    data->glTexCoord2f(maxu, minv);
slouken@1927
   659
    data->glVertex2i(maxx, miny);
slouken@1927
   660
    data->glTexCoord2f(minu, maxv);
slouken@1927
   661
    data->glVertex2i(minx, maxy);
slouken@1927
   662
    data->glTexCoord2f(maxu, maxv);
slouken@1927
   663
    data->glVertex2i(maxx, maxy);
slouken@1927
   664
    data->glEnd();
slouken@1918
   665
slouken@1918
   666
    return 0;
slouken@1918
   667
}
slouken@1918
   668
slouken@1918
   669
static void
slouken@1918
   670
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   671
{
slouken@1918
   672
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   673
}
slouken@1918
   674
slouken@1918
   675
static void
slouken@1918
   676
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   677
{
slouken@1927
   678
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   679
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   680
slouken@1918
   681
    if (!data) {
slouken@1918
   682
        return;
slouken@1918
   683
    }
slouken@1918
   684
    if (data->texture) {
slouken@1927
   685
        renderdata->glDeleteTextures(1, &data->texture);
slouken@1918
   686
    }
slouken@1920
   687
    if (data->pixels) {
slouken@1920
   688
        SDL_free(data->pixels);
slouken@1920
   689
    }
slouken@1920
   690
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
   691
    SDL_free(data);
slouken@1918
   692
    texture->driverdata = NULL;
slouken@1918
   693
}
slouken@1918
   694
slouken@1918
   695
void
slouken@1918
   696
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   697
{
slouken@1918
   698
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   699
slouken@1918
   700
    if (data) {
slouken@1920
   701
        if (data->context) {
slouken@1920
   702
            SDL_GL_MakeCurrent(0, NULL);
slouken@1920
   703
            SDL_GL_DeleteContext(data->context);
slouken@1918
   704
        }
slouken@1918
   705
        SDL_free(data);
slouken@1918
   706
    }
slouken@1918
   707
    SDL_free(renderer);
slouken@1918
   708
}
slouken@1918
   709
slouken@1918
   710
#endif /* SDL_VIDEO_OPENGL */
slouken@1918
   711
slouken@1918
   712
/* vi: set ts=4 sw=4 expandtab: */