]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Texture.cpp
Stopped using Account pointers, and removed general difficulty setting (difficulty...
[lugaru.git] / Source / Texture.cpp
index 2d3f2a8360d5e36fe39ee732950cdfac7f86fdc1..3106c8f31ac4490ff265e24b8faeb1d2b37fe3d4 100644 (file)
@@ -1,5 +1,6 @@
 /*
 Copyright (C) 2003, 2010 - Wolfire Games
+Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
 
 This file is part of Lugaru.
 
@@ -19,48 +20,23 @@ along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "gamegl.h"
 #include "Texture.h"
-#include "TGALoader.h"
+#include "ImageIO.h"
+#include "Utils/Folders.h"
 
 using namespace std;
 
-extern ImageRec texture;
 extern bool trilinear;
 
-
-class TextureRes
-{
-private:
-    static vector<TextureRes*> list;
-
-    GLuint id;
-    string filename;
-    bool hasMipmap;
-    bool hasAlpha;
-    bool isSkin;
-    int skinsize;
-    GLubyte* data;
-    int datalen;
-    GLubyte* skindata;
-
-    void load();
-
-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*> TextureRes::list;
 
 void TextureRes::load()
 {
-    //load image into 'texture' global var
-    if (!skindata) {
-        upload_image(ConvertFileName(filename.c_str()));
+    ImageRec texture;
+
+    //load image into 'texture'
+    if (!load_image(filename.c_str(), texture)) {
+        cerr << "Texture " << filename << " loading failed" << endl;
+        return;
     }
 
     skinsize = texture.sizeX;
@@ -84,18 +60,14 @@ void TextureRes::load()
     }
 
     if (isSkin) {
-        if (skindata) {
-            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, skinsize, skinsize, 0, GL_RGB, GL_UNSIGNED_BYTE, skindata);
-        } else {
-            free(data);
-            const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
-            data = (GLubyte*)malloc(nb * sizeof(GLubyte));
-            datalen = 0;
-            for (int i = 0; i < nb; i++)
-                if ((i + 1) % 4 || type == GL_RGB)
-                    data[datalen++] = texture.data[i];
-            glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
-        }
+        free(data);
+        const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
+        data = (GLubyte*)malloc(nb * sizeof(GLubyte));
+        datalen = 0;
+        for (int i = 0; i < nb; i++)
+            if ((i + 1) % 4 || type == GL_RGB)
+                data[datalen++] = texture.data[i];
+        glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
     } else {
         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
     }
@@ -106,23 +78,22 @@ void TextureRes::bind()
     glBindTexture(GL_TEXTURE_2D, id);
 }
 
-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), skindata(NULL)
+TextureRes::TextureRes(const string& _filename, bool _hasMipmap):
+    id(0), filename(_filename), hasMipmap(_hasMipmap), 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), skindata(NULL)
+    id(0), filename(_filename), hasMipmap(_hasMipmap), isSkin(true),
+    skinsize(0), data(NULL), datalen(0)
 {
     load();
     *skinsizep = skinsize;
     for (int i = 0; i < datalen; i++)
         array[i] = data[i];
-    skindata = array;
     list.push_back(this);
 }
 
@@ -137,25 +108,16 @@ TextureRes::~TextureRes()
         }
 }
 
-void TextureRes::reloadAll()
-{
-    for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++) {
-        (*it)->id = 0;
-        (*it)->load();
-    }
-}
-
-
-void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha)
+void Texture::load(const string& filename, bool hasMipmap)
 {
     destroy();
-    tex = new TextureRes(filename, hasMipmap, hasAlpha);
+    tex = new TextureRes(Folders::getResourcePath(filename), hasMipmap);
 }
 
 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
 {
     destroy();
-    tex = new TextureRes(filename, hasMipmap, array, skinsizep);
+    tex = new TextureRes(Folders::getResourcePath(filename), hasMipmap, array, skinsizep);
 }
 
 void Texture::destroy()
@@ -173,9 +135,3 @@ void Texture::bind()
     else
         glBindTexture(GL_TEXTURE_2D, 0);
 }
-
-void Texture::reloadAll()
-{
-    TextureRes::reloadAll();
-}
-