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