main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <RmlUi/Core.h>
  29. #include <RmlUi/Core/Input.h>
  30. #include <RmlUi/Debugger/Debugger.h>
  31. #include <Shell.h>
  32. #include <ShellFileInterface.h>
  33. #include <string.h>
  34. #include "SystemInterfaceSDL2.h"
  35. #include "RenderInterfaceSDL2.h"
  36. #ifdef RMLUI_PLATFORM_WIN32
  37. #include <windows.h>
  38. #endif
  39. #include <SDL.h>
  40. int main(int /*argc*/, char** /*argv*/)
  41. {
  42. #ifdef RMLUI_PLATFORM_WIN32
  43. AllocConsole();
  44. #endif
  45. int window_width = 1024;
  46. int window_height = 768;
  47. SDL_Init( SDL_INIT_VIDEO );
  48. SDL_Window * screen = SDL_CreateWindow("RmlUi SDL2 with SDL_Renderer test", 20, 20, window_width, window_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
  49. /*
  50. * Force a specific back-end
  51. SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
  52. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
  53. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
  54. */
  55. SDL_Renderer * renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  56. RmlUiSDL2Renderer Renderer(renderer, screen);
  57. RmlUiSDL2SystemInterface SystemInterface;
  58. Rml::String root = Shell::FindSamplesRoot();
  59. ShellFileInterface FileInterface(root);
  60. Rml::SetFileInterface(&FileInterface);
  61. Rml::SetRenderInterface(&Renderer);
  62. Rml::SetSystemInterface(&SystemInterface);
  63. if (!Rml::Initialise())
  64. return 1;
  65. struct FontFace {
  66. Rml::String filename;
  67. bool fallback_face;
  68. };
  69. FontFace font_faces[] = {
  70. { "LatoLatin-Regular.ttf", false },
  71. { "LatoLatin-Italic.ttf", false },
  72. { "LatoLatin-Bold.ttf", false },
  73. { "LatoLatin-BoldItalic.ttf", false },
  74. { "NotoEmoji-Regular.ttf", true },
  75. };
  76. for (const FontFace& face : font_faces)
  77. {
  78. Rml::LoadFontFace("assets/" + face.filename, face.fallback_face);
  79. }
  80. Rml::Context* Context = Rml::CreateContext("default",
  81. Rml::Vector2i(window_width, window_height));
  82. Rml::Debugger::Initialise(Context);
  83. Rml::ElementDocument* Document = Context->LoadDocument("assets/demo.rml");
  84. if (Document)
  85. {
  86. Document->Show();
  87. fprintf(stdout, "\nDocument loaded");
  88. }
  89. else
  90. {
  91. fprintf(stdout, "\nDocument is nullptr");
  92. }
  93. bool done = false;
  94. while (!done)
  95. {
  96. SDL_Event event;
  97. SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
  98. SDL_RenderClear(renderer);
  99. Context->Render();
  100. SDL_RenderPresent(renderer);
  101. while (SDL_PollEvent(&event))
  102. {
  103. switch (event.type)
  104. {
  105. case SDL_QUIT:
  106. done = true;
  107. break;
  108. case SDL_MOUSEMOTION:
  109. Context->ProcessMouseMove(event.motion.x, event.motion.y, SystemInterface.GetKeyModifiers());
  110. break;
  111. case SDL_MOUSEBUTTONDOWN:
  112. Context->ProcessMouseButtonDown(SystemInterface.TranslateMouseButton(event.button.button), SystemInterface.GetKeyModifiers());
  113. break;
  114. case SDL_MOUSEBUTTONUP:
  115. Context->ProcessMouseButtonUp(SystemInterface.TranslateMouseButton(event.button.button), SystemInterface.GetKeyModifiers());
  116. break;
  117. case SDL_MOUSEWHEEL:
  118. Context->ProcessMouseWheel(float(event.wheel.y), SystemInterface.GetKeyModifiers());
  119. break;
  120. case SDL_KEYDOWN:
  121. {
  122. // Intercept F8 key stroke to toggle RmlUi's visual debugger tool
  123. if (event.key.keysym.sym == SDLK_F8)
  124. {
  125. Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
  126. break;
  127. }
  128. Context->ProcessKeyDown(SystemInterface.TranslateKey(event.key.keysym.sym), SystemInterface.GetKeyModifiers());
  129. break;
  130. }
  131. default:
  132. break;
  133. }
  134. }
  135. Context->Update();
  136. }
  137. Rml::Shutdown();
  138. SDL_DestroyRenderer(renderer);
  139. SDL_DestroyWindow(screen);
  140. SDL_Quit();
  141. return 0;
  142. }