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