From: Côme BERNIGAUD Date: Wed, 18 May 2011 13:44:44 +0000 (+0200) Subject: adding missing source files X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;ds=sidebyside;h=01a48eb9a98fa8c7d407077aa69d493e74eb86cd;hp=e4c69ab5c1d15cdb4a909de8ba8f2057fe6f095d;p=lugaru.git adding missing source files --- diff --git a/Source/Texture.cpp b/Source/Texture.cpp new file mode 100644 index 0000000..37d4073 --- /dev/null +++ b/Source/Texture.cpp @@ -0,0 +1,92 @@ +#include "gamegl.h" +#include "Texture.h" +#include "TGALoader.h" + +using namespace std; + +map Texture::textures; + +extern TGAImageRec texture; +extern bool trilinear; + +void Texture::load() { + GLuint type; + + LOGFUNC; + + LOG(std::string("Loading texture...") + fileName); + + unsigned char fileNamep[256]; + CopyCStringToPascal(ConvertFileName(fileName.c_str()), fileNamep); + //Load Image + upload_image( fileNamep ,hasalpha); + + //Alpha channel? + if ( texture.bpp == 24 ) + type = GL_RGB; + else + type = GL_RGBA; + + glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); + + if(!id) + glGenTextures( 1, &id ); + glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); + + glBindTexture( GL_TEXTURE_2D, id); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + if(mipmap) + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear?GL_LINEAR_MIPMAP_LINEAR:GL_LINEAR_MIPMAP_NEAREST) ); + else + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + + skinsize=texture.sizeX; + + if(isSkin) { + int tempnum=0; + int nb = (texture.sizeY*texture.sizeX*(texture.bpp/8)); + array = (GLubyte*)malloc(nb*sizeof(GLubyte)); + for(int i=0;i::iterator it = textures.find(fileName); + if(it==textures.end()) { + textures.insert(make_pair(fileName,Texture(fileName,mipmap,hasalpha))); + textures[fileName].load(); + return textures[fileName].getId(); + } else { + return it->second.getId(); + } +} + +GLuint Texture::Load(const string& fileName, bool mipmap, bool hasalpha, GLubyte* array, int* skinsize) { + map::iterator it = textures.find(fileName); + if(it==textures.end()) { + textures.insert(make_pair(fileName,Texture(fileName,mipmap,hasalpha,true))); + textures[fileName].load(); + *skinsize = textures[fileName].skinsize; + for(int i=0;isecond.skinsize; + for(int i=0;isecond.arraySize;i++) { + array[i] = it->second.array[i]; + } + return it->second.getId(); + } +} + diff --git a/Source/Texture.h b/Source/Texture.h new file mode 100644 index 0000000..4424cee --- /dev/null +++ b/Source/Texture.h @@ -0,0 +1,39 @@ +#include +#include + +//keeps track of which textures are loaded +//TODO: delete them properly +class Texture { + private: + static std::map textures; + + bool isSkin; + std::string fileName; + GLuint id; + bool mipmap; + bool hasalpha; + GLubyte* array; + int arraySize; + int skinsize; + + void load(); + + public: + Texture(): + isSkin(false), skinsize(0), arraySize(0), + fileName(""), id(0), mipmap(false), hasalpha(false), array(NULL) { } + ~Texture() { + free(array); + } + Texture (const std::string& _fileName, bool _mipmap, bool _hasalpha): + isSkin(false), skinsize(0), arraySize(0), array(NULL), + fileName(_fileName), id(0), mipmap(_mipmap), hasalpha(_hasalpha) { } + Texture (const std::string& _fileName, bool _mipmap, bool _hasalpha, bool _isSkin): + isSkin(_isSkin), skinsize(0), arraySize(0), array(NULL), + fileName(_fileName), id(0), mipmap(_mipmap), hasalpha(_hasalpha) { } + GLuint getId() const { return id; } + + static GLuint Load(const std::string& fileName, bool mipmap, bool hasalpha); + static GLuint Load(const std::string& fileName, bool mipmap, bool hasalpha, GLubyte* array, int* skinsize); +}; +