00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "OSGConfig.h"
00047
00048 #include <OSGGL.h>
00049
00050 #include "OSGStateChunk.h"
00051
00052 #include "OSGState.h"
00053
00054
00055 #if !defined(OSG_DO_DOC) || defined(OSG_DOC_DEV)
00056
00057 OSG_BEGIN_NAMESPACE
00058
00064 struct ClearSlot : public std::unary_function< StateChunkPtr &,
00065 const NullFieldContainerPtr &>
00066 {
00067 const NullFieldContainerPtr &operator() (StateChunkPtr &slotPtr)
00068 {
00069 subRefCP(slotPtr);
00070
00071 return NullFC;
00072 }
00073 };
00074
00075 OSG_END_NAMESPACE
00076
00077 #endif
00078
00079 OSG_USING_NAMESPACE
00080
00081
00082
00083
00084
00085
00086
00094
00095
00096
00097
00098 char State::cvsid[] = "@(#)$Id: $";
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 void State::initMethod (void)
00109 {
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 State::State(void) :
00124 Inherited()
00125 {
00126 }
00127
00128 State::State(const State &source) :
00129 Inherited(source)
00130 {
00131 }
00132
00133 State::~State(void)
00134 {
00135 clearChunks();
00136 }
00137
00138 #if defined(OSG_FIXED_MFIELDSYNC)
00139 void State::onDestroyAspect(UInt32 uiId, UInt32 uiAspect)
00140 {
00141 Inherited::onDestroyAspect(uiId, uiAspect);
00142
00143 clearChunks();
00144 }
00145 #endif
00146
00147 void State::changed(BitVector whichField, UInt32 origin)
00148 {
00149 Inherited::changed(whichField, origin);
00150 }
00151
00152
00153
00154 void State::dump( UInt32 OSG_CHECK_ARG(uiIndent),
00155 const BitVector OSG_CHECK_ARG(bvFlags)) const
00156 {
00157 std::cerr << "State at " << this << std::endl;
00158
00159 MFStateChunkPtr::const_iterator it;
00160 UInt32 cind;
00161
00162 for(it = _mfChunks.begin(), cind = 0; it != _mfChunks.end(); it++, cind++)
00163 {
00164 std::cerr << StateChunkClass::getName(cind) << "\t";
00165 if(*it == NullFC)
00166 std::cerr << "NullChunk" << std::endl;
00167 else
00168 std::cerr << *it << std::endl;
00169 }
00170 }
00171
00172
00173
00178 void State::activate(DrawActionBase *action)
00179 {
00180 MFStateChunkPtr::iterator it;
00181 Int32 ind = 0;
00182 UInt32 cind;
00183
00184 for(it = _mfChunks.begin(), cind = 0; it != _mfChunks.end();
00185 ++it, ++cind)
00186 {
00187 if(*it != NullFC && !(*it)->getIgnore())
00188 {
00189 (*it)->activate(action, UInt32(ind));
00190 }
00191 if(++ind >= StateChunkClass::getNumSlots(cind))
00192 ind = 0;
00193 }
00194 }
00195
00196
00201 void State::changeFrom(DrawActionBase *action, State *old)
00202 {
00203 MFStateChunkPtr::iterator it;
00204 Int32 ind = 0;
00205 UInt32 i;
00206 UInt32 cind;
00207
00208 for(it = _mfChunks.begin(), cind = 0; it != _mfChunks.end();
00209 ++it, ++cind)
00210 {
00211 StateChunkPtr o = old->getChunk(cind);
00212 StateChunkPtr n = *it;
00213
00214 if(n != NullFC && !n->getIgnore())
00215 {
00216 if(o != NullFC && !o->getIgnore())
00217 n->changeFrom(action, o.getCPtr(), UInt32(ind));
00218 else
00219 n->activate(action, UInt32(ind));
00220 }
00221 else if(o != NullFC && !o->getIgnore())
00222 {
00223 o->deactivate(action, UInt32(ind));
00224 }
00225
00226 if(++ind >= StateChunkClass::getNumSlots(cind))
00227 ind = 0;
00228 }
00229
00230 if(ind >= StateChunkClass::getNumSlots(cind))
00231 ind = 0;
00232
00233 for(i = cind; i < old->getChunks().size(); ++i)
00234 {
00235 StateChunkPtr o = old->getChunk(i);
00236
00237 if(o != NullFC && !o->getIgnore())
00238 {
00239 o->deactivate(action, UInt32(ind));
00240 }
00241
00242 if(++ind >= StateChunkClass::getNumSlots(i))
00243 {
00244 ind = 0;
00245 }
00246 }
00247 }
00248
00249
00254 void State::deactivate(DrawActionBase *action)
00255 {
00256 MFStateChunkPtr::iterator it;
00257 Int32 ind = 0;
00258 UInt32 cind;
00259
00260 for(it = _mfChunks.begin(), cind = 0; it != _mfChunks.end();
00261 ++it, ++cind)
00262 {
00263 if(*it != NullFC && !(*it)->getIgnore())
00264 (*it)->deactivate(action, UInt32(ind));
00265 if(++ind >= StateChunkClass::getNumSlots(cind))
00266 ind = 0;
00267 }
00268 }
00269
00270
00271
00272
00292 bool State::addChunk(StateChunkPtr chunk, Int32 index)
00293 {
00294 if(index > 0 && index > chunk->getClass()->getNumSlots())
00295 {
00296 SWARNING << "addChunk: index "
00297 << index
00298 << " > Numslots "
00299 << chunk->getClass()->getNumSlots()
00300 << ", ignored!"
00301 << std::endl;
00302 return true;
00303 }
00304
00305 UInt32 cindex = chunk->getClassId();
00306 UInt32 csize = _mfChunks.size();
00307
00308
00309 if(index == AutoSlot || index == AutoSlotReplace)
00310 {
00311 UInt8 nslots = chunk->getClass()->getNumSlots();
00312 UInt8 ci;
00313
00314 for(ci = cindex; ci < cindex + nslots && ci < csize; ++ci)
00315 {
00316 if(_mfChunks[ci] == NullFC)
00317 {
00318 break;
00319 }
00320 }
00321
00322 if(ci >= cindex + nslots)
00323 {
00324 if(index == AutoSlot)
00325 {
00326 SWARNING << "addChunk: no free slot found for "
00327 << chunk->getClass()->getName()
00328 << " class, ignored!" << std::endl;
00329 return true;
00330 }
00331
00332 --ci;
00333 }
00334
00335 cindex = ci;
00336 }
00337 else
00338 {
00339 cindex += index;
00340 }
00341
00342
00343 if(cindex >= csize)
00344 {
00345 UInt32 oldsize = csize;
00346 UInt32 newsize = cindex + 1;
00347
00348 _mfChunks.resize(newsize);
00349
00350 for(UInt32 i = oldsize; i < newsize; i++)
00351 {
00352 _mfChunks[i] = NullFC;
00353 }
00354 }
00355
00356 setRefdCP(_mfChunks[cindex], chunk);
00357
00358 return false;
00359 }
00360
00365 bool State::subChunk(StateChunkPtr chunk)
00366 {
00367 if(chunk == NullFC)
00368 return true;
00369
00370 UInt32 cindex = chunk->getClassId();
00371 UInt32 csize = _mfChunks.size();
00372
00373
00374 UInt8 nslots = chunk->getClass()->getNumSlots();
00375 UInt8 ci;
00376
00377 for(ci = cindex; ci < cindex + nslots && ci < csize; ci++)
00378 {
00379 if(_mfChunks[ci] == chunk)
00380 {
00381 break;
00382 }
00383 }
00384
00385 if(ci >= cindex + nslots)
00386 {
00387 SWARNING << "subChunk: chunk "
00388 << chunk
00389 << " of class "
00390 << chunk->getClass()->getName()
00391 << " not found!"
00392 << std::endl;
00393 return true;
00394 }
00395
00396
00397
00398 subRefCP(_mfChunks[ci]);
00399
00400 _mfChunks[ci] = NullFC;
00401
00402 return false;
00403 }
00404
00405
00410 bool State::subChunk(UInt32 classid, Int32 index)
00411 {
00412 if(index < 0 || index > StateChunkClass::getNumSlots(classid))
00413 {
00414 SWARNING << "subChunk: index " << index << " > Numslots "
00415 << StateChunkClass::getNumSlots(classid)
00416 << ", ignored!" << std::endl;
00417 return true;
00418 }
00419
00420 if(_mfChunks[classid + index] == NullFC)
00421 return true;
00422
00423
00424
00425 subRefCP(_mfChunks[classid + index]);
00426
00427 _mfChunks[classid + index] = NullFC;
00428
00429 return false;
00430 }
00431
00435 void State::clearChunks(void)
00436 {
00437 std::transform(_mfChunks.begin(),
00438 _mfChunks.end (),
00439 _mfChunks.begin(),
00440 ClearSlot());
00441 }
00442
00443
00444
00445
00450 Real32 State::switchCost(State *OSG_CHECK_ARG(state))
00451 {
00452 return 0;
00453 }
00454
00455 bool State::operator < (const State &other) const
00456 {
00457 return this < &other;
00458 }
00459
00463 bool State::operator == (const State &OSG_CHECK_ARG(other)) const
00464 {
00465 return false;
00466 }
00467
00468 bool State::operator != (const State &other) const
00469 {
00470 return ! (*this == other);
00471 }