src/video/SDL_renderer_gl.c
changeset 1924 69217fdd2c0a
parent 1923 d4572b97b08f
child 1926 307355678142
     1.1 --- a/src/video/SDL_renderer_gl.c	Sat Jul 22 19:51:48 2006 +0000
     1.2 +++ b/src/video/SDL_renderer_gl.c	Sat Jul 22 21:02:57 2006 +0000
     1.3 @@ -72,7 +72,7 @@
     1.4        SDL_TextureBlendMode_Mod),
     1.5       (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast |
     1.6        SDL_TextureScaleMode_Slow),
     1.7 -     18,
     1.8 +     16,
     1.9       {
    1.10        SDL_PixelFormat_Index1LSB,
    1.11        SDL_PixelFormat_Index1MSB,
    1.12 @@ -88,10 +88,8 @@
    1.13        SDL_PixelFormat_RGB888,
    1.14        SDL_PixelFormat_BGR888,
    1.15        SDL_PixelFormat_ARGB8888,
    1.16 -      SDL_PixelFormat_RGBA8888,
    1.17        SDL_PixelFormat_ABGR8888,
    1.18 -      SDL_PixelFormat_BGRA8888,
    1.19 -      SDL_PixelFormat_ARGB2101010},     /* FIXME: YUV texture support */
    1.20 +      SDL_PixelFormat_ARGB2101010},
    1.21       0,
    1.22       0}
    1.23  };
    1.24 @@ -115,6 +113,43 @@
    1.25  } GL_TextureData;
    1.26  
    1.27  
    1.28 +static void
    1.29 +GL_SetError(const char *prefix, GLenum result)
    1.30 +{
    1.31 +    const char *error;
    1.32 +
    1.33 +    switch (result) {
    1.34 +    case GL_NO_ERROR:
    1.35 +        error = "GL_NO_ERROR";
    1.36 +        break;
    1.37 +    case GL_INVALID_ENUM:
    1.38 +        error = "GL_INVALID_ENUM";
    1.39 +        break;
    1.40 +    case GL_INVALID_VALUE:
    1.41 +        error = "GL_INVALID_VALUE";
    1.42 +        break;
    1.43 +    case GL_INVALID_OPERATION:
    1.44 +        error = "GL_INVALID_OPERATION";
    1.45 +        break;
    1.46 +    case GL_STACK_OVERFLOW:
    1.47 +        error = "GL_STACK_OVERFLOW";
    1.48 +        break;
    1.49 +    case GL_STACK_UNDERFLOW:
    1.50 +        error = "GL_STACK_UNDERFLOW";
    1.51 +        break;
    1.52 +    case GL_OUT_OF_MEMORY:
    1.53 +        error = "GL_OUT_OF_MEMORY";
    1.54 +        break;
    1.55 +    case GL_TABLE_TOO_LARGE:
    1.56 +        error = "GL_TABLE_TOO_LARGE";
    1.57 +        break;
    1.58 +    default:
    1.59 +        error = "UNKNOWN";
    1.60 +        break;
    1.61 +    }
    1.62 +    SDL_SetError("%s: %s", prefix, error);
    1.63 +}
    1.64 +
    1.65  void
    1.66  GL_AddRenderDriver(_THIS)
    1.67  {
    1.68 @@ -130,9 +165,10 @@
    1.69      GL_RenderData *data;
    1.70  
    1.71      if (!(window->flags & SDL_WINDOW_OPENGL)) {
    1.72 -        SDL_SetError
    1.73 -            ("The OpenGL renderer can only be used on OpenGL windows");
    1.74 -        return NULL;
    1.75 +        window->flags |= SDL_WINDOW_OPENGL;
    1.76 +        if (SDL_RecreateWindow(window) < 0) {
    1.77 +            return NULL;
    1.78 +        }
    1.79      }
    1.80  
    1.81      renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    1.82 @@ -239,6 +275,7 @@
    1.83      GLint internalFormat;
    1.84      GLenum format, type;
    1.85      int texture_w, texture_h;
    1.86 +    GLenum result;
    1.87  
    1.88      switch (texture->format) {
    1.89      case SDL_PixelFormat_Index1LSB:
    1.90 @@ -289,8 +326,8 @@
    1.91          break;
    1.92      case SDL_PixelFormat_RGB888:
    1.93          internalFormat = GL_RGB8;
    1.94 -        format = GL_RGB;
    1.95 -        type = GL_UNSIGNED_INT_8_8_8_8;
    1.96 +        format = GL_BGRA;
    1.97 +        type = GL_UNSIGNED_BYTE;
    1.98          break;
    1.99      case SDL_PixelFormat_BGR24:
   1.100          internalFormat = GL_RGB8;
   1.101 @@ -299,28 +336,18 @@
   1.102          break;
   1.103      case SDL_PixelFormat_BGR888:
   1.104          internalFormat = GL_RGB8;
   1.105 -        format = GL_BGR;
   1.106 -        type = GL_UNSIGNED_INT_8_8_8_8;
   1.107 +        format = GL_RGBA;
   1.108 +        type = GL_UNSIGNED_BYTE;
   1.109          break;
   1.110      case SDL_PixelFormat_ARGB8888:
   1.111          internalFormat = GL_RGBA8;
   1.112          format = GL_BGRA;
   1.113 -        type = GL_UNSIGNED_INT_8_8_8_8_REV;
   1.114 -        break;
   1.115 -    case SDL_PixelFormat_RGBA8888:
   1.116 -        internalFormat = GL_RGBA8;
   1.117 -        format = GL_RGBA;
   1.118 -        type = GL_UNSIGNED_INT_8_8_8_8;
   1.119 +        type = GL_UNSIGNED_BYTE;
   1.120          break;
   1.121      case SDL_PixelFormat_ABGR8888:
   1.122          internalFormat = GL_RGBA8;
   1.123          format = GL_RGBA;
   1.124 -        type = GL_UNSIGNED_INT_8_8_8_8_REV;
   1.125 -        break;
   1.126 -    case SDL_PixelFormat_BGRA8888:
   1.127 -        internalFormat = GL_RGBA8;
   1.128 -        format = GL_BGRA;
   1.129 -        type = GL_UNSIGNED_INT_8_8_8_8;
   1.130 +        type = GL_UNSIGNED_BYTE;
   1.131          break;
   1.132      case SDL_PixelFormat_ARGB2101010:
   1.133          internalFormat = GL_RGB10_A2;
   1.134 @@ -340,7 +367,7 @@
   1.135  
   1.136      texture->driverdata = data;
   1.137  
   1.138 -    /* FIXME: Check for GL_ARB_texture_rectangle and GL_EXT_texture_rectangle */
   1.139 +    glGetError();
   1.140      glGenTextures(1, &data->texture);
   1.141  #ifdef USE_GL_TEXTURE_RECTANGLE
   1.142      data->type = GL_TEXTURE_RECTANGLE_ARB;
   1.143 @@ -360,7 +387,11 @@
   1.144      glBindTexture(data->type, data->texture);
   1.145      glTexImage2D(data->type, 0, internalFormat, texture_w, texture_h, 0,
   1.146                   format, type, NULL);
   1.147 -
   1.148 +    result = glGetError();
   1.149 +    if (result != GL_NO_ERROR) {
   1.150 +        GL_SetError("glTexImage2D()", result);
   1.151 +        return -1;
   1.152 +    }
   1.153      return 0;
   1.154  }
   1.155  
   1.156 @@ -383,19 +414,36 @@
   1.157      return 0;
   1.158  }
   1.159  
   1.160 +static void
   1.161 +SetupTextureUpdate(SDL_Texture * texture, int pitch)
   1.162 +{
   1.163 +    if (texture->format == SDL_PixelFormat_Index1LSB) {
   1.164 +        glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
   1.165 +    } else if (texture->format == SDL_PixelFormat_Index1MSB) {
   1.166 +        glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
   1.167 +    }
   1.168 +    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   1.169 +    glPixelStorei(GL_UNPACK_ROW_LENGTH,
   1.170 +                  pitch / SDL_BYTESPERPIXEL(texture->format));
   1.171 +}
   1.172 +
   1.173  static int
   1.174  GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.175                   const SDL_Rect * rect, const void *pixels, int pitch)
   1.176  {
   1.177      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   1.178 +    GLenum result;
   1.179  
   1.180 -    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);      /* FIXME, what to use for RGB 4 byte formats? */
   1.181 -    glPixelStorei(GL_UNPACK_ROW_LENGTH,
   1.182 -                  pitch / SDL_BYTESPERPIXEL(texture->format));
   1.183 +    glGetError();
   1.184 +    SetupTextureUpdate(texture, pitch);
   1.185      glBindTexture(data->type, data->texture);
   1.186      glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, rect->h,
   1.187                      data->format, data->formattype, pixels);
   1.188 -    /* FIXME: check for errors */
   1.189 +    result = glGetError();
   1.190 +    if (result != GL_NO_ERROR) {
   1.191 +        GL_SetError("glTexSubImage2D()", result);
   1.192 +        return -1;
   1.193 +    }
   1.194      return 0;
   1.195  }
   1.196  
   1.197 @@ -478,9 +526,7 @@
   1.198          int bpp = SDL_BYTESPERPIXEL(texture->format);
   1.199          int pitch = texturedata->pitch;
   1.200  
   1.201 -        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);  /* FIXME, what to use for RGB 4 byte formats? */
   1.202 -        glPixelStorei(GL_UNPACK_ROW_LENGTH,
   1.203 -                      pitch / SDL_BYTESPERPIXEL(texture->format));
   1.204 +        SetupTextureUpdate(texture, pitch);
   1.205          glBindTexture(texturedata->type, texturedata->texture);
   1.206          for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
   1.207              SDL_Rect *rect = &dirty->rect;