main.cpp 5.7 KB

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