SO3Engine
SO3ResourceLoader.cpp
Go to the documentation of this file.
1
11
12template<> SO3::SResourceLoader* Ogre::Singleton<SO3::SResourceLoader>::msSingleton = 0;
13
14namespace SO3
15{
16
18 {
19 mScene = 0;
20 }
21
25
27 {
28 return msSingleton;
29 }
30
32 {
33 assert(msSingleton);
34 return (*msSingleton);
35 }
36
38 {
39 mScene = scene;
40 }
41
43 {
44 //update ogre material pointer
45 for (auto & material : mMaterialList)
46 {
47 material->_UpdateOgreMaterialPointer();
48 material->_BindTechniquesToOgre();
49 }
50
51 mMaterialList.clear();
52 }
53
54 bool SResourceLoader::handleEvent(Ogre::ScriptCompiler* compiler, Ogre::ScriptCompilerEvent* evt, void* retval)
55 {
56 // Get the type of the resource parsed by the script compiler
57 bool processed = false;
58
59 // If a scene was setted
60 if (mScene != 0)
61 {
62 // A material start to be loaded from script
63 if (evt->mType == Ogre::CreateMaterialScriptCompilerEvent::eventType)
64 {
65 // Convert the event.
66 Ogre::CreateMaterialScriptCompilerEvent* convertedEvent = static_cast<Ogre::CreateMaterialScriptCompilerEvent*>(evt);
67
68 // Create a new empty material using so3 class
69 try
70 {
71 // An SException can be throwed here, so catch it!
72 SMaterial* newMaterial = mScene->GetMaterial(convertedEvent->mResourceGroup, convertedEvent->mName, false);
73
74 if (newMaterial != 0)
75 {
76 mMaterialList.push_back(newMaterial);
77 return processed;
78 }
79
80 newMaterial = mScene->CreateMaterial(convertedEvent->mResourceGroup, convertedEvent->mName, true);
81
82 // Check if the material creation was successful
83 if (newMaterial != 0)
84 {
85 // Ok, continue.
86 Ogre::MaterialPtr ogreMaterial = newMaterial->getOgreMaterialPointer();
87
88 // Get the newly created Ogre::Material (in SMaterial) and return it to the script compiler, it will populate it!
89 Ogre::Material** retMaterial = (Ogre::Material**)retval;
90 *retMaterial = ogreMaterial.get();
91
92 mMaterialList.push_back(newMaterial);
93
94 // Tell Ogre that we have created the Ogre::Material, he do not need to do it.
95 processed = true;
96 }
97 }
98 catch (const SException&)
99 {
100 // Do nothing, the exception content is already logged to log file by the SException ctor.
101 }
102 }
103
104 // A material finished it's loading from script process
105 /*
106 if(evt->mType == Ogre::PreApplyTextureAliasesScriptCompilerEvent::eventType)
107 {
108 // Convert the event.
109 Ogre::PreApplyTextureAliasesScriptCompilerEvent* convertedEvent = static_cast<Ogre::PreApplyTextureAliasesScriptCompilerEvent*>(evt);
110
111 // Get the SO3 material
112 SMaterial* material = mScene->GetMaterial(convertedEvent->mMaterial->getGroup(), convertedEvent->mMaterial->getName());
113
114 // Bind loaded techniques to the material
115 if (material != 0)
116 {
117 //update ogre material pointer
118 material->_UpdateOgreMaterialPointer();
119 material->_BindTechniquesToOgre();
120 }
121
122 // Tell Ogre that we have processed this event. In fact, Ogre's ignore this return value for PreApplyTextureAliasesScriptCompilerEvent events.
123 processed = true;
124 }
125 */
126 }
127 return processed;
128 }
129
130}
Base class for SO3 custom exception.
Ogre::MaterialPtr getOgreMaterialPointer()
virtual bool handleEvent(Ogre::ScriptCompiler *compiler, Ogre::ScriptCompilerEvent *evt, void *retval)
static SResourceLoader & getSingleton()
void SetResourceScene(SScene *scene)
static SResourceLoader * getSingletonPtr()
SMaterial * GetMaterial(const std::string &groupName, const std::string &materialName, bool searchOtherGroups=true)
SMaterial * CreateMaterial(const std::string &groupname, const std::string &matname, const bool &loadedFromScript=false)