main.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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) 2019 Michael R. P. Ragazzon
  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 <Rocket/Core.h>
  28. #include <Rocket/Controls.h>
  29. #include <Rocket/Debugger.h>
  30. #include <Input.h>
  31. #include <Shell.h>
  32. // Performance TODO:
  33. // - Improve performance of transform parser (hashtable)
  34. // - Replace property name strings with handle IDs (ints). Tried this and reverted, see [0e390e9], too little gain for too much complexity.
  35. // - Memory pools for common elements. Also, a lot of temporary objects are created and destroyed.
  36. // - Try replacing ElementAttributes with vector.
  37. // - Can we optimize the layouting? E.g. why is ElementTextDefault::GenerateLine being called even when neither text nor size have seemingly been changed.
  38. // - During first update after construction: Create computed values of all properties, and use these instead of GetProperty.
  39. // Instead, GetComputedValue which gives either absolute length, percentage, keywords (enum), color, etc. Inherited values then only need to check their nearest parent.
  40. // - [bug] Input.range appears only after one additional frame.
  41. // Other TODO:
  42. // - The em-property depends on the current font-size, not font face lineheight! (See Element::OnPropertyChange)
  43. class DemoWindow
  44. {
  45. public:
  46. DemoWindow(const Rocket::Core::String &title, const Rocket::Core::Vector2f &position, Rocket::Core::Context *context)
  47. {
  48. using namespace Rocket::Core;
  49. document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  50. if (document != NULL)
  51. {
  52. {
  53. document->GetElementById("title")->SetInnerRML(title);
  54. document->SetProperty("left", Property(position.x, Property::PX));
  55. document->SetProperty("top", Property(position.y, Property::PX));
  56. }
  57. document->Show();
  58. }
  59. }
  60. void performance_test()
  61. {
  62. /*
  63. FPS values
  64. Original: 18.5 [957f723]
  65. Without property counter: 22.0
  66. With std::string: 23.0 [603fd40]
  67. robin_hood unordered_flat_map: 24.0 [709852f]
  68. Avoid dirtying em's: 27.5
  69. Restructuring update loop: 34.5 [f9892a9]
  70. Element constructor, remove geometry database, remove update() from Context::render: 38.0 [1aab59e]
  71. Replace Dictionary with unordered_flat_map: 40.0 [b04b4e5]
  72. Dirty flag for structure changes: 43.0 [fdf6f53]
  73. Replacing containers: 46.0 [c307140]
  74. Replace 'resize' event with virtual function call: 53.0 [7ad658f]
  75. Use all_properties_dirty flag when constructing elements: 55.0 [fa6bd0a]
  76. Don't double create input elements: 58.0 [e162637]
  77. Memory pool for ElementMeta: 59.0 [ece191a]
  78. Include chobo flat containers: 65.0 [1696aa5]
  79. Move benchmark to its own sample (no code change, fps increase because of removal of animation elements): 68.0 [2433880]
  80. Keep the element's main sizing box local: 69.0 [cf928b2]
  81. Improved hashing of element definition: 70.0 [5cb9e1d]
  82. First usage of computed values (font): 74.0 [04dc275]
  83. Computed values, clipping: 77.0
  84. Computed values, background-color, image-color, opacity: 77.0
  85. Computed values, padding, margin border++: 81.0
  86. */
  87. if (!document)
  88. return;
  89. Rocket::Core::String rml;
  90. for (int i = 0; i < 50; i++)
  91. {
  92. int index = rand() % 1000;
  93. int route = rand() % 50;
  94. int max = (rand() % 40) + 10;
  95. int value = rand() % max;
  96. Rocket::Core::String rml_row = Rocket::Core::CreateString(1000, R"(
  97. <div class="row">
  98. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  99. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  100. <div class="col col4">Assigned</div>
  101. <div class="inrow unmark_collapse">
  102. <div class="col col123 assign_text">Assign to route</div>
  103. <div class="col col4">
  104. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  105. </div>
  106. </div>
  107. </div>)",
  108. index,
  109. route,
  110. max,
  111. value
  112. );
  113. rml += rml_row;
  114. }
  115. if (auto el = document->GetElementById("performance"))
  116. el->SetInnerRML(rml);
  117. }
  118. ~DemoWindow()
  119. {
  120. if (document)
  121. {
  122. document->RemoveReference();
  123. document->Close();
  124. }
  125. }
  126. Rocket::Core::ElementDocument * GetDocument() {
  127. return document;
  128. }
  129. private:
  130. Rocket::Core::ElementDocument *document;
  131. };
  132. Rocket::Core::Context* context = NULL;
  133. ShellRenderInterfaceExtensions *shell_renderer;
  134. DemoWindow* window = NULL;
  135. bool pause_loop = false;
  136. bool single_loop = false;
  137. bool run_update = true;
  138. void GameLoop()
  139. {
  140. if (!pause_loop || single_loop)
  141. {
  142. if (run_update)
  143. {
  144. window->performance_test();
  145. }
  146. context->Update();
  147. shell_renderer->PrepareRenderBuffer();
  148. context->Render();
  149. shell_renderer->PresentRenderBuffer();
  150. single_loop = false;
  151. }
  152. static double t_prev = 0.0f;
  153. double t = Shell::GetElapsedTime();
  154. float dt = float(t - t_prev);
  155. static int count_frames = 0;
  156. count_frames += 1;
  157. if (window && dt > 0.2f)
  158. {
  159. t_prev = t;
  160. auto el = window->GetDocument()->GetElementById("fps");
  161. float fps = float(count_frames) / dt;
  162. count_frames = 0;
  163. el->SetInnerRML(Rocket::Core::CreateString( 20, "FPS: %f", fps ));
  164. }
  165. }
  166. class Event : public Rocket::Core::EventListener
  167. {
  168. public:
  169. Event(const Rocket::Core::String& value) : value(value) {}
  170. void ProcessEvent(Rocket::Core::Event& event) override
  171. {
  172. using namespace Rocket::Core;
  173. if(value == "exit")
  174. Shell::RequestExit();
  175. if (event == "keydown")
  176. {
  177. auto key_identifier = (Rocket::Core::Input::KeyIdentifier)event.GetParameter< int >("key_identifier", 0);
  178. if (key_identifier == Rocket::Core::Input::KI_SPACE)
  179. {
  180. pause_loop = !pause_loop;
  181. }
  182. else if (key_identifier == Rocket::Core::Input::KI_OEM_PLUS)
  183. {
  184. pause_loop = true;
  185. single_loop = true;
  186. }
  187. else if (key_identifier == Rocket::Core::Input::KI_RETURN)
  188. {
  189. run_update = !run_update;
  190. }
  191. else if (key_identifier == Rocket::Core::Input::KI_ESCAPE)
  192. {
  193. Shell::RequestExit();
  194. }
  195. else if (key_identifier == Rocket::Core::Input::KI_F8)
  196. {
  197. Rocket::Debugger::SetVisible(!Rocket::Debugger::IsVisible());
  198. }
  199. }
  200. }
  201. void OnDetach(Rocket::Core::Element* element) override { delete this; }
  202. private:
  203. Rocket::Core::String value;
  204. };
  205. class EventInstancer : public Rocket::Core::EventListenerInstancer
  206. {
  207. public:
  208. /// Instances a new event handle for Invaders.
  209. Rocket::Core::EventListener* InstanceEventListener(const Rocket::Core::String& value, Rocket::Core::Element* element) override
  210. {
  211. return new Event(value);
  212. }
  213. /// Destroys the instancer.
  214. void Release() override { delete this; }
  215. };
  216. #if defined ROCKET_PLATFORM_WIN32
  217. #include <windows.h>
  218. int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTANCE ROCKET_UNUSED_PARAMETER(previous_instance_handle), char* ROCKET_UNUSED_PARAMETER(command_line), int ROCKET_UNUSED_PARAMETER(command_show))
  219. #else
  220. int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv))
  221. #endif
  222. {
  223. #ifdef ROCKET_PLATFORM_WIN32
  224. ROCKET_UNUSED(instance_handle);
  225. ROCKET_UNUSED(previous_instance_handle);
  226. ROCKET_UNUSED(command_line);
  227. ROCKET_UNUSED(command_show);
  228. #else
  229. ROCKET_UNUSED(argc);
  230. ROCKET_UNUSED(argv);
  231. #endif
  232. const int width = 1800;
  233. const int height = 1000;
  234. ShellRenderInterfaceOpenGL opengl_renderer;
  235. shell_renderer = &opengl_renderer;
  236. // Generic OS initialisation, creates a window and attaches OpenGL.
  237. if (!Shell::Initialise("../../Samples/") ||
  238. !Shell::OpenWindow("Benchmark Sample", shell_renderer, width, height, true))
  239. {
  240. Shell::Shutdown();
  241. return -1;
  242. }
  243. // Rocket initialisation.
  244. Rocket::Core::SetRenderInterface(&opengl_renderer);
  245. opengl_renderer.SetViewport(width, height);
  246. ShellSystemInterface system_interface;
  247. Rocket::Core::SetSystemInterface(&system_interface);
  248. Rocket::Core::Initialise();
  249. // Create the main Rocket context and set it on the shell's input layer.
  250. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(width, height));
  251. if (context == NULL)
  252. {
  253. Rocket::Core::Shutdown();
  254. Shell::Shutdown();
  255. return -1;
  256. }
  257. Rocket::Controls::Initialise();
  258. Rocket::Debugger::Initialise(context);
  259. Input::SetContext(context);
  260. shell_renderer->SetContext(context);
  261. EventInstancer* event_instancer = new EventInstancer();
  262. Rocket::Core::Factory::RegisterEventListenerInstancer(event_instancer);
  263. event_instancer->RemoveReference();
  264. Shell::LoadFonts("assets/");
  265. window = new DemoWindow("Benchmark sample", Rocket::Core::Vector2f(81, 100), context);
  266. window->GetDocument()->AddEventListener("keydown", new Event("hello"));
  267. window->GetDocument()->AddEventListener("keyup", new Event("hello"));
  268. window->GetDocument()->AddEventListener("animationend", new Event("hello"));
  269. Shell::EventLoop(GameLoop);
  270. delete window;
  271. // Shutdown Rocket.
  272. context->RemoveReference();
  273. Rocket::Core::Shutdown();
  274. Shell::CloseWindow();
  275. Shell::Shutdown();
  276. return 0;
  277. }