main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <GL/glew.h>
  41. int main(int /*argc*/, char** /*argv*/)
  42. {
  43. #ifdef RMLUI_PLATFORM_WIN32
  44. AllocConsole();
  45. #endif
  46. int window_width = 1024;
  47. int window_height = 768;
  48. SDL_Init(SDL_INIT_VIDEO);
  49. SDL_Window* screen = SDL_CreateWindow("LibRmlUi SDL2 test", 20, 20, window_width, window_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
  50. SDL_GLContext glcontext = SDL_GL_CreateContext(screen);
  51. int oglIdx = -1;
  52. int nRD = SDL_GetNumRenderDrivers();
  53. for (int i = 0; i < nRD; i++)
  54. {
  55. SDL_RendererInfo info;
  56. if (!SDL_GetRenderDriverInfo(i, &info))
  57. {
  58. if (!strcmp(info.name, "opengl"))
  59. {
  60. oglIdx = i;
  61. }
  62. }
  63. }
  64. SDL_Renderer* renderer = SDL_CreateRenderer(screen, oglIdx, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  65. GLenum err = glewInit();
  66. if (err != GLEW_OK)
  67. fprintf(stderr, "GLEW ERROR: %s\n", glewGetErrorString(err));
  68. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  69. glMatrixMode(GL_PROJECTION | GL_MODELVIEW);
  70. glLoadIdentity();
  71. glOrtho(0, window_width, window_height, 0, 0, 1);
  72. RmlUiSDL2Renderer Renderer(renderer, screen);
  73. RmlUiSDL2SystemInterface SystemInterface;
  74. Rml::String root = Shell::FindSamplesRoot();
  75. ShellFileInterface FileInterface(root);
  76. Rml::SetFileInterface(&FileInterface);
  77. Rml::SetRenderInterface(&Renderer);
  78. Rml::SetSystemInterface(&SystemInterface);
  79. if (!Rml::Initialise())
  80. return 1;
  81. struct FontFace {
  82. Rml::String filename;
  83. bool fallback_face;
  84. };
  85. FontFace font_faces[] = {
  86. { "LatoLatin-Regular.ttf", false },
  87. { "LatoLatin-Italic.ttf", false },
  88. { "LatoLatin-Bold.ttf", false },
  89. { "LatoLatin-BoldItalic.ttf", false },
  90. { "NotoEmoji-Regular.ttf", true },
  91. };
  92. for (const FontFace& face : font_faces)
  93. {
  94. Rml::LoadFontFace("assets/" + face.filename, face.fallback_face);
  95. }
  96. Rml::Context* Context = Rml::CreateContext("default",
  97. Rml::Vector2i(window_width, window_height));
  98. Rml::Debugger::Initialise(Context);
  99. Rml::ElementDocument* Document = Context->LoadDocument("assets/demo.rml");
  100. if (Document)
  101. {
  102. Document->Show();
  103. fprintf(stdout, "\nDocument loaded");
  104. }
  105. else
  106. {
  107. fprintf(stdout, "\nDocument is nullptr");
  108. }
  109. bool done = false;
  110. while (!done)
  111. {
  112. SDL_Event event;
  113. SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
  114. SDL_RenderClear(renderer);
  115. Context->Render();
  116. SDL_RenderPresent(renderer);
  117. while (SDL_PollEvent(&event))
  118. {
  119. switch (event.type)
  120. {
  121. case SDL_QUIT:
  122. done = true;
  123. break;
  124. case SDL_MOUSEMOTION:
  125. Context->ProcessMouseMove(event.motion.x, event.motion.y, SystemInterface.GetKeyModifiers());
  126. break;
  127. case SDL_MOUSEBUTTONDOWN:
  128. Context->ProcessMouseButtonDown(SystemInterface.TranslateMouseButton(event.button.button), SystemInterface.GetKeyModifiers());
  129. break;
  130. case SDL_MOUSEBUTTONUP:
  131. Context->ProcessMouseButtonUp(SystemInterface.TranslateMouseButton(event.button.button), SystemInterface.GetKeyModifiers());
  132. break;
  133. case SDL_MOUSEWHEEL:
  134. Context->ProcessMouseWheel(float(event.wheel.y), SystemInterface.GetKeyModifiers());
  135. break;
  136. case SDL_KEYDOWN:
  137. {
  138. // Intercept F8 key stroke to toggle RmlUi's visual debugger tool
  139. if (event.key.keysym.sym == SDLK_F8)
  140. {
  141. Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
  142. break;
  143. }
  144. Context->ProcessKeyDown(SystemInterface.TranslateKey(event.key.keysym.sym), SystemInterface.GetKeyModifiers());
  145. break;
  146. }
  147. default:
  148. break;
  149. }
  150. }
  151. Context->Update();
  152. }
  153. Rml::Shutdown();
  154. SDL_DestroyRenderer(renderer);
  155. SDL_GL_DeleteContext(glcontext);
  156. SDL_DestroyWindow(screen);
  157. SDL_Quit();
  158. return 0;
  159. }