Core.cpp 7.6 KB

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