]> git.leopard-lacewing.eu Git - cgue_weave.git/commitdiff
fixed new/delete for textures
authorPeter Schaefer <schaeferpm@gmail.com>
Sat, 9 May 2015 11:53:58 +0000 (13:53 +0200)
committerPeter Schaefer <schaeferpm@gmail.com>
Sat, 9 May 2015 11:53:58 +0000 (13:53 +0200)
removed badhack for textures

Weave/Game.cpp
Weave/Graphix/Model/Model.cpp
Weave/Graphix/Model/Model.h
Weave/Graphix/Texture.cpp
Weave/Graphix/Texture.h
Weave/Scene/SceneObject.cpp

index dd53c20607c7239c3d91286102ad412ef4874505..0a6af339af388bd6a7ecdfc5e29020fa53ee31e7 100644 (file)
@@ -66,7 +66,7 @@ Game::Game() : playing(true)
        //current_world->addObject(new SceneObject(shader1, glm::mat4(1.0f), "Player.dae", "model_player.png"));
        //current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(3.f)), "cow/cow.dae", "model_cow_2D.jpg"));
        current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 0.5f, 0.4f, 1.5f), "duck.dae", "model_duck_2D.png"));
-       current_world->addObject(new SceneObject(shader1, translate(vec3(-5.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "duck.dae", "model_cow_2D.jpg"));
+       current_world->addObject(new SceneObject(shader1, translate(vec3(-5.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 3.f, 0.4f, 1.5f), "duck.dae", "model_duck_2D.png"));
 
        current_world->addObject(new EventBox(translate(vec3(3.f, .4f, 0.f)),EB_LOSTZONE));
        current_world->addObject(new EventBox(translate(vec3(3.f, .4f, -15.f)), EB_WINZONE));
index a47d7dd09d2a09f32f69369b5cd9887e0f5b7523..b4473074c8916465bdc147828b8b6ac1d7709740 100644 (file)
@@ -115,6 +115,7 @@ void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const
        getBBoxModel()->drawModel(Graphix::shader_BBox, NULL, _modelMat*translate(BBpos)*glm::scale(BBsiz*2.f));
 }
 
+
 void Model::updateBB(const vec3& _min, const vec3& _max)
 {
        BBmin = _min;
@@ -174,7 +175,6 @@ void Model::getBBsp(vec3& _size, vec3& _position) const
 }
 
 
-
 void Model::genBuffer(uint &buffer, uint size, void* value)
 {
        glGenBuffers(1, &buffer);
@@ -198,6 +198,7 @@ void Model::bindBuffer(const uint &buffer, const uint index, const uint dim)
        glBindBuffer(GL_ARRAY_BUFFER, 0);
 }
 
+
 void Model::useModel(Shader* _shader) const
 {
        _shader->useShader();
@@ -237,6 +238,7 @@ void Model::useModelMat(const mat4& _model, Shader* _shader) const
                glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_model));
 }
 
+
 Overlap Model::isColliding(const Model* _modelThis, const Model* _modelOther, const mat4& _modelMatThis, const mat4& _modelMatOther)
 {
        return _modelThis->checkColS2O(_modelMatThis, _modelOther, _modelMatOther);
@@ -339,6 +341,7 @@ void Model::checkCollideByAxis(float& _overlap, float& _inside, uint dim, const
                _inside = -1;
 }
 
+
 vec3 Model::rotateSize(const vec3& _size, const mat4& _modelMat)
 {
        vec3 Size = vec3(0.f);
@@ -358,6 +361,7 @@ void Model::scaleVec3(vec3& _vec, const vec3& _scale)
                _vec[i] *= _scale[i];
 }
 
+
 Model* Model::getBBoxModel()
 {
        if (BoundingBox == nullptr)
@@ -402,7 +406,6 @@ void Model::deleteSkyBoxModel()
        SkyBoxModel = nullptr;
 }
 
-
 void Model::deleteIMetaModel(const string& _modelpath)
 {
        if (_modelpath == "SkyBox" || _modelpath == "BBox" || _modelpath == "IMesh")
@@ -424,6 +427,7 @@ void Model::deleteIMetaModel(const string& _modelpath)
 
 }
 
+
 float Model::getPDistHit(const vec3& _P, const vec3& _direction) const
 {
 
index 5d9f581cdca9f2b10debef832c21bafc48425291..02b207656223c82ad1e43774674a5ffc4db6b69c 100644 (file)
@@ -13,8 +13,6 @@ typedef unsigned int uint;
 class Model
 {
 public:
-       Model();
-       virtual ~Model();
 
        /* Binds Model to the Model*/
        virtual void bindShader(Shader* shader);
@@ -52,6 +50,9 @@ public:
        virtual operator std::string() const = 0;
 
 protected:
+       Model();
+       virtual ~Model();
+
        uint numvertices, numfaces;
        uint vertexBuffer, indexBuffer, normalBuffer, uvBuffer;
 
index 7454dc1b2e7d8f85a5ea5cba7f8b73407d08aa8a..23e37824a2bdcb3036c3dde78a3ab3332cac3cbd 100644 (file)
@@ -8,6 +8,9 @@
 #include "Graphix.h"
 
 using std::string;
+using std::unordered_map;
+
+
 
 Texture::Texture(const string& _path, const vec4& _material) : path(_path), handle(-1), TEXTURE_TYPE(-1), material(_material)
 {
@@ -15,10 +18,12 @@ Texture::Texture(const string& _path, const vec4& _material) : path(_path), hand
        //Graphix::getGlError();
        //Message::info("Do some Stuff");
 
-       auto i = texture_map.find(_path);
-       if (i != texture_map.end())
+
+       texHandle& t_ptr = texture_Map[_path];
+
+       if (t_ptr.handle >= 0)
        {
-               handle = i->second;
+               handle = t_ptr.handle;
                return;
        }
 
@@ -88,7 +93,7 @@ Texture::Texture(const string& _path, const vec4& _material) : path(_path), hand
                glTexParameteri(TEXTURE_TYPE, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
        }
 
-       texture_map[_path] = handle;
+       t_ptr.handle = handle;
        //Graphix::getGlError();
        //Message::info("Done");
        
@@ -96,10 +101,42 @@ Texture::Texture(const string& _path, const vec4& _material) : path(_path), hand
 
 Texture::~Texture()
 {
-       texture_map.erase(path);
        glDeleteTextures(1, &handle);
 }
 
+Texture* Texture::newTexture(const string& _path, const vec4& _material)
+{
+       texHandle& t_ptr = texture_Map[_path];
+
+       if (t_ptr.ptr == nullptr)
+       {
+               t_ptr.ptr = new Texture(_path, _material);
+       }
+
+       ++t_ptr.count;
+
+       return t_ptr.ptr;
+}
+
+void Texture::deleteTexture(const string& _path)
+{
+       texHandle& t_ptr = texture_Map[_path];
+
+       if (t_ptr.ptr == nullptr)
+       {
+               Message::warning("deleteTexture: " + _path + " doesn't exist.");
+               return;
+       }
+               
+       --t_ptr.count;
+
+       if (!t_ptr.count)
+       {
+               delete t_ptr.ptr;
+               texture_Map.erase(_path);
+       }
+}
+
 void Texture::bind(int unit)
 {
        glActiveTexture(GL_TEXTURE0 + unit);
@@ -111,10 +148,4 @@ Texture::operator string() const
        return path;
 }
 
-void Texture::clean()
-{
-       //RALLLY BAD HACK!!!! No Texture will be freed!!!!
-       texture_map.clear();
-}
-
-str2int_map Texture::texture_map;
\ No newline at end of file
+unordered_map<string, Texture::texHandle> Texture::texture_Map;
\ No newline at end of file
index 313920a69dd474c62dd256457640a4866dfeba98..f0eaa10d91588671c8edf9df1fd53f0d7ccab5f9 100644 (file)
@@ -1,36 +1,36 @@
 #pragma once
 
-#include <string>
 #include "GLM.h"
 
-
+#include <string>
 #include <unordered_map>
 
-using std::unordered_map;
-
-using std::string;
-
-typedef unordered_map<string, unsigned int> str2int_map;
-
 class Texture
 {
 public:
-       Texture(const string& path, const vec4& material);
-       ~Texture();
+       static Texture* newTexture(const std::string& path, const vec4& material);
+       static void deleteTexture(const std::string& path);
 
        void bind(int unit);
 
-       operator string() const;
+       operator std::string() const;
 
        vec4 material;
 
-       //RALLLY BAD HACK!!!! No Texture will be freed!!!!
-       static void clean();
-
 private:
+       Texture(const std::string& path, const vec4& material);
+       ~Texture();
+
        unsigned int handle;
-       string path;
+       std::string path;
        unsigned int TEXTURE_TYPE;
 
-       static str2int_map texture_map;
+       typedef struct texStruct
+       {
+               int handle=-1;
+               unsigned int count=0;
+               Texture* ptr=nullptr;
+       } texHandle;
+
+       static std::unordered_map<std::string, texHandle> texture_Map;
 };
\ No newline at end of file
index 7ffd15619f66840cf92a5f24224eaac3e6d6f491..4784e5b86a44b88893b5d6e85c0935a524e66370 100644 (file)
@@ -45,7 +45,7 @@ newModel(true)
        }
 
        if (texturepath != "")
-               texture = new Texture(texturepath, _material);
+               texture = Texture::newTexture(texturepath, _material);
        //Message::info("Creating SkyBox Shader");
        //Graphix::getGlError();
 
@@ -75,7 +75,7 @@ newModel(false)
        //Graphix::getGlError();
        //modelID = _shader->getUniformLocation("modelMat");
        if (texturepath != "")
-               texture = new Texture(texturepath, _material);
+               texture = Texture::newTexture(texturepath, _material);
        //Message::info("Creating SkyBox Shader");
        //Graphix::getGlError();
 
@@ -88,9 +88,10 @@ newModel(false)
 
 SceneObject::~SceneObject()
 {
-                       delete texture;
-                       if (newModel)
-                               Model::deleteIMetaModel(*model);
+       if (texture!=nullptr)
+               Texture::deleteTexture(*texture);
+       if (newModel)
+               Model::deleteIMetaModel(*model);
 }
 
 float SceneObject::getFloor() const