GG
Texture.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /* GG is a GUI for SDL and OpenGL.
3  Copyright (C) 2003-2008 T. Zachary Laine
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public License
7  as published by the Free Software Foundation; either version 2.1
8  of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  02111-1307 USA
19 
20  If you do not wish to comply with the terms of the LGPL please
21  contact the author as other terms are available for a fee.
22 
23  Zach Laine
24  whatwasthataddress@gmail.com */
25 
31 #ifndef _GG_Texture_h_
32 #define _GG_Texture_h_
33 
34 #include <GG/Base.h>
35 #include <GG/Exception.h>
36 
37 #include <boost/serialization/access.hpp>
38 #include <boost/serialization/binary_object.hpp>
39 
40 
41 namespace GG {
42 
65 class GG_API Texture
66 {
67 public:
69  Texture();
70  virtual ~Texture();
71 
72 
74  std::string Filename() const;
75  GLenum WrapS() const;
76  GLenum WrapT() const;
77  GLenum MinFilter() const;
78  GLenum MagFilter() const;
79  unsigned int BytesPP() const;
80  X Width() const;
81  Y Height() const;
82  bool MipMapped() const;
83  GLuint OpenGLId() const;
84  const GLfloat* DefaultTexCoords() const;
85  X DefaultWidth() const;
86  Y DefaultHeight() const;
87 
90  void OrthoBlit(const Pt& pt1, const Pt& pt2, const GLfloat* tex_coords = 0) const;
91 
94  void OrthoBlit(const Pt& pt) const;
96 
98  // intialization functions
102  void Load(const std::string& filename, bool mipmap = false);
103 
108  void Init(X width, Y height, const unsigned char* image, GLenum format, GLenum type,
109  unsigned bytes_per_pixel, bool mipmap = false);
110 
115  void Init(X x, Y y, X width, Y height, X image_width,
116  const unsigned char* image, GLenum format, GLenum type,
117  unsigned int bytes_per_pixel, bool mipmap = false);
118 
119  void SetWrap(GLenum s, GLenum t);
120  void SetFilters(GLenum min, GLenum mag);
121  void Clear();
122 
123 
125 
126  GG_ABSTRACT_EXCEPTION(Exception);
127 
129  GG_CONCRETE_EXCEPTION(BadFile, GG::Texture, Exception);
130 
132  GG_CONCRETE_EXCEPTION(InvalidColorChannels, GG::Texture, Exception);
133 
135  GG_CONCRETE_EXCEPTION(InsufficientResources, GG::Texture, Exception);
137 
138 private:
139  Texture(const Texture& rhs);
140  Texture& operator=(const Texture& rhs);
141  void InitFromRawData(X width, Y height, const unsigned char* image, GLenum format, GLenum type,
142  unsigned int bytes_per_pixel, bool mipmap);
143  unsigned char* GetRawBytes();
144 
145  std::string m_filename;
146 
147  unsigned int m_bytes_pp;
148  X m_width;
149  Y m_height;
150 
151  GLenum m_wrap_s, m_wrap_t;
152  GLenum m_min_filter, m_mag_filter;
153 
154  bool m_mipmaps;
155  GLuint m_opengl_id;
156  GLenum m_format;
157  GLenum m_type;
158 
160  GLfloat m_tex_coords[4];
161  X m_default_width;
162  Y m_default_height;
163 
164  friend class boost::serialization::access;
165  template <class Archive>
166  void serialize(Archive& ar, const unsigned int version);
167 };
168 
171 class GG_API SubTexture
172 {
173 public:
175  SubTexture();
176 
181  SubTexture(const boost::shared_ptr<const Texture>& texture, X x1, Y y1, X x2, Y y2);
182 
183  SubTexture(const SubTexture& rhs);
184  const SubTexture& operator=(const SubTexture& rhs);
185  virtual ~SubTexture();
186 
187 
189  bool Empty() const;
190  const GLfloat* TexCoords() const;
191  X Width() const;
192  Y Height() const;
193  const Texture* GetTexture() const;
194 
197  void OrthoBlit(const Pt& pt1, const Pt& pt2) const;
198 
201  void OrthoBlit(const Pt& pt) const;
203 
205 
206  GG_ABSTRACT_EXCEPTION(Exception);
207 
210  GG_CONCRETE_EXCEPTION(BadTexture, GG::SubTexture, Exception);
211 
214  GG_CONCRETE_EXCEPTION(InvalidTextureCoordinates, GG::SubTexture, Exception);
216 
217 private:
218  boost::shared_ptr<const Texture> m_texture;
219  X m_width;
220  Y m_height;
221  GLfloat m_tex_coords[4];
222 };
223 
232 class GG_API TextureManager
233 {
234 public:
236 
240  boost::shared_ptr<Texture> StoreTexture(Texture* texture, const std::string& texture_name);
241 
246  boost::shared_ptr<Texture> StoreTexture(const boost::shared_ptr<Texture>& texture, const std::string& texture_name);
247 
251  boost::shared_ptr<Texture> GetTexture(const std::string& name, bool mipmap = false);
252 
256  void FreeTexture(const std::string& name);
258 
259 private:
260  TextureManager();
261  boost::shared_ptr<Texture> LoadTexture(const std::string& filename, bool mipmap);
262 
263  static bool s_created;
264  static bool s_il_initialized;
265  std::map<std::string, boost::shared_ptr<Texture> > m_textures;
266 
268 };
269 
272 
273 } // namespace GG
274 
275 #endif