main.cpp 5.7 KB

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