main.cpp 5.6 KB

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