minimal SDL2 example

C++ code posted by Quintus
created at 13 Mar 21:55

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
#include <iostream>
#include <SDL.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_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;
  }

  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;

  /***************************************
   * 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();

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

  CEGUI::System::create(renderer, p_provider, p_xml_parser, p_imgcodec, NULL);

  /* 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");

  /***************************************
   * 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);
    CEGUI::System::getSingleton().renderAllGUIContexts();
  }

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

  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;
}
4 KB in 8 ms with coderay