main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. int main(int argc, char **argv)
  45. {
  46. #ifdef RMLUI_PLATFORM_WIN32
  47. AllocConsole();
  48. #endif
  49. int window_width = 1024;
  50. int window_height = 768;
  51. sf::RenderWindow MyWindow(sf::VideoMode(window_width, window_height), "RmlUi with SFML2", sf::Style::Close);
  52. MyWindow.setVerticalSyncEnabled(true);
  53. #ifdef ENABLE_GLEW
  54. GLenum err = glewInit();
  55. if (GLEW_OK != err)
  56. {
  57. /* Problem: glewInit failed, something is seriously wrong. */
  58. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  59. //...
  60. }
  61. fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  62. #endif
  63. RmlUiSFMLRenderer Renderer;
  64. RmlUiSFMLSystemInterface SystemInterface;
  65. // NOTE: if fonts and rml are not found you'll probably have to adjust
  66. // the path information in the string
  67. Rml::Core::String root = Shell::FindSamplesRoot();
  68. ShellFileInterface FileInterface(root);
  69. if(!MyWindow.isOpen())
  70. return 1;
  71. Renderer.SetWindow(&MyWindow);
  72. Rml::Core::SetFileInterface(&FileInterface);
  73. Rml::Core::SetRenderInterface(&Renderer);
  74. Rml::Core::SetSystemInterface(&SystemInterface);
  75. if(!Rml::Core::Initialise())
  76. return 1;
  77. Rml::Core::FontDatabase::LoadFontFace("assets/Delicious-Bold.otf");
  78. Rml::Core::FontDatabase::LoadFontFace("assets/Delicious-BoldItalic.otf");
  79. Rml::Core::FontDatabase::LoadFontFace("assets/Delicious-Italic.otf");
  80. Rml::Core::FontDatabase::LoadFontFace("assets/Delicious-Roman.otf");
  81. Rml::Core::Context *Context = Rml::Core::CreateContext("default",
  82. Rml::Core::Vector2i(MyWindow.getSize().x, MyWindow.getSize().y));
  83. Rml::Debugger::Initialise(Context);
  84. Rml::Core::ElementDocument *Document = Context->LoadDocument("assets/demo.rml");
  85. if(Document)
  86. {
  87. Document->Show();
  88. fprintf(stdout, "\nDocument loaded");
  89. }
  90. else
  91. {
  92. fprintf(stdout, "\nDocument is nullptr");
  93. }
  94. while(MyWindow.isOpen())
  95. {
  96. static sf::Event event;
  97. MyWindow.clear();
  98. Context->Render();
  99. MyWindow.display();
  100. while(MyWindow.pollEvent(event))
  101. {
  102. switch(event.type)
  103. {
  104. case sf::Event::Resized:
  105. Renderer.Resize();
  106. break;
  107. case sf::Event::MouseMoved:
  108. Context->ProcessMouseMove(event.mouseMove.x, event.mouseMove.y,
  109. SystemInterface.GetKeyModifiers(&MyWindow));
  110. break;
  111. case sf::Event::MouseButtonPressed:
  112. Context->ProcessMouseButtonDown(event.mouseButton.button,
  113. SystemInterface.GetKeyModifiers(&MyWindow));
  114. break;
  115. case sf::Event::MouseButtonReleased:
  116. Context->ProcessMouseButtonUp(event.mouseButton.button,
  117. SystemInterface.GetKeyModifiers(&MyWindow));
  118. break;
  119. case sf::Event::MouseWheelMoved:
  120. Context->ProcessMouseWheel(-event.mouseWheel.delta,
  121. SystemInterface.GetKeyModifiers(&MyWindow));
  122. break;
  123. case sf::Event::TextEntered:
  124. if (event.text.unicode > 32)
  125. Context->ProcessTextInput(event.text.unicode);
  126. break;
  127. case sf::Event::KeyPressed:
  128. Context->ProcessKeyDown(SystemInterface.TranslateKey(event.key.code),
  129. SystemInterface.GetKeyModifiers(&MyWindow));
  130. break;
  131. case sf::Event::KeyReleased:
  132. if(event.key.code == sf::Keyboard::F8)
  133. {
  134. Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
  135. };
  136. if(event.key.code == sf::Keyboard::Escape) {
  137. MyWindow.close();
  138. }
  139. Context->ProcessKeyUp(SystemInterface.TranslateKey(event.key.code),
  140. SystemInterface.GetKeyModifiers(&MyWindow));
  141. break;
  142. case sf::Event::Closed:
  143. MyWindow.close();
  144. break;
  145. default:
  146. break;
  147. };
  148. };
  149. Context->Update();
  150. };
  151. Rml::Core::Shutdown();
  152. return 0;
  153. };