Browse Source

texture manager, texture loader, texture

Eduardo Bart 11 years ago
parent
commit
f10ced4626
9 changed files with 426 additions and 1 deletions
  1. 3
    0
      CMakeLists.txt
  2. 1
    1
      src/engine.cpp
  3. 2
    0
      src/graphics.cpp
  4. 83
    0
      src/texture.cpp
  5. 56
    0
      src/texture.h
  6. 125
    0
      src/textureloader.cpp
  7. 36
    0
      src/textureloader.h
  8. 71
    0
      src/texturemanager.cpp
  9. 49
    0
      src/texturemanager.h

+ 3
- 0
CMakeLists.txt View File

@@ -50,6 +50,9 @@ ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
50 50
 
51 51
 # find sources
52 52
 SET(SOURCES
53
+    src/textureloader.cpp
54
+    src/texture.cpp
55
+    src/texturemanager.cpp
53 56
     src/configmanager.cpp
54 57
     src/resourcemanager.cpp
55 58
     src/main.cpp

+ 1
- 1
src/engine.cpp View File

@@ -126,7 +126,7 @@ void Engine::onClose()
126 126
 
127 127
 void Engine::onResize(int width, int height)
128 128
 {
129
-
129
+    g_graphics.resize(width, height);
130 130
 }
131 131
 
132 132
 void Engine::onInputEvent(InputEvent *event)

+ 2
- 0
src/graphics.cpp View File

@@ -24,9 +24,11 @@
24 24
 
25 25
 #include "graphics.h"
26 26
 #include "logger.h"
27
+#include "texturemanager.h"
27 28
 
28 29
 #include <GL/gl.h>
29 30
 #include <GL/glu.h>
31
+#include "platform.h"
30 32
 
31 33
 Graphics g_graphics;
32 34
 

+ 83
- 0
src/texture.cpp View File

@@ -0,0 +1,83 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#include "texture.h"
26
+
27
+#include <GL/gl.h>
28
+#include <GL/glu.h>
29
+
30
+Texture::Texture(int width, int height, unsigned char *pixels, int components)
31
+{
32
+    m_width = width;
33
+    m_height = height;
34
+
35
+    glGenTextures(1, &m_textureId);
36
+    glBindTexture(GL_TEXTURE_2D, m_textureId);
37
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
38
+
39
+    GLenum format = 0;
40
+    switch(components) {
41
+        case 4:
42
+            format = GL_RGBA;
43
+            break;
44
+        case 3:
45
+            format = GL_RGB;
46
+            break;
47
+        case 2:
48
+            format = GL_LUMINANCE_ALPHA;
49
+            break;
50
+        case 1:
51
+            format = GL_LUMINANCE;
52
+            break;
53
+    }
54
+
55
+    glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, format, GL_UNSIGNED_BYTE, pixels);
56
+
57
+    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
58
+    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
59
+
60
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
61
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
62
+
63
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
64
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
65
+}
66
+
67
+Texture::~Texture()
68
+{
69
+    if(m_textureId > 0)
70
+        glDeleteTextures(1, &m_textureId);
71
+}
72
+
73
+void Texture::enableBilinearFilter()
74
+{
75
+    glBindTexture(GL_TEXTURE_2D, m_textureId);
76
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
77
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
78
+}
79
+
80
+void Texture::bind()
81
+{
82
+    glBindTexture(GL_TEXTURE_2D, m_textureId);
83
+}

+ 56
- 0
src/texture.h View File

@@ -0,0 +1,56 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef TEXTURE_H
26
+#define TEXTURE_H
27
+
28
+#include <GL/gl.h>
29
+#include <boost/shared_ptr.hpp>
30
+
31
+class TextureManager;
32
+
33
+class Texture
34
+{
35
+public:
36
+    Texture(int width, int height, unsigned char *pixels, int components);
37
+    virtual ~Texture();
38
+
39
+    /// Bind texture for drawing
40
+    void bind();
41
+
42
+    /// Enable texture bilinear filter (smooth scaled textures)
43
+    void enableBilinearFilter();
44
+
45
+    int getWidth() const { return m_width; }
46
+    int getHeight() const { return m_height; }
47
+
48
+private:
49
+    GLuint m_textureId;
50
+    int m_width;
51
+    int m_height;
52
+};
53
+
54
+typedef boost::shared_ptr<Texture> TexturePtr;
55
+
56
+#endif // TEXTURE_H

+ 125
- 0
src/textureloader.cpp View File

@@ -0,0 +1,125 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#include "textureloader.h"
26
+
27
+#include <cstdio>
28
+#include <png.h>
29
+
30
+TexturePtr TextureLoader::loadPNG(const unsigned char *fileData, unsigned int fileSize)
31
+{
32
+    TexturePtr texture;
33
+    FILE *pngFile = fmemopen((void*)fileData, fileSize, "rb");
34
+
35
+    if(!pngFile)
36
+        return texture;
37
+
38
+    png_byte sig[8];
39
+    if(!fread(&sig, 8, 1, pngFile))
40
+        return texture;
41
+
42
+    if(png_sig_cmp(sig, 0, 8))
43
+        return texture;
44
+
45
+    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
46
+    if(!png_ptr)
47
+        return texture;
48
+
49
+    png_infop info_ptr = png_create_info_struct(png_ptr);
50
+    if(!info_ptr) {
51
+        png_destroy_read_struct(&png_ptr, NULL, NULL);
52
+        return texture;
53
+    }
54
+
55
+    if(setjmp(png_jmpbuf(png_ptr))) {
56
+        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
57
+        return texture;
58
+    }
59
+
60
+    png_init_io(png_ptr, pngFile);
61
+    png_set_sig_bytes(png_ptr, 8);
62
+    png_read_info(png_ptr, info_ptr);
63
+
64
+    int bitDepth = png_get_bit_depth(png_ptr, info_ptr);
65
+    int colourType = png_get_color_type(png_ptr, info_ptr);
66
+
67
+    if(colourType == PNG_COLOR_TYPE_PALETTE)
68
+        png_set_palette_to_rgb(png_ptr);
69
+
70
+    if(bitDepth < 8 && (colourType == PNG_COLOR_TYPE_GRAY || colourType == PNG_COLOR_TYPE_GRAY_ALPHA))
71
+        png_set_expand_gray_1_2_4_to_8(png_ptr);
72
+
73
+    if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
74
+        png_set_tRNS_to_alpha(png_ptr);
75
+
76
+    if(bitDepth < 8)
77
+        png_set_packing(png_ptr);
78
+    else if(bitDepth == 16)
79
+        png_set_strip_16(png_ptr);
80
+
81
+    png_read_update_info(png_ptr, info_ptr);
82
+
83
+    png_uint_32 width, height;
84
+    png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colourType, NULL, NULL, NULL);
85
+
86
+    int components;
87
+    switch(colourType)
88
+    {
89
+        case PNG_COLOR_TYPE_GRAY:
90
+            components = 1;
91
+            break;
92
+        case PNG_COLOR_TYPE_GRAY_ALPHA:
93
+            components = 2;
94
+            break;
95
+        case PNG_COLOR_TYPE_RGB:
96
+            components = 3;
97
+            break;
98
+        case PNG_COLOR_TYPE_RGB_ALPHA:
99
+            components = 4;
100
+            break;
101
+        default:
102
+            if(png_ptr)
103
+                png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
104
+            return texture;
105
+    };
106
+
107
+    unsigned char *pixels = new unsigned char[width * height * components];
108
+
109
+    png_bytep *row_pointers = new png_bytep[height];
110
+    for(unsigned int i = 0; i < height; ++i)
111
+        row_pointers[i] = (png_bytep)(pixels + (i * width * components));
112
+
113
+    png_read_image(png_ptr, row_pointers);
114
+    png_read_end(png_ptr, NULL);
115
+
116
+    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
117
+    fclose(pngFile);
118
+    delete[] row_pointers;
119
+
120
+    texture = TexturePtr(new Texture(width, height, pixels, components));
121
+
122
+    delete[] pixels;
123
+
124
+    return texture;
125
+}

+ 36
- 0
src/textureloader.h View File

@@ -0,0 +1,36 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef TEXTURELOADER_H
26
+#define TEXTURELOADER_H
27
+
28
+#include "texture.h"
29
+
30
+namespace TextureLoader
31
+{
32
+    /// Load a png textures using libpng
33
+    TexturePtr loadPNG(const unsigned char *fileData, unsigned int fileSize);
34
+}
35
+
36
+#endif // TEXTURELOADER_H

+ 71
- 0
src/texturemanager.cpp View File

@@ -0,0 +1,71 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#include "texturemanager.h"
26
+#include "resourcemanager.h"
27
+#include "textureloader.h"
28
+#include "logger.h"
29
+
30
+#include <boost/algorithm/string.hpp>
31
+
32
+TextureManager g_textures;
33
+
34
+TextureManager::TextureManager()
35
+{
36
+
37
+}
38
+
39
+TextureManager::~TextureManager()
40
+{
41
+    m_texturesMap.clear();
42
+}
43
+
44
+TexturePtr TextureManager::get(const std::string& textureFile)
45
+{
46
+    TexturePtr texture;
47
+
48
+    // check if the texture is already loaded
49
+    TexturesMap::iterator it = m_texturesMap.find(textureFile);
50
+    if(it != m_texturesMap.end())
51
+        texture = it->second;
52
+    else { // load texture
53
+        // currently only png textures are supported
54
+        if(!boost::ends_with(textureFile, ".png")) {
55
+            error("Unable to load texture %s, file format no supported.", textureFile.c_str());
56
+            return texture;
57
+        }
58
+
59
+        unsigned int fileSize;
60
+        unsigned char *textureFileData = g_resources.loadFile(textureFile, &fileSize);
61
+        if(!textureFileData)
62
+            return texture;
63
+
64
+        texture = TextureLoader::loadPNG(textureFileData, fileSize);
65
+        if(!texture)
66
+            error("Unable to load texture %s, loading error.", textureFile.c_str());
67
+        delete[] textureFileData;
68
+    }
69
+
70
+    return texture;
71
+}

+ 49
- 0
src/texturemanager.h View File

@@ -0,0 +1,49 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef TEXTUREMANAGER_H
26
+#define TEXTUREMANAGER_H
27
+
28
+#include "texture.h"
29
+
30
+#include <string>
31
+#include <map>
32
+
33
+class TextureManager
34
+{
35
+public:
36
+    TextureManager();
37
+    ~TextureManager();
38
+
39
+    /// Load a texture from file, if it was already loaded a cached one will be retrieved
40
+    TexturePtr get(const std::string& textureFile);
41
+
42
+private:
43
+    typedef std::map<std::string, TexturePtr> TexturesMap;
44
+    TexturesMap m_texturesMap;
45
+};
46
+
47
+extern TextureManager g_textures;
48
+
49
+#endif // TEXTUREMANAGER_H

Loading…
Cancel
Save