main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. struct FontFace {
  89. Rml::String filename;
  90. bool fallback_face;
  91. };
  92. FontFace font_faces[] = {
  93. { "LatoLatin-Regular.ttf", false },
  94. { "LatoLatin-Italic.ttf", false },
  95. { "LatoLatin-Bold.ttf", false },
  96. { "LatoLatin-BoldItalic.ttf", false },
  97. { "NotoEmoji-Regular.ttf", true },
  98. };
  99. for (const FontFace& face : font_faces)
  100. {
  101. Rml::LoadFontFace("assets/" + face.filename, face.fallback_face);
  102. }
  103. Rml::Context* Context = Rml::CreateContext("default",
  104. Rml::Vector2i(MyWindow.getSize().x, MyWindow.getSize().y));
  105. Rml::Debugger::Initialise(Context);
  106. Rml::ElementDocument* Document = Context->LoadDocument("assets/demo.rml");
  107. if (Document)
  108. {
  109. Document->Show();
  110. fprintf(stdout, "\nDocument loaded");
  111. }
  112. else
  113. {
  114. fprintf(stdout, "\nDocument is nullptr");
  115. }
  116. while (MyWindow.isOpen())
  117. {
  118. static sf::Event event;
  119. MyWindow.clear();
  120. sf::CircleShape circle(50.f);
  121. circle.setPosition(100.f, 100.f);
  122. circle.setFillColor(sf::Color::Blue);
  123. circle.setOutlineColor(sf::Color::Red);
  124. circle.setOutlineThickness(10.f);
  125. MyWindow.draw(circle);
  126. Context->Render();
  127. MyWindow.display();
  128. while (MyWindow.pollEvent(event))
  129. {
  130. switch (event.type)
  131. {
  132. case sf::Event::Resized:
  133. updateView(MyWindow, view);
  134. break;
  135. case sf::Event::MouseMoved:
  136. Context->ProcessMouseMove(event.mouseMove.x, event.mouseMove.y,
  137. SystemInterface.GetKeyModifiers());
  138. break;
  139. case sf::Event::MouseButtonPressed:
  140. Context->ProcessMouseButtonDown(event.mouseButton.button,
  141. SystemInterface.GetKeyModifiers());
  142. break;
  143. case sf::Event::MouseButtonReleased:
  144. Context->ProcessMouseButtonUp(event.mouseButton.button,
  145. SystemInterface.GetKeyModifiers());
  146. break;
  147. case sf::Event::MouseWheelMoved:
  148. Context->ProcessMouseWheel(float(-event.mouseWheel.delta),
  149. SystemInterface.GetKeyModifiers());
  150. break;
  151. case sf::Event::TextEntered:
  152. if (event.text.unicode > 32)
  153. Context->ProcessTextInput(Rml::Character(event.text.unicode));
  154. break;
  155. case sf::Event::KeyPressed:
  156. Context->ProcessKeyDown(SystemInterface.TranslateKey(event.key.code),
  157. SystemInterface.GetKeyModifiers());
  158. break;
  159. case sf::Event::KeyReleased:
  160. switch (event.key.code)
  161. {
  162. case sf::Keyboard::Num1:
  163. multiplier = 2.f;
  164. updateView(MyWindow, view);
  165. break;
  166. case sf::Keyboard::Num2:
  167. multiplier = 1.f;
  168. updateView(MyWindow, view);
  169. break;
  170. case sf::Keyboard::Num3:
  171. multiplier = .5f;
  172. updateView(MyWindow, view);
  173. break;
  174. case sf::Keyboard::F8:
  175. Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
  176. break;
  177. case sf::Keyboard::Escape:
  178. MyWindow.close();
  179. break;
  180. default:
  181. break;
  182. }
  183. Context->ProcessKeyUp(SystemInterface.TranslateKey(event.key.code),
  184. SystemInterface.GetKeyModifiers());
  185. break;
  186. case sf::Event::Closed:
  187. MyWindow.close();
  188. break;
  189. default:
  190. break;
  191. };
  192. };
  193. Context->Update();
  194. };
  195. Rml::Shutdown();
  196. return 0;
  197. }