[Fix] Some outstanding bugs in OpenGL textures.

This commit is contained in:
Allanis 2013-06-02 14:12:15 +01:00
parent a6805df42d
commit d8f3372abe

View File

@ -158,6 +158,7 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
Uint32 saved_flags; Uint32 saved_flags;
Uint8 saved_alpha; Uint8 saved_alpha;
int potw, poth; int potw, poth;
SDL_Rect rtemp;
// Make size power of two. // Make size power of two.
potw = pot(surface->w); potw = pot(surface->w);
@ -165,64 +166,41 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
if(rw)*rw = potw; if(rw)*rw = potw;
if(rh)*rh = poth; if(rh)*rh = poth;
if((surface->w != potw) || (surface->h != poth)) { rtemp.x = rtemp.y = 0;
// Size isn't original. rtemp.w = surface->w;
SDL_Rect rtemp; rtemp.h = surface->h;
rtemp.x = rtemp.y = 0;
rtemp.w = surface->w;
rtemp.h = surface->h;
// Save alpha. // Save alpha.
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
saved_alpha = surface->format->alpha; saved_alpha = surface->format->alpha;
if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
SDL_SetAlpha(surface, 0, 0); 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. // Create the temp POT surface.
tmp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, tmp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY,
potw, poth, surface->format->BytesPerPixel*8, RGBMASK); potw, poth, surface->format->BytesPerPixel*8, RGBMASK);
if(tmp == NULL) { if(tmp == NULL) {
WARN("Unable to create POT surface %s", SDL_GetError()); WARN("Unable to create POT surface: %s", SDL_GetError());
return 0; return 0;
} }
if(SDL_FillRect(tmp, NULL, SDL_MapRGBA(surface->format, 0, 0, 0, if(SDL_FillRect(tmp, NULL,
SDL_ALPHA_TRANSPARENT))) { SDL_MapRGBA(surface->format, 0, 0, 0, SDL_ALPHA_TRANSPARENT))) {
WARN("Unable to fill rect: %s", SDL_GetError()); WARN("Unable to fill rect: %s", SDL_GetError());
return 0; 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);
} }
// 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. glGenTextures(1, &texture); // Create the texure.
glBindTexture(GL_TEXTURE_2D, texture); // Load the texture. 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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Now lead the texture data up.
SDL_LockSurface(surface); SDL_LockSurface(surface);
glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel,
surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
// Cleanup.
SDL_UnlockSurface(surface); SDL_UnlockSurface(surface);
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
@ -752,12 +732,18 @@ int gl_init(void) {
// Some openGL options. // Some openGL options.
glClearColor(0., 0., 0., 1.); glClearColor(0., 0., 0., 1.);
// Enable/Disable.
glDisable(GL_DEPTH_TEST); // Set for doing 2D shidazles. glDisable(GL_DEPTH_TEST); // Set for doing 2D shidazles.
//glEnable(GL_TEXTURE_2D); // Don't enable globally, it will break non-texture blits. //glEnable(GL_TEXTURE_2D); // Don't enable globally, it will break non-texture blits.
glDisable(GL_LIGHTING); // No lighting, it is done when rendered. glDisable(GL_LIGHTING); // No lighting, it is done when rendered.
glEnable(GL_BLEND); // Alpha blending ftw. glEnable(GL_BLEND); // Alpha blending ftw.
// Models.
glShadeModel(GL_FLAT); // Default shade model. Functions should keep this when done.. glShadeModel(GL_FLAT); // Default shade model. Functions should keep this when done..
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Good blend model. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Good blend model.
// Set up the matrix.
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
glOrtho(-SCREEN_W /2, // Left edge. glOrtho(-SCREEN_W /2, // Left edge.
@ -767,8 +753,9 @@ int gl_init(void) {
-1., // Near. -1., // Near.
1.); // Far. 1.); // Far.
glClear(GL_COLOR_BUFFER_BIT);
// Finishing touches.
glClear(GL_COLOR_BUFFER_BIT); // Must clear the buffer first.
gl_checkErr(); gl_checkErr();
return 0; return 0;