main.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <RmlUi/Core.h>
  29. #include <RmlUi/Debugger.h>
  30. #include <RmlUi_Backend.h>
  31. #include <Shell.h>
  32. class DemoWindow
  33. {
  34. public:
  35. DemoWindow(const Rml::String &title, Rml::Context *context)
  36. {
  37. using namespace Rml;
  38. document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  39. if (document != nullptr)
  40. {
  41. {
  42. document->GetElementById("title")->SetInnerRML(title);
  43. }
  44. document->Show();
  45. }
  46. }
  47. void performance_test()
  48. {
  49. RMLUI_ZoneScoped;
  50. if (!document)
  51. return;
  52. Rml::String rml;
  53. for (int i = 0; i < 50; i++)
  54. {
  55. int index = rand() % 1000;
  56. int route = rand() % 50;
  57. int max = (rand() % 40) + 10;
  58. int value = rand() % max;
  59. Rml::String rml_row = Rml::CreateString(1000, R"(
  60. <div class="row">
  61. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  62. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  63. <div class="col col4">Assigned</div>
  64. <select>
  65. <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
  66. </select>
  67. <div class="inrow unmark_collapse">
  68. <div class="col col123 assign_text">Assign to route</div>
  69. <div class="col col4">
  70. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  71. </div>
  72. </div>
  73. </div>)",
  74. index,
  75. route,
  76. max,
  77. value
  78. );
  79. rml += rml_row;
  80. }
  81. if (auto el = document->GetElementById("performance"))
  82. el->SetInnerRML(rml);
  83. }
  84. class SimpleEventListener : public Rml::EventListener {
  85. public:
  86. void ProcessEvent(Rml::Event& event) override {
  87. static int i = 0;
  88. event.GetTargetElement()->SetProperty("background-color", i++ % 2 == 0 ? "green" : "orange");
  89. }
  90. } simple_event_listener;
  91. ~DemoWindow()
  92. {
  93. if (document)
  94. {
  95. document->Close();
  96. }
  97. }
  98. Rml::ElementDocument * GetDocument() {
  99. return document;
  100. }
  101. private:
  102. Rml::ElementDocument *document;
  103. };
  104. bool run_loop = true;
  105. bool single_loop = true;
  106. bool run_update = true;
  107. bool single_update = true;
  108. class Event : public Rml::EventListener
  109. {
  110. public:
  111. Event(const Rml::String& value) : value(value) {}
  112. void ProcessEvent(Rml::Event& event) override
  113. {
  114. using namespace Rml;
  115. if(value == "exit")
  116. Backend::RequestExit();
  117. if (event == "keydown")
  118. {
  119. auto key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter< int >("key_identifier", 0);
  120. if (key_identifier == Rml::Input::KI_SPACE)
  121. {
  122. run_loop = !run_loop;
  123. }
  124. else if (key_identifier == Rml::Input::KI_DOWN)
  125. {
  126. run_loop = false;
  127. single_loop = true;
  128. }
  129. else if (key_identifier == Rml::Input::KI_RIGHT)
  130. {
  131. run_update = false;
  132. single_update = true;
  133. }
  134. else if (key_identifier == Rml::Input::KI_RETURN)
  135. {
  136. run_update = !run_update;
  137. }
  138. else if (key_identifier == Rml::Input::KI_ESCAPE)
  139. {
  140. Backend::RequestExit();
  141. }
  142. }
  143. }
  144. void OnDetach(Rml::Element* /*element*/) override { delete this; }
  145. private:
  146. Rml::String value;
  147. };
  148. class EventInstancer : public Rml::EventListenerInstancer
  149. {
  150. public:
  151. /// Instances a new event handle for Invaders.
  152. Rml::EventListener* InstanceEventListener(const Rml::String& value, Rml::Element* /*element*/) override
  153. {
  154. return new Event(value);
  155. }
  156. };
  157. #if defined RMLUI_PLATFORM_WIN32
  158. #include <RmlUi_Include_Windows.h>
  159. int APIENTRY WinMain(HINSTANCE /*instance_handle*/, HINSTANCE /*previous_instance_handle*/, char* /*command_line*/, int /*command_show*/)
  160. #else
  161. int main(int /*argc*/, char** /*argv*/)
  162. #endif
  163. {
  164. const int window_width = 1800;
  165. const int window_height = 1000;
  166. // Initializes the shell which provides common functionality used by the included samples.
  167. if (!Shell::Initialize())
  168. return -1;
  169. // Constructs the system and render interfaces, creates a window, and attaches the renderer.
  170. if (!Backend::Initialize("Benchmark Sample", window_width, window_height, true))
  171. {
  172. Shell::Shutdown();
  173. return -1;
  174. }
  175. // Install the custom interfaces constructed by the backend before initializing RmlUi.
  176. Rml::SetSystemInterface(Backend::GetSystemInterface());
  177. Rml::SetRenderInterface(Backend::GetRenderInterface());
  178. // RmlUi initialisation.
  179. Rml::Initialise();
  180. // Create the main RmlUi context.
  181. Rml::Context* context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
  182. if (!context)
  183. {
  184. Rml::Shutdown();
  185. Backend::Shutdown();
  186. Shell::Shutdown();
  187. return -1;
  188. }
  189. Rml::Debugger::Initialise(context);
  190. EventInstancer event_listener_instancer;
  191. Rml::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
  192. Shell::LoadFonts();
  193. Rml::UniquePtr<DemoWindow> window = Rml::MakeUnique<DemoWindow>("Benchmark sample", context);
  194. window->GetDocument()->AddEventListener(Rml::EventId::Keydown, new Event("hello"));
  195. window->GetDocument()->AddEventListener(Rml::EventId::Keyup, new Event("hello"));
  196. window->GetDocument()->AddEventListener(Rml::EventId::Animationend, new Event("hello"));
  197. bool running = true;
  198. while (running)
  199. {
  200. running = Backend::ProcessEvents(context, &Shell::ProcessKeyDownShortcuts);
  201. if (run_update || single_update)
  202. {
  203. single_update = false;
  204. window->performance_test();
  205. }
  206. if (run_loop || single_loop)
  207. {
  208. single_loop = false;
  209. context->Update();
  210. Backend::BeginFrame();
  211. context->Render();
  212. Backend::PresentFrame();
  213. }
  214. static constexpr int buffer_size = 200;
  215. static float fps_buffer[buffer_size] = {};
  216. static int buffer_index = 0;
  217. static double t_prev = 0.0f;
  218. double t = Rml::GetSystemInterface()->GetElapsedTime();
  219. float dt = float(t - t_prev);
  220. t_prev = t;
  221. static int count_frames = 0;
  222. count_frames += 1;
  223. float fps = 1.0f / dt;
  224. fps_buffer[buffer_index] = fps;
  225. buffer_index = ((buffer_index + 1) % buffer_size);
  226. if (window && count_frames > buffer_size / 8)
  227. {
  228. float fps_mean = 0;
  229. for (int i = 0; i < buffer_size; i++)
  230. fps_mean += fps_buffer[(buffer_index + i) % buffer_size];
  231. fps_mean = fps_mean / (float)buffer_size;
  232. auto el = window->GetDocument()->GetElementById("fps");
  233. count_frames = 0;
  234. el->SetInnerRML(Rml::CreateString(20, "FPS: %f", fps_mean));
  235. }
  236. }
  237. window.reset();
  238. // Shutdown RmlUi.
  239. Rml::Shutdown();
  240. Backend::Shutdown();
  241. Shell::Shutdown();
  242. return 0;
  243. }