Core.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "../../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. #ifndef ROCKET_NO_FILE_INTERFACE_DEFAULT
  45. static FileInterfaceDefault file_interface_default;
  46. #endif
  47. static bool initialised = false;
  48. typedef std::map< String, Context* > ContextMap;
  49. static ContextMap contexts;
  50. #ifndef ROCKET_VERSION
  51. #define ROCKET_VERSION "custom"
  52. #endif
  53. /**
  54. A 'plugin' for unobtrusively intercepting the destruction of contexts.
  55. */
  56. class PluginContextRelease : public Plugin
  57. {
  58. public:
  59. virtual void OnShutdown()
  60. {
  61. delete this;
  62. }
  63. virtual void OnContextDestroy(Context* context)
  64. {
  65. contexts.erase(context->GetName());
  66. }
  67. };
  68. bool Initialise()
  69. {
  70. // Check for valid interfaces, or install default interfaces as appropriate.
  71. if (system_interface == NULL)
  72. {
  73. Log::Message(Log::LT_ERROR, "No system interface set!");
  74. return false;
  75. }
  76. if (file_interface == NULL)
  77. {
  78. #ifndef ROCKET_NO_FILE_INTERFACE_DEFAULT
  79. file_interface = &file_interface_default;
  80. file_interface->AddReference();
  81. #else
  82. Log::Message(Log::LT_ERROR, "No file interface set!");
  83. return false;
  84. #endif
  85. }
  86. Log::Initialise();
  87. TextureDatabase::Initialise();
  88. FontDatabase::Initialise();
  89. StyleSheetSpecification::Initialise();
  90. StyleSheetFactory::Initialise();
  91. TemplateCache::Initialise();
  92. Factory::Initialise();
  93. // Notify all plugins we're starting up.
  94. PluginRegistry::RegisterPlugin(new PluginContextRelease());
  95. PluginRegistry::NotifyInitialise();
  96. initialised = true;
  97. return true;
  98. }
  99. void Shutdown()
  100. {
  101. // Notify all plugins we're being shutdown.
  102. PluginRegistry::NotifyShutdown();
  103. // Release all remaining contexts.
  104. for (ContextMap::iterator itr = contexts.begin(); itr != contexts.end(); ++itr)
  105. Core::Log::Message(Log::LT_WARNING, "Context '%s' still active on shutdown.", (*itr).first.CString());
  106. contexts.clear();
  107. TemplateCache::Shutdown();
  108. StyleSheetFactory::Shutdown();
  109. StyleSheetSpecification::Shutdown();
  110. FontDatabase::Shutdown();
  111. TextureDatabase::Shutdown();
  112. Factory::Shutdown();
  113. Log::Shutdown();
  114. initialised = false;
  115. if (render_interface != NULL)
  116. render_interface->RemoveReference();
  117. if (file_interface != NULL)
  118. file_interface->RemoveReference();
  119. if (system_interface != NULL)
  120. system_interface->RemoveReference();
  121. render_interface = NULL;
  122. file_interface = NULL;
  123. system_interface = NULL;
  124. }
  125. // Returns the version of this Rocket library.
  126. String GetVersion()
  127. {
  128. return ROCKET_VERSION;
  129. }
  130. // Sets the interface through which all Rocket messages will be routed.
  131. void SetSystemInterface(SystemInterface* _system_interface)
  132. {
  133. if (system_interface == _system_interface)
  134. return;
  135. if (system_interface != NULL)
  136. system_interface->RemoveReference();
  137. system_interface = _system_interface;
  138. if (system_interface != NULL)
  139. system_interface->AddReference();
  140. }
  141. // Returns Rocket's system interface.
  142. SystemInterface* GetSystemInterface()
  143. {
  144. return system_interface;
  145. }
  146. // Sets the interface through which all rendering requests are made.
  147. void SetRenderInterface(RenderInterface* _render_interface)
  148. {
  149. if (render_interface == _render_interface)
  150. return;
  151. if (render_interface != NULL)
  152. render_interface->RemoveReference();
  153. render_interface = _render_interface;
  154. if (render_interface != NULL)
  155. render_interface->AddReference();
  156. }
  157. // Returns Rocket's render interface.
  158. RenderInterface* GetRenderInterface()
  159. {
  160. return render_interface;
  161. }
  162. // Sets the interface through which all file I/O requests are made.
  163. void SetFileInterface(FileInterface* _file_interface)
  164. {
  165. if (file_interface == _file_interface)
  166. return;
  167. if (file_interface != NULL)
  168. file_interface->RemoveReference();
  169. file_interface = _file_interface;
  170. if (file_interface != NULL)
  171. file_interface->AddReference();
  172. }
  173. // Returns Rocket's file interface.
  174. FileInterface* GetFileInterface()
  175. {
  176. return file_interface;
  177. }
  178. // Creates a new element context.
  179. Context* CreateContext(const String& name, const Vector2i& dimensions, RenderInterface* custom_render_interface)
  180. {
  181. if (!initialised)
  182. return NULL;
  183. if (custom_render_interface == NULL &&
  184. render_interface == NULL)
  185. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.", name.CString());
  186. if (GetContext(name) != NULL)
  187. {
  188. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.CString());
  189. return NULL;
  190. }
  191. Context* new_context = Factory::InstanceContext(name);
  192. if (new_context == NULL)
  193. {
  194. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned NULL.", name.CString());
  195. return NULL;
  196. }
  197. // Set the render interface on the context, and add a reference onto it.
  198. if (custom_render_interface)
  199. new_context->render_interface = custom_render_interface;
  200. else
  201. new_context->render_interface = render_interface;
  202. new_context->render_interface->AddReference();
  203. new_context->SetDimensions(dimensions);
  204. contexts[name] = new_context;
  205. PluginRegistry::NotifyContextCreate(new_context);
  206. return new_context;
  207. }
  208. // Fetches a previously constructed context by name.
  209. Context* GetContext(const String& name)
  210. {
  211. ContextMap::iterator i = contexts.find(name);
  212. if (i == contexts.end())
  213. return NULL;
  214. return (*i).second;
  215. }
  216. // Fetches a context by index.
  217. Context* GetContext(int index)
  218. {
  219. ContextMap::iterator i = contexts.begin();
  220. int count = 0;
  221. if (index >= GetNumContexts())
  222. index = GetNumContexts() - 1;
  223. while (count < index)
  224. {
  225. ++i;
  226. ++count;
  227. }
  228. if (i == contexts.end())
  229. return NULL;
  230. return (*i).second;
  231. }
  232. // Returns the number of active contexts.
  233. int GetNumContexts()
  234. {
  235. return (int) contexts.size();
  236. }
  237. // Registers a generic rocket plugin
  238. void RegisterPlugin(Plugin* plugin)
  239. {
  240. if (initialised)
  241. plugin->OnInitialise();
  242. PluginRegistry::RegisterPlugin(plugin);
  243. }
  244. // Forces all compiled geometry handles generated by libRocket to be released.
  245. void ReleaseCompiledGeometries()
  246. {
  247. GeometryDatabase::ReleaseGeometries();
  248. }
  249. // Forces all texture handles loaded and generated by libRocket to be released.
  250. void ReleaseTextures()
  251. {
  252. TextureDatabase::ReleaseTextures();
  253. }
  254. }
  255. }