main.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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-2023 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. public:
  34. DemoWindow(const Rml::String& title, Rml::Context* context)
  35. {
  36. using namespace Rml;
  37. document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  38. if (document != nullptr)
  39. {
  40. {
  41. document->GetElementById("title")->SetInnerRML(title);
  42. }
  43. document->Show();
  44. }
  45. }
  46. void performance_test()
  47. {
  48. RMLUI_ZoneScoped;
  49. if (!document)
  50. return;
  51. Rml::String rml;
  52. for (int i = 0; i < 50; i++)
  53. {
  54. int index = rand() % 1000;
  55. int route = rand() % 50;
  56. int max = (rand() % 40) + 10;
  57. int value = rand() % max;
  58. Rml::String rml_row = Rml::CreateString(1000, R"(
  59. <div class="row">
  60. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  61. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  62. <div class="col col4">Assigned</div>
  63. <select>
  64. <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
  65. </select>
  66. <div class="inrow unmark_collapse">
  67. <div class="col col123 assign_text">Assign to route</div>
  68. <div class="col col4">
  69. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  70. </div>
  71. </div>
  72. </div>)",
  73. index, route, max, value);
  74. rml += rml_row;
  75. }
  76. if (auto el = document->GetElementById("performance"))
  77. el->SetInnerRML(rml);
  78. }
  79. class SimpleEventListener : public Rml::EventListener {
  80. public:
  81. void ProcessEvent(Rml::Event& event) override
  82. {
  83. static int i = 0;
  84. event.GetTargetElement()->SetProperty("background-color", i++ % 2 == 0 ? "green" : "orange");
  85. }
  86. } simple_event_listener;
  87. ~DemoWindow()
  88. {
  89. if (document)
  90. {
  91. document->Close();
  92. }
  93. }
  94. Rml::ElementDocument* GetDocument() { return document; }
  95. private:
  96. Rml::ElementDocument* document;
  97. };
  98. bool run_loop = true;
  99. bool single_loop = true;
  100. bool run_update = true;
  101. bool single_update = true;
  102. class Event : public Rml::EventListener {
  103. public:
  104. Event(const Rml::String& value) : value(value) {}
  105. void ProcessEvent(Rml::Event& event) override
  106. {
  107. using namespace Rml;
  108. if (value == "exit")
  109. Backend::RequestExit();
  110. if (event == "keydown")
  111. {
  112. auto key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter<int>("key_identifier", 0);
  113. if (key_identifier == Rml::Input::KI_SPACE)
  114. {
  115. run_loop = !run_loop;
  116. }
  117. else if (key_identifier == Rml::Input::KI_DOWN)
  118. {
  119. run_loop = false;
  120. single_loop = true;
  121. }
  122. else if (key_identifier == Rml::Input::KI_RIGHT)
  123. {
  124. run_update = false;
  125. single_update = true;
  126. }
  127. else if (key_identifier == Rml::Input::KI_RETURN)
  128. {
  129. run_update = !run_update;
  130. }
  131. else if (key_identifier == Rml::Input::KI_ESCAPE)
  132. {
  133. Backend::RequestExit();
  134. }
  135. }
  136. }
  137. void OnDetach(Rml::Element* /*element*/) override { delete this; }
  138. private:
  139. Rml::String value;
  140. };
  141. class EventInstancer : public Rml::EventListenerInstancer {
  142. public:
  143. /// Instances a new event handle for Invaders.
  144. Rml::EventListener* InstanceEventListener(const Rml::String& value, Rml::Element* /*element*/) override { return new Event(value); }
  145. };
  146. #if defined RMLUI_PLATFORM_WIN32
  147. #include <RmlUi_Include_Windows.h>
  148. int APIENTRY WinMain(HINSTANCE /*instance_handle*/, HINSTANCE /*previous_instance_handle*/, char* /*command_line*/, int /*command_show*/)
  149. #else
  150. int main(int /*argc*/, char** /*argv*/)
  151. #endif
  152. {
  153. const int window_width = 1800;
  154. const int window_height = 1000;
  155. // Initializes the shell which provides common functionality used by the included samples.
  156. if (!Shell::Initialize())
  157. return -1;
  158. // Constructs the system and render interfaces, creates a window, and attaches the renderer.
  159. if (!Backend::Initialize("Benchmark Sample", window_width, window_height, true))
  160. {
  161. Shell::Shutdown();
  162. return -1;
  163. }
  164. // Install the custom interfaces constructed by the backend before initializing RmlUi.
  165. Rml::SetSystemInterface(Backend::GetSystemInterface());
  166. Rml::SetRenderInterface(Backend::GetRenderInterface());
  167. // RmlUi initialisation.
  168. Rml::Initialise();
  169. // Create the main RmlUi context.
  170. Rml::Context* context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
  171. if (!context)
  172. {
  173. Rml::Shutdown();
  174. Backend::Shutdown();
  175. Shell::Shutdown();
  176. return -1;
  177. }
  178. Rml::Debugger::Initialise(context);
  179. EventInstancer event_listener_instancer;
  180. Rml::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
  181. Shell::LoadFonts();
  182. Rml::UniquePtr<DemoWindow> window = Rml::MakeUnique<DemoWindow>("Benchmark sample", context);
  183. window->GetDocument()->AddEventListener(Rml::EventId::Keydown, new Event("hello"));
  184. window->GetDocument()->AddEventListener(Rml::EventId::Keyup, new Event("hello"));
  185. window->GetDocument()->AddEventListener(Rml::EventId::Animationend, new Event("hello"));
  186. bool running = true;
  187. while (running)
  188. {
  189. running = Backend::ProcessEvents(context, &Shell::ProcessKeyDownShortcuts);
  190. if (run_update || single_update)
  191. {
  192. single_update = false;
  193. window->performance_test();
  194. }
  195. if (run_loop || single_loop)
  196. {
  197. single_loop = false;
  198. context->Update();
  199. Backend::BeginFrame();
  200. context->Render();
  201. Backend::PresentFrame();
  202. }
  203. static constexpr int buffer_size = 200;
  204. static float fps_buffer[buffer_size] = {};
  205. static int buffer_index = 0;
  206. static double t_prev = 0.0f;
  207. double t = Rml::GetSystemInterface()->GetElapsedTime();
  208. float dt = float(t - t_prev);
  209. t_prev = t;
  210. static int count_frames = 0;
  211. count_frames += 1;
  212. float fps = 1.0f / dt;
  213. fps_buffer[buffer_index] = fps;
  214. buffer_index = ((buffer_index + 1) % buffer_size);
  215. if (window && count_frames > buffer_size / 8)
  216. {
  217. float fps_mean = 0;
  218. for (int i = 0; i < buffer_size; i++)
  219. fps_mean += fps_buffer[(buffer_index + i) % buffer_size];
  220. fps_mean = fps_mean / (float)buffer_size;
  221. auto el = window->GetDocument()->GetElementById("fps");
  222. count_frames = 0;
  223. el->SetInnerRML(Rml::CreateString(20, "FPS: %f", fps_mean));
  224. }
  225. }
  226. window.reset();
  227. // Shutdown RmlUi.
  228. Rml::Shutdown();
  229. Backend::Shutdown();
  230. Shell::Shutdown();
  231. return 0;
  232. }