1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <image/ImageFactory.h>
22 #include <XML/XMLFile.h>
23 #include <GLEXT/GLTextureStore.h>
24 #include <sprites/ExplosionTextures.h>
25 #include <landscape/Landscape.h>
26 #include <lang/LangResource.h>
27 #include <common/Defines.h>
28 #include <stdio.h>
29 
30 ExplosionTextures *ExplosionTextures::instance_ = 0;
31 
instance()32 ExplosionTextures *ExplosionTextures::instance()
33 {
34 	if (!instance_)
35 	{
36 		instance_ = new ExplosionTextures;
37 	}
38 
39 	return instance_;
40 }
41 
ExplosionTextures()42 ExplosionTextures::ExplosionTextures()
43 {
44 
45 }
46 
~ExplosionTextures()47 ExplosionTextures::~ExplosionTextures()
48 {
49 
50 }
51 
createTextures(ProgressCounter * counter)52 bool ExplosionTextures::createTextures(ProgressCounter *counter)
53 {
54 	// Tidy
55 	{
56 		std::map<std::string, GLTextureSet*>::iterator itor;
57 		for (itor = textureSets.begin();
58 			itor != textureSets.end();
59 			++itor)
60 		{
61 			delete itor->second;
62 		}
63 		textureSets.clear();
64 	}
65 
66 	// Tidy
67 	{
68 		std::map<std::string, Image*>::iterator itor;
69 		for (itor = scorchedBitmaps.begin();
70 			itor != scorchedBitmaps.end();
71 			++itor)
72 		{
73 			delete itor->second;
74 		}
75 		scorchedBitmaps.clear();
76 	}
77 
78 	// Tidy
79 	GLTextureStore::instance()->resetModFiles();
80 
81 	if (counter) counter->setNewOp(LANG_RESOURCE("EXPLOSION_TEXTURES", "Explosion Textures"));
82 
83 	{
84 		Image bitmap =
85 			ImageFactory::loadImage(
86 				S3D::eDataLocation,
87 				"data/images/arrow.bmp",
88 				"data/images/arrowi.bmp",
89 				true);
90 		arrowTexture.create(bitmap);
91 		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
92 		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
93 	}
94 
95 	Image bitmap = ImageFactory::loadImage(
96 		S3D::eModLocation,
97 		"data/textures/smoke01.bmp",
98 		"data/textures/smoke01.bmp",
99 		false);
100 	smokeTexture.create(bitmap);
101 	DIALOG_ASSERT(smokeTexture.textureValid());
102 
103 	Image bitmap2 = ImageFactory::loadImage(
104 		S3D::eModLocation,
105 		"data/textures/smoke02.bmp",
106 		"data/textures/smoke02.bmp",
107 		false);
108 	smokeTexture2.create(bitmap2);
109 	DIALOG_ASSERT(smokeTexture2.textureValid());
110 
111 	Image bitmap3 = ImageFactory::loadImage(
112 		S3D::eModLocation,
113 		"data/textures/particle.bmp",
114 		"data/textures/particle.bmp",
115 		false);
116 	particleTexture.create(bitmap3);
117 	DIALOG_ASSERT(particleTexture.textureValid());
118 
119 	Image talkBitmap = ImageFactory::loadAlphaImage(
120 		S3D::eModLocation,
121 		"data/textures/talk.bmp");
122 	talkTexture.create(talkBitmap);
123 	DIALOG_ASSERT(talkTexture.textureValid());
124 
125 	Image bitmap5 = ImageFactory::loadImage(
126 		S3D::eModLocation,
127 		"data/textures/rainm.bmp",
128 		"data/textures/rain.bmp",
129 		false);
130 	rainTexture.create(bitmap5);
131 	DIALOG_ASSERT(rainTexture.textureValid());
132 
133 	Image bitmap6 = ImageFactory::loadImage(
134 		S3D::eModLocation,
135 		"data/textures/snow.bmp",
136 		"data/textures/snowm.bmp",
137 		false);
138 	snowTexture.create(bitmap6);
139 	DIALOG_ASSERT(snowTexture.textureValid());
140 
141 	XMLFile file;
142 	if (!file.readFile(S3D::getModFile("data/textureset.xml")))
143 	{
144 		S3D::dialogMessage("ExplosionTextures", S3D::formatStringBuffer(
145 					  "Failed to parse \"%s\"\n%s",
146 					  "data/textureset.xml",
147 					  file.getParserError()));
148 		return false;
149 	}
150 	if (!file.getRootNode())
151 	{
152 		S3D::dialogMessage("ExplosionTextures", S3D::formatStringBuffer(
153 					  "Failed to find explosion textures definition file \"%s\"",
154 					  "data/textureset.xml"));
155 		return false;
156 	}
157 
158     std::list<XMLNode *>::iterator childrenItor;
159 	std::list<XMLNode *> &children = file.getRootNode()->getChildren();
160     for (childrenItor = children.begin();
161 		 childrenItor != children.end();
162 		 ++childrenItor)
163     {
164 		// Check if it is a texture set
165         XMLNode *currentNode = (*childrenItor);
166 		if (strcmp(currentNode->getName(), "textureset"))
167 		{
168 			S3D::dialogMessage("ExplosionTextures",
169 						  "Failed to find textureset node");
170 			return false;
171 		}
172 		XMLNode *name = 0;
173 		if (!currentNode->getNamedParameter("name", name)) return false;
174 		const char *setName = name->getContent();
175 
176 		// Create and store the new texture set
177 		GLTextureSet *set = new GLTextureSet();
178 		textureSets[setName] = set;
179 
180 		// Load all the textures
181 		std::list<XMLNode *>::iterator textureItor;
182 		std::list<XMLNode *> &textures = currentNode->getChildren();
183 		for (textureItor = textures.begin();
184 			textureItor != textures.end();
185 			++textureItor)
186 		{
187 			// Check if it is a texture
188 			XMLNode *currentTexture = (*textureItor);
189 			if (strcmp(currentTexture->getName(), "texture"))
190 			{
191 				S3D::dialogMessage("ExplosionTextures",
192 							"Failed to find texture sub-node");
193 				return false;
194 			}
195 
196 			// Load texture
197 			std::string texFile =
198 				S3D::formatStringBuffer("data/%s", currentTexture->getContent());
199 			Image bitmap =
200 				ImageFactory::loadImage(
201 					S3D::eModLocation,
202 					texFile,
203 					texFile,
204 					false);
205 			GLTexture *texture = new GLTexture;
206 			if (!texture->create(bitmap))
207 			{
208 				S3D::dialogMessage("ExplosionTextures", S3D::formatStringBuffer(
209 					"Failed to load texture %s",
210 					texFile.c_str()));
211 				return false;
212 			}
213 			set->addTexture(texture);
214 		}
215 	}
216 
217 	return true;
218 }
219 
getTextureSetByName(const std::string & name)220 GLTextureSet *ExplosionTextures::getTextureSetByName(const std::string &name)
221 {
222 	DIALOG_ASSERT(!textureSets.empty());
223 
224 	GLTextureSet *result = (*textureSets.begin()).second;
225 	std::map<std::string, GLTextureSet*>::iterator itor =
226 		textureSets.find(name);
227 	if (itor != textureSets.end())
228 	{
229 		result = (*itor).second;
230 	}
231 	return result;
232 }
233 
getScorchBitmap(const std::string & name)234 Image &ExplosionTextures::getScorchBitmap(const std::string &name)
235 {
236 	if (!name.empty())
237 	{
238 		std::map<std::string, Image*>::iterator findItor =
239 			scorchedBitmaps.find(name);
240 		if (findItor != scorchedBitmaps.end())
241 		{
242 			return *(*findItor).second;
243 		}
244 
245 		if (S3D::fileExists(S3D::getModFile(name)))
246 		{
247 			Image *map = new Image(ImageFactory::loadImage(S3D::eModLocation, name));
248 			scorchedBitmaps[name] = map;
249 			return *map;
250 		}
251 	}
252 	return Landscape::instance()->getScorchMap();
253 }
254