main.cpp 5.1 KB

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