1 /***************************************************************************
2 						texture.h    -  description
3 							-------------------
4 	begin                : july, 2nd 2004
5 	copyright            : (C) 2004-2007 by Duong Khang NGUYEN
6 	email                : neoneurone @ gmail com
7 
8 	$Id: texture.h 375 2008-10-28 14:47:15Z neoneurone $
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   any later version.                                                    *
17  *                                                                         *
18  ***************************************************************************/
19 
20 #ifndef _OPENCITY_TEXTURE_H_
21 #define _OPENCITY_TEXTURE_H_ 1
22 
23 #include "main.h"
24 
25 #include <map>					// Used for automanaged texture loading cache
26 #include <utility>
27 
28 using std::map;
29 using std::pair;
30 
31 class BuildingLayer;
32 
33 
34 //========================================================================
35 /** This class can be considered as a tool class for texture image loading.
36 	Thanks to the SDL_image library, it can read all the file formats that
37 	SDL_image supports.
38 */
39 class Texture {
40 public:
41 	Texture();
42 	Texture( const string& rcFile );
43 	Texture( const string& rcFile, bool b3d );		// quick hack
44 
45 	~Texture();
46 
47 
48 //========================================================================
49 /** Return the texture name
50 */
51 	GLuint GetName() const;
52 
GetSize(uint & ruiWidth,uint & ruiHeight)53 	void GetSize( uint& ruiWidth, uint& ruiHeight ) const
54 	{ ruiWidth = muiWidth; ruiHeight = muiHeight; }
55 
56 
57 //========================================================================
58 /** After calling this function, the image pixels are flipped in the
59 vertical direction because the SDL_image library reads the image's
60 pixels from the upper left corner, left to right, to the bottom right
61 corner whereas the glTexImage2D builds the texture from the bottom left
62 corner, left to right, to the upper right corner
63 	\param psurface A source surface
64 	\return A new surface with the flipped pixels from the source surface. The
65 returned surface must be freed by the caller
66 */
67 	static SDL_Surface*
68 	HorizontalMirror(
69 		const SDL_Surface* const psurface
70 	);
71 
72 
73 //========================================================================
74 /** Convert a SDL surface to OpenGL texture
75 	\param psurface The SDL source surface to convert
76 	\param ruiTexture A reference to a GLuint variable.
77 	\return Nothing
78 */
79 	static void
80 	Surface2Texture(
81 		const SDL_Surface* const psurface,
82 		GLuint& ruiTexture
83 	);
84 
85 	static void
86 	Surface2Texture3D(
87 		const SDL_Surface* const psurface,
88 		GLuint& ruiTexture,
89 		GLint format
90 	);
91 
92 
93 	static void
94 	Building2Texture(
95 		const BuildingLayer* const pLayer,
96 		GLuint& ruiTexture
97 	);
98 
99 
100 private:
101 	static map<string, pair<GLuint, uint> >	mmapTextureCache;	///< Automanaged texture name cache
102 
103 	uint muiWidth, muiHeight;
104 	GLuint muiTextureName;
105 	string msTextureFile;
106 
107 
108 //========================================================================
109 // PRIVATE METHODS
110 //========================================================================
111 
112 //========================================================================
113 /** Open the specified image, read it into a SDL_surface then convert it
114 to an OpenGL texture. Use it carefully because it doesn't handle error
115 checking right now.
116 	\param rcFile The path to the image.
117 	\return The index of the new OpenGL texture (type const GLuint)
118 */
119 	static const GLuint
120 	_Load( const string& rcFile );
121 
122 	static const GLuint
123 	_Load3D( const string& rcFile );
124 
125 
126 //========================================================================
127 /** Load the specified texture file into an OpenGL texture object
128 	\param rcFile The path to the image.
129 	\param ruiW,ruiH The size of the loaded texture
130 	\return The index of the new OpenGL texture (type const GLuint)
131 	\sa Load()
132 */
133 	static const GLuint
134 	_Load(
135 		const string& rcFile,
136 		uint& ruiW,
137 		uint& ruiH
138 	);
139 
140 	static const GLuint
141 	_Load3D(
142 		const string& rcFile,
143 		uint& ruiW,
144 		uint& ruiH
145 	);
146 
147 //========================================================================
148 /** Find the correct OpenGL dimensions for given width w and height h
149 	\param w, h The origial texture width and height
150 	\param rW, rH The corrected texture width and height
151 	\return True if the size has changed, false otherwise
152 */
153 	static bool
154 	_GetCorrectSize(
155 		const uint w, const uint h,
156 		uint& rW,    uint& rH );
157 
158 
159 //========================================================================
160 /** Remove unnecessary relative path token. For example: from "abc/../file.jpg"
161 to "file.jpg". However it returns "/../" unchanged.
162 	\param rcsPath The path to process
163 	\return The resolved relative path
164 */
165 	static string
166 	_ResolveRelativePath(const string& rcsPath);
167 
168 };
169 
170 #endif
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203