]> git.jsancho.org Git - lugaru.git/blob - Source/Graphic/Texture.cpp
Added braces to all statements with clang-tidy and ran clang-format again
[lugaru.git] / Source / Graphic / Texture.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Graphic/Texture.hpp"
22
23 #include "Utils/Folders.hpp"
24 #include "Utils/ImageIO.hpp"
25
26 using namespace std;
27
28 extern bool trilinear;
29
30 void TextureRes::load()
31 {
32     ImageRec texture;
33
34     //load image into 'texture'
35     if (!load_image(filename.c_str(), texture)) {
36         cerr << "Texture " << filename << " loading failed" << endl;
37         return;
38     }
39
40     skinsize = texture.sizeX;
41     GLuint type = GL_RGBA;
42     if (texture.bpp == 24) {
43         type = GL_RGB;
44     }
45
46     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
47
48     glDeleteTextures(1, &id);
49     glGenTextures(1, &id);
50     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
51
52     glBindTexture(GL_TEXTURE_2D, id);
53     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
54     if (hasMipmap) {
55         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
56         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
57     } else {
58         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
59     }
60
61     if (isSkin) {
62         free(data);
63         const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
64         data = (GLubyte*)malloc(nb * sizeof(GLubyte));
65         datalen = 0;
66         for (int i = 0; i < nb; i++) {
67             if ((i + 1) % 4 || type == GL_RGB) {
68                 data[datalen++] = texture.data[i];
69             }
70         }
71         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
72     } else {
73         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
74     }
75 }
76
77 void TextureRes::bind()
78 {
79     glBindTexture(GL_TEXTURE_2D, id);
80 }
81
82 TextureRes::TextureRes(const string& _filename, bool _hasMipmap)
83     : id(0)
84     , filename(_filename)
85     , hasMipmap(_hasMipmap)
86     , isSkin(false)
87     , skinsize(0)
88     , data(NULL)
89     , datalen(0)
90 {
91     load();
92 }
93
94 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep)
95     : id(0)
96     , filename(_filename)
97     , hasMipmap(_hasMipmap)
98     , isSkin(true)
99     , skinsize(0)
100     , data(NULL)
101     , datalen(0)
102 {
103     load();
104     *skinsizep = skinsize;
105     for (int i = 0; i < datalen; i++) {
106         array[i] = data[i];
107     }
108 }
109
110 TextureRes::~TextureRes()
111 {
112     free(data);
113     glDeleteTextures(1, &id);
114 }
115
116 void Texture::load(const string& filename, bool hasMipmap)
117 {
118     tex.reset(new TextureRes(Folders::getResourcePath(filename), hasMipmap));
119 }
120
121 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
122 {
123     tex.reset(new TextureRes(Folders::getResourcePath(filename), hasMipmap, array, skinsizep));
124 }
125
126 void Texture::bind()
127 {
128     if (tex) {
129         tex->bind();
130     } else {
131         glBindTexture(GL_TEXTURE_2D, 0);
132     }
133 }