src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Jul 2006 08:33:18 +0000
changeset 1920 8a162bfdc838
parent 1919 00816063b9c9
child 1921 f3399f779a1d
permissions -rw-r--r--
Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
OpenGL renderer in progress
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@1918
    73
      SDL_TextureScaleMode_Best),
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@1918
   197
    glEnable(GL_TEXTURE_2D);
slouken@1920
   198
    glEnable(GL_TEXTURE_RECTANGLE_ARB);
slouken@1920
   199
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@1918
   200
    glMatrixMode(GL_PROJECTION);
slouken@1918
   201
    glLoadIdentity();
slouken@1918
   202
    glMatrixMode(GL_MODELVIEW);
slouken@1918
   203
    glLoadIdentity();
slouken@1918
   204
    glViewport(0, 0, window->w, window->h);
slouken@1918
   205
    glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0, 0.0, 1.0);
slouken@1918
   206
slouken@1918
   207
    return renderer;
slouken@1918
   208
}
slouken@1918
   209
slouken@1918
   210
static int
slouken@1918
   211
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   212
{
slouken@1918
   213
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   214
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   215
    GL_TextureData *data;
slouken@1920
   216
    GLint internalFormat;
slouken@1920
   217
    GLenum format, type;
slouken@1918
   218
slouken@1920
   219
    switch (texture->format) {
slouken@1920
   220
    case SDL_PixelFormat_Index1LSB:
slouken@1920
   221
    case SDL_PixelFormat_Index1MSB:
slouken@1920
   222
        internalFormat = GL_RGB;
slouken@1920
   223
        format = GL_COLOR_INDEX;
slouken@1920
   224
        type = GL_BITMAP;
slouken@1920
   225
        break;
slouken@1920
   226
    case SDL_PixelFormat_Index8:
slouken@1920
   227
        internalFormat = GL_RGB;
slouken@1920
   228
        format = GL_COLOR_INDEX;
slouken@1920
   229
        type = GL_UNSIGNED_BYTE;
slouken@1920
   230
        break;
slouken@1920
   231
    case SDL_PixelFormat_RGB332:
slouken@1920
   232
        internalFormat = GL_R3_G3_B2;
slouken@1920
   233
        format = GL_RGB;
slouken@1920
   234
        type = GL_UNSIGNED_BYTE_3_3_2;
slouken@1920
   235
        break;
slouken@1920
   236
    case SDL_PixelFormat_RGB444:
slouken@1920
   237
        internalFormat = GL_RGB4;
slouken@1920
   238
        format = GL_RGB;
slouken@1920
   239
        type = GL_UNSIGNED_SHORT_4_4_4_4;
slouken@1920
   240
        break;
slouken@1920
   241
    case SDL_PixelFormat_RGB555:
slouken@1920
   242
        internalFormat = GL_RGB5;
slouken@1920
   243
        format = GL_RGB;
slouken@1920
   244
        type = GL_UNSIGNED_SHORT_5_5_5_1;
slouken@1920
   245
        break;
slouken@1920
   246
    case SDL_PixelFormat_ARGB4444:
slouken@1920
   247
        internalFormat = GL_RGBA4;
slouken@1920
   248
        format = GL_BGRA;
slouken@1920
   249
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
slouken@1920
   250
        break;
slouken@1920
   251
    case SDL_PixelFormat_ARGB1555:
slouken@1920
   252
        internalFormat = GL_RGB5_A1;
slouken@1920
   253
        format = GL_BGRA;
slouken@1920
   254
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
slouken@1920
   255
        break;
slouken@1920
   256
    case SDL_PixelFormat_RGB565:
slouken@1920
   257
        internalFormat = GL_RGB8;
slouken@1920
   258
        format = GL_RGB;
slouken@1920
   259
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1920
   260
        break;
slouken@1920
   261
    case SDL_PixelFormat_RGB24:
slouken@1920
   262
        internalFormat = GL_RGB8;
slouken@1920
   263
        format = GL_RGB;
slouken@1920
   264
        type = GL_UNSIGNED_BYTE;
slouken@1920
   265
        break;
slouken@1920
   266
    case SDL_PixelFormat_RGB888:
slouken@1920
   267
        internalFormat = GL_RGB8;
slouken@1920
   268
        format = GL_RGB;
slouken@1920
   269
        type = GL_UNSIGNED_INT_8_8_8_8;
slouken@1920
   270
        break;
slouken@1920
   271
    case SDL_PixelFormat_BGR24:
slouken@1920
   272
        internalFormat = GL_RGB8;
slouken@1920
   273
        format = GL_BGR;
slouken@1920
   274
        type = GL_UNSIGNED_BYTE;
slouken@1920
   275
        break;
slouken@1920
   276
    case SDL_PixelFormat_BGR888:
slouken@1920
   277
        internalFormat = GL_RGB8;
slouken@1920
   278
        format = GL_BGR;
slouken@1920
   279
        type = GL_UNSIGNED_INT_8_8_8_8;
slouken@1920
   280
        break;
slouken@1920
   281
    case SDL_PixelFormat_ARGB8888:
slouken@1920
   282
        internalFormat = GL_RGBA8;
slouken@1920
   283
        format = GL_BGRA;
slouken@1920
   284
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@1920
   285
        break;
slouken@1920
   286
    case SDL_PixelFormat_RGBA8888:
slouken@1920
   287
        internalFormat = GL_RGBA8;
slouken@1920
   288
        format = GL_BGRA;
slouken@1920
   289
        type = GL_UNSIGNED_INT_8_8_8_8;
slouken@1920
   290
        break;
slouken@1920
   291
    case SDL_PixelFormat_ABGR8888:
slouken@1920
   292
        internalFormat = GL_RGBA8;
slouken@1920
   293
        format = GL_RGBA;
slouken@1920
   294
        type = GL_UNSIGNED_INT_8_8_8_8_REV;
slouken@1920
   295
        break;
slouken@1920
   296
    case SDL_PixelFormat_BGRA8888:
slouken@1920
   297
        internalFormat = GL_RGBA8;
slouken@1920
   298
        format = GL_BGRA;
slouken@1920
   299
        type = GL_UNSIGNED_INT_8_8_8_8;
slouken@1920
   300
        break;
slouken@1920
   301
    case SDL_PixelFormat_ARGB2101010:
slouken@1920
   302
        internalFormat = GL_RGB10_A2;
slouken@1920
   303
        format = GL_BGRA;
slouken@1920
   304
        type = GL_UNSIGNED_INT_2_10_10_10_REV;
slouken@1920
   305
        break;
slouken@1920
   306
    default:
slouken@1920
   307
        SDL_SetError("Unsupported texture format");
slouken@1920
   308
        return -1;
slouken@1920
   309
    }
slouken@1920
   310
slouken@1920
   311
    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1918
   312
    if (!data) {
slouken@1918
   313
        SDL_OutOfMemory();
slouken@1918
   314
        return -1;
slouken@1918
   315
    }
slouken@1918
   316
slouken@1918
   317
    texture->driverdata = data;
slouken@1918
   318
slouken@1920
   319
    /* FIXME: Check for GL_ARB_texture_rectangle and GL_EXT_texture_rectangle */
slouken@1920
   320
    glGenTextures(1, &data->texture);
slouken@1920
   321
    data->type = GL_TEXTURE_RECTANGLE_ARB;
slouken@1920
   322
    data->texw = (GLfloat) texture->w;
slouken@1920
   323
    data->texh = (GLfloat) texture->h;
slouken@1920
   324
    data->format = format;
slouken@1920
   325
    data->formattype = type;
slouken@1920
   326
    glBindTexture(data->type, data->texture);
slouken@1920
   327
    glTexImage2D(data->type, 0, internalFormat, texture->w, texture->h, 0,
slouken@1920
   328
                 format, type, NULL);
slouken@1918
   329
slouken@1918
   330
    return 0;
slouken@1918
   331
}
slouken@1918
   332
slouken@1918
   333
static int
slouken@1918
   334
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   335
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   336
{
slouken@1918
   337
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   338
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   339
slouken@1918
   340
    return 0;
slouken@1918
   341
}
slouken@1918
   342
slouken@1918
   343
static int
slouken@1918
   344
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   345
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   346
{
slouken@1918
   347
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   348
slouken@1918
   349
    return 0;
slouken@1918
   350
}
slouken@1918
   351
slouken@1918
   352
static int
slouken@1918
   353
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   354
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   355
{
slouken@1918
   356
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   357
slouken@1920
   358
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);      /* FIXME, what to use for RGB 4 byte formats? */
slouken@1920
   359
    glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@1920
   360
                  pitch / SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   361
    glBindTexture(data->type, data->texture);
slouken@1920
   362
    glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, rect->h,
slouken@1920
   363
                    data->format, data->formattype, pixels);
slouken@1920
   364
    /* FIXME: check for errors */
slouken@1918
   365
    return 0;
slouken@1918
   366
}
slouken@1918
   367
slouken@1918
   368
static int
slouken@1918
   369
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   370
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   371
               int *pitch)
slouken@1918
   372
{
slouken@1918
   373
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   374
slouken@1920
   375
    if (!data->pixels) {
slouken@1920
   376
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   377
        data->pixels = SDL_malloc(texture->h * data->pitch);
slouken@1920
   378
        if (!data->pixels) {
slouken@1920
   379
            SDL_OutOfMemory();
slouken@1920
   380
            return -1;
slouken@1920
   381
        }
slouken@1918
   382
    }
slouken@1918
   383
slouken@1920
   384
    if (markDirty) {
slouken@1920
   385
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1920
   386
    }
slouken@1918
   387
slouken@1920
   388
    *pixels =
slouken@1920
   389
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@1920
   390
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   391
    *pitch = data->pitch;
slouken@1918
   392
    return 0;
slouken@1918
   393
}
slouken@1918
   394
slouken@1918
   395
static void
slouken@1918
   396
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   397
{
slouken@1918
   398
}
slouken@1918
   399
slouken@1918
   400
static void
slouken@1918
   401
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1918
   402
                const SDL_Rect * rects)
slouken@1918
   403
{
slouken@1918
   404
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   405
    int i;
slouken@1918
   406
slouken@1918
   407
    for (i = 0; i < numrects; ++i) {
slouken@1920
   408
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
slouken@1918
   409
    }
slouken@1918
   410
}
slouken@1918
   411
slouken@1918
   412
static int
slouken@1918
   413
GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1918
   414
{
slouken@1918
   415
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   416
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   417
    GLclampf r, g, b, a;
slouken@1918
   418
slouken@1918
   419
    a = ((GLclampf) ((color >> 24) & 0xFF)) / 255.0f;
slouken@1918
   420
    r = ((GLclampf) ((color >> 16) & 0xFF)) / 255.0f;
slouken@1918
   421
    g = ((GLclampf) ((color >> 8) & 0xFF)) / 255.0f;
slouken@1918
   422
    b = ((GLclampf) (color & 0xFF)) / 255.0f;
slouken@1918
   423
slouken@1918
   424
    glClearColor(r, g, b, a);
slouken@1918
   425
    glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
slouken@1918
   426
    glClear(GL_COLOR_BUFFER_BIT);
slouken@1918
   427
    glViewport(0, 0, window->w, window->h);
slouken@1918
   428
    return 0;
slouken@1918
   429
}
slouken@1918
   430
slouken@1918
   431
static int
slouken@1918
   432
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   433
              const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
   434
              int blendMode, int scaleMode)
slouken@1918
   435
{
slouken@1918
   436
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   437
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   438
    int minx, miny, maxx, maxy;
slouken@1918
   439
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   440
slouken@1920
   441
    if (texturedata->dirty.count > 0) {
slouken@1920
   442
        SDL_DirtyRect *dirty;
slouken@1920
   443
        void *pixels;
slouken@1920
   444
        int bpp = SDL_BYTESPERPIXEL(texture->format);
slouken@1920
   445
        int pitch = texturedata->pitch;
slouken@1920
   446
slouken@1920
   447
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);  /* FIXME, what to use for RGB 4 byte formats? */
slouken@1920
   448
        glPixelStorei(GL_UNPACK_ROW_LENGTH,
slouken@1920
   449
                      pitch / SDL_BYTESPERPIXEL(texture->format));
slouken@1920
   450
        glBindTexture(texturedata->type, texturedata->texture);
slouken@1920
   451
        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
slouken@1920
   452
            SDL_Rect *rect = &dirty->rect;
slouken@1920
   453
            pixels =
slouken@1920
   454
                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
slouken@1920
   455
                          rect->x * bpp);
slouken@1920
   456
            glTexSubImage2D(texturedata->type, 0, rect->x, rect->y, rect->w,
slouken@1920
   457
                            rect->h, texturedata->format,
slouken@1920
   458
                            texturedata->formattype, pixels);
slouken@1920
   459
        }
slouken@1920
   460
        SDL_ClearDirtyRects(&texturedata->dirty);
slouken@1920
   461
    }
slouken@1920
   462
slouken@1918
   463
    minx = dstrect->x;
slouken@1918
   464
    miny = dstrect->y;
slouken@1918
   465
    maxx = dstrect->x + dstrect->w;
slouken@1918
   466
    maxy = dstrect->y + dstrect->h;
slouken@1918
   467
slouken@1918
   468
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1920
   469
    minu *= texturedata->texw;
slouken@1918
   470
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1920
   471
    maxu *= texturedata->texw;
slouken@1918
   472
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1920
   473
    minv *= texturedata->texh;
slouken@1918
   474
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1920
   475
    maxv *= texturedata->texh;
slouken@1918
   476
slouken@1920
   477
    glBindTexture(texturedata->type, texturedata->texture);
slouken@1918
   478
slouken@1918
   479
    switch (blendMode) {
slouken@1918
   480
    case SDL_TextureBlendMode_None:
slouken@1918
   481
        glDisable(GL_BLEND);
slouken@1918
   482
        break;
slouken@1918
   483
    case SDL_TextureBlendMode_Mask:
slouken@1918
   484
    case SDL_TextureBlendMode_Blend:
slouken@1918
   485
        glEnable(GL_BLEND);
slouken@1918
   486
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1918
   487
        break;
slouken@1918
   488
    case SDL_TextureBlendMode_Add:
slouken@1918
   489
        glEnable(GL_BLEND);
slouken@1918
   490
        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@1918
   491
        break;
slouken@1918
   492
    case SDL_TextureBlendMode_Mod:
slouken@1918
   493
        glEnable(GL_BLEND);
slouken@1918
   494
        glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@1918
   495
        break;
slouken@1918
   496
    }
slouken@1918
   497
slouken@1918
   498
    switch (scaleMode) {
slouken@1918
   499
    case SDL_TextureScaleMode_None:
slouken@1918
   500
    case SDL_TextureScaleMode_Fast:
slouken@1920
   501
        glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
slouken@1920
   502
        glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
slouken@1918
   503
        break;
slouken@1918
   504
    case SDL_TextureScaleMode_Slow:
slouken@1918
   505
    case SDL_TextureScaleMode_Best:
slouken@1920
   506
        glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
slouken@1920
   507
        glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
slouken@1918
   508
        break;
slouken@1918
   509
    }
slouken@1918
   510
slouken@1918
   511
    glBegin(GL_TRIANGLE_STRIP);
slouken@1918
   512
    glTexCoord2f(minu, minv);
slouken@1918
   513
    glVertex2i(minx, miny);
slouken@1918
   514
    glTexCoord2f(maxu, minv);
slouken@1918
   515
    glVertex2i(maxx, miny);
slouken@1918
   516
    glTexCoord2f(minu, maxv);
slouken@1918
   517
    glVertex2i(miny, maxy);
slouken@1918
   518
    glTexCoord2f(maxu, maxv);
slouken@1918
   519
    glVertex2i(maxx, maxy);
slouken@1918
   520
    glEnd();
slouken@1918
   521
slouken@1918
   522
    return 0;
slouken@1918
   523
}
slouken@1918
   524
slouken@1918
   525
static void
slouken@1918
   526
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   527
{
slouken@1918
   528
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   529
}
slouken@1918
   530
slouken@1918
   531
static void
slouken@1918
   532
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   533
{
slouken@1918
   534
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   535
slouken@1918
   536
    if (!data) {
slouken@1918
   537
        return;
slouken@1918
   538
    }
slouken@1918
   539
    if (data->texture) {
slouken@1920
   540
        glDeleteTextures(1, &data->texture);
slouken@1918
   541
    }
slouken@1920
   542
    if (data->pixels) {
slouken@1920
   543
        SDL_free(data->pixels);
slouken@1920
   544
    }
slouken@1920
   545
    SDL_FreeDirtyRects(&data->dirty);
slouken@1918
   546
    SDL_free(data);
slouken@1918
   547
    texture->driverdata = NULL;
slouken@1918
   548
}
slouken@1918
   549
slouken@1918
   550
void
slouken@1918
   551
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   552
{
slouken@1918
   553
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   554
slouken@1918
   555
    if (data) {
slouken@1920
   556
        if (data->context) {
slouken@1920
   557
            SDL_GL_MakeCurrent(0, NULL);
slouken@1920
   558
            SDL_GL_DeleteContext(data->context);
slouken@1918
   559
        }
slouken@1918
   560
        SDL_free(data);
slouken@1918
   561
    }
slouken@1918
   562
    SDL_free(renderer);
slouken@1918
   563
}
slouken@1918
   564
slouken@1918
   565
#endif /* SDL_VIDEO_OPENGL */
slouken@1918
   566
slouken@1918
   567
/* vi: set ts=4 sw=4 expandtab: */