1 /*
2     Copyright (c) 2009 Andrew Caudwell (acaudwell@gmail.com)
3     All rights reserved.
4 
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8     1. Redistributions of source code must retain the above copyright
9        notice, this list of conditions and the following disclaimer.
10     2. Redistributions in binary form must reproduce the above copyright
11        notice, this list of conditions and the following disclaimer in the
12        documentation and/or other materials provided with the distribution.
13     3. The name of the author may not be used to endorse or promote products
14        derived from this software without specific prior written permission.
15 
16     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef TEXTURE_H
29 #define TEXTURE_H
30 
31 #include "SDL_image.h"
32 
33 #include "resource.h"
34 #include "gl.h"
35 
36 #include <string>
37 
38 class TextureException : public ResourceException {
39 public:
TextureException(std::string & texture_file)40     TextureException(std::string& texture_file) : ResourceException(texture_file) {}
41 };
42 
43 class TextureResource : public Resource {
44     bool mipmaps;
45     GLint wrap;
46     GLint min_filter;
47     GLint mag_filter;
48     std::string filename;
49 
50     GLenum colourFormat(SDL_Surface* surface);
51 public:
52     int w, h;
53     GLenum target;
54     GLenum format;
55     GLuint textureid;
56     GLubyte* data;
57 
58     TextureResource();
59     TextureResource(int width, int height,  bool mipmaps, GLint wrap, GLenum format, GLubyte* data = 0);
60     TextureResource(const std::string& filename, bool mipmaps, GLint wrap, bool external);
61 
62     void setWrapStyle(GLint wrap);
63 
64     void setFiltering(GLint min_filter, GLint mag_filter);
65     void setDefaultFiltering();
66 
67     void bind();
68 
69     void createTexture();
70 
71     void reload();
72 
73     void load(bool reload = false);
74 
75     void unload();
76 
77     ~TextureResource();
78 };
79 
80 class TextureManager : public ResourceManager {
81     int  resource_seq;
82 
83     void addResource(TextureResource* r);
84 public:
85     bool trilinear;
86 
87     TextureManager();
88 
89     TextureResource* grabFile(const std::string& filename, bool mipmaps = true, GLint wrap = GL_CLAMP_TO_EDGE);
90     TextureResource*     grab(const std::string& filename, bool mipmaps = true, GLint wrap = GL_CLAMP_TO_EDGE, bool external_file = false);
91 
92     TextureResource* create(int width, int height, bool mipmaps, GLint wrap, GLenum format, GLubyte* data  = 0);
93     TextureResource* create(GLenum target = GL_TEXTURE_2D);
94 
95     void unload();
96     void reload();
97 };
98 
99 extern TextureManager texturemanager;
100 
101 #endif
102