From 35b5d8a643d2ff04614a8b9f57026b57d4a82667 Mon Sep 17 00:00:00 2001 From: sf17k Date: Wed, 8 Jun 2011 08:51:45 -0400 Subject: [PATCH] major refactor of texture system --- Source/Game.cpp | 20 ++-- Source/Game.h | 24 +++-- Source/GameDraw.cpp | 16 +-- Source/GameInitDispose.cpp | 138 ++++++++++++-------------- Source/GameTick.cpp | 104 +++++++++----------- Source/Menu.cpp | 20 ++-- Source/Menu.h | 6 +- Source/Models.cpp | 54 +++++++--- Source/Models.h | 8 +- Source/Objects.cpp | 12 +-- Source/Objects.h | 9 +- Source/Person.cpp | 8 +- Source/Person.h | 2 +- Source/Skybox.cpp | 36 +++---- Source/Skybox.h | 5 +- Source/Sprite.cpp | 70 ++++++------- Source/Sprite.h | 23 ++--- Source/Terrain.cpp | 31 +++--- Source/Terrain.h | 15 +-- Source/Text.cpp | 18 ++-- Source/Text.h | 3 +- Source/Texture.cpp | 196 +++++++++++++++++++++++-------------- Source/Texture.h | 42 +++----- Source/Weapons.cpp | 28 +++--- Source/Weapons.h | 15 +-- 25 files changed, 460 insertions(+), 443 deletions(-) diff --git a/Source/Game.cpp b/Source/Game.cpp index 2beac5a..debf51a 100644 --- a/Source/Game.cpp +++ b/Source/Game.cpp @@ -27,18 +27,16 @@ float dialoguetime; int dialoguegonethrough[20]; namespace Game{ - GLuint terraintexture = 0; - GLuint terraintexture2 = 0; - GLuint terraintexture3 = 0; + Texture terraintexture; + Texture terraintexture2; + Texture loadscreentexture; + Texture Maparrowtexture; + Texture Mapboxtexture; + Texture Mapcircletexture; + Texture cursortexture; GLuint screentexture = 0; GLuint screentexture2 = 0; - GLuint logotexture = 0; - GLuint loadscreentexture = 0; - GLuint Maparrowtexture = 0; - GLuint Mapboxtexture = 0; - GLuint Mapcircletexture = 0; - GLuint cursortexture = 0; - GLuint Mainmenuitems[10] = {}; + Texture Mainmenuitems[10]; int selected = 0; int keyselect = 0; @@ -60,7 +58,7 @@ namespace Game{ bool cameramode = 0; bool firstload = 0; - GLuint hawktexture = 0; + Texture hawktexture; float hawkyaw = 0; float hawkcalldelay = 0; float leveltime = 0; diff --git a/Source/Game.h b/Source/Game.h index ec30c86..a378efc 100644 --- a/Source/Game.h +++ b/Source/Game.h @@ -58,22 +58,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Stereo.h" #include "Account.h" #include "Sounds.h" +#include "Texture.h" #define NB_CAMPAIGN_MENU_ITEM 7 namespace Game { - extern GLuint terraintexture; - extern GLuint terraintexture2; - extern GLuint terraintexture3; + extern Texture terraintexture; + extern Texture terraintexture2; + extern Texture loadscreentexture; + extern Texture Maparrowtexture; + extern Texture Mapboxtexture; + extern Texture Mapcircletexture; + extern Texture cursortexture; extern GLuint screentexture; extern GLuint screentexture2; - extern GLuint logotexture; - extern GLuint loadscreentexture; - extern GLuint Maparrowtexture; - extern GLuint Mapboxtexture; - extern GLuint Mapcircletexture; - extern GLuint cursortexture; - extern GLuint Mainmenuitems[10]; + extern Texture Mainmenuitems[10]; extern int selected; extern int keyselect; @@ -97,7 +96,7 @@ namespace Game { extern Model hawk; extern XYZ hawkcoords; extern XYZ realhawkcoords; - extern GLuint hawktexture; + extern Texture hawktexture; extern float hawkyaw; extern float hawkcalldelay; @@ -161,10 +160,9 @@ namespace Game { void newGame(); void deleteGame(); - void LoadTexture(const string fileName, GLuint *textureid,int mipmap, bool hasalpha); - void LoadTextureSave(const string fileName, GLuint *textureid,int mipmap,GLubyte *array, int *skinsize); void InitGame(); void LoadStuff(); + void LoadScreenTexture(); void LoadingScreen(); int DrawGLScene(StereoSide side); void LoadMenu(); diff --git a/Source/GameDraw.cpp b/Source/GameDraw.cpp index 11a9401..a6b02a5 100644 --- a/Source/GameDraw.cpp +++ b/Source/GameDraw.cpp @@ -404,9 +404,9 @@ int Game::DrawGLScene(StereoSide side) glDisable(GL_BLEND); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glBindTexture( GL_TEXTURE_2D, terraintexture); + terraintexture.bind(); terrain.draw(0); - glBindTexture( GL_TEXTURE_2D, terraintexture2); + terraintexture2.bind(); terrain.draw(1); //glBindTexture( GL_TEXTURE_2D, terraintexture3); //glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); @@ -1502,7 +1502,7 @@ int Game::DrawGLScene(StereoSide side) if(objects.type[i]==treetrunktype){ distcheck=findDistancefast(&player[0].coords,&objects.position[i]); if(distchecktypesleeping)glColor4f(0,0,0,opac*(1-distcheck/mapviewdist)); else if(player[i].dead)glColor4f(.3,.3,.3,opac*(1-distcheck/mapviewdist)); @@ -2078,7 +2078,7 @@ void DrawMenu() { glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); glEnable(GL_TEXTURE_2D); - glBindTexture( GL_TEXTURE_2D, Game::Mainmenuitems[4]); + Game::Mainmenuitems[4].bind(); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3f(-1,-1,0); @@ -2138,7 +2138,7 @@ void DrawMenu() { glTranslatef(1,-1,0); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glColor4f(1,1,1,1); - glBindTexture( GL_TEXTURE_2D, Game::cursortexture); + Game::cursortexture.bind(); glPushMatrix(); //glScalef(.25,.25,.25); glBegin(GL_QUADS); diff --git a/Source/GameInitDispose.cpp b/Source/GameInitDispose.cpp index 8c09179..24edee3 100644 --- a/Source/GameInitDispose.cpp +++ b/Source/GameInitDispose.cpp @@ -138,36 +138,28 @@ void Game::newGame(){ } void Game::deleteGame(){ - if(skybox) - delete skybox; - if(text) - delete text; - glDeleteTextures( 10, &Mainmenuitems[0] ); - glDeleteTextures( 1, &cursortexture ); - glDeleteTextures( 1, &Maparrowtexture ); - glDeleteTextures( 1, &Mapboxtexture ); - glDeleteTextures( 1, &Mapcircletexture ); - glDeleteTextures( 1, &terraintexture ); - glDeleteTextures( 1, &terraintexture2 ); - glDeleteTextures( 1, &screentexture ); - glDeleteTextures( 1, &screentexture2 ); - glDeleteTextures( 1, &hawktexture ); - glDeleteTextures( 1, &logotexture ); - glDeleteTextures( 1, &loadscreentexture ); + if(skybox) delete skybox; + if(text) delete text; + terraintexture.destroy(); + terraintexture2.destroy(); + cursortexture.destroy(); + Maparrowtexture.destroy(); + Mapboxtexture.destroy(); + Mapcircletexture.destroy(); + hawktexture.destroy(); + loadscreentexture.destroy(); + + for(int i=0;i<10;i++) + Mainmenuitems[i].destroy(); + + glDeleteTextures(1,&screentexture); + glDeleteTextures(1,&screentexture2); Dispose(); } -void Game::LoadTexture(const string fileName, GLuint *textureid,int mipmap, bool hasalpha) { - *textureid = Texture::Load(fileName,mipmap,hasalpha); -} - -void Game::LoadTextureSave(const string fileName, GLuint *textureid,int mipmap,GLubyte *array, int *skinsize) { - *textureid = Texture::Load(fileName,mipmap,false,array,skinsize); -} - void LoadSave(const char *fileName, GLuint *textureid,bool mipmap,GLubyte *array, int *skinsize) { int i; @@ -263,7 +255,7 @@ void Game::LoadingScreen() //Background glEnable(GL_TEXTURE_2D); - glBindTexture( GL_TEXTURE_2D, loadscreentexture); + loadscreentexture.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glDisable(GL_DEPTH_TEST); // Disables Depth Testing @@ -318,7 +310,7 @@ void Game::LoadingScreen() glDepthMask(1); glEnable(GL_TEXTURE_2D); - glBindTexture( GL_TEXTURE_2D, loadscreentexture); + loadscreentexture.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glDisable(GL_DEPTH_TEST); // Disables Depth Testing @@ -360,7 +352,7 @@ void Game::LoadingScreen() glDepthMask(1); glEnable(GL_TEXTURE_2D); - glBindTexture( GL_TEXTURE_2D, loadscreentexture); + loadscreentexture.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glDisable(GL_DEPTH_TEST); // Disables Depth Testing @@ -681,21 +673,21 @@ void Game::InitGame() if(musictoggle) emit_stream_np(stream_menutheme); - LoadTexture(":Data:Textures:Cursor.png",&cursortexture,0,1); + cursortexture.load(":Data:Textures:Cursor.png",0,1); - LoadTexture(":Data:Textures:MapCircle.png",&Mapcircletexture,0,1); - LoadTexture(":Data:Textures:MapBox.png",&Mapboxtexture,0,1); - LoadTexture(":Data:Textures:MapArrow.png",&Maparrowtexture,0,1); + Mapcircletexture.load(":Data:Textures:MapCircle.png",0,1); + Mapboxtexture.load(":Data:Textures:MapBox.png",0,1); + Maparrowtexture.load(":Data:Textures:MapArrow.png",0,1); temptexdetail=texdetail; if(texdetail>2)texdetail=2; - LoadTexture(":Data:Textures:Lugaru.png",&Mainmenuitems[0],0,0); - LoadTexture(":Data:Textures:Newgame.png",&Mainmenuitems[1],0,0); - LoadTexture(":Data:Textures:Options.png",&Mainmenuitems[2],0,0); - LoadTexture(":Data:Textures:Quit.png",&Mainmenuitems[3],0,0); - LoadTexture(":Data:Textures:Eyelid.png",&Mainmenuitems[4],0,1); - LoadTexture(":Data:Textures:Resume.png",&Mainmenuitems[5],0,0); - LoadTexture(":Data:Textures:Endgame.png",&Mainmenuitems[6],0,0); + Mainmenuitems[0].load(":Data:Textures:Lugaru.png",0,0); + Mainmenuitems[1].load(":Data:Textures:Newgame.png",0,0); + Mainmenuitems[2].load(":Data:Textures:Options.png",0,0); + Mainmenuitems[3].load(":Data:Textures:Quit.png",0,0); + Mainmenuitems[4].load(":Data:Textures:Eyelid.png",0,1); + Mainmenuitems[5].load(":Data:Textures:Resume.png",0,0); + Mainmenuitems[6].load(":Data:Textures:Endgame.png",0,0); //LoadTexture(":Data:Textures:Eye.jpg",&Mainmenuitems[5],0,1); //~ LoadTexture(":Data:Textures:World.png",&Mainmenuitems[7],0,0); // LoadCampaign will take care of that @@ -718,7 +710,7 @@ void Game::InitGame() } -void LoadScreenTexture() { +void Game::LoadScreenTexture() { glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); if(!Game::screentexture) @@ -744,20 +736,17 @@ void Game::LoadStuff() LOGFUNC; - visibleloading=1; - loadtime=0; stillloading=1; for(i=0;igetCurrentCampaign()+":World.png").c_str())); if(test.good()) { - LoadTexture((":Data:Textures:"+accountactive->getCurrentCampaign()+":World.png").c_str(),&Mainmenuitems[7],0,0); + Mainmenuitems[7].load((":Data:Textures:"+accountactive->getCurrentCampaign()+":World.png").c_str(),0,0); } else { - LoadTexture(":Data:Textures:World.png",&Mainmenuitems[7],0,0); + Mainmenuitems[7].load(":Data:Textures:World.png",0,0); } if(accountactive->getCampaignChoicesMade()==0) { @@ -2076,7 +2064,7 @@ void Loadlevel(const char *name) { (char *)":Data:Models:Body7.solid", (char *)":Data:Models:Bodylow.solid", (char *)":Data:Models:Belt.solid",1); - LoadTexture(":Data:Textures:Belt.png",&player[i].skeleton.drawmodelclothes.textureptr,1,1); + player[i].skeleton.drawmodelclothes.textureptr.load(":Data:Textures:Belt.png",1,1); } if(player[i].creature==wolftype){ player[i].skeleton.Load( @@ -2099,7 +2087,7 @@ void Loadlevel(const char *name) { //~ int texsize; //~ texsize=512*512*3/texdetail/texdetail; - LoadTextureSave(creatureskin[player[i].creature][player[i].whichskin],&player[i].skeleton.drawmodel.textureptr,1,&player[i].skeleton.skinText[0],&player[i].skeleton.skinsize); + player[i].skeleton.drawmodel.textureptr.load(creatureskin[player[i].creature][player[i].whichskin],1,&player[i].skeleton.skinText[0],&player[i].skeleton.skinsize); if(player[i].numclothes){ for(int j=0;j2&&player[closest].creature==wolftype) player[closest].whichskin=0; - LoadTextureSave(creatureskin[player[closest].creature][player[closest].whichskin], - &player[closest].skeleton.drawmodel.textureptr,1,&player[closest].skeleton.skinText[0],&player[closest].skeleton.skinsize); + player[closest].skeleton.drawmodel.textureptr.load(creatureskin[player[closest].creature][player[closest].whichskin],1, + &player[closest].skeleton.skinText[0],&player[closest].skeleton.skinsize); } if(player[closest].numclothes){ @@ -2778,7 +2766,7 @@ void doDebugKeys(){ if(player[closest].creature==rabbittype){ player[closest].skeleton.id=closest; player[closest].skeleton.Load((char *)":Data:Skeleton:Basic Figure Wolf",(char *)":Data:Skeleton:Basic Figure Wolf Low",(char *)":Data:Skeleton:Rabbitbelt",(char *)":Data:Models:Wolf.solid",(char *)":Data:Models:Wolf2.solid",(char *)":Data:Models:Wolf3.solid",(char *)":Data:Models:Wolf4.solid",(char *)":Data:Models:Wolf5.solid",(char *)":Data:Models:Wolf6.solid",(char *)":Data:Models:Wolf7.solid",(char *)":Data:Models:Wolflow.solid",(char *)":Data:Models:Belt.solid",0); - LoadTextureSave(":Data:Textures:Wolf.jpg",&player[closest].skeleton.drawmodel.textureptr,1,&player[closest].skeleton.skinText[closest],&player[closest].skeleton.skinsize); + player[closest].skeleton.drawmodel.textureptr.load(":Data:Textures:Wolf.jpg",1,&player[closest].skeleton.skinText[closest],&player[closest].skeleton.skinsize); player[closest].whichskin=0; player[closest].creature=wolftype; @@ -2795,7 +2783,7 @@ void doDebugKeys(){ { player[closest].skeleton.id=closest; player[closest].skeleton.Load((char *)":Data:Skeleton:Basic Figure",(char *)":Data:Skeleton:Basic Figurelow",(char *)":Data:Skeleton:Rabbitbelt",(char *)":Data:Models:Body.solid",(char *)":Data:Models:Body2.solid",(char *)":Data:Models:Body3.solid",(char *)":Data:Models:Body4.solid",(char *)":Data:Models:Body5.solid",(char *)":Data:Models:Body6.solid",(char *)":Data:Models:Body7.solid",(char *)":Data:Models:Bodylow.solid",(char *)":Data:Models:Belt.solid",1); - LoadTextureSave(":Data:Textures:Fur3.jpg",&player[closest].skeleton.drawmodel.textureptr,1,&player[closest].skeleton.skinText[0],&player[closest].skeleton.skinsize); + player[closest].skeleton.drawmodel.textureptr.load(":Data:Textures:Fur3.jpg",1,&player[closest].skeleton.skinText[0],&player[closest].skeleton.skinsize); player[closest].whichskin=0; player[closest].creature=rabbittype; @@ -3115,19 +3103,19 @@ void doDebugKeys(){ int k=abs(Random()%2)+1; if(k==0){ - LoadTextureSave(":Data:Textures:Fur3.jpg",&player[numplayers].skeleton.drawmodel.textureptr,1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize); + player[numplayers].skeleton.drawmodel.textureptr.load(":Data:Textures:Fur3.jpg",1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize); player[numplayers].whichskin=0; } else if(k==1){ - LoadTextureSave(":Data:Textures:Fur.jpg",&player[numplayers].skeleton.drawmodel.textureptr,1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize); + player[numplayers].skeleton.drawmodel.textureptr.load(":Data:Textures:Fur.jpg",1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize); player[numplayers].whichskin=1; } else { - LoadTextureSave(":Data:Textures:Fur2.jpg",&player[numplayers].skeleton.drawmodel.textureptr,1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize); + player[numplayers].skeleton.drawmodel.textureptr.load(":Data:Textures:Fur2.jpg",1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize); player[numplayers].whichskin=2; } - LoadTexture(":Data:Textures:Belt.png",&player[numplayers].skeleton.drawmodelclothes.textureptr,1,1); + player[numplayers].skeleton.drawmodelclothes.textureptr.load(":Data:Textures:Belt.png",1,1); player[numplayers].power=1; player[numplayers].speedmult=1; player[numplayers].currentanimation=bounceidleanim; diff --git a/Source/Menu.cpp b/Source/Menu.cpp index f6a1957..1d8718d 100644 --- a/Source/Menu.cpp +++ b/Source/Menu.cpp @@ -11,7 +11,7 @@ struct MenuItem { enum MenuItemType{NONE,LABEL,BUTTON,IMAGE,IMAGEBUTTON,MAPMARKER,MAPLINE,MAPLABEL} type; int id; string text; - int texture; + Texture texture; int x,y,w,h; float r,g,b; float effectfade; @@ -19,7 +19,7 @@ struct MenuItem { float linestartsize; float lineendsize; - void init(MenuItemType _type, int _id, const string& _text, int _texture, + void init(MenuItemType _type, int _id, const string& _text, Texture _texture, int _x, int _y, int _w, int _h, float _r, float _g, float _b, float _linestartsize=1, float _lineendsize=1){ type=_type; @@ -51,31 +51,31 @@ void Menu::clearMenu(){ void Menu::addLabel(int id,const string& text,int x,int y,float r,float g,float b){ items.push_back(MenuItem()); - items.back().init(MenuItem::LABEL,id,text,0,x,y,-1,-1,r,g,b); + items.back().init(MenuItem::LABEL,id,text,Texture(),x,y,-1,-1,r,g,b); } void Menu::addButton(int id,const string& text,int x,int y,float r,float g,float b){ items.push_back(MenuItem()); - items.back().init(MenuItem::BUTTON,id,text,0,x,y,-1,-1,r,g,b); + items.back().init(MenuItem::BUTTON,id,text,Texture(),x,y,-1,-1,r,g,b); } -void Menu::addImage(int id,int texture,int x,int y,int w,int h,float r,float g,float b){ +void Menu::addImage(int id,Texture texture,int x,int y,int w,int h,float r,float g,float b){ items.push_back(MenuItem()); items.back().init(MenuItem::IMAGE,id,"",texture,x,y,w,h,r,g,b); } -void Menu::addButtonImage(int id,int texture,int x,int y,int w,int h,float r,float g,float b){ +void Menu::addButtonImage(int id,Texture texture,int x,int y,int w,int h,float r,float g,float b){ items.push_back(MenuItem()); items.back().init(MenuItem::IMAGEBUTTON,id,"",texture,x,y,w,h,r,g,b); } void Menu::addMapLine(int x, int y, int w, int h, float startsize, float endsize, float r,float g,float b){ items.push_back(MenuItem()); - items.back().init(MenuItem::MAPLINE,-1,"",0,x,y,w,h,r,g,b,startsize,endsize); + items.back().init(MenuItem::MAPLINE,-1,"",Texture(),x,y,w,h,r,g,b,startsize,endsize); } -void Menu::addMapMarker(int id,int texture,int x,int y,int w,int h,float r,float g,float b){ +void Menu::addMapMarker(int id,Texture texture,int x,int y,int w,int h,float r,float g,float b){ items.push_back(MenuItem()); items.back().init(MenuItem::MAPMARKER,id,"",texture,x,y,w,h,r,g,b); } void Menu::addMapLabel(int id,const string& text,int x,int y,float r,float g,float b){ items.push_back(MenuItem()); - items.back().init(MenuItem::MAPLABEL,id,text,0,x,y,-1,-1,r,g,b); + items.back().init(MenuItem::MAPLABEL,id,text,Texture(),x,y,-1,-1,r,g,b); } void Menu::setText(int id,const string& text){ @@ -148,7 +148,7 @@ void Menu::drawItems(){ }else{ glBlendFunc(GL_SRC_ALPHA,GL_ONE); } - glBindTexture(GL_TEXTURE_2D,it->texture); + it->texture.bind(); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBegin(GL_QUADS); diff --git a/Source/Menu.h b/Source/Menu.h index a4a2e9c..6a1911a 100644 --- a/Source/Menu.h +++ b/Source/Menu.h @@ -7,10 +7,10 @@ namespace Menu { void clearMenu(); void addLabel(int id,const string& text,int x,int y,float r=1,float g=0,float b=0); void addButton(int id,const string& text,int x,int y,float r=1,float g=0,float b=0); - void addImage(int id,int texture,int x,int y,int w,int h,float r=1,float g=1,float b=1); - void addButtonImage(int id,int texture,int x,int y,int w,int h,float r=1,float g=1,float b=1); + void addImage(int id,Texture texture,int x,int y,int w,int h,float r=1,float g=1,float b=1); + void addButtonImage(int id,Texture texture,int x,int y,int w,int h,float r=1,float g=1,float b=1); void addMapLine(int x, int y, int w, int h, float startsize, float endsize, float r,float g,float b); - void addMapMarker(int id,int texture,int x,int y,int w,int h,float r,float g,float b); + void addMapMarker(int id,Texture texture,int x,int y,int w,int h,float r,float g,float b); void addMapLabel(int id,const string& text,int x,int y,float r=1,float g=0,float b=0); void setText(int id,const string& text); void setText(int id,const string& text,int x,int y,int w,int h); diff --git a/Source/Models.cpp b/Source/Models.cpp index bdc44dc..95b93fe 100644 --- a/Source/Models.cpp +++ b/Source/Models.cpp @@ -524,7 +524,7 @@ bool Model::load(const char *filename,bool texture ) funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]); } - Texture.xsz=0; + modelTexture.xsz=0; fclose(tfile); @@ -607,7 +607,7 @@ bool Model::loaddecal(const char *filename,bool texture ) } - Texture.xsz=0; + modelTexture.xsz=0; fclose(tfile); @@ -878,7 +878,7 @@ void Model::CalculateNormals(bool facenormalise) void Model::drawimmediate() { - glBindTexture(GL_TEXTURE_2D,(unsigned long)textureptr); + textureptr.bind(); glBegin(GL_TRIANGLES); for(int i=0;i=0&&Triangles[i].vertex[1]>=0&&Triangles[i].vertex[2]>=0){ @@ -919,7 +919,7 @@ void Model::draw() if(!color)glInterleavedArrays( GL_T2F_N3F_V3F,8*sizeof(GLfloat),&vArray[0]); if(color)glInterleavedArrays( GL_T2F_C3F_V3F,8*sizeof(GLfloat),&vArray[0]); - glBindTexture(GL_TEXTURE_2D,(unsigned long)textureptr); + textureptr.bind(); #if PLATFORM_MACOSX glLockArraysEXT( 0, TriangleNum*3); @@ -937,6 +937,7 @@ void Model::draw() //drawimmediate(); } +//TODO: phase out in favor of Texture void Model::drawdifftex(GLuint texture) { glEnableClientState(GL_NORMAL_ARRAY); @@ -950,6 +951,35 @@ void Model::drawdifftex(GLuint texture) glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); +#ifndef WIN32 + glLockArraysEXT( 0, TriangleNum*3); +#endif + glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3); +#ifndef WIN32 + glUnlockArraysEXT(); +#endif + + + if(!color)glDisableClientState(GL_NORMAL_ARRAY); + if(color)glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + //drawdiffteximmediate(texture); +} + +void Model::drawdifftex(Texture texture) +{ + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + if(!color)glInterleavedArrays( GL_T2F_N3F_V3F,8*sizeof(GLfloat),&vArray[0]); + if(color)glInterleavedArrays( GL_T2F_C3F_V3F,8*sizeof(GLfloat),&vArray[0]); + + texture.bind(); + glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); + glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); + + #ifndef WIN32 glLockArraysEXT( 0, TriangleNum*3); #endif @@ -999,7 +1029,7 @@ void Model::drawdiffteximmediate(GLuint texture) glEnd(); } -void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtexture2,GLuint breaktexture) +void Model::drawdecals(Texture shadowtexture,Texture bloodtexture,Texture bloodtexture2,Texture breaktexture) { if(decals){ if(type!=decalstype)return; @@ -1023,7 +1053,7 @@ void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtext if(decaltype[i]==blooddecalfast&&decalalivetime[i]<2)decalalivetime[i]=2; if(decaltype[i]==shadowdecal&&decaltype[i]!=lasttype){ - glBindTexture( GL_TEXTURE_2D, shadowtexture); + shadowtexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -1031,7 +1061,7 @@ void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtext } } if(decaltype[i]==breakdecal&&decaltype[i]!=lasttype){ - glBindTexture( GL_TEXTURE_2D, breaktexture); + breaktexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -1039,7 +1069,7 @@ void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtext } } if((decaltype[i]==blooddecal||decaltype[i]==blooddecalslow)&&decaltype[i]!=lasttype){ - glBindTexture( GL_TEXTURE_2D, bloodtexture); + bloodtexture.bind(); if(blend){ blend=0; glAlphaFunc(GL_GREATER, 0.15); @@ -1047,7 +1077,7 @@ void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtext } } if((decaltype[i]==blooddecalfast)&&decaltype[i]!=lasttype){ - glBindTexture( GL_TEXTURE_2D, bloodtexture2); + bloodtexture2.bind(); if(blend){ blend=0; glAlphaFunc(GL_GREATER, 0.15); @@ -1385,8 +1415,7 @@ void Model::MakeDecal(int atype, XYZ where,float size, float opacity, float rota Model::~Model() { deallocate(); - - glDeleteTextures( 1, &textureptr ); + textureptr.destroy(); } void Model::deallocate() @@ -1474,8 +1503,7 @@ Model::Model() Triangles=0; vArray=0; - textureptr = 0; - memset(&Texture, 0, sizeof(Texture)); + memset(&modelTexture, 0, sizeof(modelTexture)); numpossible = 0; color = 0; diff --git a/Source/Models.h b/Source/Models.h index caad18e..a82f85d 100644 --- a/Source/Models.h +++ b/Source/Models.h @@ -35,6 +35,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Terrain.h" #include "binio.h" #include "Quaternions.h" +#include "Texture.h" // // Textures List @@ -85,8 +86,8 @@ public: TexturedTriangle Triangles[max_textured_triangle]; GLfloat vArray[max_textured_triangle*24];*/ - GLuint textureptr; - ModelTexture Texture; + Texture textureptr; + ModelTexture modelTexture; int numpossible; bool color; @@ -116,7 +117,7 @@ public: void DeleteDecal(int which); void MakeDecal(int atype, XYZ *where, float *size, float *opacity, float *rotation); void MakeDecal(int atype, XYZ where, float size, float opacity, float rotation); - void drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtexture2,GLuint breaktexture); + void drawdecals(Texture shadowtexture,Texture bloodtexture,Texture bloodtexture2,Texture breaktexture); int SphereCheck(XYZ *p1,float radius, XYZ *p, XYZ *move, float *rotate); int SphereCheckPossible(XYZ *p1,float radius, XYZ *move, float *rotate); int LineCheck(XYZ *p1,XYZ *p2, XYZ *p, XYZ *move, float *rotate); @@ -139,6 +140,7 @@ public: void CalculateNormals(bool facenormalise); void draw(); void drawdifftex(GLuint texture); + void drawdifftex(Texture texture); void drawimmediate(); void drawdiffteximmediate(GLuint texture); void Rotate(float xang,float yang,float zang); diff --git a/Source/Objects.cpp b/Source/Objects.cpp index de603c1..8f2544f 100644 --- a/Source/Objects.cpp +++ b/Source/Objects.cpp @@ -759,10 +759,6 @@ Objects::Objects() center = 0; radius = 0; numobjects = 0; - boxtextureptr = 0; - treetextureptr = 0; - bushtextureptr = 0; - rocktextureptr = 0; memset(position, 0, sizeof(position)); memset(type, 0, sizeof(type)); @@ -787,9 +783,9 @@ Objects::Objects() Objects::~Objects() { - glDeleteTextures( 1, &boxtextureptr ); - glDeleteTextures( 1, &treetextureptr ); - glDeleteTextures( 1, &bushtextureptr ); - glDeleteTextures( 1, &rocktextureptr ); + boxtextureptr.destroy(); + treetextureptr.destroy(); + bushtextureptr.destroy(); + rocktextureptr.destroy(); }; diff --git a/Source/Objects.h b/Source/Objects.h index 4d35552..a99c646 100644 --- a/Source/Objects.h +++ b/Source/Objects.h @@ -31,6 +31,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Models.h" #include "Terrain.h" #include "Sprite.h" +#include "Texture.h" #include // // Model Structures @@ -73,10 +74,10 @@ public: float scale[max_objects]; float messedwith[max_objects]; float checked[max_objects]; - GLuint boxtextureptr; - GLuint treetextureptr; - GLuint bushtextureptr; - GLuint rocktextureptr; + Texture boxtextureptr; + Texture treetextureptr; + Texture bushtextureptr; + Texture rocktextureptr; float shadowed[max_objects]; float occluded[max_objects]; bool checkcollide(XYZ startpoint, XYZ endpoint,int which); diff --git a/Source/Person.cpp b/Source/Person.cpp index 4a9898f..87a4bbe 100644 --- a/Source/Person.cpp +++ b/Source/Person.cpp @@ -475,7 +475,7 @@ void Person::DoBloodBig(float howmuch,int which){ } } } - glBindTexture(GL_TEXTURE_2D,skeleton.drawmodel.textureptr); + skeleton.drawmodel.textureptr.bind(); DoMipmaps(); bleedxint=0; @@ -703,7 +703,7 @@ bool Person::DoBloodBigWhere(float howmuch,int which, XYZ where){ } } } - glBindTexture(GL_TEXTURE_2D,skeleton.drawmodel.textureptr); + skeleton.drawmodel.textureptr.bind(); DoMipmaps(); bleedy=(1+coordsy)*512; @@ -3858,7 +3858,7 @@ void Person::DoStuff(){ if(bleeding>0){ bleeding-=multiplier*.3; if(bloodtoggle==2){ - glBindTexture(GL_TEXTURE_2D,skeleton.drawmodel.textureptr); + skeleton.drawmodel.textureptr.bind(); if(bleeding<=0&&(detail!=2||osx))DoMipmaps(); } } @@ -3984,7 +3984,7 @@ void Person::DoStuff(){ } } if(!osx&&detail>1){ - glBindTexture(GL_TEXTURE_2D,skeleton.drawmodel.textureptr); + skeleton.drawmodel.textureptr.bind(); DoMipmaps(); } diff --git a/Source/Person.h b/Source/Person.h index 8832a65..f37a0ee 100644 --- a/Source/Person.h +++ b/Source/Person.h @@ -412,7 +412,7 @@ class Person void DoHead(); void DoMipmaps() { - glBindTexture(GL_TEXTURE_2D, skeleton.drawmodel.textureptr); + skeleton.drawmodel.textureptr.bind(); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, skeleton.skinsize, skeleton.skinsize, 0, GL_RGB, GL_UNSIGNED_BYTE, &skeleton.skinText[0]); } diff --git a/Source/Skybox.cpp b/Source/Skybox.cpp index f42dd4d..bf145f2 100644 --- a/Source/Skybox.cpp +++ b/Source/Skybox.cpp @@ -34,12 +34,12 @@ void SkyBox::load (const char *ffront, const char *fleft, const char *fback, const char *fright, const char *fup, const char *fdown) { - Game::LoadTexture(ffront, &front, true, false); - Game::LoadTexture(fleft, &left, true, false); - Game::LoadTexture(fback, &back, true, false); - Game::LoadTexture(fright, &right, true, false); - Game::LoadTexture(fup, &up, true, false); - Game::LoadTexture(fdown, &down, true, false); + front.load(ffront, true, false); + left.load(fleft, true, false); + back.load(fback, true, false); + right.load(fright, true, false); + up.load(fup, true, false); + down.load(fdown, true, false); } void SkyBox::draw() @@ -66,7 +66,7 @@ void SkyBox::draw() glDisable(GL_LIGHTING); if(skyboxtexture)glEnable(GL_TEXTURE_2D); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); - glBindTexture(GL_TEXTURE_2D, front); + front.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glBegin(GL_QUADS); @@ -76,7 +76,7 @@ void SkyBox::draw() glTexCoord2f(1, 1); glVertex3f( size, size, size); glTexCoord2f(0, 1); glVertex3f(-size, size, size); glEnd(); - glBindTexture(GL_TEXTURE_2D, back); + back.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glBegin(GL_QUADS); @@ -86,7 +86,7 @@ void SkyBox::draw() glTexCoord2f(0, 1); glVertex3f( size, size, -size); glTexCoord2f(0, 0); glVertex3f( size, -size, -size); glEnd(); - glBindTexture(GL_TEXTURE_2D, up); + up.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glBegin(GL_QUADS); @@ -96,7 +96,7 @@ void SkyBox::draw() glTexCoord2f(1, 0); glVertex3f( size, size, size); glTexCoord2f(1, 1); glVertex3f( size, size, -size); glEnd(); - glBindTexture(GL_TEXTURE_2D, down); + down.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glBegin(GL_QUADS); @@ -107,7 +107,7 @@ void SkyBox::draw() glTexCoord2f(1, 1); glVertex3f( size, -size, size); glTexCoord2f(0, 1); glVertex3f(-size, -size, size); glEnd(); - glBindTexture(GL_TEXTURE_2D, right); + right.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glBegin(GL_QUADS); @@ -117,7 +117,7 @@ void SkyBox::draw() glTexCoord2f(0, 1); glVertex3f( size, size, size); glTexCoord2f(0, 0); glVertex3f( size, -size, size); glEnd(); - glBindTexture(GL_TEXTURE_2D, left); + left.bind(); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glBegin(GL_QUADS); @@ -134,11 +134,11 @@ void SkyBox::draw() SkyBox::~SkyBox() { - glDeleteTextures( 1, &front ); - glDeleteTextures( 1, &left ); - glDeleteTextures( 1, &back ); - glDeleteTextures( 1, &right ); - glDeleteTextures( 1, &up ); - glDeleteTextures( 1, &down ); + front.destroy(); + left.destroy(); + back.destroy(); + right.destroy(); + up.destroy(); + down.destroy(); }; diff --git a/Source/Skybox.h b/Source/Skybox.h index dc602ce..f1f5422 100644 --- a/Source/Skybox.h +++ b/Source/Skybox.h @@ -26,16 +26,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "TGALoader.h" #include "Quaternions.h" #include "gamegl.h" +#include "Texture.h" class SkyBox{ public: - GLuint front, left, back, right, up, down; + Texture front, left, back, right, up, down; void load(const char *ffront, const char *fleft, const char *fback, const char *fright, const char *fup, const char *fdown); void draw(); - SkyBox(): front(0), left(0), back(0), right(0), up(0), down(0) {} + SkyBox() {} ~SkyBox(); }; diff --git a/Source/Sprite.cpp b/Source/Sprite.cpp index 79a5af4..334a6f5 100644 --- a/Source/Sprite.cpp +++ b/Source/Sprite.cpp @@ -38,17 +38,17 @@ extern int bloodtoggle; extern XYZ windvector; // init statics -GLuint Sprite::cloudtexture = 0; -GLuint Sprite::cloudimpacttexture = 0; -GLuint Sprite::bloodtexture = 0; -GLuint Sprite::flametexture = 0; -GLuint Sprite::bloodflametexture = 0; -GLuint Sprite::smoketexture = 0; -GLuint Sprite::snowflaketexture = 0; -GLuint Sprite::shinetexture = 0; -GLuint Sprite::splintertexture = 0; -GLuint Sprite::leaftexture = 0; -GLuint Sprite::toothtexture = 0; +Texture Sprite::cloudtexture; +Texture Sprite::cloudimpacttexture; +Texture Sprite::bloodtexture; +Texture Sprite::flametexture; +Texture Sprite::bloodflametexture; +Texture Sprite::smoketexture; +Texture Sprite::snowflaketexture; +Texture Sprite::shinetexture; +Texture Sprite::splintertexture; +Texture Sprite::leaftexture; +Texture Sprite::toothtexture; float Sprite::checkdelay = 0; @@ -101,7 +101,7 @@ void Sprite::Draw() if(lasttype!=sprites[i]->type) { switch(sprites[i]->type) { case cloudsprite: - glBindTexture( GL_TEXTURE_2D, cloudtexture); + cloudtexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -109,7 +109,7 @@ void Sprite::Draw() } break; case cloudimpactsprite: - glBindTexture( GL_TEXTURE_2D, cloudimpacttexture); + cloudimpacttexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -117,7 +117,7 @@ void Sprite::Draw() } break; case breathsprite: - glBindTexture( GL_TEXTURE_2D, cloudimpacttexture); + cloudimpacttexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -125,7 +125,7 @@ void Sprite::Draw() } break; case smoketype: - glBindTexture( GL_TEXTURE_2D, smoketexture); + smoketexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -133,7 +133,7 @@ void Sprite::Draw() } break; case bloodsprite: - glBindTexture( GL_TEXTURE_2D, bloodtexture); + bloodtexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -142,10 +142,10 @@ void Sprite::Draw() break; case splintersprite : if(lastspecial!=sprites[i]->special) { - if(sprites[i]->special==0)glBindTexture( GL_TEXTURE_2D, splintertexture); - if(sprites[i]->special==1)glBindTexture( GL_TEXTURE_2D, leaftexture); - if(sprites[i]->special==2)glBindTexture( GL_TEXTURE_2D, snowflaketexture); - if(sprites[i]->special==3)glBindTexture( GL_TEXTURE_2D, toothtexture); + if(sprites[i]->special==0)splintertexture.bind(); + if(sprites[i]->special==1)leaftexture.bind(); + if(sprites[i]->special==2)snowflaketexture.bind(); + if(sprites[i]->special==3)toothtexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -154,7 +154,7 @@ void Sprite::Draw() } break; case snowsprite: - glBindTexture( GL_TEXTURE_2D, snowflaketexture); + snowflaketexture.bind(); if(!blend){ blend=1; glAlphaFunc(GL_GREATER, 0.0001); @@ -162,7 +162,7 @@ void Sprite::Draw() } break; case weaponshinesprite: - glBindTexture( GL_TEXTURE_2D, shinetexture); + shinetexture.bind(); if(blend){ blend=0; glAlphaFunc(GL_GREATER, 0.001); @@ -171,7 +171,7 @@ void Sprite::Draw() break; case flamesprite: case weaponflamesprite: - glBindTexture( GL_TEXTURE_2D, flametexture); + flametexture.bind(); if(blend||lasttype==bloodflamesprite){ blend=0; glAlphaFunc(GL_GREATER, 0.3); @@ -179,7 +179,7 @@ void Sprite::Draw() } break; case bloodflamesprite: - glBindTexture( GL_TEXTURE_2D, bloodflametexture); + bloodflametexture.bind(); if(blend){ blend=0; glAlphaFunc(GL_GREATER, 0.3); @@ -438,16 +438,16 @@ Sprite::Sprite() void Sprite::clearTextures() { - glDeleteTextures( 1, &toothtexture ); - glDeleteTextures( 1, &cloudtexture ); - glDeleteTextures( 1, &cloudimpacttexture ); - glDeleteTextures( 1, &bloodtexture ); - glDeleteTextures( 1, &flametexture ); - glDeleteTextures( 1, &bloodflametexture ); - glDeleteTextures( 1, &smoketexture ); - glDeleteTextures( 1, &snowflaketexture ); - glDeleteTextures( 1, &shinetexture ); - glDeleteTextures( 1, &splintertexture ); - glDeleteTextures( 1, &leaftexture ); + toothtexture.destroy(); + cloudtexture.destroy(); + cloudimpacttexture.destroy(); + bloodtexture.destroy(); + flametexture.destroy(); + bloodflametexture.destroy(); + smoketexture.destroy(); + snowflaketexture.destroy(); + shinetexture.destroy(); + splintertexture.destroy(); + leaftexture.destroy(); } diff --git a/Source/Sprite.h b/Source/Sprite.h index 7f25a84..819db20 100644 --- a/Source/Sprite.h +++ b/Source/Sprite.h @@ -30,6 +30,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Lights.h" #include "Terrain.h" #include "Objects.h" +#include "Texture.h" #include @@ -88,20 +89,20 @@ class Sprite{ } static void clearTextures(); - static GLuint cloudtexture; - static GLuint bloodtexture; - static GLuint flametexture; - static GLuint smoketexture; + static Texture cloudtexture; + static Texture bloodtexture; + static Texture flametexture; + static Texture smoketexture; - static GLuint cloudimpacttexture; - static GLuint snowflaketexture; - static GLuint shinetexture; - static GLuint bloodflametexture; + static Texture cloudimpacttexture; + static Texture snowflaketexture; + static Texture shinetexture; + static Texture bloodflametexture; - static GLuint splintertexture; + static Texture splintertexture; - static GLuint leaftexture; - static GLuint toothtexture; + static Texture leaftexture; + static Texture toothtexture; Sprite(); ~Sprite(); diff --git a/Source/Terrain.cpp b/Source/Terrain.cpp index acdc322..ecc8757 100644 --- a/Source/Terrain.cpp +++ b/Source/Terrain.cpp @@ -1047,7 +1047,7 @@ void Terrain::drawdecals() for(i=0;i Texture::textures; - extern TGAImageRec texture; extern bool trilinear; -Texture::~Texture() { - free(array); - glDeleteTextures(1,&id); -} -void Texture::load() { - GLuint type; +class TextureRes { +private: + static vector list; - LOGFUNC; + GLuint id; + string filename; + bool hasMipmap; + bool hasAlpha; + bool isSkin; + int skinsize; + GLubyte* data; + int datalen; - LOG(std::string("Loading texture...") + fileName); + void load(); - 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 ); - glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE ); +public: + TextureRes(const string& filename, bool hasMipmap, bool hasAlpha); + TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize); + ~TextureRes(); + void bind(); + + static void reloadAll(); +}; + + +vector TextureRes::list; + +void TextureRes::load(){ + //load image into 'texture' global var + unsigned char filenamep[256]; + CopyCStringToPascal(ConvertFileName(filename.c_str()),filenamep); + upload_image(filenamep,hasAlpha); skinsize=texture.sizeX; + GLuint type=GL_RGBA; + if(texture.bpp==24) + type=GL_RGB; - 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(); - } +void TextureRes::bind(){ + glBindTexture(GL_TEXTURE_2D,id); } -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(); - } +TextureRes::TextureRes(const string& _filename, bool _hasMipmap, bool _hasAlpha): + id(0),filename(_filename),hasMipmap(_hasMipmap),hasAlpha(_hasAlpha),isSkin(false), + skinsize(0),data(NULL),datalen(0) { + load(); + list.push_back(this); +} + +TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep): + id(0),filename(_filename),hasMipmap(_hasMipmap),hasAlpha(false),isSkin(true), + skinsize(0),data(NULL),datalen(0) { + load(); + *skinsizep=skinsize; + for(int i=0;i::iterator it=list.begin();it!=list.end();it++) + if(*it==this){ + list.erase(it); + break; + } +} + +void TextureRes::reloadAll(){ + for(vector::iterator it=list.begin();it!=list.end();it++) + (*it)->load(); +} + + + + +void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha){ + destroy(); + tex=new TextureRes(filename,hasMipmap,hasAlpha); +} + +void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep){ + destroy(); + tex=new TextureRes(filename,hasMipmap,array,skinsizep); +} + +void Texture::destroy(){ + if(tex){ + delete tex; + tex=NULL; + } +} + +void Texture::bind(){ + if(tex) + tex->bind(); + else + glBindTexture(GL_TEXTURE_2D,0); +} + +void Texture::reloadAll(){ + TextureRes::reloadAll(); } diff --git a/Source/Texture.h b/Source/Texture.h index f50af0e..a684bd5 100644 --- a/Source/Texture.h +++ b/Source/Texture.h @@ -2,39 +2,23 @@ #define _TEXTURE_H_ #include +#include #include +using namespace std; -//keeps track of which textures are loaded -class Texture { - private: - static std::map textures; - - bool isSkin; - std::string fileName; - GLuint id; - bool mipmap; - bool hasalpha; - GLubyte* array; - int arraySize; - int skinsize; +class TextureRes; - void load(); +class Texture { +private: + TextureRes* tex; +public: + inline Texture(): tex(NULL) {} + void load(const string& filename, bool hasMipmap, bool hasAlpha); + void load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep); + void destroy(); + void bind(); - public: - Texture(): - isSkin(false), skinsize(0), arraySize(0), - fileName(""), id(0), mipmap(false), hasalpha(false), array(NULL) { } - ~Texture(); - 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); + static void reloadAll(); }; #endif diff --git a/Source/Weapons.cpp b/Source/Weapons.cpp index 4d8f32e..a7d587f 100644 --- a/Source/Weapons.cpp +++ b/Source/Weapons.cpp @@ -54,17 +54,17 @@ extern int tutoriallevel; extern int numthrowkill; Model Weapon::throwingknifemodel; -GLuint Weapon::knifetextureptr = 0; -GLuint Weapon::lightbloodknifetextureptr = 0; -GLuint Weapon::bloodknifetextureptr = 0; +Texture Weapon::knifetextureptr; +Texture Weapon::lightbloodknifetextureptr; +Texture Weapon::bloodknifetextureptr; Model Weapon::swordmodel; -GLuint Weapon::swordtextureptr = 0; -GLuint Weapon::lightbloodswordtextureptr = 0; -GLuint Weapon::bloodswordtextureptr = 0; +Texture Weapon::swordtextureptr; +Texture Weapon::lightbloodswordtextureptr; +Texture Weapon::bloodswordtextureptr; Model Weapon::staffmodel; -GLuint Weapon::stafftextureptr = 0; +Texture Weapon::stafftextureptr; Weapon::Weapon(int t, int o) : owner(o) { setType(t); @@ -1073,12 +1073,12 @@ Weapons::Weapons() Weapons::~Weapons() { - glDeleteTextures( 1, &Weapon::stafftextureptr ); - glDeleteTextures( 1, &Weapon::knifetextureptr ); - glDeleteTextures( 1, &Weapon::lightbloodknifetextureptr ); - glDeleteTextures( 1, &Weapon::bloodknifetextureptr ); - glDeleteTextures( 1, &Weapon::swordtextureptr ); - glDeleteTextures( 1, &Weapon::lightbloodswordtextureptr ); - glDeleteTextures( 1, &Weapon::bloodswordtextureptr ); + Weapon::stafftextureptr.destroy(); + Weapon::knifetextureptr.destroy(); + Weapon::lightbloodknifetextureptr.destroy(); + Weapon::bloodknifetextureptr.destroy(); + Weapon::swordtextureptr.destroy(); + Weapon::lightbloodswordtextureptr.destroy(); + Weapon::bloodswordtextureptr.destroy(); } diff --git a/Source/Weapons.h b/Source/Weapons.h index bb178c1..6b2ebba 100644 --- a/Source/Weapons.h +++ b/Source/Weapons.h @@ -31,6 +31,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Terrain.h" #include "Sprite.h" #include "Person.h" +#include "Texture.h" #include #define max_weapons 30 @@ -45,17 +46,17 @@ class Weapon { Weapon(int type, int owner); static Model throwingknifemodel; - static GLuint knifetextureptr; - static GLuint lightbloodknifetextureptr; - static GLuint bloodknifetextureptr; + static Texture knifetextureptr; + static Texture lightbloodknifetextureptr; + static Texture bloodknifetextureptr; static Model swordmodel; - static GLuint swordtextureptr; - static GLuint lightbloodswordtextureptr; - static GLuint bloodswordtextureptr; + static Texture swordtextureptr; + static Texture lightbloodswordtextureptr; + static Texture bloodswordtextureptr; static Model staffmodel; - static GLuint stafftextureptr; + static Texture stafftextureptr; void Draw(); void DoStuff(int); -- 2.39.2