main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <Rocket/Core.h>
  28. #include "SystemInterfaceSFML.h"
  29. #include "RenderInterfaceSFML.h"
  30. #include <Rocket/Core/Input.h>
  31. #include <Rocket/Debugger/Debugger.h>
  32. #include "ShellFileInterface.h"
  33. int main(int argc, char **argv)
  34. {
  35. #ifdef ROCKET_PLATFORM_WIN32
  36. DoAllocConsole();
  37. #endif
  38. int window_width = 1024;
  39. int window_height = 768;
  40. sf::RenderWindow MyWindow(sf::VideoMode(window_width, window_height), "libRocket with SFML");
  41. RocketSFMLRenderer Renderer;
  42. RocketSFMLSystemInterface SystemInterface;
  43. ShellFileInterface FileInterface("../Samples/assets/");
  44. if(!MyWindow.IsOpened())
  45. return 1;
  46. Renderer.SetWindow(&MyWindow);
  47. Rocket::Core::SetFileInterface(&FileInterface);
  48. Rocket::Core::SetRenderInterface(&Renderer);
  49. Rocket::Core::SetSystemInterface(&SystemInterface);
  50. if(!Rocket::Core::Initialise())
  51. return 1;
  52. Rocket::Core::FontDatabase::LoadFontFace("Delicious-Bold.otf");
  53. Rocket::Core::FontDatabase::LoadFontFace("Delicious-BoldItalic.otf");
  54. Rocket::Core::FontDatabase::LoadFontFace("Delicious-Italic.otf");
  55. Rocket::Core::FontDatabase::LoadFontFace("Delicious-Roman.otf");
  56. Rocket::Core::Context *Context = Rocket::Core::CreateContext("default",
  57. Rocket::Core::Vector2i(MyWindow.GetWidth(), MyWindow.GetHeight()));
  58. Rocket::Debugger::Initialise(Context);
  59. Rocket::Core::ElementDocument *Document = Context->LoadDocument("demo.rml");
  60. if(Document)
  61. {
  62. Document->Show();
  63. Document->RemoveReference();
  64. };
  65. while(MyWindow.IsOpened())
  66. {
  67. static sf::Event event;
  68. MyWindow.Clear();
  69. Context->Render();
  70. MyWindow.Display();
  71. while(MyWindow.GetEvent(event))
  72. {
  73. switch(event.Type)
  74. {
  75. case sf::Event::Resized:
  76. Renderer.Resize();
  77. break;
  78. case sf::Event::MouseMoved:
  79. Context->ProcessMouseMove(event.MouseMove.X, event.MouseMove.Y,
  80. SystemInterface.GetKeyModifiers(&MyWindow));
  81. break;
  82. case sf::Event::MouseButtonPressed:
  83. Context->ProcessMouseButtonDown(event.MouseButton.Button,
  84. SystemInterface.GetKeyModifiers(&MyWindow));
  85. break;
  86. case sf::Event::MouseButtonReleased:
  87. Context->ProcessMouseButtonUp(event.MouseButton.Button,
  88. SystemInterface.GetKeyModifiers(&MyWindow));
  89. break;
  90. case sf::Event::MouseWheelMoved:
  91. Context->ProcessMouseWheel(event.MouseWheel.Delta,
  92. SystemInterface.GetKeyModifiers(&MyWindow));
  93. break;
  94. case sf::Event::TextEntered:
  95. if (event.Text.Unicode > 32)
  96. Context->ProcessTextInput(event.Text.Unicode);
  97. break;
  98. case sf::Event::KeyPressed:
  99. Context->ProcessKeyDown(SystemInterface.TranslateKey(event.Key.Code),
  100. SystemInterface.GetKeyModifiers(&MyWindow));
  101. break;
  102. case sf::Event::KeyReleased:
  103. if(event.Key.Code == sf::Key::F8)
  104. {
  105. Rocket::Debugger::SetVisible(!Rocket::Debugger::IsVisible());
  106. };
  107. Context->ProcessKeyUp(SystemInterface.TranslateKey(event.Key.Code),
  108. SystemInterface.GetKeyModifiers(&MyWindow));
  109. break;
  110. case sf::Event::Closed:
  111. return 1;
  112. break;
  113. };
  114. };
  115. Context->Update();
  116. };
  117. return 0;
  118. };