Spinning Topp Logo BlackTopp Studios
inc
meshmanager.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _graphicsmeshmanager_cpp
41 #define _graphicsmeshmanager_cpp
42 
43 #include "Graphics/meshmanager.h"
44 #include "Graphics/mesh.h"
45 
46 #include "vector2.h"
47 #include "exception.h"
48 
49 #include "Internal/iostreamwrapper.h.cpp"
50 
51 #include <Ogre.h>
52 
53 namespace Mezzanine
54 {
55  template<> Graphics::MeshManager* Singleton<Graphics::MeshManager>::SingletonPtr = NULL;
56 
57  namespace Graphics
58  {
59  const String MeshManager::ImplementationName = "DefaultMeshManager";
60  const ManagerBase::ManagerType MeshManager::InterfaceType = ManagerBase::MT_MeshManager;
61 
63  { }
64 
66  {
67  /// @todo This class currently doesn't initialize anything from XML, if that changes this constructor needs to be expanded.
68  }
69 
71  {
72  this->Deinitialize();
73  this->UnloadAllMeshes();
74  }
75 
77  {
78  String MeshName = ToAdd->GetName();
79  MeshIterator MeshIt = this->Meshes.find( MeshName );
80  if( MeshIt == this->Meshes.end() ) {
81  this->Meshes.insert( std::pair<String,Mesh*>(MeshName,ToAdd) );
82  }else{
83  MEZZ_EXCEPTION(ExceptionBase::II_DUPLICATE_IDENTITY_EXCEPTION,"Meshes must have unique names when loaded!");
84  }
85  }
86 
87  ///////////////////////////////////////////////////////////////////////////////
88  // Mesh Management
89 
90  Mesh* MeshManager::LoadMesh(const String& MeshName, const String& Group)
91  {
92  MeshIterator MeshIt = this->Meshes.find(MeshName);
93  if( MeshIt != this->Meshes.end() ) {
94  return (*MeshIt).second;
95  }
96  return this->_WrapInternalMesh( this->_GetInternalManager()->load(MeshName,Group) );
97  }
98 
99  Mesh* MeshManager::LoadMesh(const String& FilePathAndName)
100  {
101  size_t Slash = FilePathAndName.find_last_of("\\/");
102  if( Slash != String::npos ) {
103  std::ifstream Stream;
104  Stream.open(FilePathAndName.c_str());
105  Ogre::MeshSerializer OgreSerializer;
106  Ogre::DataStreamPtr OgreStreamPtr(new Internal::IStreamWrapper(&Stream,false));
107  Ogre::MeshPtr NewMesh = Ogre::MeshManager::getSingleton().createManual(FilePathAndName.substr(Slash+1),Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
108  OgreSerializer.importMesh(OgreStreamPtr,NewMesh.get());
109  return this->_WrapInternalMesh(NewMesh);
110  }
111  return NULL;
112  }
113 
114  Mesh* MeshManager::LoadMesh(std::istream* Stream)
115  {
116  /*Ogre::MeshPtr NewMesh = Ogre::MeshManager::getSingleton().createManual();
117  Ogre::MeshSerializer OgreSerializer;// */
118  MEZZ_EXCEPTION(ExceptionBase::NOT_IMPLEMENTED_EXCEPTION,"Loading Meshes via exclusively a standard stream is not supported yet.");
119  }
120 
121  void MeshManager::UnloadMesh(const String& MeshName)
122  {
123  MeshIterator MeshIt = this->Meshes.find(MeshName);
124  if( MeshIt == this->Meshes.end() ) {
125  return;
126  }
127  this->_GetInternalManager()->unload(MeshName);
128  delete (*MeshIt).second;
129  this->Meshes.erase(MeshIt);
130  }
131 
133  {
134  for( MeshIterator MeshIt = this->Meshes.begin() ; MeshIt != this->Meshes.end() ; ++MeshIt )
135  { delete (*MeshIt).second; }
136  this->Meshes.clear();
137  this->_GetInternalManager()->unloadAll();
138  }
139 
140  void MeshManager::SaveMesh(Mesh* ToSave, const String& FileName, const String& GroupName)
141  {
142  MEZZ_EXCEPTION(ExceptionBase::NOT_IMPLEMENTED_EXCEPTION,"Saving Meshes via asset groups is not supported yet.");
143  }
144 
145  void MeshManager::SaveMesh(Mesh* ToSave, const String& FilePathAndName)
146  {
147  Ogre::MeshSerializer OgreSerializer;
148  OgreSerializer.exportMesh(ToSave->_GetInternalMesh().get(),FilePathAndName);
149  }
150 
151  void MeshManager::SaveMesh(Mesh* ToSave, std::ostream* Stream)
152  {
153  Ogre::MeshSerializer OgreSerializer;
154  Ogre::DataStreamPtr OgreStreamPtr(new Internal::OStreamWrapper(Stream,false));
155  OgreSerializer.exportMesh(ToSave->_GetInternalMesh().get(),OgreStreamPtr);
156  }
157 
158  Mesh* MeshManager::GetMesh(const String& MeshName)
159  {
160  MeshIterator MeshIt = this->Meshes.find(MeshName);
161  if( MeshIt != this->Meshes.end() ) {
162  return (*MeshIt).second;
163  }
164  return NULL;
165  }
166 
168  {
169  return this->Meshes.size();
170  }
171 
172  ///////////////////////////////////////////////////////////////////////////////
173  // Utility
174 
176  { this->Initialized = true; }
177 
179  { this->Initialized = false; }
180 
181  ///////////////////////////////////////////////////////////////////////////////
182  // Type Identifier Methods
183 
185  { return MeshManager::InterfaceType; }
186 
189 
190  ///////////////////////////////////////////////////////////////////////////////
191  // Internal Methods
192 
194  {
195  Mesh* Wrapped = new Mesh(ToWrap);
196  this->AddMesh( Wrapped );
197  return Wrapped;
198  }
199 
200  Ogre::MeshManager* MeshManager::_GetInternalManager() const
201  { return Ogre::MeshManager::getSingletonPtr(); }
202 
203  ///////////////////////////////////////////////////////////////////////////////
204  // DefaultMeshManagerFactory Methods
205 
207  { }
208 
210  { }
211 
214 
216  { return MeshManager::InterfaceType; }
217 
219  {
221  /// @todo Add something to log a warning that the manager exists and was requested to be constructed when we have a logging manager set up.
223  }else return new MeshManager();
224  }
225 
227  {
229  /// @todo Add something to log a warning that the manager exists and was requested to be constructed when we have a logging manager set up.
231  }else return new MeshManager(XMLNode);
232  }
233 
235  { delete ToBeDestroyed; }
236  }//Graphics
237 }//Mezzanine
238 
239 #endif
virtual void AddMesh(Mesh *ToAdd)
Adds a Mesh to this manager. the name of the Mesh being added is not unique a II_DUPLICATE_IDENTITY_E...
Definition: meshmanager.cpp:76
virtual void Deinitialize()
Removes this manager from any necessary configuration so it can be safely disposed of...
Thrown when duplicates of teh same identity string exist.
Definition: exception.h:95
String GetManagerImplName() const
Gets the name of the manager that is created by this factory.
void SaveMesh(Mesh *ToSave, const String &FileName, const String &GroupName)
Writes a Mesh to the asset group.
void UnloadAllMeshes()
Unloads every Mesh that is currently loaded.
EntresolManager * CreateManager(const NameValuePairList &Params)
Creates a manager of the type represented by this factory.
static const ManagerBase::ManagerType InterfaceType
A ManagerType enum value used to describe the type of interface/functionality this manager provides...
Definition: meshmanager.h:89
ManagerType
A listing of Manager Types.
Definition: managerbase.h:65
Whole GetNumMeshes()
Gets the number of currently loaded meshes.
Ogre::MeshManager * _GetInternalManager() const
Gets the internal MeshManager.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
This class is used to check and modify the properties of a graphics mesh.
Definition: mesh.h:63
Thrown when we just have not coded a thing yet, but we knew what the API should look like...
Definition: exception.h:117
Ogre::MeshPtr _GetInternalMesh() const
Gets the internal Mesh pointer.
Definition: mesh.cpp:151
Mesh * _WrapInternalMesh(Ogre::MeshPtr ToWrap)
Wraps and stores an Ogre Mesh instance.
MeshManager()
Class constructor.
Definition: meshmanager.cpp:62
MeshContainer Meshes
Container storing all of the currently loaded Meshes.
Definition: meshmanager.h:93
This implements the exception hiearchy for Mezzanine.
Mesh * LoadMesh(const String &ResourceName, const String &ResourceGroup)
Loads a Mesh file from an asset group and prepares it for use.
Definition: meshmanager.cpp:90
void UnloadMesh(const String &MeshName)
Unloads a Mesh from memory.
static const String ImplementationName
A String containing the name of this manager implementation.
Definition: meshmanager.h:87
Mesh * GetMesh(const String &MeshName)
Gets a Mesh stored in this manager.
void DestroyManager(EntresolManager *ToBeDestroyed)
Destroys a Manager created by this factory.
static Boole SingletonValid()
Checks to see if the singleton pointer is valid.
Definition: singleton.h:110
ConstString & GetName() const
Gets the Name of this Mesh.
Definition: mesh.cpp:142
virtual ManagerType GetInterfaceType() const
This returns the type of this manager.
static MeshManager * GetSingletonPtr()
Fetches a pointer to the singleton.
Definition: singleton.h:97
This is the base class for all managers that do no describe properties of a single world...
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
virtual ~DefaultMeshManagerFactory()
Class destructor.
MeshContainer::iterator MeshIterator
Iterator type for Mesh instances stored in this class.
Definition: meshmanager.h:82
std::list< NameValuePair > NameValuePairList
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:206
virtual ~MeshManager()
Class destructor.
Definition: meshmanager.cpp:70
virtual void Initialize()
Configures this manager for use prior to entering the main loop.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
CountedPtr< DataStream > DataStreamPtr
This is a convenience type for a data stream in a counted pointer.
Definition: datastream.h:383
This manager handles the storage, generation, and query of of Graphics Meshes.
Definition: meshmanager.h:76
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
ManagerBase::ManagerType GetManagerType() const
Gets the type of manager that is created by this factory.
Boole Initialized
Simple Boole indicating whether or not this manager has been initialized.
Definition: managerbase.h:111
virtual String GetImplementationTypeName() const
This Allows any manager name to be sent to a stream. Primarily used for logging.