3 * Daniel Nelson - 9/13/0
5 * Copyright (C) 2000 Daniel Nelson
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
25 * Handles turning on and off the required lights during drawing.
34 #include "Displayer.h"
35 #include "LightManager.h"
36 #include "SparkleManager.h"
37 #include "CountDownManager.h"
38 #include "CelebrationManager.h"
40 // assumed to all be 1.0f in setupHeadLight()
41 const GLfloat headlight_normal_color
[]
42 = { 1.0f
, 1.0f
, 1.0f
, 1.0f
};
44 GLfloat headlight_color
[]
45 = { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
47 Light
LightManager::lights
[DC_NUMBER_EXTRA_LIGHTS
];
48 int LightManager::last_associated
;
49 bool LightManager::headlight_normal
;
51 void LightManager::initialize ( )
53 if (!(MetaState::mode
& CM_REALLY_LOW_GRAPHICS
)) {
54 for (int n
= DC_NUMBER_EXTRA_LIGHTS
; n
--; ) {
55 lights
[n
].location
[2] = DC_PLAY_OFFSET_Z
+ DC_MOTE_LIGHT_OFFSET_Z
;
56 lights
[n
].location
[3] = 1.0f
;
57 lights
[n
].enabled
= false;
60 headlight_normal
= false;
64 void LightManager::setupHeadLightPlay_inline_split_ ( )
66 if (!(MetaState::mode
& CM_REALLY_LOW_GRAPHICS
)) {
68 GLfloat headlight_level
;
70 headlight_normal
= false;
72 if (!(Game::state
& GS_SYNC_WAIT
))
73 headlight_level
= 1.0f
;
75 headlight_level
= DC_SYNC_WAIT_LIGHT_LEVEL
;
77 if (CountDownManager::state
!= -1) {
78 float fade
= (GC_START_PAUSE_DELAY
- CountDownManager::start_pause_alarm
)
79 * (1.0f
/ (float) GC_START_PAUSE_DELAY
);
80 headlight_level
*= Game::sqrt(fade
);
83 headlight_color
[0] = headlight_color
[1] = headlight_color
[2] =
87 X::modifyHeadlightColor(headlight_color
);
89 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, headlight_color
);
90 glLightfv(GL_LIGHT0
, GL_SPECULAR
, headlight_color
);
94 void LightManager::setupHeadLightMeta ( )
96 * Setup the headlight for the game end celebration fade out.
99 if (!(MetaState::mode
& CM_REALLY_LOW_GRAPHICS
)) {
100 headlight_color
[0] = headlight_color
[1] = headlight_color
[2]
101 = CelebrationManager::light_level
;
103 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, headlight_color
);
104 glLightfv(GL_LIGHT0
, GL_SPECULAR
, headlight_color
);
105 headlight_normal
= false;
109 void LightManager::resetHeadLight_inline_split_ ( )
111 * If the headlight is not normal, reset it to standard values.
114 if (!(MetaState::mode
& CM_REALLY_LOW_GRAPHICS
)) {
115 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, headlight_normal_color
);
116 glLightfv(GL_LIGHT0
, GL_SPECULAR
, headlight_normal_color
);
117 headlight_normal
= true;
121 void LightManager::setupBlockLights ( float x
, float y
)
123 * Here's an out line of the algorithm. We loop through the motes. If a
124 * mote is in range, we check to see if it has an associated light. If so,
125 * we make sure the light is enabled. If not, we find a light to associate
126 * with the mote. If the mote is out of range, we disable the light but we
127 * don't disassociate. This way, if we have space, we can use a minimum of
128 * light color and position reasignments.
130 * To find a light to associate with a mote, first we look for an unassociated
131 * light. If there are none, we look for a disabled light. If there are none,
132 * we disable a currently enabled light and use it.
135 if (!(MetaState::mode
& CM_REALLY_LOW_GRAPHICS
)) {
136 for (int n
= DC_NUMBER_EXTRA_LIGHTS
; n
--; )
137 lights
[n
].to_be_enabled
= false;
139 int c
= SparkleManager::mote_count
;
140 for (int m
= 0; c
; m
++)
141 if (SparkleManager::motes
[m
].active
) {
142 Mote
&mote
= SparkleManager::motes
[m
];
145 float r
= (x
- mote
.x
) * (x
- mote
.x
) + (y
- mote
.y
) * (y
- mote
.y
);
147 // if the mote's light needs to be on for this draw; note that range is
148 // not the light range but the range to the center of block any block
150 if (r
< DC_MOTE_LIGHT_RANGE
* DC_MOTE_LIGHT_RANGE
) {
152 // if this mote has been previously associated with a light and that
153 // light's association still matches
154 if (mote
.associated_light
!= -1
155 && lights
[mote
.associated_light
].association
== m
) {
157 // make sure the light is enabled
158 lights
[mote
.associated_light
].to_be_enabled
= true;
159 configureBlockBrightness(lights
[mote
.associated_light
],
160 mote
.associated_light
, mote
, r
);
162 // if this mote has no active association; we must try to find an
163 // unassociated light
166 // find an unassociated light
168 int l
= last_associated
;
170 if (++last_associated
== DC_NUMBER_EXTRA_LIGHTS
)
173 if (lights
[last_associated
].association
== -1) {
177 } while (last_associated
!= l
);
183 associateLight(lights
[a
], a
, mote
, m
);
186 lights
[a
].to_be_enabled
= true;
187 configureBlockBrightness(lights
[a
], a
, mote
, r
);
189 // if we couldn't find an unassociated light
192 // find an associated but disabled light
195 if (++last_associated
== DC_NUMBER_EXTRA_LIGHTS
)
198 if (!lights
[last_associated
].to_be_enabled
) {
202 } while (last_associated
!= l
);
208 associateLight(lights
[a
], a
, mote
, m
);
211 lights
[a
].to_be_enabled
= true;
212 configureBlockBrightness(lights
[a
], a
, mote
, r
);
214 // if we couldn't find a disabled light
217 // we have no choice but to steal an enabled one or give up
224 for (int n
= DC_NUMBER_EXTRA_LIGHTS
; n
--; )
225 if (lights
[n
].to_be_enabled
) {
226 if (!lights
[n
].enabled
) {
227 glEnable((GLenum
) (DC_EXTRA_LIGHT_BASE
+ n
));
228 lights
[n
].enabled
= true;
231 if (lights
[n
].enabled
) {
232 glDisable((GLenum
) (DC_EXTRA_LIGHT_BASE
+ n
));
233 lights
[n
].enabled
= false;
239 void LightManager::setupGarbageLights ( float x
, float y
, int h
, int w
)
241 * We only hand dim the light for motes not in the garbage. Plus, we have to
242 * use an attenuated light source.
245 if (!(MetaState::mode
& CM_REALLY_LOW_GRAPHICS
)) {
247 for (int n
= DC_NUMBER_EXTRA_LIGHTS
; n
--; )
248 lights
[n
].to_be_enabled
= false;
250 int c
= SparkleManager::mote_count
;
251 for (int m
= 0; c
; m
++)
252 if (SparkleManager::motes
[m
].active
) {
253 Mote
&mote
= SparkleManager::motes
[m
];
256 float x_high
= x
+ (w
- 1) * DC_GRID_ELEMENT_LENGTH
;
257 float y_high
= y
+ (h
- 1) * DC_GRID_ELEMENT_LENGTH
;
262 r
+= (x
- mote
.x
) * (x
- mote
.x
);
263 else if (mote
.x
> x_high
)
264 r
+= (x_high
- mote
.x
) * (x_high
- mote
.x
);
266 r
+= (y
- mote
.y
) * (y
- mote
.y
);
267 else if (mote
.y
> y_high
)
268 r
+= (y_high
- mote
.y
) * (y_high
- mote
.y
);
270 // if the mote's light needs to be on for this draw
271 if (r
< DC_MOTE_LIGHT_RANGE
* DC_MOTE_LIGHT_RANGE
) {
273 // if this mote has been previously associated with a light and that
274 // light's association still matches
275 if (mote
.associated_light
!= -1
276 && lights
[mote
.associated_light
].association
== m
) {
278 // make sure the light is enabled
279 lights
[mote
.associated_light
].to_be_enabled
= true;
280 configureGarbageBrightness(lights
[mote
.associated_light
],
281 mote
.associated_light
, mote
, r
);
283 // if this mote has no active association; we must try to find an
284 // unassociated light
287 // find an unassociated light
289 int l
= last_associated
;
291 if (++last_associated
== DC_NUMBER_EXTRA_LIGHTS
)
294 if (lights
[last_associated
].association
== -1) {
298 } while (last_associated
!= l
);
304 associateLight(lights
[a
], a
, mote
, m
);
307 lights
[a
].to_be_enabled
= true;
308 configureGarbageBrightness(lights
[a
], a
, mote
, r
);
310 // if we couldn't find an unassociated light
313 // find an associated but disabled light
316 if (++last_associated
== DC_NUMBER_EXTRA_LIGHTS
)
319 if (!lights
[last_associated
].to_be_enabled
) {
323 } while (last_associated
!= l
);
329 associateLight(lights
[a
], a
, mote
, m
);
332 lights
[a
].to_be_enabled
= true;
333 configureGarbageBrightness(lights
[a
], a
, mote
, r
);
335 // if we couldn't find a disabled light
338 // we have no choice but to steal an enabled one or give up
345 for (int n
= DC_NUMBER_EXTRA_LIGHTS
; n
--; )
346 if (lights
[n
].to_be_enabled
) {
347 if (!lights
[n
].enabled
) {
348 glEnable((GLenum
) (DC_EXTRA_LIGHT_BASE
+ n
));
349 if (!lights
[n
].attenuated
)
350 attenuateLight(lights
[n
], n
);
351 lights
[n
].enabled
= true;
354 if (lights
[n
].enabled
) {
355 glDisable((GLenum
) (DC_EXTRA_LIGHT_BASE
+ n
));
356 lights
[n
].enabled
= false;