src/video/SDL_renderer_gl.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 19 Jul 2006 07:18:45 +0000
changeset 1918 092bd3a019c5
child 1919 00816063b9c9
permissions -rw-r--r--
Starting on the OpenGL renderer...
slouken@1918
     1
/*
slouken@1918
     2
    SDL - Simple DirectMedia Layer
slouken@1918
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1918
     4
slouken@1918
     5
    This library is free software; you can redistribute it and/or
slouken@1918
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1918
     7
    License as published by the Free Software Foundation; either
slouken@1918
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1918
     9
slouken@1918
    10
    This library is distributed in the hope that it will be useful,
slouken@1918
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1918
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1918
    13
    Lesser General Public License for more details.
slouken@1918
    14
slouken@1918
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1918
    16
    License along with this library; if not, write to the Free Software
slouken@1918
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1918
    18
slouken@1918
    19
    Sam Lantinga
slouken@1918
    20
    slouken@libsdl.org
slouken@1918
    21
*/
slouken@1918
    22
#include "SDL_config.h"
slouken@1918
    23
slouken@1918
    24
#if SDL_VIDEO_OPENGL
slouken@1918
    25
slouken@1918
    26
#include "SDL_win32video.h"
slouken@1918
    27
slouken@1918
    28
/* OpenGL renderer implementation */
slouken@1918
    29
slouken@1918
    30
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1918
    31
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    32
static int GL_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    33
                                SDL_Texture * texture,
slouken@1918
    34
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    35
                                int ncolors);
slouken@1918
    36
static int GL_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    37
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    38
                                int firstcolor, int ncolors);
slouken@1918
    39
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    40
                            const SDL_Rect * rect, const void *pixels,
slouken@1918
    41
                            int pitch);
slouken@1918
    42
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    43
                          const SDL_Rect * rect, int markDirty,
slouken@1918
    44
                          void **pixels, int *pitch);
slouken@1918
    45
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    46
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    47
                            int numrects, const SDL_Rect * rects);
slouken@1918
    48
static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1918
    49
                         Uint32 color);
slouken@1918
    50
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    51
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
    52
                         int blendMode, int scaleMode);
slouken@1918
    53
static void GL_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    54
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    55
static void GL_DestroyRenderer(SDL_Renderer * renderer);
slouken@1918
    56
slouken@1918
    57
slouken@1918
    58
SDL_RenderDriver GL_RenderDriver = {
slouken@1918
    59
    GL_CreateRenderer,
slouken@1918
    60
    {
slouken@1918
    61
     "opengl",
slouken@1918
    62
     (SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync |
slouken@1918
    63
      SDL_Renderer_Accelerated),
slouken@1918
    64
     (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask |
slouken@1918
    65
      SDL_TextureBlendMode_Blend | SDL_TextureBlendMode_Add |
slouken@1918
    66
      SDL_TextureBlendMode_Mod),
slouken@1918
    67
     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast |
slouken@1918
    68
      SDL_TextureScaleMode_Best),
slouken@1918
    69
     12,
slouken@1918
    70
     {
slouken@1918
    71
      SDL_PixelFormat_Index8,
slouken@1918
    72
      SDL_PixelFormat_RGB332,
slouken@1918
    73
      SDL_PixelFormat_RGB444,
slouken@1918
    74
      SDL_PixelFormat_RGB555,
slouken@1918
    75
      SDL_PixelFormat_ARGB4444,
slouken@1918
    76
      SDL_PixelFormat_ARGB1555,
slouken@1918
    77
      SDL_PixelFormat_RGB565,
slouken@1918
    78
      SDL_PixelFormat_RGB888,
slouken@1918
    79
      SDL_PixelFormat_ARGB8888,
slouken@1918
    80
      SDL_PixelFormat_ARGB2101010,
slouken@1918
    81
      SDL_PixelFormat_UYVY,
slouken@1918
    82
      SDL_PixelFormat_YUY2},
slouken@1918
    83
     0,
slouken@1918
    84
     0}
slouken@1918
    85
};
slouken@1918
    86
slouken@1918
    87
typedef struct
slouken@1918
    88
{
slouken@1918
    89
    SDL_GLContext context;
slouken@1918
    90
    SDL_bool beginScene;
slouken@1918
    91
} GL_RenderData;
slouken@1918
    92
slouken@1918
    93
typedef struct
slouken@1918
    94
{
slouken@1918
    95
    GLuint texture;
slouken@1918
    96
    GLfloat texw;
slouken@1918
    97
    GLfloat texh;
slouken@1918
    98
    void *pixels;
slouken@1918
    99
    int pitch;
slouken@1918
   100
} GL_TextureData;
slouken@1918
   101
slouken@1918
   102
static GLFORMAT
slouken@1918
   103
PixelFormatToOpenGL(Uint32 format,)
slouken@1918
   104
{
slouken@1918
   105
    switch (format) {
slouken@1918
   106
    case SDL_PixelFormat_Index8:
slouken@1918
   107
        return GLFMT_P8;
slouken@1918
   108
    case SDL_PixelFormat_RGB332:
slouken@1918
   109
        return GLFMT_R3G3B2;
slouken@1918
   110
    case SDL_PixelFormat_RGB444:
slouken@1918
   111
        return GLFMT_X4R4G4B4;
slouken@1918
   112
    case SDL_PixelFormat_RGB555:
slouken@1918
   113
        return GLFMT_X1R5G5B5;
slouken@1918
   114
    case SDL_PixelFormat_ARGB4444:
slouken@1918
   115
        return GLFMT_A4R4G4B4;
slouken@1918
   116
    case SDL_PixelFormat_ARGB1555:
slouken@1918
   117
        return GLFMT_A1R5G5B5;
slouken@1918
   118
    case SDL_PixelFormat_RGB565:
slouken@1918
   119
        return GLFMT_R5G6B5;
slouken@1918
   120
    case SDL_PixelFormat_RGB888:
slouken@1918
   121
        return GLFMT_X8R8G8B8;
slouken@1918
   122
    case SDL_PixelFormat_ARGB8888:
slouken@1918
   123
        return GLFMT_A8R8G8B8;
slouken@1918
   124
    case SDL_PixelFormat_ARGB2101010:
slouken@1918
   125
        return GLFMT_A2R10G10B10;
slouken@1918
   126
    case SDL_PixelFormat_UYVY:
slouken@1918
   127
        return GLFMT_UYVY;
slouken@1918
   128
    case SDL_PixelFormat_YUY2:
slouken@1918
   129
        return GLFMT_YUY2;
slouken@1918
   130
    default:
slouken@1918
   131
        return GLFMT_UNKNOWN;
slouken@1918
   132
    }
slouken@1918
   133
}
slouken@1918
   134
slouken@1918
   135
void
slouken@1918
   136
GL_AddRenderDriver(_THIS)
slouken@1918
   137
{
slouken@1918
   138
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1918
   139
slouken@1918
   140
    if (data->d3d) {
slouken@1918
   141
        SDL_AddRenderDriver(0, &GL_RenderDriver);
slouken@1918
   142
    }
slouken@1918
   143
}
slouken@1918
   144
slouken@1918
   145
SDL_Renderer *
slouken@1918
   146
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1918
   147
{
slouken@1918
   148
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1918
   149
    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
slouken@1918
   150
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
slouken@1918
   151
    SDL_Renderer *renderer;
slouken@1918
   152
    GL_RenderData *data;
slouken@1918
   153
    HRESULT result;
slouken@1918
   154
    GLPRESENT_PARAMETERS pparams;
slouken@1918
   155
    IDirect3DSwapChain9 *chain;
slouken@1918
   156
slouken@1918
   157
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@1918
   158
        SDL_SetError
slouken@1918
   159
            ("The OpenGL renderer can only be used on OpenGL windows");
slouken@1918
   160
        return NULL;
slouken@1918
   161
    }
slouken@1918
   162
slouken@1918
   163
    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
slouken@1918
   164
    if (!renderer) {
slouken@1918
   165
        SDL_OutOfMemory();
slouken@1918
   166
        return NULL;
slouken@1918
   167
    }
slouken@1918
   168
    SDL_zerop(renderer);
slouken@1918
   169
slouken@1918
   170
    data = (GL_RenderData *) SDL_malloc(sizeof(*data));
slouken@1918
   171
    if (!data) {
slouken@1918
   172
        GL_DestroyRenderer(renderer);
slouken@1918
   173
        SDL_OutOfMemory();
slouken@1918
   174
        return NULL;
slouken@1918
   175
    }
slouken@1918
   176
    SDL_zerop(data);
slouken@1918
   177
slouken@1918
   178
    renderer->CreateTexture = GL_CreateTexture;
slouken@1918
   179
    renderer->SetTexturePalette = GL_SetTexturePalette;
slouken@1918
   180
    renderer->GetTexturePalette = GL_GetTexturePalette;
slouken@1918
   181
    renderer->UpdateTexture = GL_UpdateTexture;
slouken@1918
   182
    renderer->LockTexture = GL_LockTexture;
slouken@1918
   183
    renderer->UnlockTexture = GL_UnlockTexture;
slouken@1918
   184
    renderer->DirtyTexture = GL_DirtyTexture;
slouken@1918
   185
    renderer->RenderFill = GL_RenderFill;
slouken@1918
   186
    renderer->RenderCopy = GL_RenderCopy;
slouken@1918
   187
    renderer->RenderPresent = GL_RenderPresent;
slouken@1918
   188
    renderer->DestroyTexture = GL_DestroyTexture;
slouken@1918
   189
    renderer->DestroyRenderer = GL_DestroyRenderer;
slouken@1918
   190
    renderer->info = GL_RenderDriver.info;
slouken@1918
   191
    renderer->window = window->id;
slouken@1918
   192
    renderer->driverdata = data;
slouken@1918
   193
slouken@1918
   194
    renderer->info.flags =
slouken@1918
   195
        (SDL_Renderer_PresentDiscard | SDL_Renderer_Accelerated);
slouken@1918
   196
slouken@1918
   197
    data->context = SDL_GL_CreateContext(window->id);
slouken@1918
   198
    if (!data->context) {
slouken@1918
   199
        GL_DestroyRenderer(renderer);
slouken@1918
   200
        return NULL;
slouken@1918
   201
    }
slouken@1918
   202
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
slouken@1918
   203
        GL_DestroyRenderer(renderer);
slouken@1918
   204
        return NULL;
slouken@1918
   205
    }
slouken@1918
   206
    data->beginScene = SDL_TRUE;
slouken@1918
   207
slouken@1918
   208
    if (flags & SDL_Renderer_PresentVSync) {
slouken@1918
   209
        SDL_GL_SetSwapInterval(1);
slouken@1918
   210
    } else {
slouken@1918
   211
        SDL_GL_SetSwapInterval(0);
slouken@1918
   212
    }
slouken@1918
   213
    if (SDL_GL_GetSwapInterval() > 0) {
slouken@1918
   214
        renderer->info.flags |= SDL_Renderer_PresentVSync;
slouken@1918
   215
    }
slouken@1918
   216
slouken@1918
   217
    /* Set up parameters for rendering */
slouken@1918
   218
    glDisable(GL_DEPTH_TEST);
slouken@1918
   219
    glDisable(GL_CULL_FACE);
slouken@1918
   220
    glEnable(GL_TEXTURE_2D);
slouken@1918
   221
    glMatrixMode(GL_PROJECTION);
slouken@1918
   222
    glLoadIdentity();
slouken@1918
   223
    glMatrixMode(GL_MODELVIEW);
slouken@1918
   224
    glLoadIdentity();
slouken@1918
   225
    glViewport(0, 0, window->w, window->h);
slouken@1918
   226
    glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0, 0.0, 1.0);
slouken@1918
   227
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@1918
   228
slouken@1918
   229
    return renderer;
slouken@1918
   230
}
slouken@1918
   231
slouken@1918
   232
/* Quick utility function for texture creation */
slouken@1918
   233
static int
slouken@1918
   234
power_of_two(int input)
slouken@1918
   235
{
slouken@1918
   236
    int value = 1;
slouken@1918
   237
slouken@1918
   238
    while (value < input) {
slouken@1918
   239
        value <<= 1;
slouken@1918
   240
    }
slouken@1918
   241
    return value;
slouken@1918
   242
}
slouken@1918
   243
slouken@1918
   244
static int
slouken@1918
   245
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   246
{
slouken@1918
   247
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   248
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   249
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1918
   250
    GL_TextureData *data;
slouken@1918
   251
    GLPOOL pool;
slouken@1918
   252
    HRESULT result;
slouken@1918
   253
slouken@1918
   254
    data = (GL_TextureData *) SDL_malloc(sizeof(*data));
slouken@1918
   255
    if (!data) {
slouken@1918
   256
        SDL_OutOfMemory();
slouken@1918
   257
        return -1;
slouken@1918
   258
    }
slouken@1918
   259
    SDL_zerop(data);
slouken@1918
   260
slouken@1918
   261
    texture->driverdata = data;
slouken@1918
   262
slouken@1918
   263
    if (texture->access == SDL_TextureAccess_Local) {
slouken@1918
   264
        pool = GLPOOL_MANAGED;
slouken@1918
   265
    } else {
slouken@1918
   266
        pool = GLPOOL_DEFAULT;
slouken@1918
   267
    }
slouken@1918
   268
    result =
slouken@1918
   269
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1918
   270
                                       texture->h, 1, 0,
slouken@1918
   271
                                       PixelFormatToGLFMT(texture->format),
slouken@1918
   272
                                       pool, &data->texture, NULL);
slouken@1918
   273
    if (FAILED(result)) {
slouken@1918
   274
        SDL_free(data);
slouken@1918
   275
        GL_SetError("CreateTexture()", result);
slouken@1918
   276
        return -1;
slouken@1918
   277
    }
slouken@1918
   278
slouken@1918
   279
    return 0;
slouken@1918
   280
}
slouken@1918
   281
slouken@1918
   282
static int
slouken@1918
   283
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   284
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   285
{
slouken@1918
   286
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   287
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   288
slouken@1918
   289
    return 0;
slouken@1918
   290
}
slouken@1918
   291
slouken@1918
   292
static int
slouken@1918
   293
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   294
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1918
   295
{
slouken@1918
   296
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   297
slouken@1918
   298
    return 0;
slouken@1918
   299
}
slouken@1918
   300
slouken@1918
   301
static int
slouken@1918
   302
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   303
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1918
   304
{
slouken@1918
   305
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   306
    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
slouken@1918
   307
    IDirect3DTexture9 *temp;
slouken@1918
   308
    RECT d3drect;
slouken@1918
   309
    GLLOCKED_RECT locked;
slouken@1918
   310
    const Uint8 *src;
slouken@1918
   311
    Uint8 *dst;
slouken@1918
   312
    int row, length;
slouken@1918
   313
    HRESULT result;
slouken@1918
   314
slouken@1918
   315
    result =
slouken@1918
   316
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1918
   317
                                       texture->h, 1, 0,
slouken@1918
   318
                                       PixelFormatToGLFMT(texture->format),
slouken@1918
   319
                                       GLPOOL_SYSTEMMEM, &temp, NULL);
slouken@1918
   320
    if (FAILED(result)) {
slouken@1918
   321
        GL_SetError("CreateTexture()", result);
slouken@1918
   322
        return -1;
slouken@1918
   323
    }
slouken@1918
   324
slouken@1918
   325
    d3drect.left = rect->x;
slouken@1918
   326
    d3drect.right = rect->x + rect->w;
slouken@1918
   327
    d3drect.top = rect->y;
slouken@1918
   328
    d3drect.bottom = rect->y + rect->h;
slouken@1918
   329
slouken@1918
   330
    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
slouken@1918
   331
    if (FAILED(result)) {
slouken@1918
   332
        IDirect3DTexture9_Release(temp);
slouken@1918
   333
        GL_SetError("LockRect()", result);
slouken@1918
   334
        return -1;
slouken@1918
   335
    }
slouken@1918
   336
slouken@1918
   337
    src = pixels;
slouken@1918
   338
    dst = locked.pBits;
slouken@1918
   339
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1918
   340
    for (row = 0; row < rect->h; ++row) {
slouken@1918
   341
        SDL_memcpy(dst, src, length);
slouken@1918
   342
        src += pitch;
slouken@1918
   343
        dst += locked.Pitch;
slouken@1918
   344
    }
slouken@1918
   345
    IDirect3DTexture9_UnlockRect(temp, 0);
slouken@1918
   346
slouken@1918
   347
    result =
slouken@1918
   348
        IDirect3DDevice9_UpdateTexture(renderdata->device,
slouken@1918
   349
                                       (IDirect3DBaseTexture9 *) temp,
slouken@1918
   350
                                       (IDirect3DBaseTexture9 *) data->
slouken@1918
   351
                                       texture);
slouken@1918
   352
    IDirect3DTexture9_Release(temp);
slouken@1918
   353
    if (FAILED(result)) {
slouken@1918
   354
        GL_SetError("UpdateTexture()", result);
slouken@1918
   355
        return -1;
slouken@1918
   356
    }
slouken@1918
   357
    return 0;
slouken@1918
   358
}
slouken@1918
   359
slouken@1918
   360
static int
slouken@1918
   361
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   362
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   363
               int *pitch)
slouken@1918
   364
{
slouken@1918
   365
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   366
    RECT d3drect;
slouken@1918
   367
    GLLOCKED_RECT locked;
slouken@1918
   368
    HRESULT result;
slouken@1918
   369
slouken@1918
   370
    if (texture->access != SDL_TextureAccess_Local) {
slouken@1918
   371
        SDL_SetError("Can't lock remote video memory");
slouken@1918
   372
        return -1;
slouken@1918
   373
    }
slouken@1918
   374
slouken@1918
   375
    d3drect.left = rect->x;
slouken@1918
   376
    d3drect.right = rect->x + rect->w;
slouken@1918
   377
    d3drect.top = rect->y;
slouken@1918
   378
    d3drect.bottom = rect->y + rect->h;
slouken@1918
   379
slouken@1918
   380
    result =
slouken@1918
   381
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@1918
   382
                                   markDirty ? 0 : GLLOCK_NO_DIRTY_UPDATE);
slouken@1918
   383
    if (FAILED(result)) {
slouken@1918
   384
        GL_SetError("LockRect()", result);
slouken@1918
   385
        return -1;
slouken@1918
   386
    }
slouken@1918
   387
    *pixels = locked.pBits;
slouken@1918
   388
    *pitch = locked.Pitch;
slouken@1918
   389
    return 0;
slouken@1918
   390
}
slouken@1918
   391
slouken@1918
   392
static void
slouken@1918
   393
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   394
{
slouken@1918
   395
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   396
slouken@1918
   397
    IDirect3DTexture9_UnlockRect(data->texture, 0);
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
    RECT d3drect;
slouken@1918
   406
    int i;
slouken@1918
   407
slouken@1918
   408
    for (i = 0; i < numrects; ++i) {
slouken@1918
   409
        const SDL_Rect *rect = &rects[i];
slouken@1918
   410
slouken@1918
   411
        d3drect.left = rect->x;
slouken@1918
   412
        d3drect.right = rect->x + rect->w;
slouken@1918
   413
        d3drect.top = rect->y;
slouken@1918
   414
        d3drect.bottom = rect->y + rect->h;
slouken@1918
   415
slouken@1918
   416
        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
slouken@1918
   417
    }
slouken@1918
   418
}
slouken@1918
   419
slouken@1918
   420
static int
slouken@1918
   421
GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1918
   422
{
slouken@1918
   423
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   424
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1918
   425
    GLclampf r, g, b, a;
slouken@1918
   426
slouken@1918
   427
    a = ((GLclampf) ((color >> 24) & 0xFF)) / 255.0f;
slouken@1918
   428
    r = ((GLclampf) ((color >> 16) & 0xFF)) / 255.0f;
slouken@1918
   429
    g = ((GLclampf) ((color >> 8) & 0xFF)) / 255.0f;
slouken@1918
   430
    b = ((GLclampf) (color & 0xFF)) / 255.0f;
slouken@1918
   431
slouken@1918
   432
    glClearColor(r, g, b, a);
slouken@1918
   433
    glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
slouken@1918
   434
    glClear(GL_COLOR_BUFFER_BIT);
slouken@1918
   435
    glViewport(0, 0, window->w, window->h);
slouken@1918
   436
    return 0;
slouken@1918
   437
}
slouken@1918
   438
slouken@1918
   439
static int
slouken@1918
   440
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   441
              const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
   442
              int blendMode, int scaleMode)
slouken@1918
   443
{
slouken@1918
   444
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   445
    GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
slouken@1918
   446
    int minx, miny, maxx, maxy;
slouken@1918
   447
    GLfloat minu, maxu, minv, maxv;
slouken@1918
   448
slouken@1918
   449
    minx = dstrect->x;
slouken@1918
   450
    miny = dstrect->y;
slouken@1918
   451
    maxx = dstrect->x + dstrect->w;
slouken@1918
   452
    maxy = dstrect->y + dstrect->h;
slouken@1918
   453
slouken@1918
   454
    minu = (GLfloat) srcrect->x / texture->w;
slouken@1918
   455
    maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
slouken@1918
   456
    minv = (GLfloat) srcrect->y / texture->h;
slouken@1918
   457
    maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
slouken@1918
   458
slouken@1918
   459
    glBindTexture(GL_TEXTURE_2D, texturedata->texture);
slouken@1918
   460
slouken@1918
   461
    switch (blendMode) {
slouken@1918
   462
    case SDL_TextureBlendMode_None:
slouken@1918
   463
        glDisable(GL_BLEND);
slouken@1918
   464
        break;
slouken@1918
   465
    case SDL_TextureBlendMode_Mask:
slouken@1918
   466
    case SDL_TextureBlendMode_Blend:
slouken@1918
   467
        glEnable(GL_BLEND);
slouken@1918
   468
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1918
   469
        break;
slouken@1918
   470
    case SDL_TextureBlendMode_Add:
slouken@1918
   471
        glEnable(GL_BLEND);
slouken@1918
   472
        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
slouken@1918
   473
        break;
slouken@1918
   474
    case SDL_TextureBlendMode_Mod:
slouken@1918
   475
        glEnable(GL_BLEND);
slouken@1918
   476
        glBlendFunc(GL_ZERO, GL_SRC_COLOR);
slouken@1918
   477
        break;
slouken@1918
   478
    }
slouken@1918
   479
slouken@1918
   480
    switch (scaleMode) {
slouken@1918
   481
    case SDL_TextureScaleMode_None:
slouken@1918
   482
    case SDL_TextureScaleMode_Fast:
slouken@1918
   483
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
slouken@1918
   484
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
slouken@1918
   485
        break;
slouken@1918
   486
    case SDL_TextureScaleMode_Slow:
slouken@1918
   487
    case SDL_TextureScaleMode_Best:
slouken@1918
   488
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
slouken@1918
   489
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
slouken@1918
   490
        break;
slouken@1918
   491
    }
slouken@1918
   492
slouken@1918
   493
    glBegin(GL_TRIANGLE_STRIP);
slouken@1918
   494
    glTexCoord2f(minu, minv);
slouken@1918
   495
    glVertex2i(minx, miny);
slouken@1918
   496
    glTexCoord2f(maxu, minv);
slouken@1918
   497
    glVertex2i(maxx, miny);
slouken@1918
   498
    glTexCoord2f(minu, maxv);
slouken@1918
   499
    glVertex2i(miny, maxy);
slouken@1918
   500
    glTexCoord2f(maxu, maxv);
slouken@1918
   501
    glVertex2i(maxx, maxy);
slouken@1918
   502
    glEnd();
slouken@1918
   503
slouken@1918
   504
    return 0;
slouken@1918
   505
}
slouken@1918
   506
slouken@1918
   507
static void
slouken@1918
   508
GL_RenderPresent(SDL_Renderer * renderer)
slouken@1918
   509
{
slouken@1918
   510
    SDL_GL_SwapWindow(renderer->window);
slouken@1918
   511
}
slouken@1918
   512
slouken@1918
   513
static void
slouken@1918
   514
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1918
   515
{
slouken@1918
   516
    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
slouken@1918
   517
slouken@1918
   518
    if (!data) {
slouken@1918
   519
        return;
slouken@1918
   520
    }
slouken@1918
   521
    if (data->texture) {
slouken@1918
   522
        IDirect3DTexture9_Release(data->texture);
slouken@1918
   523
    }
slouken@1918
   524
    SDL_free(data);
slouken@1918
   525
    texture->driverdata = NULL;
slouken@1918
   526
}
slouken@1918
   527
slouken@1918
   528
void
slouken@1918
   529
GL_DestroyRenderer(SDL_Renderer * renderer)
slouken@1918
   530
{
slouken@1918
   531
    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
slouken@1918
   532
slouken@1918
   533
    if (data) {
slouken@1918
   534
        if (data->device) {
slouken@1918
   535
            IDirect3DDevice9_Release(data->device);
slouken@1918
   536
        }
slouken@1918
   537
        SDL_free(data);
slouken@1918
   538
    }
slouken@1918
   539
    SDL_free(renderer);
slouken@1918
   540
}
slouken@1918
   541
slouken@1918
   542
#endif /* SDL_VIDEO_OPENGL */
slouken@1918
   543
slouken@1918
   544
/* vi: set ts=4 sw=4 expandtab: */