Added paletted OpenGL texture support.
authorSam Lantinga <slouken@libsdl.org>
Sun, 06 Aug 2006 23:34:59 +0000
changeset 197470deaf574153
parent 1973 81255f93dfcd
child 1975 ccef0d0c40c6
Added paletted OpenGL texture support.
src/SDL_compat.c
src/video/SDL_renderer_gl.c
     1.1 --- a/src/SDL_compat.c	Sun Aug 06 08:55:37 2006 +0000
     1.2 +++ b/src/SDL_compat.c	Sun Aug 06 23:34:59 2006 +0000
     1.3 @@ -286,7 +286,13 @@
     1.4          }
     1.5      }
     1.6      if (userdata == SDL_VideoSurface) {
     1.7 -        return SDL_SetDisplayPalette(palette->colors, 0, palette->ncolors);
     1.8 +        if (SDL_SetDisplayPalette(palette->colors, 0, palette->ncolors) < 0) {
     1.9 +            return -1;
    1.10 +        }
    1.11 +        if (SDL_SetTexturePalette
    1.12 +            (SDL_VideoTexture, palette->colors, 0, palette->ncolors) < 0) {
    1.13 +            return -1;
    1.14 +        }
    1.15      }
    1.16      return 0;
    1.17  }
     2.1 --- a/src/video/SDL_renderer_gl.c	Sun Aug 06 08:55:37 2006 +0000
     2.2 +++ b/src/video/SDL_renderer_gl.c	Sun Aug 06 23:34:59 2006 +0000
     2.3 @@ -66,8 +66,8 @@
     2.4      GL_CreateRenderer,
     2.5      {
     2.6       "opengl",
     2.7 -     (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC |
     2.8 -      SDL_RENDERER_ACCELERATED),
     2.9 +     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
    2.10 +      SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
    2.11       (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
    2.12        SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
    2.13        SDL_TEXTUREBLENDMODE_MOD),
    2.14 @@ -99,6 +99,7 @@
    2.15  {
    2.16      SDL_GLContext context;
    2.17      SDL_bool updateSize;
    2.18 +    SDL_bool GL_EXT_paletted_texture_supported;
    2.19      SDL_bool GL_ARB_texture_rectangle_supported;
    2.20      int blendMode;
    2.21      int scaleMode;
    2.22 @@ -107,6 +108,8 @@
    2.23  #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
    2.24  #include "SDL_glfuncs.h"
    2.25  #undef SDL_PROC
    2.26 +
    2.27 +    PFNGLCOLORTABLEEXTPROC glColorTableEXT;
    2.28  } GL_RenderData;
    2.29  
    2.30  typedef struct
    2.31 @@ -117,6 +120,7 @@
    2.32      GLfloat texh;
    2.33      GLenum format;
    2.34      GLenum formattype;
    2.35 +    Uint8 *palette;
    2.36      void *pixels;
    2.37      int pitch;
    2.38      SDL_DirtyRectList dirty;
    2.39 @@ -200,7 +204,14 @@
    2.40      SDL_Renderer *renderer;
    2.41      GL_RenderData *data;
    2.42      GLint value;
    2.43 +    int doublebuffer;
    2.44  
    2.45 +    /* Render directly to the window, unless we're compositing */
    2.46 +#ifndef __MACOSX__
    2.47 +    if (flags & SDL_RENDERER_SINGLEBUFFER) {
    2.48 +        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
    2.49 +    }
    2.50 +#endif
    2.51      if (!(window->flags & SDL_WINDOW_OPENGL)) {
    2.52          if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
    2.53              return NULL;
    2.54 @@ -265,6 +276,12 @@
    2.55          renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
    2.56      }
    2.57  
    2.58 +    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
    2.59 +        if (!doublebuffer) {
    2.60 +            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
    2.61 +        }
    2.62 +    }
    2.63 +
    2.64      data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
    2.65      renderer->info.max_texture_width = value;
    2.66      data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
    2.67 @@ -274,6 +291,21 @@
    2.68          || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
    2.69          data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
    2.70      }
    2.71 +    if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) {
    2.72 +        data->GL_EXT_paletted_texture_supported = SDL_TRUE;
    2.73 +        data->glColorTableEXT =
    2.74 +            (PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT");
    2.75 +    } else {
    2.76 +        /* Don't advertise support for 8-bit indexed texture format */
    2.77 +        Uint32 i, j;
    2.78 +        SDL_RendererInfo *info = &renderer->info;
    2.79 +        for (i = 0, j = 0; i < info->num_texture_formats; ++i) {
    2.80 +            if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) {
    2.81 +                info->texture_formats[j++] = info->texture_formats[i];
    2.82 +            }
    2.83 +        }
    2.84 +        --info->num_texture_formats;
    2.85 +    }
    2.86  
    2.87      /* Set up parameters for rendering */
    2.88      data->blendMode = -1;
    2.89 @@ -351,7 +383,11 @@
    2.90          type = GL_BITMAP;
    2.91          break;
    2.92      case SDL_PIXELFORMAT_INDEX8:
    2.93 -        internalFormat = GL_RGB;
    2.94 +        if (!renderdata->GL_EXT_paletted_texture_supported) {
    2.95 +            SDL_SetError("Unsupported texture format");
    2.96 +            return -1;
    2.97 +        }
    2.98 +        internalFormat = GL_COLOR_INDEX8_EXT;
    2.99          format = GL_COLOR_INDEX;
   2.100          type = GL_UNSIGNED_BYTE;
   2.101          break;
   2.102 @@ -431,6 +467,16 @@
   2.103          return -1;
   2.104      }
   2.105  
   2.106 +    if (texture->format == SDL_PIXELFORMAT_INDEX8) {
   2.107 +        data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8));
   2.108 +        if (!data->palette) {
   2.109 +            SDL_OutOfMemory();
   2.110 +            SDL_free(data);
   2.111 +            return -1;
   2.112 +        }
   2.113 +        SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8));
   2.114 +    }
   2.115 +
   2.116      texture->driverdata = data;
   2.117  
   2.118      renderdata->glGetError();
   2.119 @@ -467,7 +513,22 @@
   2.120  {
   2.121      GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   2.122      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   2.123 +    Uint8 *palette;
   2.124  
   2.125 +    if (!data->palette) {
   2.126 +        SDL_SetError("Texture doesn't have a palette");
   2.127 +        return -1;
   2.128 +    }
   2.129 +    palette = data->palette + firstcolor * 3;
   2.130 +    while (ncolors--) {
   2.131 +        *palette++ = colors->r;
   2.132 +        *palette++ = colors->g;
   2.133 +        *palette++ = colors->b;
   2.134 +        ++colors;
   2.135 +    }
   2.136 +    renderdata->glBindTexture(data->type, data->texture);
   2.137 +    renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
   2.138 +                                GL_UNSIGNED_BYTE, data->palette);
   2.139      return 0;
   2.140  }
   2.141  
   2.142 @@ -475,8 +536,22 @@
   2.143  GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   2.144                       SDL_Color * colors, int firstcolor, int ncolors)
   2.145  {
   2.146 +    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   2.147      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   2.148 +    Uint8 *palette;
   2.149  
   2.150 +    if (!data->palette) {
   2.151 +        SDL_SetError("Texture doesn't have a palette");
   2.152 +        return -1;
   2.153 +    }
   2.154 +    palette = data->palette + firstcolor * 3;
   2.155 +    while (ncolors--) {
   2.156 +        colors->r = *palette++;
   2.157 +        colors->g = *palette++;
   2.158 +        colors->b = *palette++;
   2.159 +        colors->unused = SDL_ALPHA_OPAQUE;
   2.160 +        ++colors;
   2.161 +    }
   2.162      return 0;
   2.163  }
   2.164  
   2.165 @@ -703,6 +778,9 @@
   2.166      if (data->texture) {
   2.167          renderdata->glDeleteTextures(1, &data->texture);
   2.168      }
   2.169 +    if (data->palette) {
   2.170 +        SDL_free(data->palette);
   2.171 +    }
   2.172      if (data->pixels) {
   2.173          SDL_free(data->pixels);
   2.174      }