From 61b51eb9ed00183d091b020d051b09982fb8aa76 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 6 Jan 2012 23:34:49 +0000 Subject: [PATCH] - finally a fix for upside down icon (uses png file) --- .../shared_lib/sources/graphics/texture.cpp | 103 +++++++++++++++++ .../sources/platform/sdl/gl_wrap.cpp | 104 ------------------ 2 files changed, 103 insertions(+), 104 deletions(-) diff --git a/source/shared_lib/sources/graphics/texture.cpp b/source/shared_lib/sources/graphics/texture.cpp index a9595fd8..ff60726c 100644 --- a/source/shared_lib/sources/graphics/texture.cpp +++ b/source/shared_lib/sources/graphics/texture.cpp @@ -26,6 +26,16 @@ const int Texture::defaultSize = 256; const int Texture::defaultComponents = 4; bool Texture::useTextureCompression = false; +/* Quick utility function for texture creation */ +static int powerOfTwo(int input) { + int value = 1; + + while (value < input) { + value <<= 1; + } + return value; +} + Texture::Texture() { assert(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false); @@ -86,6 +96,99 @@ SDL_Surface* Texture2D::CreateSDLSurface(bool newPixelData) const { delete[] surfData; } +// SDL_Surface *prepGLTexture(SDL_Surface *surface, GLfloat *texCoords = NULL, const bool +// freeSource = false) { + /* Use the surface width and height expanded to powers of 2 */ + int w = powerOfTwo(surface->w); + int h = powerOfTwo(surface->h); +// if (texCoords != 0) { +// texCoords[0] = 0.0f; /* Min +// X */ +// texCoords[1] = 0.0f; /* Min +// Y */ +// texCoords[2] = (GLfloat)surface->w / w; /* Max X */ +// texCoords[3] = (GLfloat)surface->h / h; /* Max Y */ +// } + + SDL_Surface *image = SDL_CreateRGBSurface( + SDL_SWSURFACE, + w, h, + 32, + #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ + 0x000000FF, + 0x0000FF00, + 0x00FF0000, + 0xFF000000 + #else + 0xFF000000, + 0x00FF0000, + 0x0000FF00, + 0x000000FF + #endif + ); + if ( image == NULL ) { + return 0; + } + + /* Save the alpha blending attributes */ + Uint32 savedFlags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); + Uint8 savedAlpha = surface->format->alpha; + if ( (savedFlags & SDL_SRCALPHA) == SDL_SRCALPHA ) { + SDL_SetAlpha(surface, 0, 0); + } + + SDL_Rect srcArea, destArea; + /* Copy the surface into the GL texture image */ + srcArea.x = 0; destArea.x = 0; + /* Copy it in at the bottom, because we're going to flip + this image upside-down in a moment + */ + srcArea.y = 0; destArea.y = h - surface->h; + srcArea.w = surface->w; + srcArea.h = surface->h; + SDL_BlitSurface(surface, &srcArea, image, &destArea); + + /* Restore the alpha blending attributes */ + if ((savedFlags & SDL_SRCALPHA) == SDL_SRCALPHA) { + SDL_SetAlpha(surface, savedFlags, savedAlpha); + } + + /* Turn the image upside-down, because OpenGL textures + start at the bottom-left, instead of the top-left + */ + #ifdef _MSC_VER + Uint8 *line = new Uint8[image->pitch]; + #else + Uint8 line[image->pitch]; + #endif + /* These two make the following more readable */ + Uint8 *pixels = static_cast(image->pixels); + Uint16 pitch = image->pitch; + int ybegin = 0; + int yend = image->h - 1; + + // TODO: consider if this lock is legal/appropriate + if (SDL_MUSTLOCK(image)) { SDL_LockSurface(image); } + while (ybegin < yend) { + memcpy(line, pixels + pitch*ybegin, pitch); + memcpy(pixels + pitch*ybegin, pixels + pitch*yend, pitch); + memcpy(pixels + pitch*yend, line, pitch); + ybegin++; + yend--; + } + if (SDL_MUSTLOCK(image)) { SDL_UnlockSurface(image); } + +// if (freeSource) { +// SDL_FreeSurface(surface); +// } + + #ifdef _MSC_VER + delete[] line; + #endif + + return image; +// } + return surface; } diff --git a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp index 552f642c..62a47363 100644 --- a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp +++ b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp @@ -50,109 +50,6 @@ PlatformContextGl::~PlatformContextGl() { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } -/* Quick utility function for texture creation */ -static int powerOfTwo(int input) { - int value = 1; - - while (value < input) { - value <<= 1; - } - return value; -} - -SDL_Surface *prepGLTexture(SDL_Surface *surface, GLfloat *texCoords = NULL, const bool -freeSource = false) { - /* Use the surface width and height expanded to powers of 2 */ - int w = powerOfTwo(surface->w); - int h = powerOfTwo(surface->h); - if (texCoords != 0) { - texCoords[0] = 0.0f; /* Min -X */ - texCoords[1] = 0.0f; /* Min -Y */ - texCoords[2] = (GLfloat)surface->w / w; /* Max X */ - texCoords[3] = (GLfloat)surface->h / h; /* Max Y */ - } - - SDL_Surface *image = SDL_CreateRGBSurface( - SDL_SWSURFACE, - w, h, - 32, -#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ - 0x000000FF, - 0x0000FF00, - 0x00FF0000, - 0xFF000000 -#else - 0xFF000000, - 0x00FF0000, - 0x0000FF00, - 0x000000FF -#endif - ); - if ( image == NULL ) { - return 0; - } - - /* Save the alpha blending attributes */ - Uint32 savedFlags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); - Uint8 savedAlpha = surface->format->alpha; - if ( (savedFlags & SDL_SRCALPHA) == SDL_SRCALPHA ) { - SDL_SetAlpha(surface, 0, 0); - } - - SDL_Rect srcArea, destArea; - /* Copy the surface into the GL texture image */ - srcArea.x = 0; destArea.x = 0; - /* Copy it in at the bottom, because we're going to flip - this image upside-down in a moment - */ - srcArea.y = 0; destArea.y = h - surface->h; - srcArea.w = surface->w; - srcArea.h = surface->h; - SDL_BlitSurface(surface, &srcArea, image, &destArea); - - /* Restore the alpha blending attributes */ - if ((savedFlags & SDL_SRCALPHA) == SDL_SRCALPHA) { - SDL_SetAlpha(surface, savedFlags, savedAlpha); - } - - /* Turn the image upside-down, because OpenGL textures - start at the bottom-left, instead of the top-left - */ -#ifdef _MSC_VER - Uint8 *line = new Uint8[image->pitch]; -#else - Uint8 line[image->pitch]; -#endif - /* These two make the following more readable */ - Uint8 *pixels = static_cast(image->pixels); - Uint16 pitch = image->pitch; - int ybegin = 0; - int yend = image->h - 1; - - // TODO: consider if this lock is legal/appropriate - if (SDL_MUSTLOCK(image)) { SDL_LockSurface(image); } - while (ybegin < yend) { - memcpy(line, pixels + pitch*ybegin, pitch); - memcpy(pixels + pitch*ybegin, pixels + pitch*yend, pitch); - memcpy(pixels + pitch*yend, line, pitch); - ybegin++; - yend--; - } - if (SDL_MUSTLOCK(image)) { SDL_UnlockSurface(image); } - - if (freeSource) { - SDL_FreeSurface(surface); - } - -#ifdef _MSC_VER - delete[] line; -#endif - - return image; -} - void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, bool hardware_acceleration, bool fullscreen_anti_aliasing, float gammaValue) { @@ -214,7 +111,6 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, //printf("Loading icon [%s]\n",mg_icon_file.c_str()); if(extractExtension(mg_icon_file) == "bmp") { icon = SDL_LoadBMP(mg_icon_file.c_str()); - icon = prepGLTexture(icon); } else { //printf("Loadng png icon\n");