From d8f3372abe85ddd29568349c0b253f53c289da33 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sun, 2 Jun 2013 14:12:15 +0100 Subject: [PATCH] [Fix] Some outstanding bugs in OpenGL textures. --- src/opengl.c | 97 +++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 55 deletions(-) diff --git a/src/opengl.c b/src/opengl.c index 0e9d8dd..8fc3cff 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -158,6 +158,7 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) { Uint32 saved_flags; Uint8 saved_alpha; int potw, poth; + SDL_Rect rtemp; // Make size power of two. potw = pot(surface->w); @@ -165,64 +166,41 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) { if(rw)*rw = potw; if(rh)*rh = poth; - if((surface->w != potw) || (surface->h != poth)) { - // Size isn't original. - SDL_Rect rtemp; - rtemp.x = rtemp.y = 0; - rtemp.w = surface->w; - rtemp.h = surface->h; + rtemp.x = rtemp.y = 0; + rtemp.w = surface->w; + rtemp.h = surface->h; - // Save alpha. - saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); - saved_alpha = surface->format->alpha; - if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) - SDL_SetAlpha(surface, 0, 0); + // Save alpha. + saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); + saved_alpha = surface->format->alpha; + if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) + SDL_SetAlpha(surface, 0, SDL_ALPHA_OPAQUE); + if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) + SDL_SetColorKey(surface, 0, surface->format->colorkey); - // Create the temp POT surface. - tmp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, - potw, poth, surface->format->BytesPerPixel*8, RGBMASK); - if(tmp == NULL) { - WARN("Unable to create POT surface %s", SDL_GetError()); - return 0; - } - if(SDL_FillRect(tmp, NULL, SDL_MapRGBA(surface->format, 0, 0, 0, - SDL_ALPHA_TRANSPARENT))) { - WARN("Unable to fill rect: %s", SDL_GetError()); - return 0; - } - - SDL_BlitSurface(surface, &rtemp, tmp, &rtemp); - SDL_FreeSurface(surface); - - surface = tmp; - - // Set saved alpha. - if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) - SDL_SetAlpha(surface, 0, 0); - - // Create the temp POT surface. - tmp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, - potw, poth, surface->format->BytesPerPixel*8, RGBMASK); - if(tmp == NULL) { - WARN("Unable to create POT surface %s", SDL_GetError()); - return 0; - } - if(SDL_FillRect(tmp, NULL, SDL_MapRGBA(surface->format, 0, 0, 0, - SDL_ALPHA_TRANSPARENT))) { - WARN("Unable to fill rect: %s", SDL_GetError()); - return 0; - } - - SDL_BlitSurface(surface, &rtemp, tmp, &rtemp); - SDL_FreeSurface(surface); - - surface = tmp; - - // Set saved alpha. - if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) - SDL_SetAlpha(surface, saved_flags, saved_alpha); + // Create the temp POT surface. + tmp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, + potw, poth, surface->format->BytesPerPixel*8, RGBMASK); + if(tmp == NULL) { + WARN("Unable to create POT surface: %s", SDL_GetError()); + return 0; + } + if(SDL_FillRect(tmp, NULL, + SDL_MapRGBA(surface->format, 0, 0, 0, SDL_ALPHA_TRANSPARENT))) { + WARN("Unable to fill rect: %s", SDL_GetError()); + return 0; } + // Change the surface to the new blitted one. + SDL_BlitSurface(surface, &rtemp, tmp, &rtemp); + SDL_FreeSurface(surface); + surface = tmp; + + // Set saved alpha. + if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) + SDL_SetAlpha(surface, 0, 0); + + // Opengl texture binding. glGenTextures(1, &texture); // Create the texure. glBindTexture(GL_TEXTURE_2D, texture); // Load the texture. @@ -231,10 +209,12 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + // Now lead the texture data up. SDL_LockSurface(surface); glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + // Cleanup. SDL_UnlockSurface(surface); SDL_FreeSurface(surface); @@ -752,12 +732,18 @@ int gl_init(void) { // Some openGL options. glClearColor(0., 0., 0., 1.); + + // Enable/Disable. glDisable(GL_DEPTH_TEST); // Set for doing 2D shidazles. //glEnable(GL_TEXTURE_2D); // Don't enable globally, it will break non-texture blits. glDisable(GL_LIGHTING); // No lighting, it is done when rendered. glEnable(GL_BLEND); // Alpha blending ftw. + + // Models. glShadeModel(GL_FLAT); // Default shade model. Functions should keep this when done.. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Good blend model. + + // Set up the matrix. glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-SCREEN_W /2, // Left edge. @@ -767,8 +753,9 @@ int gl_init(void) { -1., // Near. 1.); // Far. - glClear(GL_COLOR_BUFFER_BIT); + // Finishing touches. + glClear(GL_COLOR_BUFFER_BIT); // Must clear the buffer first. gl_checkErr(); return 0;