2 * GarbageFlavorImage.cxx
3 * Daniel Nelson - 10/7/0
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
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
26 * Handles the the garbage flavor image.
35 #include "TextureLoader.h"
37 #include "Displayer.h"
38 #include "GarbageFlavorImage.h"
41 int GarbageFlavorImage::current_texture
;
42 int GarbageFlavorImage::associated_garbage_id
;
43 int GarbageFlavorImage::x
, GarbageFlavorImage::y
;
44 bool GarbageFlavorImage::network_texture
= false;
46 void GarbageFlavorImage::initialize ( )
48 // network garbage flavor image stuff is setup before initialization
50 associated_garbage_id
= -1;
52 // insure existence of default files
54 for (int n
= GC_NUMBER_STANDARD_GARBAGE_TEX
; n
--; ) {
55 GarbageFlavorImage::buildGarbageTextureFileName(file_name
, n
);
57 if (!TextureLoader::fileExists(file_name
)) {
58 char original_file_name
[256];
59 GarbageFlavorImage::buildOriginalGarbageTextureFileName(
60 original_file_name
, n
);
62 GLubyte
*texture
= TextureLoader::loadTGA(original_file_name
,
63 DC_GARBAGE_TEX_LENGTH
, DC_GARBAGE_TEX_LENGTH
);
64 TextureLoader::createTGA(file_name
, texture
, DC_GARBAGE_TEX_LENGTH
,
65 DC_GARBAGE_TEX_LENGTH
, TL_GARBAGE_TEXTURE_TGA_ID
);
66 if (texture
!= null
) {
74 bool GarbageFlavorImage::personalGarbageFlavorImageExists ( )
76 * Called by Communicator or obj_garbage.cxx at startup to determine if we have
77 * a personal garbage flavor image to send.
81 TextureLoader::buildLocalDataFileName(GC_GARBAGE_MY_TEX_FILE_NAME
, file_name
);
82 return TextureLoader::fileExists(file_name
);
85 GLubyte
*GarbageFlavorImage::loadPersonalGarbageFlavorImage ( )
87 * Called by Communicator at startup to obtain the personal garbage flavor
88 * image or by obj_garbage.cxx if in solo mode.
92 TextureLoader::buildLocalDataFileName(GC_GARBAGE_MY_TEX_FILE_NAME
, file_name
);
95 TextureLoader::determineTGASize(file_name
, width
, height
);
97 if (width
> DC_GARBAGE_TEX_LENGTH
|| height
> DC_GARBAGE_TEX_LENGTH
) {
98 cerr
<< "Texture file '" << file_name
<< "' exceeds allowed " "size of [" <<
99 DC_GARBAGE_TEX_LENGTH
<< 'x' << DC_GARBAGE_TEX_LENGTH
<< "]." << endl
;
103 GLubyte
*original_texture
= TextureLoader::loadTGA(file_name
, width
, height
);
105 if (width
== DC_GARBAGE_TEX_LENGTH
&& height
== DC_GARBAGE_TEX_LENGTH
)
106 return original_texture
;
109 = new GLubyte
[DC_GARBAGE_TEX_LENGTH
* DC_GARBAGE_TEX_LENGTH
* 4];
111 for (int t
= DC_GARBAGE_TEX_LENGTH
; t
--; )
112 for (int s
= DC_GARBAGE_TEX_LENGTH
; s
--; ) {
113 if (s
+ height
> DC_GARBAGE_TEX_LENGTH
114 && t
+ width
> DC_GARBAGE_TEX_LENGTH
) {
115 int texel
= ((t
- DC_GARBAGE_TEX_LENGTH
+ width
) * height
116 + (s
- DC_GARBAGE_TEX_LENGTH
+ height
)) * 4;
117 texture
[(t
* DC_GARBAGE_TEX_LENGTH
+ s
) * 4 + 0]
118 = original_texture
[texel
+ 0];
119 texture
[(t
* DC_GARBAGE_TEX_LENGTH
+ s
) * 4 + 1]
120 = original_texture
[texel
+ 1];
121 texture
[(t
* DC_GARBAGE_TEX_LENGTH
+ s
) * 4 + 2]
122 = original_texture
[texel
+ 2];
123 texture
[(t
* DC_GARBAGE_TEX_LENGTH
+ s
) * 4 + 3]
124 = original_texture
[texel
+ 3];
126 texture
[(t
* DC_GARBAGE_TEX_LENGTH
+ s
) * 4 + 0] = 0;
127 texture
[(t
* DC_GARBAGE_TEX_LENGTH
+ s
) * 4 + 1] = 0;
128 texture
[(t
* DC_GARBAGE_TEX_LENGTH
+ s
) * 4 + 2] = 0;
129 texture
[(t
* DC_GARBAGE_TEX_LENGTH
+ s
) * 4 + 3] = 0;
133 if (original_texture
!= null
) {
134 delete [] original_texture
;
135 original_texture
= null
;
141 void GarbageFlavorImage::handleNetworkGarbageFlavorImage ( GLubyte
*texture
)
143 * Called by Communicator at startup if opponent sends us his personal garbage
147 char net_tex_file_name
[256];
148 TextureLoader::buildLocalDataFileName(GC_GARBAGE_NET_TEX_FILE_NAME
,
151 network_texture
= true;
153 // create the network texture file
154 TextureLoader::createTGA(net_tex_file_name
, texture
, DC_GARBAGE_TEX_LENGTH
,
155 DC_GARBAGE_TEX_LENGTH
, TL_GARBAGE_TEXTURE_TGA_ID
);
157 // determine it's check sum
158 unsigned long check_sum
159 = TextureLoader::determineTGACheckSum(net_tex_file_name
,
160 DC_GARBAGE_TEX_LENGTH
, DC_GARBAGE_TEX_LENGTH
);
162 // check to see if we already have a copy of it in our main list
164 int first_open_slot
= -1;
166 for (n
= GC_GARBAGE_TEX_MAX_NUMBER
; n
--; ) {
167 GarbageFlavorImage::buildGarbageTextureFileName(file_name
, n
);
169 if (TextureLoader::fileExists(file_name
)) {
170 if (check_sum
== TextureLoader::determineTGACheckSum(file_name
,
171 DC_GARBAGE_TEX_LENGTH
, DC_GARBAGE_TEX_LENGTH
))
177 // if we don't already have a copy, create one
178 if (n
== -1 && first_open_slot
!= -1) {
179 GarbageFlavorImage::buildGarbageTextureFileName(file_name
, first_open_slot
);
180 TextureLoader::createTGA(file_name
, texture
, DC_GARBAGE_TEX_LENGTH
,
181 DC_GARBAGE_TEX_LENGTH
, TL_GARBAGE_TEXTURE_TGA_ID
);
185 void GarbageFlavorImage::buildGarbageTextureFileName ( char file_name
[256],
186 const char *dir_name
, int n
)
189 s
<< dir_name
<< GC_GARBAGE_TEX_FILE_NAME_BASE
"_"
190 << setw(GC_GARBAGE_TEX_NUMBER_DIGITS
) << setfill('0') << n
<< ".tga" << ends
;
191 strncpy(file_name
, s
.str().data(), 256);
194 void GarbageFlavorImage::buildGarbageTextureFileName ( char file_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_ (
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))
223 y
= 1 + Random::number(garbage
.height
- 3);
225 int last_texture
= current_texture
;
228 = (Random::number(DC_CHANCE_USE_NET_TEX
) ? 0 : 1 + Random::number(3));
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
]);