Implemented support for loading textures on models and added a simple example.
[crack-attack.git] / src / DrawBlocks.cxx
blob362f3dca6f7c8c016b8d1c7cdd4056d3aeb57259
1 /*
2 * DrawBlocks.cxx
3 * Daniel Nelson - 8/31/0
5 * Copyright (C) 2000 Daniel Nelson
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Daniel Nelson - aluminumangel.org
22 * 174 W. 18th Ave.
23 * Columbus, OH 43210
25 * Handle's the displaying of the blocks.
28 #include <GL/gl.h>
29 #include <GL/glut.h>
31 #include "glext.h"
33 #include "Game.h"
34 #include "Displayer.h"
35 #include "Creep.h"
36 #include "Block.h"
37 #include "BlockManager.h"
38 #include "LightManager.h"
39 #include "Swapper.h"
40 #include "X.h"
42 const GLfloat Displayer::block_colors[BF_NUMBER][3]
43 = { { 0.73f, 0.0f, 0.73f }, // purple
44 { 0.2f, 0.2f, 0.8f }, // blue
45 { 0.0f, 0.6f, 0.05f }, // green
46 { 0.85f, 0.85f, 0.0f }, // yellow
47 { 1.0f, 0.4f, 0.0f }, // orange
48 { 1.0f, 0.0f, 0.0f }, // wild
49 { 0.4f, 0.4f, 0.4f }, // gray
50 { 0.05f, 0.05f, 0.05f }, // black
51 { 0.95f, 0.95f, 0.95f }, // white
52 { 2.0f * 0.73f, 2.0f * 0.0f, 2.0f * 0.73f }, // special purple
53 { 2.0f * 0.2f, 2.0f * 0.2f, 2.0f * 0.8f }, // special blue
54 { 2.0f * 0.0f, 2.0f * 0.6f, 2.0f * 0.05f }, // special green
55 { 2.0f * 0.85f, 2.0f * 0.85f, 2.0f * 0.0f }, // special yellow
56 { 2.0f * 1.0f, 2.0f * 0.4f, 2.0f * 0.0f } }; // special orange
58 const GLfloat creep_colors[BF_NUMBER][3]
59 = { { 0.25f * 0.73f, 0.25f * 0.0f, 0.25f * 0.73f }, // purple
60 { 0.25f * 0.2f, 0.25f * 0.2f, 0.25f * 0.8f }, // blue
61 { 0.25f * 0.0f, 0.25f * 0.6f, 0.25f * 0.05f }, // green
62 { 0.25f * 0.85f, 0.25f * 0.85f, 0.25f * 0.0f }, // yellow
63 { 0.25f * 1.0f, 0.25f * 0.4f, 0.25f * 0.0f }, // orange
64 { 0.25f * 1.0f, 0.25f * 0.0f, 0.25f * 0.0f }, // wild
65 { 0.25f * 0.4f, 0.25f * 0.4f, 0.25f * 0.4f }, // gray
66 { 0.25f * 0.05f, 0.25f * 0.05f, 0.25f * 0.05f }, // black
67 { 0.25f * 0.95f, 0.25f * 0.95f, 0.25f * 0.95f }, // white
68 { 0.5f * 0.73f, 0.5f * 0.0f, 0.5f * 0.73f }, // special purple
69 { 0.5f * 0.2f, 0.5f * 0.2f, 0.5f * 0.8f }, // special blue
70 { 0.5f * 0.0f, 0.5f * 0.6f, 0.5f * 0.05f }, // special green
71 { 0.5f * 0.85f, 0.5f * 0.85f, 0.5f * 0.0f }, // special yellow
72 { 0.5f * 1.0f, 0.5f * 0.4f, 0.5f * 0.0f } }; // special orange
74 void Displayer::setWildBlockColor ( Block &block, float flash )
76 Wild &wild = X::wild(block);
78 if (wild.alarm < GC_WILD_POLYMORPH_PERIOD) {
79 GLfloat fade = wild.alarm * (1.0f / (GLfloat) GC_WILD_POLYMORPH_PERIOD);
81 int next_color;
82 if (wild.flavor != BF_NORMAL_1)
83 next_color = wild.flavor - 1;
84 else
85 next_color = BF_WILD;
87 GLfloat color[3];
88 if (block.y != 0) {
89 color[0] = fade * block_colors[wild.flavor][0]
90 + (1.0f - fade) * block_colors[next_color][0];
91 color[1] = fade * block_colors[wild.flavor][1]
92 + (1.0f - fade) * block_colors[next_color][1];
93 color[2] = fade * block_colors[wild.flavor][2]
94 + (1.0f - fade) * block_colors[next_color][2];
95 } else {
96 color[0] = fade * creep_colors[wild.flavor][0]
97 + (1.0f - fade) * creep_colors[next_color][0];
98 color[1] = fade * creep_colors[wild.flavor][1]
99 + (1.0f - fade) * creep_colors[next_color][1];
100 color[2] = fade * creep_colors[wild.flavor][2]
101 + (1.0f - fade) * creep_colors[next_color][2];
104 glColor3f(color[0] + flash * (DC_FLASH_COLOR_RED - color[0]),
105 color[1] + flash * (DC_FLASH_COLOR_GREEN - color[1]),
106 color[2] + flash * (DC_FLASH_COLOR_BLUE - color[2]));
108 } else
109 if (block.y != 0)
110 glColor3f(block_colors[wild.flavor][0]
111 + flash * (DC_FLASH_COLOR_RED - block_colors[wild.flavor][0]),
112 block_colors[wild.flavor][1]
113 + flash * (DC_FLASH_COLOR_GREEN - block_colors[wild.flavor][1]),
114 block_colors[wild.flavor][2]
115 + flash * (DC_FLASH_COLOR_BLUE - block_colors[wild.flavor][2]));
116 else
117 glColor3f(creep_colors[wild.flavor][0]
118 + flash * (DC_FLASH_COLOR_RED - creep_colors[wild.flavor][0]),
119 creep_colors[wild.flavor][1]
120 + flash * (DC_FLASH_COLOR_GREEN - creep_colors[wild.flavor][1]),
121 creep_colors[wild.flavor][2]
122 + flash * (DC_FLASH_COLOR_BLUE - creep_colors[wild.flavor][2]));
125 void Displayer::drawBlocks ( )
127 glColorMaterial(GL_FRONT, GL_DIFFUSE);
129 // GL_FRONT_AND_BACK because the garbage uses these material properties
130 // also and I don't think it hurts with cull face on.
131 GLfloat color[4];
132 color[0] = 0.0f; color[1] = 0.0f; color[2] = 0.0f; color[3] = 1.0f;
133 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
134 color[0] = 0.5f; color[1] = 0.5f; color[2] = 0.5f; color[3] = 1.0f;
135 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
136 color[0] = 0.0f; color[1] = 0.0f; color[2] = 0.0f; color[3] = 1.0f;
137 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
138 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0f);
140 // First we'll draw the non-special color blocks.
142 int c = BlockManager::block_count;
143 for (int n = 0; c; n++)
144 if (BlockManager::storeMap[n]) {
145 Block &block = BlockManager::blockStore[n];
146 c--;
148 if (BlockManager::isSpecialColorFlavor(block.flavor)) continue;
150 if (block.y > GC_SAFE_HEIGHT) continue;
152 drawBlock(block, false);
155 if (!X::specialColorActive()) return;
157 // Then we set things up for special color blocks and draw them.
159 if (opengl_version_1_2)
160 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
162 glEnable(GL_TEXTURE_1D);
164 glBindTexture(GL_TEXTURE_1D, special_block_lightmap);
166 c = X::special_color_count;
167 for (int n = 0; c; n++)
168 if (BlockManager::storeMap[n]) {
169 Block &block = BlockManager::blockStore[n];
171 if (!BlockManager::isSpecialColorFlavor(block.flavor)) continue;
172 c--;
174 glMatrixMode(GL_TEXTURE);
175 glPushMatrix();
177 glTranslatef(((Game::time_step + block.X) & (DC_GLEAM_PERIOD - 1))
178 * (1.0f / (GLfloat) DC_GLEAM_PERIOD), 0.0f, 0.0f);
179 glRotatef(DC_GLEAM_ROTATION_RATE * (Game::time_step + block.X),
180 DC_GLEAM_ROTATION_AXIS_X, DC_GLEAM_ROTATION_AXIS_Y,
181 DC_GLEAM_ROTATION_AXIS_Z);
182 glMatrixMode(GL_MODELVIEW);
184 drawBlock(block, true);
186 glMatrixMode(GL_TEXTURE);
187 glPopMatrix();
188 glMatrixMode(GL_MODELVIEW);
191 glDisable(GL_TEXTURE_1D);
193 if (opengl_version_1_2)
194 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
197 inline void Displayer::drawBlock ( Block &block, bool special )
199 GLfloat x, y;
200 GLfloat d_x;
202 glPushMatrix();
204 y = block.y * DC_GRID_ELEMENT_LENGTH
205 + block.f_y * (DC_GRID_ELEMENT_LENGTH / (GLfloat) GC_STEPS_PER_GRID)
206 + play_offset_y;
208 // normal
209 if (block.state & (BS_STATIC | BS_FALLING)) {
210 x = block.x * DC_GRID_ELEMENT_LENGTH + DC_PLAY_OFFSET_X;
212 if (block.flavor != BF_WILD)
213 if (block.y != 0)
214 glColor3fv(block_colors[block.flavor]);
215 else
216 glColor3fv(creep_colors[block.flavor]);
217 else
218 setWildBlockColor(block, 0.0f);
220 LightManager::setupBlockLights(x, y);
221 glTranslatef(x, y, DC_PLAY_OFFSET_Z);
223 if (!special)
224 glCallList(block_list);
225 else
226 glCallList(special_block_list);
228 // awaking and popping
229 } else if (block.state & BS_AWAKING) {
230 x = block.x * DC_GRID_ELEMENT_LENGTH + DC_PLAY_OFFSET_X;
232 LightManager::setupBlockLights(x, y);
233 glTranslatef(x, y, DC_PLAY_OFFSET_Z);
235 // if we haven't popped yet
236 if (block.pop_alarm != 0) {
238 // if we're just about to pop
239 if (block.pop_alarm - Game::time_step < DC_POP_ROTATE_TIME) {
240 GLfloat p = (block.pop_alarm - Game::time_step)
241 * (1.0f / (GLfloat) DC_POP_ROTATE_TIME);
243 // each block rotates a different direction
244 if (block.pop_direction & BR_DIRECTION_1) {
245 glRotatef(45.0f * p, 1.0f, 0.0f, 0.0f);
246 glRotatef(45.0f * p, 0.0f, 1.0f, 0.0f);
247 } else if (block.pop_direction & BR_DIRECTION_2) {
248 glRotatef(90.0f - 45.0f * p, 1.0f, 0.0f, 0.0f);
249 glRotatef(45.0f * p, 0.0f, 1.0f, 0.0f);
250 } else if (block.pop_direction & BR_DIRECTION_3) {
251 glRotatef(90.0f - 45.0f * p, 1.0f, 0.0f, 0.0f);
252 glRotatef(-45.0f * p, 0.0f, 1.0f, 0.0f);
253 } else {
254 glRotatef(45.0f * p, 1.0f, 0.0f, 0.0f);
255 glRotatef(-45.0f * p, 0.0f, 1.0f, 0.0f);
258 GLfloat scale = 1.0f - 0.5f * p;
259 glScalef(scale, scale, scale);
261 glColor3f( (1.0f - p) * block_colors[block.flavor][0]
262 + p * garbage_colors[block.pop_color][0],
263 (1.0f - p) * block_colors[block.flavor][1]
264 + p * garbage_colors[block.pop_color][1],
265 (1.0f - p) * block_colors[block.flavor][2]
266 + p * garbage_colors[block.pop_color][2]);
268 glEnable(rescale_method);
269 glCallList(block_list);
270 glDisable(rescale_method);
272 // if we're not popping
273 } else {
275 glColor3fv(garbage_colors[block.pop_color]);
277 // the small block is pre-rotated
278 glCallList(small_block_list);
281 // if we've already popped
282 } else {
283 glColor3fv(block_colors[block.flavor]);
285 glCallList(block_list);
288 // swapping
289 } else if (block.state & BS_SWAPPING) {
290 x = block.x * DC_GRID_ELEMENT_LENGTH + DC_PLAY_OFFSET_X;
292 if (block.state & BS_SWAP_DIRECTION_MASK) {
293 LightManager::setupBlockLights(x + Swapper::swap_factor
294 * DC_GRID_ELEMENT_LENGTH, y);
295 d_x = -DC_GRID_ELEMENT_LENGTH / 2.0f;
297 } else {
298 LightManager::setupBlockLights(x - Swapper::swap_factor
299 * DC_GRID_ELEMENT_LENGTH, y);
300 d_x = DC_GRID_ELEMENT_LENGTH / 2.0f;
303 if (block.flavor != BF_WILD)
304 glColor3fv(block_colors[block.flavor]);
305 else
306 setWildBlockColor(block, 0.0f);
308 glTranslatef(x - d_x, y, DC_PLAY_OFFSET_Z);
309 glRotatef(-180.0f * (1.0f - Swapper::swap_factor), 0.0f, 1.0f, 0.0f);
310 glTranslatef(d_x, 0.0f, 0.0f);
312 if (!special)
313 glCallList(block_list);
314 else
315 glCallList(special_block_list);
317 // dying
318 } else if (block.state & BS_DYING) {
319 x = block.x * DC_GRID_ELEMENT_LENGTH + DC_PLAY_OFFSET_X;
321 // when dying, first we flash
322 if (GC_DYING_DELAY - block.alarm < DC_DYING_FLASH_TIME) {
324 GLfloat flash = ((GLfloat) GC_DYING_DELAY - block.alarm)
325 * (4.0f / (GLfloat) DC_DYING_FLASH_TIME);
326 if (flash > 2.0f) flash = 4.0f - flash;
327 if (flash > 1.0f) flash = 2.0f - flash;
329 if (block.flavor != BF_WILD)
330 glColor3f(block_colors[block.flavor][0]
331 + flash * (DC_FLASH_COLOR_RED - block_colors[block.flavor][0]),
332 block_colors[block.flavor][1]
333 + flash * (DC_FLASH_COLOR_GREEN - block_colors[block.flavor][1]),
334 block_colors[block.flavor][2]
335 + flash * (DC_FLASH_COLOR_BLUE - block_colors[block.flavor][2]));
336 else
337 setWildBlockColor(block, flash);
339 LightManager::setupBlockLights(x, y);
340 glTranslatef(x, y, DC_PLAY_OFFSET_Z);
341 if (!special)
342 glCallList(block_list);
343 else
344 glCallList(special_block_list);
346 // then we shrink and spin
347 } else {
348 if (block.flavor != BF_WILD)
349 glColor3fv(block_colors[block.flavor]);
350 else
351 setWildBlockColor(block, 0.0f);
353 LightManager::setupBlockLights(x, y);
354 glTranslatef(x, y, DC_PLAY_OFFSET_Z);
355 glRotatef((GC_DYING_DELAY - DC_DYING_FLASH_TIME - block.alarm)
356 * (GC_DYING_DELAY - DC_DYING_FLASH_TIME - block.alarm)
357 * DC_DYING_ROTATE_SPEED, block.axis_x, block.axis_y, 0.0f);
359 GLfloat scale = block.alarm * DC_DYING_SHRINK_SPEED
360 + DC_DYING_SHRINK_MIN_SIZE;
361 glScalef(scale, scale, scale);
363 glEnable(rescale_method);
364 if (!special)
365 glCallList(block_list);
366 else
367 glCallList(special_block_list);
368 glDisable(rescale_method);
372 glPopMatrix();