main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 Nuno Silva
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. /*
  28. * Modifed 2013 by Megavolt2013 in order to get the SFML2 sample working
  29. * with the revised libraries of SFML2
  30. * Please check the comments starting with NOTE in the files "main.cpp" and
  31. * "RenderInterfaceSFML.h" if you have trouble building this sample
  32. */
  33. // NOTE: uncomment this only when you want to use the
  34. // OpenGL Extension Wrangler Library (GLEW)
  35. //#include <GL/glew.h>
  36. #include <Rocket/Core.h>
  37. #include "SystemInterfaceSFML.h"
  38. #include "RenderInterfaceSFML.h"
  39. #include <Rocket/Core/Input.h>
  40. #include <Rocket/Debugger/Debugger.h>
  41. #include "ShellFileInterface.h"
  42. int main(int argc, char **argv)
  43. {
  44. #ifdef ROCKET_PLATFORM_WIN32
  45. AllocConsole();
  46. #endif
  47. int window_width = 1024;
  48. int window_height = 768;
  49. sf::RenderWindow MyWindow(sf::VideoMode(window_width, window_height), "libRocket with SFML2", sf::Style::Close);
  50. MyWindow.setVerticalSyncEnabled(true);
  51. #ifdef ENABLE_GLEW
  52. GLenum err = glewInit();
  53. if (GLEW_OK != err)
  54. {
  55. /* Problem: glewInit failed, something is seriously wrong. */
  56. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  57. //...
  58. }
  59. fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  60. #endif
  61. RocketSFMLRenderer Renderer;
  62. RocketSFMLSystemInterface SystemInterface;
  63. // NOTE: if fonts and rml are not found you'll probably have to adjust
  64. // the path information in the string
  65. ShellFileInterface FileInterface("../../assets/");
  66. if(!MyWindow.isOpen())
  67. return 1;
  68. Renderer.SetWindow(&MyWindow);
  69. Rocket::Core::SetFileInterface(&FileInterface);
  70. Rocket::Core::SetRenderInterface(&Renderer);
  71. Rocket::Core::SetSystemInterface(&SystemInterface);
  72. if(!Rocket::Core::Initialise())
  73. return 1;
  74. Rocket::Core::FontDatabase::LoadFontFace("Delicious-Bold.otf");
  75. Rocket::Core::FontDatabase::LoadFontFace("Delicious-BoldItalic.otf");
  76. Rocket::Core::FontDatabase::LoadFontFace("Delicious-Italic.otf");
  77. Rocket::Core::FontDatabase::LoadFontFace("Delicious-Roman.otf");
  78. Rocket::Core::Context *Context = Rocket::Core::CreateContext("default",
  79. Rocket::Core::Vector2i(MyWindow.getSize().x, MyWindow.getSize().y));
  80. Rocket::Debugger::Initialise(Context);
  81. Rocket::Core::ElementDocument *Document = Context->LoadDocument("demo.rml");
  82. if(Document)
  83. {
  84. Document->Show();
  85. Document->RemoveReference();
  86. fprintf(stdout, "\nDocument loaded");
  87. }
  88. else
  89. {
  90. fprintf(stdout, "\nDocument is NULL");
  91. }
  92. while(MyWindow.isOpen())
  93. {
  94. static sf::Event event;
  95. MyWindow.clear();
  96. Context->Render();
  97. MyWindow.display();
  98. while(MyWindow.pollEvent(event))
  99. {
  100. switch(event.type)
  101. {
  102. case sf::Event::Resized:
  103. Renderer.Resize();
  104. break;
  105. case sf::Event::MouseMoved:
  106. Context->ProcessMouseMove(event.mouseMove.x, event.mouseMove.y,
  107. SystemInterface.GetKeyModifiers(&MyWindow));
  108. break;
  109. case sf::Event::MouseButtonPressed:
  110. Context->ProcessMouseButtonDown(event.mouseButton.button,
  111. SystemInterface.GetKeyModifiers(&MyWindow));
  112. break;
  113. case sf::Event::MouseButtonReleased:
  114. Context->ProcessMouseButtonUp(event.mouseButton.button,
  115. SystemInterface.GetKeyModifiers(&MyWindow));
  116. break;
  117. case sf::Event::MouseWheelMoved:
  118. Context->ProcessMouseWheel(-event.mouseWheel.delta,
  119. SystemInterface.GetKeyModifiers(&MyWindow));
  120. break;
  121. case sf::Event::TextEntered:
  122. if (event.text.unicode > 32)
  123. Context->ProcessTextInput(event.text.unicode);
  124. break;
  125. case sf::Event::KeyPressed:
  126. Context->ProcessKeyDown(SystemInterface.TranslateKey(event.key.code),
  127. SystemInterface.GetKeyModifiers(&MyWindow));
  128. break;
  129. case sf::Event::KeyReleased:
  130. if(event.key.code == sf::Keyboard::F8)
  131. {
  132. Rocket::Debugger::SetVisible(!Rocket::Debugger::IsVisible());
  133. };
  134. if(event.key.code == sf::Keyboard::Escape) {
  135. MyWindow.close();
  136. }
  137. Context->ProcessKeyUp(SystemInterface.TranslateKey(event.key.code),
  138. SystemInterface.GetKeyModifiers(&MyWindow));
  139. break;
  140. case sf::Event::Closed:
  141. MyWindow.close();
  142. break;
  143. };
  144. };
  145. Context->Update();
  146. };
  147. Context->RemoveReference();
  148. Rocket::Core::Shutdown();
  149. return 0;
  150. };