]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Texture.cpp
Mention devtools option in man page
[lugaru.git] / Source / Texture.cpp
index 5c8834d70be6e83ec69a6b6fa5b3bf5cf078293c..3106c8f31ac4490ff265e24b8faeb1d2b37fe3d4 100644 (file)
+/*
+Copyright (C) 2003, 2010 - Wolfire Games
+Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
+
+This file is part of Lugaru.
+
+Lugaru is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+Lugaru is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+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 TGAImageRec texture;
 extern bool trilinear;
 
+vector<TextureRes*> TextureRes::list;
 
-class TextureRes {
-private:
-    static vector<TextureRes*> list;
-
-    GLuint id;
-    string filename;
-    bool hasMipmap;
-    bool hasAlpha;
-    bool isSkin;
-    int skinsize;
-    GLubyte* data;
-    int datalen;
-
-    void load();
+void TextureRes::load()
+{
+    ImageRec texture;
 
-public:
-    TextureRes(const string& filename, bool hasMipmap, bool hasAlpha);
-    TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize);
-    ~TextureRes();
-    void bind();
+    //load image into 'texture'
+    if (!load_image(filename.c_str(), texture)) {
+        cerr << "Texture " << filename << " loading failed" << endl;
+        return;
+    }
 
-    static void reloadAll();
-};
+    skinsize = texture.sizeX;
+    GLuint type = GL_RGBA;
+    if (texture.bpp == 24)
+        type = GL_RGB;
 
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
-vector<TextureRes*> TextureRes::list;
+    glDeleteTextures(1, &id);
+    glGenTextures(1, &id);
+    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
-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;
-       
-       glPixelStorei(GL_UNPACK_ALIGNMENT,1);
-
-    glDeleteTextures(1,&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(hasMipmap){
-               glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,(trilinear?GL_LINEAR_MIPMAP_LINEAR:GL_LINEAR_MIPMAP_NEAREST));
-        glTexParameteri(GL_TEXTURE_2D,GL_GENERATE_MIPMAP,GL_TRUE);
-    }else{
-               glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
+    glBindTexture(GL_TEXTURE_2D, id);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+    if (hasMipmap) {
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
+        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
+    } else {
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     }
-       
-       if(isSkin){
+
+    if (isSkin) {
         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];
+        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{
+    } else {
         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
-       }
+    }
 }
 
-void TextureRes::bind(){
-    glBindTexture(GL_TEXTURE_2D,id);
+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) {
+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) {
+    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];
+    *skinsizep = skinsize;
+    for (int i = 0; i < datalen; i++)
+        array[i] = data[i];
     list.push_back(this);
 }
 
-TextureRes::~TextureRes(){
+TextureRes::~TextureRes()
+{
     free(data);
-    glDeleteTextures(1,&id);
-    for(vector<TextureRes*>::iterator it=list.begin();it!=list.end();it++)
-        if(*it==this){
+    glDeleteTextures(1, &id);
+    for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
+        if (*it == this) {
             list.erase(it);
             break;
         }
 }
 
-void TextureRes::reloadAll(){
-    for(vector<TextureRes*>::iterator it=list.begin();it!=list.end();it++)
-        (*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){
+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(){
-    if(tex){
+void Texture::destroy()
+{
+    if (tex) {
         delete tex;
-        tex=NULL;
+        tex = NULL;
     }
 }
 
-void Texture::bind(){
-    if(tex)
+void Texture::bind()
+{
+    if (tex)
         tex->bind();
     else
-        glBindTexture(GL_TEXTURE_2D,0);
+        glBindTexture(GL_TEXTURE_2D, 0);
 }
-
-void Texture::reloadAll(){
-    TextureRes::reloadAll();
-}
-