Each module now includes its own header first. Removed sstream.h and the definition...
[crack-attack.git] / src / GarbageFlavorImage.cxx
blobe821b55220ab9f05b37db3336f38547cdfe29acb
1 /*
2 * GarbageFlavorImage.cxx
3 * Daniel Nelson - 10/7/0
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Daniel Nelson - aluminumangel.org
23 * 174 W. 18th Ave.
24 * Columbus, OH 43210
26 * Handles the the garbage flavor image.
29 #include "GarbageFlavorImage.h"
31 #include <iomanip>
32 #include <sstream>
34 #include "TextureLoader.h"
35 #include "Game.h"
36 #include "Displayer.h"
37 #include "Random.h"
39 int GarbageFlavorImage::current_texture;
40 int GarbageFlavorImage::associated_garbage_id;
41 int GarbageFlavorImage::x, GarbageFlavorImage::y;
42 bool GarbageFlavorImage::network_texture = false;
44 void GarbageFlavorImage::initialize ( )
46 // network garbage flavor image stuff is setup before initialization
47 current_texture = 0;
48 associated_garbage_id = -1;
50 // insure existence of default files
51 char file_name[256];
52 for (int n = GC_NUMBER_STANDARD_GARBAGE_TEX; n--; ) {
53 GarbageFlavorImage::buildGarbageTextureFileName(file_name, n);
55 if (!TextureLoader::fileExists(file_name)) {
56 char original_file_name[256];
57 GarbageFlavorImage::buildOriginalGarbageTextureFileName(
58 original_file_name, n);
60 GLubyte *texture = TextureLoader::loadTGA(original_file_name,
61 DC_GARBAGE_TEX_LENGTH, DC_GARBAGE_TEX_LENGTH);
62 TextureLoader::createTGA(file_name, texture, DC_GARBAGE_TEX_LENGTH,
63 DC_GARBAGE_TEX_LENGTH, TL_GARBAGE_TEXTURE_TGA_ID);
64 if (texture != null) {
65 delete [] texture;
66 texture = null;
72 bool GarbageFlavorImage::personalGarbageFlavorImageExists ( )
74 * Called by Communicator or obj_garbage.cxx at startup to determine if we have
75 * a personal garbage flavor image to send.
78 char file_name[256];
79 TextureLoader::buildLocalDataFileName(GC_GARBAGE_MY_TEX_FILE_NAME, file_name);
80 return TextureLoader::fileExists(file_name);
83 GLubyte *GarbageFlavorImage::loadPersonalGarbageFlavorImage ( )
85 * Called by Communicator at startup to obtain the personal garbage flavor
86 * image or by obj_garbage.cxx if in solo mode.
89 char file_name[256];
90 TextureLoader::buildLocalDataFileName(GC_GARBAGE_MY_TEX_FILE_NAME, file_name);
92 int width, height;
93 TextureLoader::determineTGASize(file_name, width, height);
95 if (width > DC_GARBAGE_TEX_LENGTH || height > DC_GARBAGE_TEX_LENGTH) {
96 std::cerr << "Texture file '" << file_name << "' exceeds allowed size of "
97 << "[" << DC_GARBAGE_TEX_LENGTH << 'x' << DC_GARBAGE_TEX_LENGTH << "]."
98 << std::endl;
99 exit(1);
102 GLubyte *original_texture = TextureLoader::loadTGA(file_name, width, height);
104 if (width == DC_GARBAGE_TEX_LENGTH && height == DC_GARBAGE_TEX_LENGTH)
105 return original_texture;
107 GLubyte *texture
108 = new GLubyte[DC_GARBAGE_TEX_LENGTH * DC_GARBAGE_TEX_LENGTH * 4];
110 for (int t = DC_GARBAGE_TEX_LENGTH; t--; )
111 for (int s = DC_GARBAGE_TEX_LENGTH; s--; ) {
112 if (s + height > DC_GARBAGE_TEX_LENGTH
113 && t + width > DC_GARBAGE_TEX_LENGTH) {
114 int texel = ((t - DC_GARBAGE_TEX_LENGTH + width) * height
115 + (s - DC_GARBAGE_TEX_LENGTH + height)) * 4;
116 texture[(t * DC_GARBAGE_TEX_LENGTH + s) * 4 + 0]
117 = original_texture[texel + 0];
118 texture[(t * DC_GARBAGE_TEX_LENGTH + s) * 4 + 1]
119 = original_texture[texel + 1];
120 texture[(t * DC_GARBAGE_TEX_LENGTH + s) * 4 + 2]
121 = original_texture[texel + 2];
122 texture[(t * DC_GARBAGE_TEX_LENGTH + s) * 4 + 3]
123 = original_texture[texel + 3];
124 } else {
125 texture[(t * DC_GARBAGE_TEX_LENGTH + s) * 4 + 0] = 0;
126 texture[(t * DC_GARBAGE_TEX_LENGTH + s) * 4 + 1] = 0;
127 texture[(t * DC_GARBAGE_TEX_LENGTH + s) * 4 + 2] = 0;
128 texture[(t * DC_GARBAGE_TEX_LENGTH + s) * 4 + 3] = 0;
132 if (original_texture != null) {
133 delete [] original_texture;
134 original_texture = null;
137 return texture;
140 void GarbageFlavorImage::handleNetworkGarbageFlavorImage ( GLubyte *texture )
142 * Called by Communicator at startup if opponent sends us his personal garbage
143 * flavor image.
146 char net_tex_file_name[256];
147 TextureLoader::buildLocalDataFileName(GC_GARBAGE_NET_TEX_FILE_NAME,
148 net_tex_file_name);
150 network_texture = true;
152 // create the network texture file
153 TextureLoader::createTGA(net_tex_file_name, texture, DC_GARBAGE_TEX_LENGTH,
154 DC_GARBAGE_TEX_LENGTH, TL_GARBAGE_TEXTURE_TGA_ID);
156 // determine it's check sum
157 unsigned long check_sum
158 = TextureLoader::determineTGACheckSum(net_tex_file_name,
159 DC_GARBAGE_TEX_LENGTH, DC_GARBAGE_TEX_LENGTH);
161 // check to see if we already have a copy of it in our main list
162 int n;
163 int first_open_slot = -1;
164 char file_name[256];
165 for (n = GC_GARBAGE_TEX_MAX_NUMBER; n--; ) {
166 GarbageFlavorImage::buildGarbageTextureFileName(file_name, n);
168 if (TextureLoader::fileExists(file_name)) {
169 if (check_sum == TextureLoader::determineTGACheckSum(file_name,
170 DC_GARBAGE_TEX_LENGTH, DC_GARBAGE_TEX_LENGTH))
171 break;
172 } else
173 first_open_slot = n;
176 // if we don't already have a copy, create one
177 if (n == -1 && first_open_slot != -1) {
178 GarbageFlavorImage::buildGarbageTextureFileName(file_name, first_open_slot);
179 TextureLoader::createTGA(file_name, texture, DC_GARBAGE_TEX_LENGTH,
180 DC_GARBAGE_TEX_LENGTH, TL_GARBAGE_TEXTURE_TGA_ID);
184 void GarbageFlavorImage::buildGarbageTextureFileName ( char file_name[256],
185 const char *dir_name, int n )
187 std::ostringstream s;
188 s << dir_name << GC_GARBAGE_TEX_FILE_NAME_BASE "_"
189 << std::setw(GC_GARBAGE_TEX_NUMBER_DIGITS) << std::setfill('0') << n
190 << ".tga" << std::ends;
191 strncpy(file_name, s.str().data(), 256);
194 void GarbageFlavorImage::buildGarbageTextureFileName ( char file_name[256],
195 int n )
197 char base_name[256];
198 buildGarbageTextureFileName(base_name, "", n);
199 TextureLoader::buildLocalDataFileName(base_name, file_name);
202 void GarbageFlavorImage::buildOriginalGarbageTextureFileName (
203 char file_name[256], int n )
205 buildGarbageTextureFileName(file_name, GC_DATA_DIRECTORY(""), n);
208 void GarbageFlavorImage::requestGarbageFlavorImage_inline_split_ (
209 Garbage &garbage )
211 * Request to use the garbage flavor image for this piece of garbage. If the
212 * garbage flavor image is free, it will be set to the given garbage. The
213 * height and width of the garbage must be four or more.
216 associated_garbage_id = garbage.id;
218 x = 1 + Random::number(garbage.width - 3);
220 if (Random::number2(4))
221 y = 1;
222 else
223 y = 1 + Random::number(garbage.height - 3);
225 int last_texture = current_texture;
226 if (network_texture)
227 current_texture
228 = (Random::number(DC_CHANCE_USE_NET_TEX) ? 0 : 1 + Random::number(3));
229 else
230 current_texture = Random::number2(4);
232 if (last_texture != current_texture) {
233 glBindTexture(GL_TEXTURE_2D, Displayer::garbage_texture);
234 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, DC_GARBAGE_TEX_LENGTH,
235 DC_GARBAGE_TEX_LENGTH, GL_RGBA, GL_UNSIGNED_BYTE,
236 Displayer::garbage_texture_data[current_texture]);