From 15178a25faa12800f063dc00ed629d7fbbe514c7 Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 13 Oct 2003 21:40:05 +0000 Subject: [PATCH] SDLGL code move --- include/ZE_Includes.h | 3 +- include/ZE_Utility.h | 31 +++++++++++++++-- src/ZE_Utility.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++- src/ZE_ZImage.cpp | 4 +-- 4 files changed, 108 insertions(+), 8 deletions(-) diff --git a/include/ZE_Includes.h b/include/ZE_Includes.h index 478512e..5b3b74a 100644 --- a/include/ZE_Includes.h +++ b/include/ZE_Includes.h @@ -14,7 +14,7 @@ ZE_*.h files should only include this file and any other ZE_*.h files that they need, External Library or C/C++ Standard Library files should be included from within this file. - $Id: ZE_Includes.h,v 1.19 2003/10/13 20:59:30 cozman Exp $
+ $Id: ZE_Includes.h,v 1.20 2003/10/13 21:40:05 cozman Exp $
\author James Turk **/ @@ -26,7 +26,6 @@ #include "SDL.h" #if (GFX_BACKEND == ZE_OGL) #include "SDL_opengl.h" -#include "external/SDLGL_Util.h" #endif #ifdef USE_SDL_IMAGE #include "SDL_image.h" diff --git a/include/ZE_Utility.h b/include/ZE_Utility.h index de13936..e2178c3 100755 --- a/include/ZE_Utility.h +++ b/include/ZE_Utility.h @@ -14,7 +14,7 @@ Definition file for ZEngine Utilities which are used throughout the engine and can be used in conjunction with ZEngine. -
$Id: ZE_Utility.h,v 1.7 2003/10/05 19:42:34 cozman Exp $
+
$Id: ZE_Utility.h,v 1.8 2003/10/13 21:40:05 cozman Exp $
\author James Turk **/ @@ -42,8 +42,8 @@ std::string FormatStr(const char *fmtstr, ...); \brief Extracts a SDL_RWops memory structure from a zip archive. Attempts to open a file from within a zipfile and return a SDL_RWops which can be used - to load a resource from memory. Used by 'LoadFromZip' members of ZImage/ZSound/ZFont - so generally not called. + to load a resource from memory. + Used internally, generally shouldn't be called by users. \param zipname Name of zip-format archive to open. \param filename Name of file within archive to access. \return On success, pointer to SDL_RWops, on failure, NULL. @@ -51,6 +51,31 @@ std::string FormatStr(const char *fmtstr, ...); **/ SDL_RWops* RWFromZip(std::string zipname, std::string filename); +/*! + \brief Rounds a number up to the nearest power of two. + + Rounds a number up to the next highest power of two, used for OpenGL textures. (From testgl.c) + Used internally, generally shouldn't be called by users. + \param in Number to round up. + \return num rounded up to closest power of two. + \since 0.8.6 +**/ +int PowerOfTwo(int num); + +/*! + \brief Converts an SDL_Surface to an OpenGL texture ID. + + Given an SDL_Surface returns a texture ID representing the OpenGL + texture assigned to that surface. Also returns texture coordinates + via texcoord parameter. (From SDL_GL_LoadTexture in testgl.c) + Used internally, generally shouldn't be called by users. + \param surface SDL_Surface to assign an OpenGL ID, returns unmodified. + \param texcoord Should be an array of 4 GLfloat, assigned texture coordinates for OpenGL use. + \return OpenGL texture ID for SDL_Surface, 0 if an error occurs. + \since 0.8.6 +**/ +GLuint SurfaceToTexture(SDL_Surface *surface, GLfloat *texcoord); + /*! \brief Properly free SDL_Surface. diff --git a/src/ZE_Utility.cpp b/src/ZE_Utility.cpp index 9b82d38..3d0fa37 100755 --- a/src/ZE_Utility.cpp +++ b/src/ZE_Utility.cpp @@ -13,7 +13,7 @@ \brief Source file for ZEngine utility functions. Source file containing open utilities for use inside and alongside ZEngine. -
$Id: ZE_Utility.cpp,v 1.11 2003/09/24 02:03:18 cozman Exp $
+
$Id: ZE_Utility.cpp,v 1.12 2003/10/13 21:40:05 cozman Exp $
\author James Turk **/ @@ -75,6 +75,82 @@ SDL_RWops* RWFromZip(std::string zipname, std::string filename) return SDL_RWFromMem(buffer, info.uncompressed_size); //return buffer in RW form } +//from SDL's testgl.c power_of_two +int PowerOfTwo(int num) +{ + int value = 1; + + while(value < num) //texture coord must be >= input + value <<= 1; //value <<= 1 is the same as value *= 2 + return value; +} + +//from SDL's testgl.c SDL_GL_LoadTexture +GLuint SurfaceToTexture(SDL_Surface *surface, GLfloat *texcoord) +{ + GLuint texture; + int w, h; + SDL_Surface *temp; + SDL_Rect area; + Uint32 saved_flags; + Uint8 saved_alpha; + + //expand width and height to nearest powers of 2 + w = PowerOfTwo(surface->w); + h = PowerOfTwo(surface->h); + texcoord[0] = 0.0f; //min X + texcoord[1] = 0.0f; //min Y + texcoord[2] = (GLfloat)surface->w / w; //max X + texcoord[3] = (GLfloat)surface->h / h; //max Y + + temp = SDL_CreateRGBSurface( + SDL_SWSURFACE, + w, h, + 32, +#if SDL_BYTEORDER == SDL_LIL_ENDIAN //endian specific color masks + 0x000000FF, + 0x0000FF00, + 0x00FF0000, + 0xFF000000 +#else + 0xFF000000, + 0x00FF0000, + 0x0000FF00, + 0x000000FF +#endif + ); + if(!temp) //failure in CreateRGBSurface + return 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, 0); + + //copy surface (do not alter passed surface to allow this function to be used in special situations) + area.x = 0; + area.y = 0; + area.w = static_cast(surface->w); + area.h = static_cast(surface->h); + SDL_BlitSurface(surface, &area, temp, &area); + + //restore saved alpha + if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) + SDL_SetAlpha(surface, saved_flags, saved_alpha); + + //create the OpenGL texture + glGenTextures(1, &texture); + //setup texture parmaters + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp->pixels); + SDL_FreeSurface(temp); //temp surface no longer needed + + return texture; +} + //Each of the Free*s safely frees & NULLs the pointer void FreeImage(SDL_Surface *&image) { diff --git a/src/ZE_ZImage.cpp b/src/ZE_ZImage.cpp index a3da999..0888325 100644 --- a/src/ZE_ZImage.cpp +++ b/src/ZE_ZImage.cpp @@ -13,7 +13,7 @@ \brief Source file for ZImage. Implementation of ZImage, the Image class for ZEngine. -
$Id: ZE_ZImage.cpp,v 1.48 2003/10/11 16:21:50 cozman Exp $
+
$Id: ZE_ZImage.cpp,v 1.49 2003/10/13 21:40:05 cozman Exp $
\author James Turk **/ @@ -172,7 +172,7 @@ void ZImage::Attach(SDL_Surface *surface) rWidth = static_cast(surface->w); rHeight = static_cast(surface->h); - rTexID = SDL_GL_LoadTexture(surface,coord); //major helper, not written by me, from libsdl.org + rTexID = SurfaceToTexture(surface,coord); rTexMinX = coord[0]; rTexMinY = coord[1]; rTexMaxX = coord[2];