Singleton resource provider code

C++ code posted by Quintus
created at 14 Mar 00:38

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <SDL.h>
#include <SDL_opengl.h>
#include <CEGUI/CEGUI.h>
#include <CEGUI/RendererModules/OpenGL/GLRenderer.h>
#include <CEGUI/XMLParserModules/Expat/XMLParser.h>
#include <CEGUI/ImageCodecModules/DevIL/ImageCodec.h>

int main(int, char**)
{
  /***************************************
   * Init SDL
   ***************************************/

  if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
    std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
    SDL_Quit();
    return 1;
  }

  SDL_Window* p_window = SDL_CreateWindow("Test window", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
  if (!p_window) {
    std::cerr << "failed to set up window." << std::endl;
    SDL_Quit();
    return 2;
  }

  SDL_ShowCursor(0);

  SDL_Renderer* p_sdl_renderer = SDL_CreateRenderer(p_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  if (!p_sdl_renderer) {
    SDL_DestroyWindow(p_window);
    SDL_Quit();
    std::cerr << "Faield to set up renderer." << std::endl;
    return 3;
  }

  /***************************************
   * Init CEGUI
   ***************************************/

  //CEGUI::OpenGLRenderer& renderer = CEGUI::OpenGLRenderer::bootstrapSystem();
  CEGUI::OpenGLRenderer& renderer = CEGUI::OpenGLRenderer::create();
  //CEGUI::DefaultResourceProvider* p_provider = new CEGUI::DefaultResourceProvider();
  CEGUI::ExpatParser* p_xml_parser = new CEGUI::ExpatParser();
  CEGUI::DevILImageCodec* p_imgcodec = new CEGUI::DevILImageCodec();

  CEGUI::System::create(renderer, NULL, p_xml_parser, p_imgcodec, NULL);
  CEGUI::DefaultResourceProvider* rp = static_cast<CEGUI::DefaultResourceProvider*>(CEGUI::System::getSingleton().getResourceProvider());

  rp->setResourceGroupDirectory("schemes", "/usr/share/cegui-0/schemes/");
  rp->setResourceGroupDirectory("imagesets", "/usr/share/cegui-0/imagesets/");
  rp->setResourceGroupDirectory("fonts", "/usr/share/cegui-0/fonts/");
  rp->setResourceGroupDirectory("layouts", "/usr/share/cegui-0/layouts/");
  rp->setResourceGroupDirectory("looknfeels", "/usr/share/cegui-0/looknfeel/");
  rp->setResourceGroupDirectory("lua_scripts", "/usr/share/cegui-0/lua_scripts/");

  /* Now tell each of CEGUI’s resource-consuming classes (the "*Manager" classes)
   * which group of the filesystem provider to ask for its stuff. */
  CEGUI::ImageManager::setImagesetDefaultResourceGroup("imagesets");
  CEGUI::Font::setDefaultResourceGroup("fonts");
  CEGUI::Scheme::setDefaultResourceGroup("schemes");
  CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeels");
  CEGUI::WindowManager::setDefaultResourceGroup("layouts");
  CEGUI::ScriptModule::setDefaultResourceGroup("lua_scripts");

  CEGUI::SchemeManager::getSingleton().createFromFile("TaharezLook.scheme");
  CEGUI::FontManager::getSingleton().createFromFile("DejaVuSans-12.font");
  CEGUI::System::getSingleton().getDefaultGUIContext().setDefaultFont("DejaVuSans-12");
  CEGUI::System::getSingleton().getDefaultGUIContext().getMouseCursor().setDefaultImage("TaharezLook/MouseArrow");

  // Tell CEGUI how large our Window is. Forget this and you’ll never see anything.
  CEGUI::System::getSingleton().notifyDisplaySizeChanged(CEGUI::Sizef(640, 480));

  CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
  CEGUI::Window* p_cegui_rootwin = wmgr.createWindow("DefaultWindow", "root");
  CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(p_cegui_rootwin);

  CEGUI::FrameWindow* p_testframe = static_cast<CEGUI::FrameWindow*>(wmgr.createWindow("TaharezLook/FrameWindow", "testWindow"));
  p_testframe->setPosition(CEGUI::UVector2(CEGUI::UDim(0.25, 0.0), CEGUI::UDim(0.25, 0.0f)));
  p_testframe->setSize(CEGUI::USize(CEGUI::UDim(0.5f, 0.0f), CEGUI::UDim(0.5f, 0.0f)));
  p_testframe->setText("Hello, World!");
  p_cegui_rootwin->addChild(p_testframe);

  glClearColor(0, 0, 0, 255);

  SDL_Surface* p_image = SDL_LoadBMP("../hello.bmp");
  SDL_Texture* p_imgtexture = SDL_CreateTextureFromSurface(p_sdl_renderer, p_image);
  SDL_FreeSurface(p_image);
  p_image = NULL;

  /***************************************
   * Main loop
   ***************************************/
  bool quit = false;
  SDL_Event evt;
  while (!quit) {
    // Grab all events from the queue
    while (SDL_PollEvent(&evt)) {
      if (evt.type == SDL_QUIT) {
        quit = true;
      }
      else {
        std::cout << "Unknown event received." << std::endl;
      }
    }

    SDL_RenderClear(p_sdl_renderer);
    SDL_RenderCopy(p_sdl_renderer, p_imgtexture, NULL, NULL);
    SDL_RenderPresent(p_sdl_renderer);

    SDL_GLContext context = SDL_GL_CreateContext(p_window);
    glClear(GL_COLOR_BUFFER_BIT);
    CEGUI::System::getSingleton().renderAllGUIContexts();
    SDL_GL_SwapWindow(p_window);
    SDL_GL_DeleteContext(context);
  }

  /***************************************
   * Cleanup
   ***************************************/

  CEGUI::System::destroy();
  CEGUI::OpenGLRenderer::destroy(renderer);
  delete p_imgcodec;
  delete p_xml_parser;
  //delete p_provider;

  SDL_DestroyTexture(p_imgtexture);
  SDL_DestroyRenderer(p_sdl_renderer);
  SDL_DestroyWindow(p_window);
  SDL_Quit();
  return 0;
}
5.21 KB in 9 ms with coderay