main.cpp 4.0 KB

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