Core.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. }
  122. // Returns the version of this Rocket library.
  123. String GetVersion()
  124. {
  125. return ROCKET_VERSION;
  126. }
  127. // Sets the interface through which all Rocket messages will be routed.
  128. void SetSystemInterface(SystemInterface* _system_interface)
  129. {
  130. if (system_interface == _system_interface)
  131. return;
  132. if (system_interface != NULL)
  133. system_interface->RemoveReference();
  134. system_interface = _system_interface;
  135. if (system_interface != NULL)
  136. system_interface->AddReference();
  137. }
  138. // Returns Rocket's system interface.
  139. SystemInterface* GetSystemInterface()
  140. {
  141. return system_interface;
  142. }
  143. // Sets the interface through which all rendering requests are made.
  144. void SetRenderInterface(RenderInterface* _render_interface)
  145. {
  146. if (render_interface == _render_interface)
  147. return;
  148. if (render_interface != NULL)
  149. render_interface->RemoveReference();
  150. render_interface = _render_interface;
  151. if (render_interface != NULL)
  152. render_interface->AddReference();
  153. }
  154. // Returns Rocket's render interface.
  155. RenderInterface* GetRenderInterface()
  156. {
  157. return render_interface;
  158. }
  159. // Sets the interface through which all file I/O requests are made.
  160. void SetFileInterface(FileInterface* _file_interface)
  161. {
  162. if (file_interface == _file_interface)
  163. return;
  164. if (file_interface != NULL)
  165. file_interface->RemoveReference();
  166. file_interface = _file_interface;
  167. if (file_interface != NULL)
  168. file_interface->AddReference();
  169. }
  170. // Returns Rocket's file interface.
  171. FileInterface* GetFileInterface()
  172. {
  173. return file_interface;
  174. }
  175. // Creates a new element context.
  176. Context* CreateContext(const String& name, const Vector2i& dimensions, RenderInterface* custom_render_interface)
  177. {
  178. if (!initialised)
  179. return NULL;
  180. if (custom_render_interface == NULL &&
  181. render_interface == NULL)
  182. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.", name.CString());
  183. if (GetContext(name) != NULL)
  184. {
  185. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.CString());
  186. return NULL;
  187. }
  188. Context* new_context = Factory::InstanceContext(name);
  189. if (new_context == NULL)
  190. {
  191. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned NULL.", name.CString());
  192. return NULL;
  193. }
  194. // Set the render interface on the context, and add a reference onto it.
  195. if (custom_render_interface)
  196. new_context->render_interface = custom_render_interface;
  197. else
  198. new_context->render_interface = render_interface;
  199. new_context->render_interface->AddReference();
  200. new_context->SetDimensions(dimensions);
  201. contexts[name] = new_context;
  202. PluginRegistry::NotifyContextCreate(new_context);
  203. return new_context;
  204. }
  205. // Fetches a previously constructed context by name.
  206. Context* GetContext(const String& name)
  207. {
  208. ContextMap::iterator i = contexts.find(name);
  209. if (i == contexts.end())
  210. return NULL;
  211. return (*i).second;
  212. }
  213. // Fetches a context by index.
  214. Context* GetContext(int index)
  215. {
  216. ContextMap::iterator i = contexts.begin();
  217. int count = 0;
  218. if (index >= GetNumContexts())
  219. index = GetNumContexts() - 1;
  220. while (count < index)
  221. {
  222. ++i;
  223. ++count;
  224. }
  225. if (i == contexts.end())
  226. return NULL;
  227. return (*i).second;
  228. }
  229. // Returns the number of active contexts.
  230. int GetNumContexts()
  231. {
  232. return (int) contexts.size();
  233. }
  234. // Registers a generic rocket plugin
  235. void RegisterPlugin(Plugin* plugin)
  236. {
  237. if (initialised)
  238. plugin->OnInitialise();
  239. PluginRegistry::RegisterPlugin(plugin);
  240. }
  241. // Forces all compiled geometry handles generated by libRocket to be released.
  242. void ReleaseCompiledGeometries()
  243. {
  244. GeometryDatabase::ReleaseGeometries();
  245. }
  246. // Forces all texture handles loaded and generated by libRocket to be released.
  247. void ReleaseTextures()
  248. {
  249. TextureDatabase::ReleaseTextures();
  250. }
  251. }
  252. }