00001 // Filename: graphicsStateGuardianBase.cxx 00002 // Created by: drose (06Oct99) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "graphicsStateGuardianBase.h" 00016 #include "lightMutexHolder.h" 00017 #include <algorithm> 00018 00019 GraphicsStateGuardianBase::GSGs GraphicsStateGuardianBase::_gsgs; 00020 GraphicsStateGuardianBase *GraphicsStateGuardianBase::_default_gsg; 00021 LightMutex GraphicsStateGuardianBase::_lock; 00022 TypeHandle GraphicsStateGuardianBase::_type_handle; 00023 00024 //////////////////////////////////////////////////////////////////// 00025 // Function: GraphicsStateGuardianBase::get_default_gsg 00026 // Access: Published, Static 00027 // Description: Returns a pointer to the "default" GSG. This is 00028 // typically the first GSG created in an application; in 00029 // a single-window application, it will be the only GSG. 00030 // This GSG is used to determine default optimization 00031 // choices for loaded geometry. 00032 // 00033 // The return value may be NULL if a GSG has not been 00034 // created. 00035 //////////////////////////////////////////////////////////////////// 00036 GraphicsStateGuardianBase *GraphicsStateGuardianBase:: 00037 get_default_gsg() { 00038 LightMutexHolder holder(_lock); 00039 return _default_gsg; 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: GraphicsStateGuardianBase::set_default_gsg 00044 // Access: Published, Static 00045 // Description: Specifies a particular GSG to use as the "default" 00046 // GSG. See get_default_gsg(). 00047 //////////////////////////////////////////////////////////////////// 00048 void GraphicsStateGuardianBase:: 00049 set_default_gsg(GraphicsStateGuardianBase *default_gsg) { 00050 LightMutexHolder holder(_lock); 00051 if (find(_gsgs.begin(), _gsgs.end(), default_gsg) == _gsgs.end()) { 00052 // The specified GSG doesn't exist or it has already destructed. 00053 nassertv(false); 00054 return; 00055 } 00056 00057 _default_gsg = default_gsg; 00058 } 00059 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: GraphicsStateGuardianBase::get_num_gsgs 00062 // Access: Published, Static 00063 // Description: Returns the total number of GSG's in the universe. 00064 //////////////////////////////////////////////////////////////////// 00065 int GraphicsStateGuardianBase:: 00066 get_num_gsgs() { 00067 return _gsgs.size(); 00068 } 00069 00070 //////////////////////////////////////////////////////////////////// 00071 // Function: GraphicsStateGuardianBase::get_gsg 00072 // Access: Published, Static 00073 // Description: Returns the nth GSG in the universe. GSG's 00074 // automatically add themselves and remove themselves 00075 // from this list as they are created and destroyed. 00076 //////////////////////////////////////////////////////////////////// 00077 GraphicsStateGuardianBase *GraphicsStateGuardianBase:: 00078 get_gsg(int n) { 00079 nassertr(n >= 0 && n < (int)_gsgs.size(), NULL); 00080 return _gsgs[n]; 00081 } 00082 00083 //////////////////////////////////////////////////////////////////// 00084 // Function: GraphicsStateGuardianBase::add_gsg 00085 // Access: Public, Static 00086 // Description: Called by a GSG after it has been initialized, to add 00087 // a new GSG to the available list. 00088 //////////////////////////////////////////////////////////////////// 00089 void GraphicsStateGuardianBase:: 00090 add_gsg(GraphicsStateGuardianBase *gsg) { 00091 LightMutexHolder holder(_lock); 00092 00093 if (find(_gsgs.begin(), _gsgs.end(), gsg) != _gsgs.end()) { 00094 // Already on the list. 00095 return; 00096 } 00097 00098 _gsgs.push_back(gsg); 00099 00100 if (_default_gsg == (GraphicsStateGuardianBase *)NULL) { 00101 _default_gsg = gsg; 00102 } 00103 } 00104 00105 //////////////////////////////////////////////////////////////////// 00106 // Function: GraphicsStateGuardianBase::remove_gsg 00107 // Access: Public, Static 00108 // Description: Called by a GSG destructor to remove a GSG from the 00109 // available list. 00110 //////////////////////////////////////////////////////////////////// 00111 void GraphicsStateGuardianBase:: 00112 remove_gsg(GraphicsStateGuardianBase *gsg) { 00113 LightMutexHolder holder(_lock); 00114 00115 GSGs::iterator gi = find(_gsgs.begin(), _gsgs.end(), gsg); 00116 if (gi == _gsgs.end()) { 00117 // Already removed, or never added. 00118 return; 00119 } 00120 00121 _gsgs.erase(gi); 00122 00123 if (_default_gsg == gsg) { 00124 if (!_gsgs.empty()) { 00125 _default_gsg = *_gsgs.begin(); 00126 } else { 00127 _default_gsg = NULL; 00128 } 00129 } 00130 }