main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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) 2014 Markus Schöngart
  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/Controls.h>
  30. #include <RmlUi/Debugger.h>
  31. #include <Input.h>
  32. #include <Shell.h>
  33. #include <ShellRenderInterfaceOpenGL.h>
  34. #include <cmath>
  35. #include <sstream>
  36. static bool run_rotate = true;
  37. class DemoWindow : public Rml::Core::EventListener
  38. {
  39. public:
  40. DemoWindow(const Rml::Core::String &title, const Rml::Core::Vector2f &position, Rml::Core::Context *context)
  41. {
  42. document = context->LoadDocument("basic/transform/data/transform.rml");
  43. if (document)
  44. {
  45. document->GetElementById("title")->SetInnerRML(title);
  46. document->SetProperty(Rml::Core::PropertyId::Left, Rml::Core::Property(position.x, Rml::Core::Property::PX));
  47. document->SetProperty(Rml::Core::PropertyId::Top, Rml::Core::Property(position.y, Rml::Core::Property::PX));
  48. document->Show();
  49. }
  50. }
  51. ~DemoWindow()
  52. {
  53. if (document)
  54. document->Close();
  55. }
  56. void SetPerspective(float distance)
  57. {
  58. perspective = distance;
  59. if (document && perspective > 0)
  60. {
  61. std::stringstream s;
  62. s << "perspective(" << perspective << "px) ";
  63. document->SetProperty("transform", s.str().c_str());
  64. }
  65. }
  66. void SetRotation(float degrees)
  67. {
  68. if(document)
  69. {
  70. std::stringstream s;
  71. if (perspective > 0)
  72. s << "perspective(" << perspective << "px) ";
  73. s << "rotate3d(0.0, 1.0, 0.0, " << degrees << "deg)";
  74. document->SetProperty("transform", s.str().c_str());
  75. }
  76. }
  77. void ProcessEvent(Rml::Core::Event& ev) override
  78. {
  79. if (ev == Rml::Core::EventId::Keydown)
  80. {
  81. Rml::Core::Input::KeyIdentifier key_identifier = (Rml::Core::Input::KeyIdentifier) ev.GetParameter< int >("key_identifier", 0);
  82. if (key_identifier == Rml::Core::Input::KI_SPACE)
  83. {
  84. run_rotate = !run_rotate;
  85. }
  86. else if (key_identifier == Rml::Core::Input::KI_ESCAPE)
  87. {
  88. Shell::RequestExit();
  89. }
  90. }
  91. }
  92. private:
  93. float perspective = 0;
  94. Rml::Core::ElementDocument *document;
  95. };
  96. Rml::Core::Context* context = nullptr;
  97. ShellRenderInterfaceExtensions* shell_renderer;
  98. DemoWindow* window_1 = nullptr;
  99. DemoWindow* window_2 = nullptr;
  100. void GameLoop()
  101. {
  102. context->Update();
  103. shell_renderer->PrepareRenderBuffer();
  104. context->Render();
  105. shell_renderer->PresentRenderBuffer();
  106. double t = Rml::Core::GetSystemInterface()->GetElapsedTime();
  107. static double t_prev = t;
  108. double dt = t - t_prev;
  109. t_prev = t;
  110. if(run_rotate)
  111. {
  112. static float deg = 0;
  113. deg = (float)std::fmod(deg + dt * 50.0, 360.0);
  114. if (window_1)
  115. window_1->SetRotation(deg);
  116. if (window_2)
  117. window_2->SetRotation(deg);
  118. }
  119. }
  120. #if defined RMLUI_PLATFORM_WIN32
  121. #include <windows.h>
  122. int APIENTRY WinMain(HINSTANCE RMLUI_UNUSED_PARAMETER(instance_handle), HINSTANCE RMLUI_UNUSED_PARAMETER(previous_instance_handle), char* RMLUI_UNUSED_PARAMETER(command_line), int RMLUI_UNUSED_PARAMETER(command_show))
  123. #else
  124. int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
  125. #endif
  126. {
  127. #ifdef RMLUI_PLATFORM_WIN32
  128. RMLUI_UNUSED(instance_handle);
  129. RMLUI_UNUSED(previous_instance_handle);
  130. RMLUI_UNUSED(command_line);
  131. RMLUI_UNUSED(command_show);
  132. #else
  133. RMLUI_UNUSED(argc);
  134. RMLUI_UNUSED(argv);
  135. #endif
  136. constexpr int width = 1600;
  137. constexpr int height = 950;
  138. ShellRenderInterfaceOpenGL opengl_renderer;
  139. shell_renderer = &opengl_renderer;
  140. // Generic OS initialisation, creates a window and attaches OpenGL.
  141. if (!Shell::Initialise() ||
  142. !Shell::OpenWindow("Transform Sample", shell_renderer, width, height, true))
  143. {
  144. Shell::Shutdown();
  145. return -1;
  146. }
  147. // RmlUi initialisation.
  148. Rml::Core::SetRenderInterface(&opengl_renderer);
  149. opengl_renderer.SetViewport(width, height);
  150. ShellSystemInterface system_interface;
  151. Rml::Core::SetSystemInterface(&system_interface);
  152. Rml::Core::Initialise();
  153. // Create the main RmlUi context and set it on the shell's input layer.
  154. context = Rml::Core::CreateContext("main", Rml::Core::Vector2i(width, height));
  155. if (context == nullptr)
  156. {
  157. Rml::Core::Shutdown();
  158. Shell::Shutdown();
  159. return -1;
  160. }
  161. Rml::Controls::Initialise();
  162. Rml::Debugger::Initialise(context);
  163. Input::SetContext(context);
  164. shell_renderer->SetContext(context);
  165. Shell::LoadFonts("assets/");
  166. window_1 = new DemoWindow("Orthographic transform", Rml::Core::Vector2f(120, 180), context);
  167. if (window_1)
  168. {
  169. context->GetRootElement()->AddEventListener(Rml::Core::EventId::Keydown, window_1);
  170. }
  171. window_2 = new DemoWindow("Perspective transform", Rml::Core::Vector2f(900, 180), context);
  172. if (window_2)
  173. {
  174. window_2->SetPerspective(800);
  175. }
  176. Shell::EventLoop(GameLoop);
  177. delete window_1;
  178. delete window_2;
  179. // Shutdown RmlUi.
  180. Rml::Core::Shutdown();
  181. Shell::CloseWindow();
  182. Shell::Shutdown();
  183. return 0;
  184. }