Panda3D
graphicsStateGuardianBase.cxx
1 // Filename: graphicsStateGuardianBase.cxx
2 // Created by: drose (06Oct99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "graphicsStateGuardianBase.h"
16 #include "lightMutexHolder.h"
17 #include <algorithm>
18 
19 GraphicsStateGuardianBase::GSGs GraphicsStateGuardianBase::_gsgs;
20 GraphicsStateGuardianBase *GraphicsStateGuardianBase::_default_gsg;
21 LightMutex GraphicsStateGuardianBase::_lock;
22 TypeHandle GraphicsStateGuardianBase::_type_handle;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: GraphicsStateGuardianBase::get_default_gsg
26 // Access: Published, Static
27 // Description: Returns a pointer to the "default" GSG. This is
28 // typically the first GSG created in an application; in
29 // a single-window application, it will be the only GSG.
30 // This GSG is used to determine default optimization
31 // choices for loaded geometry.
32 //
33 // The return value may be NULL if a GSG has not been
34 // created.
35 ////////////////////////////////////////////////////////////////////
38  LightMutexHolder holder(_lock);
39  return _default_gsg;
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: GraphicsStateGuardianBase::set_default_gsg
44 // Access: Published, Static
45 // Description: Specifies a particular GSG to use as the "default"
46 // GSG. See get_default_gsg().
47 ////////////////////////////////////////////////////////////////////
50  LightMutexHolder holder(_lock);
51  if (find(_gsgs.begin(), _gsgs.end(), default_gsg) == _gsgs.end()) {
52  // The specified GSG doesn't exist or it has already destructed.
53  nassertv(false);
54  return;
55  }
56 
57  _default_gsg = default_gsg;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: GraphicsStateGuardianBase::get_num_gsgs
62 // Access: Published, Static
63 // Description: Returns the total number of GSG's in the universe.
64 ////////////////////////////////////////////////////////////////////
67  return _gsgs.size();
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: GraphicsStateGuardianBase::get_gsg
72 // Access: Published, Static
73 // Description: Returns the nth GSG in the universe. GSG's
74 // automatically add themselves and remove themselves
75 // from this list as they are created and destroyed.
76 ////////////////////////////////////////////////////////////////////
78 get_gsg(int n) {
79  nassertr(n >= 0 && n < (int)_gsgs.size(), NULL);
80  return _gsgs[n];
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: GraphicsStateGuardianBase::add_gsg
85 // Access: Public, Static
86 // Description: Called by a GSG after it has been initialized, to add
87 // a new GSG to the available list.
88 ////////////////////////////////////////////////////////////////////
91  LightMutexHolder holder(_lock);
92 
93  if (find(_gsgs.begin(), _gsgs.end(), gsg) != _gsgs.end()) {
94  // Already on the list.
95  return;
96  }
97 
98  _gsgs.push_back(gsg);
99 
100  if (_default_gsg == (GraphicsStateGuardianBase *)NULL) {
101  _default_gsg = gsg;
102  }
103 }
104 
105 ////////////////////////////////////////////////////////////////////
106 // Function: GraphicsStateGuardianBase::remove_gsg
107 // Access: Public, Static
108 // Description: Called by a GSG destructor to remove a GSG from the
109 // available list.
110 ////////////////////////////////////////////////////////////////////
113  LightMutexHolder holder(_lock);
114 
115  GSGs::iterator gi = find(_gsgs.begin(), _gsgs.end(), gsg);
116  if (gi == _gsgs.end()) {
117  // Already removed, or never added.
118  return;
119  }
120 
121  _gsgs.erase(gi);
122 
123  if (_default_gsg == gsg) {
124  if (!_gsgs.empty()) {
125  _default_gsg = *_gsgs.begin();
126  } else {
127  _default_gsg = NULL;
128  }
129  }
130 }
static void remove_gsg(GraphicsStateGuardianBase *gsg)
Called by a GSG destructor to remove a GSG from the available list.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
Similar to MutexHolder, but for a light mutex.
static GraphicsStateGuardianBase * get_default_gsg()
Returns a pointer to the "default" GSG.
static GraphicsStateGuardianBase * get_gsg(int n)
Returns the nth GSG in the universe.
static int get_num_gsgs()
Returns the total number of GSG&#39;s in the universe.
static void set_default_gsg(GraphicsStateGuardianBase *default_gsg)
Specifies a particular GSG to use as the "default" GSG.
static void add_gsg(GraphicsStateGuardianBase *gsg)
Called by a GSG after it has been initialized, to add a new GSG to the available list.
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This is a standard, non-reentrant mutex, similar to the Mutex class.
Definition: lightMutex.h:45