1 /*
2      PLIB - A Suite of Portable Game Libraries
3      Copyright (C) 1998,2002  Steve Baker
4 
5      This library is free software; you can redistribute it and/or
6      modify it under the terms of the GNU Library General Public
7      License as published by the Free Software Foundation; either
8      version 2 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      Library General Public License for more details.
14 
15      You should have received a copy of the GNU Library General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 
19      For further information visit http://plib.sourceforge.net
20 
21      $Id: ssgStateSelector.cxx 1950 2004-09-13 11:28:34Z wolfram_kuss $
22 */
23 
24 
25 #include "ssgLocal.h"
26 
copy_from(ssgStateSelector * src,int clone_flags)27 void ssgStateSelector::copy_from ( ssgStateSelector *src, int clone_flags )
28 {
29   ssgSimpleState::copy_from ( src, clone_flags ) ;
30 
31   nstates   = src -> getNumSteps   () ;
32   selection = src -> getSelectStep () ;
33   statelist = new ssgSimpleState * [ nstates ] ;
34 
35   for ( int i = 0 ; i < nstates ; i++ )
36   {
37     ssgSimpleState *s = src -> getStep ( i ) ;
38 
39     if ( s != NULL && (clone_flags & SSG_CLONE_STATE_RECURSIVE) )
40       statelist [ i ] = (ssgSimpleState *)( s -> clone ( clone_flags )) ;
41     else
42       statelist [ i ] = s ;
43 
44   //~~ T.G. needs ref count incremented
45   if (statelist [ i ] != NULL )
46      statelist [ i ] -> ref();
47 
48   }
49 }
50 
51 
clone(int clone_flags)52 ssgBase *ssgStateSelector::clone ( int clone_flags )
53 {
54   ssgStateSelector *b = new ssgStateSelector ;
55   b -> copy_from ( this, clone_flags ) ;
56   return b ;
57 }
58 
59 
ssgStateSelector()60 ssgStateSelector::ssgStateSelector ()
61 {
62   type = ssgTypeStateSelector () ;
63   nstates = 0 ;
64   selection = -1 ;
65   statelist = NULL ;
66 }
67 
ssgStateSelector(int ns)68 ssgStateSelector::ssgStateSelector ( int ns )
69 {
70   type = ssgTypeStateSelector () ;
71   nstates   = ns ;
72   selection = -1 ;
73   statelist = new ssgSimpleState * [ nstates ] ;
74 
75   for ( int i = 0 ; i < nstates ; i++ )
76     statelist [ i ] = NULL ;
77 }
78 
~ssgStateSelector(void)79 ssgStateSelector::~ssgStateSelector (void)
80 {
81   //~~ T.G. deref states before deleting list
82   for ( int i = 0 ; i < nstates ; i++ )
83     ssgDeRefDelete( statelist [ i ] );
84   delete [] statelist ;
85 }
86 
selectStep(unsigned int s)87 void ssgStateSelector::selectStep ( unsigned int s )
88 {
89   selection = s ;
90 }
91 
getSelectStep(void)92 unsigned int ssgStateSelector::getSelectStep (void)
93 {
94   return selection ;
95 }
96 
getCurrentStep(void)97 ssgSimpleState *ssgStateSelector::getCurrentStep  (void)
98 {
99   return ( selection < 0 ||
100            selection >= nstates ||
101            statelist [ selection ] == NULL ) ? this : statelist[selection] ;
102 }
103 
getStep(int i)104 ssgSimpleState *ssgStateSelector::getStep ( int i )
105 {
106   return ( i < 0 ||
107            i >= nstates ||
108            statelist [ i ] == NULL ) ? this : statelist[i] ;
109 }
110 
111 
112 
113 
setStep(int i,ssgSimpleState * step)114 void ssgStateSelector::setStep  (int i, ssgSimpleState *step)
115 {
116   if ( i < 0 || i >= nstates ) return ;
117 
118   //~~ T.G. removed null test -- not necessary
119   ssgDeRefDelete ( statelist[i] ) ;
120 
121   statelist [ i ] = step ;
122 
123   if ( step != NULL )
124     step -> ref () ;
125 }
126 
127 
isTranslucent(void)128 int ssgStateSelector::isTranslucent (void)
129 {
130   return getCurrentStep()->isTranslucent() ;
131 }
132 
133 
setTranslucent(void)134 void ssgStateSelector::setTranslucent (void)
135 {
136   getCurrentStep()->setTranslucent() ;
137 }
138 
139 
140 
setOpaque(void)141 void ssgStateSelector::setOpaque (void)
142 {
143   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
144 
145   if ( s == this )
146     ssgSimpleState::setOpaque() ;
147   else
148     s -> setOpaque() ;
149 }
150 
151 
force(void)152 void ssgStateSelector::force (void)
153 {
154   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
155 
156   if ( s == this )
157     ssgSimpleState::force() ;
158   else
159     s -> force() ;
160 }
161 
apply(void)162 void ssgStateSelector::apply (void)
163 {
164   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
165 
166   if ( s == this )
167     ssgSimpleState::apply() ;
168   else
169     s -> apply() ;
170 }
171 
172 
care_about(int mode)173 void ssgStateSelector:: care_about ( int mode )
174 {
175   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
176 
177   if ( s == this )
178     ssgSimpleState::care_about (mode) ;
179   else
180     s -> care_about (mode);
181 }
182 
183 
dont_care_about(int mode)184 void ssgStateSelector::dont_care_about ( int mode )
185 {
186   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
187 
188   if ( s == this )
189     ssgSimpleState::dont_care_about(mode) ;
190   else
191     s -> dont_care_about(mode);
192 }
193 
194 
isEnabled(GLenum mode)195 int  ssgStateSelector::isEnabled ( GLenum mode )
196 {
197   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
198 
199   if ( s == this )
200     return ssgSimpleState::isEnabled(mode) ;
201   else
202     return s -> isEnabled(mode) ;
203 }
204 
disable(GLenum mode)205 void ssgStateSelector::disable ( GLenum mode )
206 {
207   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
208 
209   if ( s == this )
210     ssgSimpleState::disable(mode) ;
211   else
212     s  -> disable(mode) ;
213 }
214 
enable(GLenum mode)215 void ssgStateSelector::enable ( GLenum mode )
216 {
217   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
218 
219   if ( s == this )
220     ssgSimpleState::enable(mode) ;
221   else
222     s -> enable(mode) ;
223 }
224 
225 
setTexture(char * fname,int _wrapu,int _wrapv,int _mipmap)226 void ssgStateSelector::setTexture ( char *fname, int _wrapu, int _wrapv,
227 				    int _mipmap )
228 {
229   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
230 
231   if ( s == this )
232     ssgSimpleState::setTexture ( fname, _wrapu, _wrapv, _mipmap ) ;
233   else
234     s  -> setTexture ( fname, _wrapu, _wrapv, _mipmap ) ;
235 }
236 
getTextureFilename(void)237 char *ssgStateSelector::getTextureFilename (void)
238 {
239   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
240 
241   if ( s == this )
242     return ssgSimpleState::getTextureFilename() ;
243   else
244     return s -> getTextureFilename();
245 }
246 
getTextureHandle(void)247 GLuint ssgStateSelector::getTextureHandle (void)
248 {
249   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
250 
251   if ( s == this )
252     return ssgSimpleState::getTextureHandle() ;
253   else
254     return s -> getTextureHandle() ;
255 }
256 
setTexture(ssgTexture * tex)257 void ssgStateSelector::setTexture ( ssgTexture *tex )
258 {
259   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
260 
261   if ( s == this )
262     ssgSimpleState::setTexture(tex) ;
263   else
264     s -> setTexture(tex) ;
265 }
266 
setTextureFilename(char * fname)267 void ssgStateSelector::setTextureFilename ( char *fname )
268 {
269   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
270 
271   ssgTexture* texture ;
272   if ( s == this )
273     texture = ssgSimpleState::getTexture () ;
274   else
275     texture = s -> getTexture () ;
276 
277   if ( texture != NULL )
278     texture -> setFilename ( fname ) ;
279 }
280 
setTexture(GLuint tex)281 void ssgStateSelector::setTexture ( GLuint tex )
282 {
283   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
284 
285   ssgTexture* texture ;
286   if ( s == this )
287     texture = ssgSimpleState::getTexture () ;
288   else
289     texture = s -> getTexture () ;
290 
291   if ( texture != NULL )
292     texture -> setHandle (tex) ;
293 }
294 
setColourMaterial(GLenum which)295 void ssgStateSelector::setColourMaterial(GLenum which)
296 {
297   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
298 
299   if ( s == this )
300     ssgSimpleState::setColourMaterial(which) ;
301   else
302     s -> setColourMaterial(which) ;
303 }
304 
setMaterial(GLenum which,float r,float g,float b,float a)305 void ssgStateSelector::setMaterial ( GLenum which, float r, float g,
306                                                    float b, float a )
307 {
308   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
309 
310   if ( s == this )
311     ssgSimpleState::setMaterial(which,r,g,b,a) ;
312   else
313     s -> setMaterial(which,r,g,b,a) ;
314 }
315 
316 
setMaterial(GLenum which,sgVec4 rgba)317 void ssgStateSelector::setMaterial ( GLenum which, sgVec4 rgba )
318 {
319   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
320 
321   if ( s == this )
322     ssgSimpleState::setMaterial(which,rgba) ;
323   else
324     s -> setMaterial(which,rgba) ;
325 }
326 
getMaterial(GLenum which)327 float *ssgStateSelector::getMaterial ( GLenum which )
328 {
329   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
330 
331   if ( s == this )
332     return ssgSimpleState::getMaterial(which) ;
333   else
334     return s -> getMaterial(which) ;
335 }
336 
getShininess(void)337 float ssgStateSelector::getShininess (void)
338 {
339   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
340 
341   if ( s == this )
342     return ssgSimpleState::getShininess() ;
343   else
344     return s -> getShininess() ;
345 }
346 
setShininess(float sh)347 void ssgStateSelector::setShininess ( float sh )
348 {
349   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
350 
351   if ( s == this )
352     ssgSimpleState::setShininess(sh) ;
353   else
354     s -> setShininess(sh) ;
355 }
356 
setShadeModel(GLenum model)357 void ssgStateSelector::setShadeModel ( GLenum model )
358 {
359   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
360 
361   if ( s == this )
362     ssgSimpleState::setShadeModel(model) ;
363   else
364     s -> setShadeModel(model) ;
365 }
366 
setAlphaClamp(float clamp)367 void ssgStateSelector::setAlphaClamp ( float clamp )
368 {
369   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
370 
371   if ( s == this )
372     ssgSimpleState::setAlphaClamp(clamp) ;
373   else
374     s -> setAlphaClamp(clamp) ;
375 }
376 
print(FILE * fd,char * indent,int how_much)377 void ssgStateSelector::print ( FILE *fd, char *indent, int how_much )
378 {
379   ssgStateSelector * s = (ssgStateSelector *) getCurrentStep() ;
380 
381   if ( s == this )
382     ssgSimpleState::print(fd, indent, how_much) ;
383   else
384     s -> print(fd, indent, how_much) ;
385 }
386 
387 
load(FILE * fd)388 int ssgStateSelector::load ( FILE *fd )
389 {
390   int i ;
391 
392   _ssgReadInt ( fd, & nstates   ) ;
393   _ssgReadInt ( fd, & selection ) ;
394 
395   if (statelist != NULL)
396   {
397     for ( i = 0 ; i < nstates ; i++ )
398       ssgDeRefDelete( statelist [ i ] );
399     delete [] statelist ;
400   }
401 
402   statelist = new ssgSimpleState * [ nstates ] ;
403   for ( i = 0 ; i < nstates ; i++ )
404   {
405     if ( ! _ssgLoadObject ( fd, (ssgBase **) &statelist[i], ssgTypeState () ) )
406       return FALSE ;
407   }
408 
409   return ssgSimpleState::load(fd) ;
410 }
411 
412 
save(FILE * fd)413 int ssgStateSelector::save ( FILE *fd )
414 {
415 
416   _ssgWriteInt ( fd, nstates   ) ;
417   _ssgWriteInt ( fd, selection ) ;
418   for ( int i = 0 ; i < nstates ; i++ )
419   {
420     if ( ! _ssgSaveObject ( fd, statelist[i] ) )
421       return FALSE ;
422   }
423 
424   return ssgSimpleState::save(fd) ;
425 }
426 
427