From a3775aa01cd00672b37785365675f6842b9db2dc Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Thu, 15 Dec 2016 20:51:01 +0700 Subject: [PATCH] =?utf8?q?Cleaned=20up=20TextureRes=20lifecycle=20to=20mak?= =?utf8?q?e=20sure=20it=E2=80=99s=20destroyed=20by=20RAII?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Source/Environment/Skybox.cpp | 10 ---------- Source/Environment/Skybox.hpp | 1 - Source/Environment/Terrain.cpp | 11 ----------- Source/Environment/Terrain.hpp | 1 - Source/GameInitDispose.cpp | 15 --------------- Source/Graphic/Models.cpp | 1 - Source/Graphic/Sprite.cpp | 16 ---------------- Source/Graphic/Sprite.hpp | 1 - Source/Graphic/Text.cpp | 1 - Source/Graphic/Texture.cpp | 17 ++++------------- Source/Graphic/Texture.hpp | 12 +++++++----- Source/Objects/Weapons.cpp | 12 ------------ Source/Objects/Weapons.hpp | 1 - 13 files changed, 11 insertions(+), 88 deletions(-) diff --git a/Source/Environment/Skybox.cpp b/Source/Environment/Skybox.cpp index 97dcd6d..5ace90a 100644 --- a/Source/Environment/Skybox.cpp +++ b/Source/Environment/Skybox.cpp @@ -158,13 +158,3 @@ void SkyBox::draw() glDepthMask(1); glPopMatrix(); } - -SkyBox::~SkyBox() -{ - front.destroy(); - left.destroy(); - back.destroy(); - right.destroy(); - up.destroy(); - down.destroy(); -} diff --git a/Source/Environment/Skybox.hpp b/Source/Environment/Skybox.hpp index 1a9d36a..2fb2d5d 100644 --- a/Source/Environment/Skybox.hpp +++ b/Source/Environment/Skybox.hpp @@ -37,7 +37,6 @@ public: void draw(); SkyBox() {} - ~SkyBox(); }; #endif diff --git a/Source/Environment/Terrain.cpp b/Source/Environment/Terrain.cpp index 77ae539..806bb4b 100644 --- a/Source/Environment/Terrain.cpp +++ b/Source/Environment/Terrain.cpp @@ -1515,14 +1515,3 @@ Terrain::Terrain() memset(decalposition, 0, sizeof(decalposition)); numdecals = 0; } -Terrain::~Terrain() -{ - terraintexture.destroy(); - shadowtexture.destroy(); - bodyprinttexture.destroy(); - footprinttexture.destroy(); - bloodtexture.destroy(); - bloodtexture2.destroy(); - breaktexture.destroy(); -} - diff --git a/Source/Environment/Terrain.hpp b/Source/Environment/Terrain.hpp index 3c30e3d..097cdbd 100644 --- a/Source/Environment/Terrain.hpp +++ b/Source/Environment/Terrain.hpp @@ -126,7 +126,6 @@ public: void DoShadows(); Terrain(); - ~Terrain(); }; #endif diff --git a/Source/GameInitDispose.cpp b/Source/GameInitDispose.cpp index 38815d9..5e6ce04 100644 --- a/Source/GameInitDispose.cpp +++ b/Source/GameInitDispose.cpp @@ -114,17 +114,6 @@ void Game::deleteGame() 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); @@ -663,10 +652,6 @@ void Game::LoadStuff() stillloading = 1; - for (auto p:Person::players) { - p->skeleton.drawmodel.textureptr.destroy(); - } - visibleloading = 0; //don't use loadscreentexture yet loadscreentexture.load("Textures/Fire.jpg", 1); visibleloading = 1; diff --git a/Source/Graphic/Models.cpp b/Source/Graphic/Models.cpp index 38e2447..1e59472 100644 --- a/Source/Graphic/Models.cpp +++ b/Source/Graphic/Models.cpp @@ -1358,7 +1358,6 @@ void Model::MakeDecal(int atype, XYZ where, float size, float opacity, float rot Model::~Model() { deallocate(); - textureptr.destroy(); } void Model::deallocate() diff --git a/Source/Graphic/Sprite.cpp b/Source/Graphic/Sprite.cpp index 3f582f9..0fb5a40 100644 --- a/Source/Graphic/Sprite.cpp +++ b/Source/Graphic/Sprite.cpp @@ -462,19 +462,3 @@ Sprite::Sprite() speed = 0; rotatespeed = 0; } - -void Sprite::clearTextures() -{ - 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/Graphic/Sprite.hpp b/Source/Graphic/Sprite.hpp index 66debe7..0eb1d19 100644 --- a/Source/Graphic/Sprite.hpp +++ b/Source/Graphic/Sprite.hpp @@ -86,7 +86,6 @@ public: static void setLastSpriteAlivetime(float al) { sprites.back()->alivetime = al; } - static void clearTextures(); static Texture cloudtexture; static Texture bloodtexture; diff --git a/Source/Graphic/Text.cpp b/Source/Graphic/Text.cpp index 2cf73aa..1253ba9 100644 --- a/Source/Graphic/Text.cpp +++ b/Source/Graphic/Text.cpp @@ -147,6 +147,5 @@ Text::~Text() glDeleteLists(base, 512); base = 0; } - FontTexture.destroy(); } diff --git a/Source/Graphic/Texture.cpp b/Source/Graphic/Texture.cpp index fe732bf..9dd4ea6 100644 --- a/Source/Graphic/Texture.cpp +++ b/Source/Graphic/Texture.cpp @@ -89,8 +89,9 @@ TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, { load(); *skinsizep = skinsize; - for (int i = 0; i < datalen; i++) + for (int i = 0; i < datalen; i++) { array[i] = data[i]; + } } TextureRes::~TextureRes() @@ -101,22 +102,12 @@ TextureRes::~TextureRes() void Texture::load(const string& filename, bool hasMipmap) { - destroy(); - tex = new TextureRes(Folders::getResourcePath(filename), hasMipmap); + tex.reset(new TextureRes(Folders::getResourcePath(filename), hasMipmap)); } void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep) { - destroy(); - tex = new TextureRes(Folders::getResourcePath(filename), hasMipmap, array, skinsizep); -} - -void Texture::destroy() -{ - if (tex) { - delete tex; - tex = NULL; - } + tex.reset(new TextureRes(Folders::getResourcePath(filename), hasMipmap, array, skinsizep)); } void Texture::bind() diff --git a/Source/Graphic/Texture.hpp b/Source/Graphic/Texture.hpp index 0f12828..54df613 100644 --- a/Source/Graphic/Texture.hpp +++ b/Source/Graphic/Texture.hpp @@ -26,8 +26,7 @@ along with Lugaru. If not, see . #include #include #include - -using namespace std; +#include class TextureRes { @@ -47,17 +46,20 @@ public: TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize); ~TextureRes(); void bind(); + + /* Make sure TextureRes never gets copied */ + TextureRes(TextureRes const& other) = delete; + TextureRes & operator=(TextureRes const& other) = delete; }; class Texture { private: - TextureRes* tex; + std::shared_ptr tex; public: - inline Texture(): tex(NULL) {} + inline Texture(): tex(nullptr) {} void load(const string& filename, bool hasMipmap); void load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep); - void destroy(); void bind(); }; diff --git a/Source/Objects/Weapons.cpp b/Source/Objects/Weapons.cpp index 127c014..d762ddc 100644 --- a/Source/Objects/Weapons.cpp +++ b/Source/Objects/Weapons.cpp @@ -1111,15 +1111,3 @@ int Weapons::Draw() Weapons::Weapons() { } - -Weapons::~Weapons() -{ - 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/Objects/Weapons.hpp b/Source/Objects/Weapons.hpp index 9258e9e..2897cfb 100644 --- a/Source/Objects/Weapons.hpp +++ b/Source/Objects/Weapons.hpp @@ -122,7 +122,6 @@ class Weapons : public std::vector { public: Weapons(); - ~Weapons(); int Draw(); void DoStuff(); -- 2.39.2