Debian Codegraph @

apps/demos/drop/TsRemoval.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 _______________________________________________________________________
39 ______________ S I L I C O N G R A P H I C S I N C . ____________
40 |
41 | $Revision: 1.1.1.1 $
42 |
43 | Classes : TsField::removeRows
44 | TsField::shiftRows
45 | TsField::endShift
46 |
47 | Author : Dave Immel
48 |
49 ______________ S I L I C O N G R A P H I C S I N C . ____________
50 _______________________________________________________________________
51 */
52
53 //--------------------------- Include ----------------------------------
54 #include <cstdio>
55 #include <Inventor/SbLinear.h>
56 #include <Inventor/nodes/SoMaterial.h>
57 #include <Inventor/nodes/SoSeparator.h>
58 #include <Inventor/nodes/SoTranslation.h>
59 #include <Inventor/sensors/SoTimerSensor.h>
60 #include "TsField.h"
61
62 #define FILL_HIGHLIGHT_DURATION 1.0
63 #define REMOVAL_ANIMATE 1
64 #define REMOVAL_DROP_RATE 4.0
65 #define LEVELS_REQUIRED 4
66
67 static int currentRemoval;
68
69 ////////////////////////////////////////////////////////////////////////
70 //
71 // Description:
72 // This callback routine for the removalSensor alarm sensor will be
73 // called after a period of time. The routine removes all completed
74 // rows and shedules a timer sensor used to animate the remaining
75 // rows downward.
76 //
77 // Use: public
78
79 void
80 TsField::removeRows( void *data, SoSensor * )
81
82 //
83 ////////////////////////////////////////////////////////////////////////
84 {
85 TsField *field = (TsField *)data;
86 int i;
87
88 //
89 // For each row being removed, replace the level node with a removal
90 // translation node and restore the original level material.
91 //
92 SoSeparator *levelSep;
93 for (i=0; i<field->numRemovals; i++)
94 {
95 field->otherBlocks->replaceChild(
96 field->levelGroup[field->removalList[i]],
97 field->removalTranslation[i]);
98
99 levelSep = (SoSeparator *)field->levelGroup[field->removalList[i]];
100 levelSep->replaceChild (field->removalMaterial,
101 field->levelMaterial[field->removalList[i]]);
102 }
103
104
105 // Initialize the removal translation nodes
106 SoTranslation *rt;
107 for (i=0; i<3; i++)
108 {
109 rt = (SoTranslation *)field->removalTranslation[i];
110 rt->translation.setValue (0.0, 0.0, 0.0);
111 }
112
113 // Schedule a timer sensor to animate the remaining rows downward
114 currentRemoval = 0;
115 field->currentFraction = 0.0;
116 field->wallClock.setToTimeOfDay();
117 field->shiftSensor->schedule();
118 }
119
120
121 ////////////////////////////////////////////////////////////////////////
122 //
123 // Description:
124 // This callback routine for the shiftSensor alarm sensor animates
125 // the remaining rows downward. When all shifting has been finished,
126 // the timer is unscheduled and a new piece is put into play.
127 //
128 // Use: static, private
129
130 void
131 TsField::shiftRows( void *data, SoSensor * )
132
133 //
134 ////////////////////////////////////////////////////////////////////////
135 {
136 TsField *field = (TsField *)data;
137 SoTranslation *rt;
138 SbTime elapsedTime;
139 float tmpFraction, shiftDistance;
140 time_t clockSeconds;
141 long clockMSeconds; // System long
142
143 //
144 // Calculate the distance the rows should fall. This is calculated
145 // given the change in time and the rate at which the piece is falling.
146 // The distance is expressed as a value from 0.0 to -2.0. This represents
147 // the intermediate distance from one integer position to the next.
148 //
149 elapsedTime.setToTimeOfDay();
150 elapsedTime -= field->wallClock;
151 field->wallClock.setToTimeOfDay();
152 elapsedTime.getValue (clockSeconds, clockMSeconds);
153 shiftDistance = (clockSeconds + clockMSeconds * 1e-6) * REMOVAL_DROP_RATE;
154 tmpFraction = field->currentFraction - shiftDistance;
155
156 if (tmpFraction > -2.0)
157 {
158 //
159 // Just translate down.
160 //
161 field->currentFraction = tmpFraction;
162 }
163 else
164 {
165 while (tmpFraction < -2.0)
166 {
167 //
168 // The rows have crossed an integer boundary.
169 // assign -2 to the current removal translation and increment the
170 // current removal translation. If there are no more rows to be
171 // translated stop the animation and update the database.
172 //
173 rt = (SoTranslation *)field->removalTranslation[currentRemoval];
174 rt->translation.setValue (0.0, -2.0, 0.0);
175 if (++currentRemoval == field->numRemovals)
176 {
177 field->shiftSensor->unschedule();
178 field->endShift();
179 return;
180 }
181
182 // Assign any extra animation difference to the next removal
183 // translation.
184 rt = (SoTranslation *)field->removalTranslation[currentRemoval];
185 rt->translation.setValue (0.0, tmpFraction + 2.0, 0.0);
186
187 tmpFraction += 2.0;
188 field->currentFraction = tmpFraction;
189 }
190 return;
191 }
192
193 // Update the correct removalTranslation node to move the rows downward.
194 rt = (SoTranslation *)field->removalTranslation[currentRemoval];
195 const SbVec3f &tpos = rt->translation.getValue ();
196 SbVec3f spos;
197 spos[0] = tpos[0];
198 spos[1] = tpos[1] - shiftDistance;
199 spos[2] = tpos[2];
200 rt->translation.setValue (spos);
201 }
202
203
204
205
206 ////////////////////////////////////////////////////////////////////////
207 //
208 // Description:
209 // This routine updates the database and fieldTable after completed
210 // rows have been removed.
211 //
212 // Use: private
213
214 void
215 TsField::endShift()
216
217 //
218 ////////////////////////////////////////////////////////////////////////
219 {
220 SoSeparator *lg;
221 int i, j, n;
222
223 //
224 // replace the completed rows back into the scene graph and remove
225 // all of their block children.
226 //
227 for (i=0; i<numRemovals; i++)
228 {
229 lg = (SoSeparator *)levelGroup[removalList[i]];
230 otherBlocks->replaceChild (removalTranslation[i], lg);
231
232 for (j=0; j<(resolution*resolution); j++)
233 lg->removeChild(2);
234 }
235
236 //
237 // For each empty row, loop through the rows above and shift their
238 // children down one row. Shift the fieldTable values down one row
239 // as well.
240 //
241 SoSeparator *next;
242 SoNode *blk;
243 int level, fieldPos;
244 int rr = resolution * resolution;
245
246 for (i=numRemovals; --i>=0;)
247 {
248 lg = (SoSeparator *)levelGroup[removalList[i]];
249 fieldPos = removalList[i] * rr;
250 for (level=removalList[i]+1; level<=currentHeight; level++, lg = next)
251 {
252 // Shift the block children down one row
253 next = (SoSeparator *)levelGroup[level];
254 n = next->getNumChildren();
255 for (j=0; j<(n-2); j++)
256 {
257 blk = next->getChild(2);
258 next->removeChild(blk);
259 lg->addChild(blk);
260 }
261
262 // Shift the fieldTable values down one row
263 for (j=0; j<rr; j++, fieldPos++)
264 fieldTable[fieldPos] = fieldTable[fieldPos+rr];
265 }
266
267 // Clear the top level of the field and reduce the currentHeight
268 for (j=0; j<rr; j++, fieldPos++)
269 fieldTable[fieldPos] = 0;
270 --currentHeight;
271 }
272 numRemovals = 0;
273
274 // Reattach the falling piece to the scene graph
275 fieldRoot->insertChild (fallingRoot, 2);
276
277 // Add a new piece to the playing field.
278 addPoints( pointsEarned );
279 pointsEarned = 0;
280 addPiece ();
281 }
282
283
284
285
286