1 /*
2  * DrawLevelLights.cxx
3  * Daniel Nelson - 10/13/0
4  *
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.
11  *
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.
16  *
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.
20  *
21  * Daniel Nelson - aluminumangel.org
22  * 174 W. 18th Ave.
23  * Columbus, OH  43210
24  *
25  * Draws the cute level lights.
26  */
27 
28 #include <GL/glut.h>
29 
30 #include "glext.h"
31 
32 using namespace std;
33 
34 #include "Game.h"
35 #include "Displayer.h"
36 #include "LevelLights.h"
37 #include "MetaState.h"
38 
39 #define CS_UNDEFINED         (0)
40 #define CS_RED               (1)
41 #define CS_BLUE              (2)
42 
43 const GLfloat ambient[]
44  = { 0.0f, 0.0f, 0.0f, 1.0f };
45 
46 const GLfloat diffuse[]
47  = { 0.0f, 0.0f, 0.0f, 1.0f };
48 
49 const GLfloat specular[]
50  = { 0.8f, 0.8f, 0.8f, 1.0f };
51 
drawLevelLights()52 void Displayer::drawLevelLights (   )
53 {
54   int color_state;
55   int next_color_state;
56   GLfloat death_flash = 0.0f;
57   GLfloat color[3];
58 
59   glColorMaterial(GL_FRONT, GL_EMISSION);
60 
61   glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
62   glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
63   glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
64   glMaterialf(GL_FRONT, GL_SHININESS, 2.0f);
65 
66   for (int set = 2; set--; ) {
67 
68     glPushMatrix();
69 
70       if (set == LL_LOCAL_LIGHTS)
71         glTranslatef(DC_LEVEL_LIGHT_LOCAL_OFFSET_X,
72          DC_PLAY_OFFSET_Y + (DC_GRID_ELEMENT_LENGTH / 2.0f), 0.0f);
73       else {
74         glTranslatef(DC_LEVEL_LIGHT_OPPONENT_OFFSET_X,
75          DC_PLAY_OFFSET_Y + (DC_GRID_ELEMENT_LENGTH / 2.0f), 0.0f);
76         glScalef(-1.0f, 1.0f, 1.0f);
77       }
78 
79       static int hold_set = 0;
80       if ((MetaState::mode & CM_SOLO) && !(MetaState::mode & CM_AI)) {
81         hold_set = set;
82         set = LL_LOCAL_LIGHTS;
83       }
84 
85       color_state = CS_UNDEFINED;
86 
87       if (LevelLights::death_flash_alarm[set] != -1) {
88         death_flash = LevelLights::death_flash_alarm[set]
89          * (2.0f / (GLfloat) DC_LEVEL_LIGHT_DEATH_FLASH_TIME);
90 
91         if (death_flash > 1.0f) death_flash = 2.0f - death_flash;
92       }
93 
94       for (int n = 0; n < LL_NUMBER_LEVEL_LIGHTS; n++) {
95         LevelLight &light = LevelLights::lights[set][n];
96         color[0] = color[1] = color[2] = 0.0f;
97 
98         if (light.state & LS_RED) {
99           color[0] = DC_LEVEL_LIGHT_RED;
100           next_color_state = CS_RED;
101 
102         } else if (light.state & LS_BLUE) {
103           color[2] = DC_LEVEL_LIGHT_BLUE;
104           next_color_state = CS_BLUE;
105 
106         } else {
107           GLfloat fade = light.fade_alarm
108            * (1.0f / (GLfloat) DC_LEVEL_LIGHT_FADE_TIME);
109 
110           if (light.state & LS_FADE_TO_RED) {
111             color[0] = DC_LEVEL_LIGHT_RED * Game::sqrt(1.0f - fade);
112             color[2] = DC_LEVEL_LIGHT_BLUE * Game::sqrt(fade);
113 
114           } else {
115             color[0] = DC_LEVEL_LIGHT_RED * Game::sqrt(fade);
116             color[2] = DC_LEVEL_LIGHT_BLUE * Game::sqrt(1.0f - fade);
117           }
118 
119           next_color_state = CS_UNDEFINED;
120         }
121 
122         if (light.state & LS_IMPACT_FLASH) {
123           GLfloat flash = light.flash_alarm
124            * (1.0f / (GLfloat) DC_LEVEL_LIGHT_IMPACT_FLASH_TIME);
125 
126           if (flash > DC_LEVEL_LIGHT_FLASH_INFLECTION)
127            flash = (1.0f - flash)
128             * (1.0f / (1.0f - DC_LEVEL_LIGHT_FLASH_INFLECTION));
129           else
130            flash *= 1.0f / DC_LEVEL_LIGHT_FLASH_INFLECTION;
131           flash *= flash;
132 
133           color[0] += (1.0f - color[0]) * flash;
134           color[1] = flash;
135           color[2] += (1.0f - color[2]) * flash;
136 
137           next_color_state = CS_UNDEFINED;
138         }
139 
140         if (LevelLights::death_flash_alarm[set] != -1) {
141           color[0] += (1.0f - color[0]) * death_flash;
142           color[1] += (1.0f - color[1]) * death_flash;
143           color[2] += (1.0f - color[2]) * death_flash;
144         }
145 
146         if (next_color_state != color_state || color_state == CS_UNDEFINED) {
147           glColor3fv(color);
148           color_state = next_color_state;
149         }
150 
151         glTranslatef(0.0f, DC_GRID_ELEMENT_LENGTH, 0.0f);
152 
153         glCallList(level_light_list);
154       }
155 
156       if ((MetaState::mode & CM_SOLO) && !(MetaState::mode & CM_AI))
157         set = hold_set;
158 
159     glPopMatrix();
160 
161   }
162 }
163