Core.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include <Rocket/Core.h>
  29. #include <algorithm>
  30. #include "FileInterfaceDefault.h"
  31. #include "GeometryDatabase.h"
  32. #include "PluginRegistry.h"
  33. #include "StyleSheetFactory.h"
  34. #include "TemplateCache.h"
  35. #include "TextureDatabase.h"
  36. namespace Rocket {
  37. namespace Core {
  38. // Rocket's renderer interface.
  39. static RenderInterface* render_interface = NULL;
  40. /// Rocket's system interface.
  41. static SystemInterface* system_interface = NULL;
  42. // Rocket's file I/O interface.
  43. FileInterface* file_interface = NULL;
  44. static FileInterfaceDefault file_interface_default;
  45. static bool initialised = false;
  46. typedef std::map< String, Context* > ContextMap;
  47. static ContextMap contexts;
  48. #ifndef ROCKET_VERSION
  49. #define ROCKET_VERSION "custom"
  50. #endif
  51. /**
  52. A 'plugin' for unobtrusively intercepting the destruction of contexts.
  53. */
  54. class PluginContextRelease : public Plugin
  55. {
  56. public:
  57. virtual void OnShutdown()
  58. {
  59. delete this;
  60. }
  61. virtual void OnContextDestroy(Context* context)
  62. {
  63. contexts.erase(context->GetName());
  64. }
  65. };
  66. bool Initialise()
  67. {
  68. // Check for valid interfaces, or install default interfaces as appropriate.
  69. if (system_interface == NULL)
  70. {
  71. Log::Message(Log::LT_ERROR, "No system interface set!");
  72. return false;
  73. }
  74. if (file_interface == NULL)
  75. {
  76. file_interface = &file_interface_default;
  77. file_interface->AddReference();
  78. }
  79. Log::Initialise();
  80. TextureDatabase::Initialise();
  81. FontDatabase::Initialise();
  82. StyleSheetSpecification::Initialise();
  83. StyleSheetFactory::Initialise();
  84. TemplateCache::Initialise();
  85. Factory::Initialise();
  86. // Notify all plugins we're starting up.
  87. PluginRegistry::RegisterPlugin(new PluginContextRelease());
  88. PluginRegistry::NotifyInitialise();
  89. initialised = true;
  90. return true;
  91. }
  92. void Shutdown()
  93. {
  94. // Notify all plugins we're being shutdown.
  95. PluginRegistry::NotifyShutdown();
  96. // Release all remaining contexts.
  97. for (ContextMap::iterator itr = contexts.begin(); itr != contexts.end(); ++itr)
  98. Core::Log::Message(Log::LT_WARNING, "Context '%s' still active on shutdown.", (*itr).first.CString());
  99. contexts.clear();
  100. TemplateCache::Shutdown();
  101. StyleSheetFactory::Shutdown();
  102. StyleSheetSpecification::Shutdown();
  103. FontDatabase::Shutdown();
  104. TextureDatabase::Shutdown();
  105. Factory::Shutdown();
  106. Log::Shutdown();
  107. initialised = false;
  108. if (render_interface != NULL)
  109. render_interface->RemoveReference();
  110. if (file_interface != NULL)
  111. file_interface->RemoveReference();
  112. if (system_interface != NULL)
  113. system_interface->RemoveReference();
  114. render_interface = NULL;
  115. file_interface = NULL;
  116. system_interface = NULL;
  117. StringStorage::ClearPools();
  118. }
  119. // Returns the version of this Rocket library.
  120. String GetVersion()
  121. {
  122. return ROCKET_VERSION;
  123. }
  124. // Sets the interface through which all Rocket messages will be routed.
  125. void SetSystemInterface(SystemInterface* _system_interface)
  126. {
  127. if (system_interface == _system_interface)
  128. return;
  129. if (system_interface != NULL)
  130. system_interface->RemoveReference();
  131. system_interface = _system_interface;
  132. if (system_interface != NULL)
  133. system_interface->AddReference();
  134. }
  135. // Returns Rocket's system interface.
  136. SystemInterface* GetSystemInterface()
  137. {
  138. return system_interface;
  139. }
  140. // Sets the interface through which all rendering requests are made.
  141. void SetRenderInterface(RenderInterface* _render_interface)
  142. {
  143. if (render_interface == _render_interface)
  144. return;
  145. if (render_interface != NULL)
  146. render_interface->RemoveReference();
  147. render_interface = _render_interface;
  148. if (render_interface != NULL)
  149. render_interface->AddReference();
  150. }
  151. // Returns Rocket's render interface.
  152. RenderInterface* GetRenderInterface()
  153. {
  154. return render_interface;
  155. }
  156. // Sets the interface through which all file I/O requests are made.
  157. void SetFileInterface(FileInterface* _file_interface)
  158. {
  159. if (file_interface == _file_interface)
  160. return;
  161. if (file_interface != NULL)
  162. file_interface->RemoveReference();
  163. file_interface = _file_interface;
  164. if (file_interface != NULL)
  165. file_interface->AddReference();
  166. }
  167. // Returns Rocket's file interface.
  168. FileInterface* GetFileInterface()
  169. {
  170. return file_interface;
  171. }
  172. // Creates a new element context.
  173. Context* CreateContext(const String& name, const Vector2i& dimensions, RenderInterface* custom_render_interface)
  174. {
  175. if (!initialised)
  176. return NULL;
  177. if (custom_render_interface == NULL &&
  178. render_interface == NULL)
  179. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.", name.CString());
  180. if (GetContext(name) != NULL)
  181. {
  182. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.CString());
  183. return NULL;
  184. }
  185. Context* new_context = Factory::InstanceContext(name);
  186. if (new_context == NULL)
  187. {
  188. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned NULL.", name.CString());
  189. return NULL;
  190. }
  191. // Set the render interface on the context, and add a reference onto it.
  192. if (custom_render_interface)
  193. new_context->render_interface = custom_render_interface;
  194. else
  195. new_context->render_interface = render_interface;
  196. new_context->render_interface->AddReference();
  197. new_context->SetDimensions(dimensions);
  198. contexts[name] = new_context;
  199. PluginRegistry::NotifyContextCreate(new_context);
  200. return new_context;
  201. }
  202. // Fetches a previously constructed context by name.
  203. Context* GetContext(const String& name)
  204. {
  205. ContextMap::iterator i = contexts.find(name);
  206. if (i == contexts.end())
  207. return NULL;
  208. return (*i).second;
  209. }
  210. // Fetches a context by index.
  211. Context* GetContext(int index)
  212. {
  213. ContextMap::iterator i = contexts.begin();
  214. int count = 0;
  215. if (index >= GetNumContexts())
  216. index = GetNumContexts() - 1;
  217. while (count < index)
  218. {
  219. ++i;
  220. ++count;
  221. }
  222. if (i == contexts.end())
  223. return NULL;
  224. return (*i).second;
  225. }
  226. // Returns the number of active contexts.
  227. int GetNumContexts()
  228. {
  229. return (int) contexts.size();
  230. }
  231. // Registers a generic rocket plugin
  232. void RegisterPlugin(Plugin* plugin)
  233. {
  234. if (initialised)
  235. plugin->OnInitialise();
  236. PluginRegistry::RegisterPlugin(plugin);
  237. }
  238. // Forces all compiled geometry handles generated by libRocket to be released.
  239. void ReleaseCompiledGeometries()
  240. {
  241. GeometryDatabase::ReleaseGeometries();
  242. }
  243. // Forces all texture handles loaded and generated by libRocket to be released.
  244. void ReleaseTextures()
  245. {
  246. TextureDatabase::ReleaseTextures();
  247. }
  248. }
  249. }