main.cpp 6.0 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) 2008-2010 Nuno Silva
  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. /*
  29. * Modifed 2013 by Megavolt2013 in order to get the SFML2 sample working
  30. * with the revised libraries of SFML2
  31. * Please check the comments starting with NOTE in the files "main.cpp" and
  32. * "RenderInterfaceSFML.h" if you have trouble building this sample
  33. */
  34. // NOTE: uncomment this only when you want to use the
  35. // OpenGL Extension Wrangler Library (GLEW)
  36. //#include <GL/glew.h>
  37. #include <RmlUi/Core.h>
  38. #include "SystemInterfaceSFML.h"
  39. #include "RenderInterfaceSFML.h"
  40. #include <RmlUi/Core/Input.h>
  41. #include <RmlUi/Debugger/Debugger.h>
  42. #include <Shell.h>
  43. #include <ShellFileInterface.h>
  44. #ifdef RMLUI_PLATFORM_WIN32
  45. #include <windows.h>
  46. #endif
  47. float multiplier = 1.f;
  48. void updateView(sf::RenderWindow& window, sf::View& view)
  49. {
  50. view.reset(sf::FloatRect(0.f, 0.f, window.getSize().x * multiplier, window.getSize().y * multiplier));
  51. window.setView(view);
  52. }
  53. int main(int /*argc*/, char** /*argv*/)
  54. {
  55. #ifdef RMLUI_PLATFORM_WIN32
  56. AllocConsole();
  57. #endif
  58. int window_width = 1024;
  59. int window_height = 768;
  60. sf::RenderWindow MyWindow(sf::VideoMode(window_width, window_height), "RmlUi with SFML2");
  61. MyWindow.setVerticalSyncEnabled(true);
  62. #ifdef ENABLE_GLEW
  63. GLenum err = glewInit();
  64. if (GLEW_OK != err)
  65. {
  66. /* Problem: glewInit failed, something is seriously wrong. */
  67. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  68. //...
  69. }
  70. fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  71. #endif
  72. RmlUiSFMLRenderer Renderer;
  73. RmlUiSFMLSystemInterface SystemInterface;
  74. // NOTE: if fonts and rml are not found you'll probably have to adjust
  75. // the path information in the string
  76. Rml::String root = Shell::FindSamplesRoot();
  77. ShellFileInterface FileInterface(root);
  78. if (!MyWindow.isOpen())
  79. return 1;
  80. sf::View view(sf::FloatRect(0.f, 0.f, (float)MyWindow.getSize().x, (float)MyWindow.getSize().y));
  81. MyWindow.setView(view);
  82. Renderer.SetWindow(&MyWindow);
  83. Rml::SetFileInterface(&FileInterface);
  84. Rml::SetRenderInterface(&Renderer);
  85. Rml::SetSystemInterface(&SystemInterface);
  86. if (!Rml::Initialise())
  87. return 1;
  88. Rml::LoadFontFace("assets/Delicious-Bold.otf");
  89. Rml::LoadFontFace("assets/Delicious-BoldItalic.otf");
  90. Rml::LoadFontFace("assets/Delicious-Italic.otf");
  91. Rml::LoadFontFace("assets/Delicious-Roman.otf");
  92. Rml::Context* Context = Rml::CreateContext("default",
  93. Rml::Vector2i(MyWindow.getSize().x, MyWindow.getSize().y));
  94. Rml::Debugger::Initialise(Context);
  95. Rml::ElementDocument* Document = Context->LoadDocument("assets/demo.rml");
  96. if (Document)
  97. {
  98. Document->Show();
  99. fprintf(stdout, "\nDocument loaded");
  100. }
  101. else
  102. {
  103. fprintf(stdout, "\nDocument is nullptr");
  104. }
  105. while (MyWindow.isOpen())
  106. {
  107. static sf::Event event;
  108. MyWindow.clear();
  109. sf::CircleShape circle(50.f);
  110. circle.setPosition(100.f, 100.f);
  111. circle.setFillColor(sf::Color::Blue);
  112. circle.setOutlineColor(sf::Color::Red);
  113. circle.setOutlineThickness(10.f);
  114. MyWindow.draw(circle);
  115. Context->Render();
  116. MyWindow.display();
  117. while (MyWindow.pollEvent(event))
  118. {
  119. switch (event.type)
  120. {
  121. case sf::Event::Resized:
  122. updateView(MyWindow, view);
  123. break;
  124. case sf::Event::MouseMoved:
  125. Context->ProcessMouseMove(event.mouseMove.x, event.mouseMove.y,
  126. SystemInterface.GetKeyModifiers());
  127. break;
  128. case sf::Event::MouseButtonPressed:
  129. Context->ProcessMouseButtonDown(event.mouseButton.button,
  130. SystemInterface.GetKeyModifiers());
  131. break;
  132. case sf::Event::MouseButtonReleased:
  133. Context->ProcessMouseButtonUp(event.mouseButton.button,
  134. SystemInterface.GetKeyModifiers());
  135. break;
  136. case sf::Event::MouseWheelMoved:
  137. Context->ProcessMouseWheel(float(-event.mouseWheel.delta),
  138. SystemInterface.GetKeyModifiers());
  139. break;
  140. case sf::Event::TextEntered:
  141. if (event.text.unicode > 32)
  142. Context->ProcessTextInput(Rml::Character(event.text.unicode));
  143. break;
  144. case sf::Event::KeyPressed:
  145. Context->ProcessKeyDown(SystemInterface.TranslateKey(event.key.code),
  146. SystemInterface.GetKeyModifiers());
  147. break;
  148. case sf::Event::KeyReleased:
  149. switch (event.key.code)
  150. {
  151. case sf::Keyboard::Num1:
  152. multiplier = 2.f;
  153. updateView(MyWindow, view);
  154. break;
  155. case sf::Keyboard::Num2:
  156. multiplier = 1.f;
  157. updateView(MyWindow, view);
  158. break;
  159. case sf::Keyboard::Num3:
  160. multiplier = .5f;
  161. updateView(MyWindow, view);
  162. break;
  163. case sf::Keyboard::F8:
  164. Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
  165. break;
  166. case sf::Keyboard::Escape:
  167. MyWindow.close();
  168. break;
  169. default:
  170. break;
  171. }
  172. Context->ProcessKeyUp(SystemInterface.TranslateKey(event.key.code),
  173. SystemInterface.GetKeyModifiers());
  174. break;
  175. case sf::Event::Closed:
  176. MyWindow.close();
  177. break;
  178. default:
  179. break;
  180. };
  181. };
  182. Context->Update();
  183. };
  184. Rml::Shutdown();
  185. return 0;
  186. }