src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Jul 2006 19:03:31 +0000
changeset 1922 4905cac7a4bd
parent 1921 f3399f779a1d
child 1923 d4572b97b08f
permissions -rw-r--r--
Fixed OpenGL blend modes, added power of 2 texture code
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@1918
    36
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    37
static int GL_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    38
                                SDL_Texture * texture,
slouken@1918
    39
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    40
                                int ncolors);
slouken@1918
    41
static int GL_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    42
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    43
                                int firstcolor, int ncolors);
slouken@1918
    44
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    45
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    46
                            int pitch);
slouken@1918
    47
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    48
                          const SDL_Rect * rect, int markDirty,
slouken@1918
    49
                          void **pixels, int *pitch);
slouken@1918
    50
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    51
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    52
                            int numrects, const SDL_Rect * rects);
slouken@1918
    53
static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1918
    54
                         Uint32 color);
slouken@1918
    55
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    56
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
    57
                         int blendMode, int scaleMode);
slouken@1918
    58
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    59
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    60
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    61
slouken@1918
    62
slouken@1918
    63
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    64
    GL_CreateRenderer,
slouken@1918
    65
    {
slouken@1918
    66
     "opengl",
slouken@1918
    67
     (SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync |
slouken@1918
    68
      SDL_Renderer_Accelerated),
slouken@1918
    69
     (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask |
slouken@1918
    70
      SDL_TextureBlendMode_Blend | SDL_TextureBlendMode_Add |
slouken@1918
    71
      SDL_TextureBlendMode_Mod),
slouken@1918
    72
     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast |
slouken@1921
    73
      SDL_TextureScaleMode_Slow),
slouken@1920
    74
     18,
slouken@1918
    75
     {
slouken@1920
    76
      SDL_PixelFormat_Index1LSB,
slouken@1920
    77
      SDL_PixelFormat_Index1MSB,
slouken@1918
    78
      SDL_PixelFormat_Index8,
slouken@1918
    79
      SDL_PixelFormat_RGB332,
slouken@1918
    80
      SDL_PixelFormat_RGB444,
slouken@1918
    81
      SDL_PixelFormat_RGB555,
slouken@1918
    82
      SDL_PixelFormat_ARGB4444,
slouken@1918
    83
      SDL_PixelFormat_ARGB1555,
slouken@1918
    84
      SDL_PixelFormat_RGB565,
slouken@1920
    85
      SDL_PixelFormat_RGB24,
slouken@1920
    86
      SDL_PixelFormat_BGR24,
slouken@1918
    87
      SDL_PixelFormat_RGB888,
slouken@1920
    88
      SDL_PixelFormat_BGR888,
slouken@1918
    89
      SDL_PixelFormat_ARGB8888,
slouken@1920
    90
      SDL_PixelFormat_RGBA8888,
slouken@1920
    91
      SDL_PixelFormat_ABGR8888,
slouken@1920
    92
      SDL_PixelFormat_BGRA8888,
slouken@1920
    93
      SDL_PixelFormat_ARGB2101010},     /* FIXME: YUV texture support */
slouken@1918
    94
     0,
slouken@1918
    95
     0}
slouken@1918
    96
};
slouken@1918
    97
slouken@1918
    98
typedef struct
slouken@1918
    99
{
slouken@1918
   100
    SDL_GLContext context;
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@1918
   117
void
slouken@1918
   118
GL_AddRenderDriver(_THIS)
slouken@1918
   119
{
slouken@1920
   120
    if (_this->GL_CreateContext) {
slouken@1918
   121
        SDL_AddRenderDriver(0, &GL_RenderDriver);
slouken@1918
   122
    }
slouken@1918
   123
}
slouken@1918
   124
slouken@1918
   125
SDL_Renderer *
slouken@1918
   126
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   127
{
slouken@1918
   128
    SDL_Renderer *renderer;
slouken@1918
   129
    GL_RenderData *data;
slouken@1918
   130
slouken@1918
   131
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1918
   132
        SDL_SetError
slouken@1918
   133
            ("The OpenGL renderer can only be used on OpenGL windows");
slouken@1918
   134
        return NULL;
slouken@1918
   135
    }
slouken@1918
   136
slouken@1920
   137
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1918
   138
    if (!renderer) {
slouken@1918
   139
        SDL_OutOfMemory();
slouken@1918
   140
        return NULL;
slouken@1918
   141
    }
slouken@1918
   142
slouken@1920
   143
    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   144
    if (!data) {
slouken@1918
   145
        GL_DestroyRenderer(renderer);
slouken@1918
   146
        SDL_OutOfMemory();
slouken@1918
   147
        return NULL;
slouken@1918
   148
    }
slouken@1918
   149
slouken@1918
   150
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   151
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   152
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1918
   153
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   154
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   155
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   156
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@1918
   157
    renderer->RenderFill = GL_RenderFill;
slouken@1918
   158
    renderer->RenderCopy = GL_RenderCopy;
slouken@1918
   159
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   160
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   161
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   162
    renderer->info = GL_RenderDriver.info;
slouken@1918
   163
    renderer->window = window->id;
slouken@1918
   164
    renderer->driverdata = data;
slouken@1918
   165
slouken@1918
   166
    renderer->info.flags =
slouken@1918
   167
        (SDL_Renderer_PresentDiscard | SDL_Renderer_Accelerated);
slouken@1918
   168
slouken@1918
   169
    data->context = SDL_GL_CreateContext(window->id);
slouken@1918
   170
    if (!data->context) {
slouken@1918
   171
        GL_DestroyRenderer(renderer);
slouken@1918
   172
        return NULL;
slouken@1918
   173
    }
slouken@1918
   174
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1918
   175
        GL_DestroyRenderer(renderer);
slouken@1918
   176
        return NULL;
slouken@1918
   177
    }
slouken@1918
   178
slouken@1918
   179
    if (flags & SDL_Renderer_PresentVSync) {
slouken@1918
   180
        SDL_GL_SetSwapInterval(1);
slouken@1918
   181
    } else {
slouken@1918
   182
        SDL_GL_SetSwapInterval(0);
slouken@1918
   183
    }
slouken@1918
   184
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1918
   185
        renderer->info.flags |= SDL_Renderer_PresentVSync;
slouken@1918
   186
    }
slouken@1918
   187
slouken@1920
   188
    /* FIXME: Add a function to make the rendering context current when selecting the renderer */
slouken@1920
   189
slouken@1920
   190
    /* FIXME: Query maximum texture size */
slouken@1920
   191
slouken@1920
   192
    /* FIXME: Check for GL_ARB_texture_rectangle and GL_EXT_texture_rectangle */
slouken@1920
   193
slouken@1918
   194
    /* Set up parameters for rendering */
slouken@1918
   195
    glDisable(GL_DEPTH_TEST);
slouken@1918
   196
    glDisable(GL_CULL_FACE);
slouken@1922
   197
#ifdef USE_GL_TEXTURE_RECTANGLE
slouken@1922
   198
    glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1922
   199
#else
slouken@1918
   200
    glEnable(GL_TEXTURE_2D);
slouken@1922
   201
#endif
slouken@1918
   202
    glMatrixMode(GL_PROJECTION);
slouken@1918
   203
    glLoadIdentity();
slouken@1918
   204
    glMatrixMode(GL_MODELVIEW);
slouken@1918
   205
    glLoadIdentity();
slouken@1918
   206
    glViewport(0, 0, window->w, window->h);
slouken@1918
   207
    glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0, 0.0, 1.0);
slouken@1918
   208
slouken@1918
   209
    return renderer;
slouken@1918
   210
}
slouken@1918
   211
slouken@1922
   212
static __inline__ int
slouken@1922
   213
power_of_2(int input)
slouken@1922
   214
{
slouken@1922
   215
    int value = 1;
slouken@1922
   216
slouken@1922
   217
    while (value < input) {
slouken@1922
   218
        value <<= 1;
slouken@1922
   219
    }
slouken@1922
   220
    return value;
slouken@1922
   221
}
slouken@1922
   222
slouken@1918
   223
static int
slouken@1918
   224
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   225
{
slouken@1918
   226
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   227
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   228
    GL_TextureData *data;
slouken@1920
   229
    GLint internalFormat;
slouken@1920
   230
    GLenum format, type;
slouken@1922
   231
    int texture_w, texture_h;
slouken@1918
   232
slouken@1920
   233
    switch (texture->format) {
slouken@1920
   234
    case SDL_PixelFormat_Index1LSB:
slouken@1920
   235
    case SDL_PixelFormat_Index1MSB:
slouken@1920
   236
        internalFormat = GL_RGB;
slouken@1920
   237
        format = GL_COLOR_INDEX;
slouken@1920
   238
        type = GL_BITMAP;
slouken@1920
   239
        break;
slouken@1920
   240
    case SDL_PixelFormat_Index8:
slouken@1920
   241
        internalFormat = GL_RGB;
slouken@1920
   242
        format = GL_COLOR_INDEX;
slouken@1920
   243
        type = GL_UNSIGNED_BYTE;
slouken@1920
   244
        break;
slouken@1920
   245
    case SDL_PixelFormat_RGB332:
slouken@1920
   246
        internalFormat = GL_R3_G3_B2;
slouken@1920
   247
        format = GL_RGB;
slouken@1920
   248
        type = GL_UNSIGNED_BYTE_3_3_2;
slouken@1920
   249
        break;
slouken@1920
   250
    case SDL_PixelFormat_RGB444:
slouken@1920
   251
        internalFormat = GL_RGB4;
slouken@1920
   252
        format = GL_RGB;
slouken@1920
   253
        type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@1920
   254
        break;
slouken@1920
   255
    case SDL_PixelFormat_RGB555:
slouken@1920
   256
        internalFormat = GL_RGB5;
slouken@1920
   257
        format = GL_RGB;
slouken@1920
   258
        type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@1920
   259
        break;
slouken@1920
   260
    case SDL_PixelFormat_ARGB4444:
slouken@1920
   261
        internalFormat = GL_RGBA4;
slouken@1920
   262
        format = GL_BGRA;
slouken@1920
   263
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@1920
   264
        break;
slouken@1920
   265
    case SDL_PixelFormat_ARGB1555:
slouken@1920
   266
        internalFormat = GL_RGB5_A1;
slouken@1920
   267
        format = GL_BGRA;
slouken@1920
   268
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@1920
   269
        break;
slouken@1920
   270
    case SDL_PixelFormat_RGB565:
slouken@1920
   271
        internalFormat = GL_RGB8;
slouken@1920
   272
        format = GL_RGB;
slouken@1920
   273
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1920
   274
        break;
slouken@1920
   275
    case SDL_PixelFormat_RGB24:
slouken@1920
   276
        internalFormat = GL_RGB8;
slouken@1920
   277
        format = GL_RGB;
slouken@1920
   278
        type = GL_UNSIGNED_BYTE;
slouken@1920
   279
        break;
slouken@1920
   280
    case SDL_PixelFormat_RGB888:
slouken@1920
   281
        internalFormat = GL_RGB8;
slouken@1920
   282
        format = GL_RGB;
slouken@1920
   283
        type = GL_UNSIGNED_INT_8_8_8_8;
slouken@1920
   284
        break;
slouken@1920
   285
    case SDL_PixelFormat_BGR24:
slouken@1920
   286
        internalFormat = GL_RGB8;
slouken@1920
   287
        format = GL_BGR;
slouken@1920
   288
        type = GL_UNSIGNED_BYTE;
slouken@1920
   289
        break;
slouken@1920
   290
    case SDL_PixelFormat_BGR888:
slouken@1920
   291
        internalFormat = GL_RGB8;
slouken@1920
   292
        format = GL_BGR;
slouken@1920
   293
        type = GL_UNSIGNED_INT_8_8_8_8;
slouken@1920
   294
        break;
slouken@1920
   295
    case SDL_PixelFormat_ARGB8888:
slouken@1920
   296
        internalFormat = GL_RGBA8;
slouken@1920
   297
        format = GL_BGRA;
slouken@1920
   298
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@1920
   299
        break;
slouken@1920
   300
    case SDL_PixelFormat_RGBA8888:
slouken@1920
   301
        internalFormat = GL_RGBA8;
slouken@1921
   302
        format = GL_RGBA;
slouken@1920
   303
        type = GL_UNSIGNED_INT_8_8_8_8;
slouken@1920
   304
        break;
slouken@1920
   305
    case SDL_PixelFormat_ABGR8888:
slouken@1920
   306
        internalFormat = GL_RGBA8;
slouken@1920
   307
        format = GL_RGBA;
slouken@1920
   308
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@1920
   309
        break;
slouken@1920
   310
    case SDL_PixelFormat_BGRA8888:
slouken@1920
   311
        internalFormat = GL_RGBA8;
slouken@1920
   312
        format = GL_BGRA;
slouken@1920
   313
        type = GL_UNSIGNED_INT_8_8_8_8;
slouken@1920
   314
        break;
slouken@1920
   315
    case SDL_PixelFormat_ARGB2101010:
slouken@1920
   316
        internalFormat = GL_RGB10_A2;
slouken@1920
   317
        format = GL_BGRA;
slouken@1920
   318
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@1920
   319
        break;
slouken@1920
   320
    default:
slouken@1920
   321
        SDL_SetError("Unsupported texture format");
slouken@1920
   322
        return -1;
slouken@1920
   323
    }
slouken@1920
   324
slouken@1920
   325
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   326
    if (!data) {
slouken@1918
   327
        SDL_OutOfMemory();
slouken@1918
   328
        return -1;
slouken@1918
   329
    }
slouken@1918
   330
slouken@1918
   331
    texture->driverdata = data;
slouken@1918
   332
slouken@1920
   333
    /* FIXME: Check for GL_ARB_texture_rectangle and GL_EXT_texture_rectangle */
slouken@1920
   334
    glGenTextures(1, &data->texture);
slouken@1922
   335
#ifdef USE_GL_TEXTURE_RECTANGLE
slouken@1920
   336
    data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1922
   337
    texture_w = texture->w;
slouken@1922
   338
    texture_h = texture->h;
slouken@1920
   339
    data->texw = (GLfloat) texture->w;
slouken@1920
   340
    data->texh = (GLfloat) texture->h;
slouken@1922
   341
#else
slouken@1922
   342
    data->type = GL_TEXTURE_2D;
slouken@1922
   343
    texture_w = power_of_2(texture->w);
slouken@1922
   344
    texture_h = power_of_2(texture->h);
slouken@1922
   345
    data->texw = (GLfloat) texture->w / texture_w;
slouken@1922
   346
    data->texh = (GLfloat) texture->h / texture_h;
slouken@1922
   347
#endif
slouken@1920
   348
    data->format = format;
slouken@1920
   349
    data->formattype = type;
slouken@1920
   350
    glBindTexture(data->type, data->texture);
slouken@1922
   351
    glTexImage2D(data->type, 0, internalFormat, texture_w, texture_h, 0,
slouken@1920
   352
                 format, type, NULL);
slouken@1918
   353
slouken@1918
   354
    return 0;
slouken@1918
   355
}
slouken@1918
   356
slouken@1918
   357
static int
slouken@1918
   358
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   359
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   360
{
slouken@1918
   361
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   362
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   363
slouken@1918
   364
    return 0;
slouken@1918
   365
}
slouken@1918
   366
slouken@1918
   367
static int
slouken@1918
   368
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   369
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   370
{
slouken@1918
   371
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   372
slouken@1918
   373
    return 0;
slouken@1918
   374
}
slouken@1918
   375
slouken@1918
   376
static int
slouken@1918
   377
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   378
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   379
{
slouken@1918
   380
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   381
slouken@1920
   382
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);      /* FIXME, what to use for RGB 4 byte formats? */
slouken@1920
   383
    glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@1920
   384
                  pitch / SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   385
    glBindTexture(data->type, data->texture);
slouken@1920
   386
    glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, rect->h,
slouken@1920
   387
                    data->format, data->formattype, pixels);
slouken@1920
   388
    /* FIXME: check for errors */
slouken@1918
   389
    return 0;
slouken@1918
   390
}
slouken@1918
   391
slouken@1918
   392
static int
slouken@1918
   393
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   394
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   395
               int *pitch)
slouken@1918
   396
{
slouken@1918
   397
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   398
slouken@1920
   399
    if (!data->pixels) {
slouken@1920
   400
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   401
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@1920
   402
        if (!data->pixels) {
slouken@1920
   403
            SDL_OutOfMemory();
slouken@1920
   404
            return -1;
slouken@1920
   405
        }
slouken@1918
   406
    }
slouken@1918
   407
slouken@1920
   408
    if (markDirty) {
slouken@1920
   409
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
   410
    }
slouken@1918
   411
slouken@1920
   412
    *pixels =
slouken@1920
   413
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@1920
   414
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   415
    *pitch = data->pitch;
slouken@1918
   416
    return 0;
slouken@1918
   417
}
slouken@1918
   418
slouken@1918
   419
static void
slouken@1918
   420
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   421
{
slouken@1918
   422
}
slouken@1918
   423
slouken@1918
   424
static void
slouken@1918
   425
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
   426
                const SDL_Rect * rects)
slouken@1918
   427
{
slouken@1918
   428
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   429
    int i;
slouken@1918
   430
slouken@1918
   431
    for (i = 0; i < numrects; ++i) {
slouken@1920
   432
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
   433
    }
slouken@1918
   434
}
slouken@1918
   435
slouken@1918
   436
static int
slouken@1918
   437
GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1918
   438
{
slouken@1918
   439
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   440
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   441
    GLclampf r, g, b, a;
slouken@1918
   442
slouken@1918
   443
    a = ((GLclampf) ((color >> 24) & 0xFF)) / 255.0f;
slouken@1918
   444
    r = ((GLclampf) ((color >> 16) & 0xFF)) / 255.0f;
slouken@1918
   445
    g = ((GLclampf) ((color >> 8) & 0xFF)) / 255.0f;
slouken@1918
   446
    b = ((GLclampf) (color & 0xFF)) / 255.0f;
slouken@1918
   447
slouken@1918
   448
    glClearColor(r, g, b, a);
slouken@1918
   449
    glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
slouken@1918
   450
    glClear(GL_COLOR_BUFFER_BIT);
slouken@1918
   451
    glViewport(0, 0, window->w, window->h);
slouken@1918
   452
    return 0;
slouken@1918
   453
}
slouken@1918
   454
slouken@1918
   455
static int
slouken@1918
   456
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   457
              const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
   458
              int blendMode, int scaleMode)
slouken@1918
   459
{
slouken@1918
   460
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   461
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   462
    int minx, miny, maxx, maxy;
slouken@1918
   463
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   464
slouken@1920
   465
    if (texturedata->dirty.count > 0) {
slouken@1920
   466
        SDL_DirtyRect *dirty;
slouken@1920
   467
        void *pixels;
slouken@1920
   468
        int bpp = SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   469
        int pitch = texturedata->pitch;
slouken@1920
   470
slouken@1920
   471
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);  /* FIXME, what to use for RGB 4 byte formats? */
slouken@1920
   472
        glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@1920
   473
                      pitch / SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   474
        glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
   475
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
   476
            SDL_Rect *rect = &dirty->rect;
slouken@1920
   477
            pixels =
slouken@1920
   478
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
   479
                          rect->x * bpp);
slouken@1920
   480
            glTexSubImage2D(texturedata->type, 0, rect->x, rect->y, rect->w,
slouken@1920
   481
                            rect->h, texturedata->format,
slouken@1920
   482
                            texturedata->formattype, pixels);
slouken@1920
   483
        }
slouken@1920
   484
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
   485
    }
slouken@1920
   486
slouken@1918
   487
    minx = dstrect->x;
slouken@1918
   488
    miny = dstrect->y;
slouken@1918
   489
    maxx = dstrect->x + dstrect->w;
slouken@1918
   490
    maxy = dstrect->y + dstrect->h;
slouken@1918
   491
slouken@1918
   492
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   493
    minu *= texturedata->texw;
slouken@1918
   494
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   495
    maxu *= texturedata->texw;
slouken@1918
   496
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   497
    minv *= texturedata->texh;
slouken@1918
   498
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   499
    maxv *= texturedata->texh;
slouken@1918
   500
slouken@1920
   501
    glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   502
slouken@1918
   503
    switch (blendMode) {
slouken@1918
   504
    case SDL_TextureBlendMode_None:
slouken@1922
   505
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@1918
   506
        glDisable(GL_BLEND);
slouken@1918
   507
        break;
slouken@1918
   508
    case SDL_TextureBlendMode_Mask:
slouken@1918
   509
    case SDL_TextureBlendMode_Blend:
slouken@1922
   510
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1918
   511
        glEnable(GL_BLEND);
slouken@1918
   512
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1918
   513
        break;
slouken@1918
   514
    case SDL_TextureBlendMode_Add:
slouken@1922
   515
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1918
   516
        glEnable(GL_BLEND);
slouken@1918
   517
        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@1918
   518
        break;
slouken@1918
   519
    case SDL_TextureBlendMode_Mod:
slouken@1922
   520
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
slouken@1918
   521
        glEnable(GL_BLEND);
slouken@1918
   522
        glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@1918
   523
        break;
slouken@1918
   524
    }
slouken@1918
   525
slouken@1918
   526
    switch (scaleMode) {
slouken@1918
   527
    case SDL_TextureScaleMode_None:
slouken@1918
   528
    case SDL_TextureScaleMode_Fast:
slouken@1920
   529
        glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
slouken@1920
   530
        glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
slouken@1918
   531
        break;
slouken@1918
   532
    case SDL_TextureScaleMode_Slow:
slouken@1922
   533
    case SDL_TextureScaleMode_Best:
slouken@1920
   534
        glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
slouken@1920
   535
        glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
slouken@1918
   536
        break;
slouken@1918
   537
    }
slouken@1918
   538
slouken@1918
   539
    glBegin(GL_TRIANGLE_STRIP);
slouken@1918
   540
    glTexCoord2f(minu, minv);
slouken@1918
   541
    glVertex2i(minx, miny);
slouken@1918
   542
    glTexCoord2f(maxu, minv);
slouken@1918
   543
    glVertex2i(maxx, miny);
slouken@1918
   544
    glTexCoord2f(minu, maxv);
slouken@1921
   545
    glVertex2i(minx, maxy);
slouken@1918
   546
    glTexCoord2f(maxu, maxv);
slouken@1918
   547
    glVertex2i(maxx, maxy);
slouken@1918
   548
    glEnd();
slouken@1918
   549
slouken@1918
   550
    return 0;
slouken@1918
   551
}
slouken@1918
   552
slouken@1918
   553
static void
slouken@1918
   554
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   555
{
slouken@1918
   556
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   557
}
slouken@1918
   558
slouken@1918
   559
static void
slouken@1918
   560
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   561
{
slouken@1918
   562
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   563
slouken@1918
   564
    if (!data) {
slouken@1918
   565
        return;
slouken@1918
   566
    }
slouken@1918
   567
    if (data->texture) {
slouken@1920
   568
        glDeleteTextures(1, &data->texture);
slouken@1918
   569
    }
slouken@1920
   570
    if (data->pixels) {
slouken@1920
   571
        SDL_free(data->pixels);
slouken@1920
   572
    }
slouken@1920
   573
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
   574
    SDL_free(data);
slouken@1918
   575
    texture->driverdata = NULL;
slouken@1918
   576
}
slouken@1918
   577
slouken@1918
   578
void
slouken@1918
   579
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   580
{
slouken@1918
   581
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   582
slouken@1918
   583
    if (data) {
slouken@1920
   584
        if (data->context) {
slouken@1920
   585
            SDL_GL_MakeCurrent(0, NULL);
slouken@1920
   586
            SDL_GL_DeleteContext(data->context);
slouken@1918
   587
        }
slouken@1918
   588
        SDL_free(data);
slouken@1918
   589
    }
slouken@1918
   590
    SDL_free(renderer);
slouken@1918
   591
}
slouken@1918
   592
slouken@1918
   593
#endif /* SDL_VIDEO_OPENGL */
slouken@1918
   594
slouken@1918
   595
/* vi: set ts=4 sw=4 expandtab: */