main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. class DemoWindow
  37. {
  38. public:
  39. DemoWindow(const Rml::Core::String &title, const Rml::Core::Vector2f &position, Rml::Core::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::Core::PropertyId::Left, Rml::Core::Property(position.x, Rml::Core::Property::PX));
  46. document->SetProperty(Rml::Core::PropertyId::Top, Rml::Core::Property(position.y, Rml::Core::Property::PX));
  47. document->Show();
  48. }
  49. }
  50. ~DemoWindow()
  51. {
  52. if (document)
  53. document->Close();
  54. }
  55. void SetPerspective(float distance)
  56. {
  57. if (document)
  58. {
  59. std::stringstream s;
  60. s << distance << "px";
  61. document->SetProperty("perspective", s.str().c_str());
  62. }
  63. }
  64. void SetPerspectiveOrigin(float x, float y)
  65. {
  66. if (document)
  67. {
  68. std::stringstream s;
  69. s << x * 100 << "%" << " " << y * 100 << "%";
  70. document->SetProperty("perspective-origin", s.str().c_str());
  71. }
  72. }
  73. void SetRotation(float degrees)
  74. {
  75. if (document)
  76. {
  77. std::stringstream s;
  78. s << "rotate3d(0.0, 1.0, 0.0, " << degrees << ")";
  79. document->SetProperty("transform", s.str().c_str());
  80. }
  81. }
  82. private:
  83. Rml::Core::ElementDocument *document;
  84. };
  85. Rml::Core::Context* context = NULL;
  86. ShellRenderInterfaceExtensions *shell_renderer;
  87. DemoWindow* window_1 = NULL;
  88. DemoWindow* window_2 = NULL;
  89. void GameLoop()
  90. {
  91. context->Update();
  92. shell_renderer->PrepareRenderBuffer();
  93. context->Render();
  94. shell_renderer->PresentRenderBuffer();
  95. static float deg = 0;
  96. Rml::Core::SystemInterface* system_interface = Rml::Core::GetSystemInterface();
  97. deg = (float)std::fmod(system_interface->GetElapsedTime() * 30.0, 360.0);
  98. if (window_1)
  99. {
  100. window_1->SetRotation(deg);
  101. }
  102. if (window_2)
  103. {
  104. window_2->SetRotation(deg);
  105. }
  106. }
  107. #if defined RMLUI_PLATFORM_WIN32
  108. #include <windows.h>
  109. 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))
  110. #else
  111. int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
  112. #endif
  113. {
  114. #ifdef RMLUI_PLATFORM_WIN32
  115. RMLUI_UNUSED(instance_handle);
  116. RMLUI_UNUSED(previous_instance_handle);
  117. RMLUI_UNUSED(command_line);
  118. RMLUI_UNUSED(command_show);
  119. #else
  120. RMLUI_UNUSED(argc);
  121. RMLUI_UNUSED(argv);
  122. #endif
  123. auto opengl_renderer = std::make_shared<ShellRenderInterfaceOpenGL>();
  124. shell_renderer = opengl_renderer.get();
  125. // Generic OS initialisation, creates a window and attaches OpenGL.
  126. if (!Shell::Initialise() ||
  127. !Shell::OpenWindow("Transform Sample", shell_renderer, 1024, 768, true))
  128. {
  129. Shell::Shutdown();
  130. return -1;
  131. }
  132. // RmlUi initialisation.
  133. Rml::Core::SetRenderInterface(opengl_renderer);
  134. opengl_renderer->SetViewport(1024,768);
  135. ShellSystemInterface system_interface;
  136. Rml::Core::SetSystemInterface(std::make_shared<ShellSystemInterface>());
  137. Rml::Core::Initialise();
  138. // Create the main RmlUi context and set it on the shell's input layer.
  139. context = Rml::Core::CreateContext("main", Rml::Core::Vector2i(1024, 768));
  140. if (context == NULL)
  141. {
  142. Rml::Core::Shutdown();
  143. Shell::Shutdown();
  144. return -1;
  145. }
  146. Rml::Controls::Initialise();
  147. Rml::Debugger::Initialise(context);
  148. Input::SetContext(context);
  149. shell_renderer->SetContext(context);
  150. Shell::LoadFonts("assets/");
  151. window_1 = new DemoWindow("Orthographic transform", Rml::Core::Vector2f(81, 200), context);
  152. if (window_1)
  153. {
  154. window_1->SetPerspective(100000);
  155. }
  156. window_2 = new DemoWindow("Perspective transform", Rml::Core::Vector2f(593, 200), context);
  157. if (window_2)
  158. {
  159. window_2->SetPerspective(800);
  160. window_2->SetPerspectiveOrigin(0.5, 0.75);
  161. }
  162. Shell::EventLoop(GameLoop);
  163. delete window_1;
  164. delete window_2;
  165. // Shutdown RmlUi.
  166. Rml::Core::Shutdown();
  167. Shell::CloseWindow();
  168. Shell::Shutdown();
  169. return 0;
  170. }