Advertisement
Guest User

librocket integration

a guest
Aug 18th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include "Engine.h"
  2.  
  3. Engine::Engine(){
  4.     rocketContext = NULL;
  5.     rockDoc = NULL;
  6. }
  7. void Engine::Start(const char* title, int width, int height, int bpp, bool fullscreen){
  8.     int flags = 0;
  9.     if (SDL_Init(SDL_INIT_EVERYTHING) == -1 )   { //turn on sdl
  10.         printf("%s\n", SDL_GetError());
  11.         return;
  12.     }
  13.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  14.     screen = NULL;
  15.     if (fullscreen) {//turn on fullscreen
  16.         flags = SDL_FULLSCREEN;
  17.     } else {
  18.         flags = SDL_OPENGL;
  19.     }
  20.     screen = SDL_SetVideoMode(width, height, bpp, flags);//create a screen surface to draw on
  21.     GLSetup();
  22.     SDL_WM_SetCaption(title, title);//set window title
  23.     if (screen == NULL) {
  24.         SDL_GetError();
  25.         return;
  26.     }
  27.     SDL_EnableUNICODE( SDL_TRUE );
  28.     SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
  29.     InitSDLtoRocketKeymap();
  30.     fullScreen = fullscreen;
  31.     //-
  32.     Rocket::Core::SetSystemInterface( &rocketSys );
  33.     Rocket::Core::SetRenderInterface( &rocketRender );
  34.     Rocket::Core::Initialise();
  35.     Rocket::Controls::Initialise();
  36.     rocketContext = Rocket::Core::CreateContext( "main", Rocket::Core::Vector2i(width, height) );
  37.     if (Rocket::Debugger::Initialise(rocketContext) && Rocket::Debugger::SetContext(rocketContext))
  38.         Rocket::Debugger::SetVisible(true);
  39.     bool success = Rocket::Core::FontDatabase::LoadFontFace("interface/Delicious-Roman.otf");
  40.     if( !success ) std::cout << "Failed to load font!" << std::endl;
  41.     rockDoc = rocketContext->LoadDocument( "interface/test.rml" );
  42.     rockDoc->Show();
  43.     //--
  44.     running = true;//go live
  45.  
  46. }
  47.  
  48. void Engine::GLSetup(){
  49.     glEnableClientState(GL_VERTEX_ARRAY);
  50.     glEnableClientState(GL_COLOR_ARRAY);
  51.     glEnable(GL_BLEND);
  52.     glEnable(GL_TEXTURE_2D);
  53.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  54.     glMatrixMode(GL_PROJECTION);
  55.     glLoadIdentity();
  56.     glOrtho(0, screen->w, screen->h, 0, -1, 1);
  57.     glMatrixMode(GL_MODELVIEW);
  58.     glLoadIdentity();
  59.     glClearColor( 0.2f, 0.2f, 0.2f, 1.0f );
  60. }
  61.  
  62. void Engine::ChangeState(EngineState* state){
  63.     if (!states.empty()){
  64.         states.back()->Clean();
  65.         states.pop_back();
  66.     }
  67.     states.push_back(state);
  68.     states.back()->Start();
  69. }
  70. void Engine::PushState(EngineState* state){
  71.     if (!states.empty()) {
  72.         states.back()->Pause();
  73.     }
  74.     states.push_back(state);
  75.     states.back()->Start();
  76. }
  77. void Engine::PopState(){
  78.     if (!states.empty()) {
  79.         states.back()->Clean();
  80.         states.pop_back();
  81.     }
  82.     if (!states.empty())
  83.         states.back()->Resume();
  84. }
  85.  
  86. void Engine::HandleEvents(){
  87.     states.back()->HandleEvents(this);
  88. }
  89.  
  90. void Engine::Update(){
  91.     states.back()->Update(this);
  92.     rocketContext->Update();
  93. }
  94.  
  95. void Engine::Draw(){
  96.     states.back()->Draw(this);
  97.     rocketContext->Render();
  98. }
  99.  
  100. void Engine::Clean(){
  101.     while (!states.empty()) {
  102.         states.back()->Clean();
  103.         states.pop_back();
  104.     }
  105.     rockDoc->RemoveReference();
  106.     rocketContext->RemoveReference();
  107.     Rocket::Core::Shutdown();
  108. }
  109.  
  110. void Engine::Quit(){
  111.     SDL_FreeSurface(screen);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement