Advertisement
Guest User

Untitled

a guest
Nov 7th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.11 KB | None | 0 0
  1. /*
  2.  * This is a rather minimal 'hello world' test of CEGUI, used we SDL and OpenGL,
  3.  * on GNU/Linux.
  4.  *
  5.  * Largely inspired from
  6.  * http://www.cegui.org.uk/wiki/index.php/Using_CEGUI_with_SDL_and_OpenGL_%280.7%29
  7.  *
  8.  * Author: Olivier Boudeville (olivier.boudeville@esperide.com)
  9.  *
  10.  */
  11.  
  12. #include "SDL.h"
  13.  
  14. #include "CEGUI/CEGUI.h"
  15. #include "CEGUI/RendererModules/OpenGL/GLRenderer.h"
  16.  
  17. #include <GL/gl.h>
  18.  
  19. #include <iostream>
  20.  
  21. using namespace std ;
  22.  
  23. using namespace CEGUI ;
  24.  
  25.  
  26. // Input management: from SDL to CEGUI.
  27.  
  28. void handle_mouse_down( Uint8 button )
  29. {
  30.  
  31.     switch ( button )
  32.     {
  33.  
  34.         case SDL_BUTTON_LEFT:
  35.             CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown( CEGUI::LeftButton ) ;
  36.             break ;
  37.  
  38.         case SDL_BUTTON_MIDDLE:
  39.             CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown( CEGUI::MiddleButton ) ;
  40.             break ;
  41.  
  42.         case SDL_BUTTON_RIGHT:
  43.             CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown( CEGUI::RightButton) ;
  44.             break ;
  45.  
  46.         case SDL_BUTTON_WHEELDOWN:
  47.             CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseWheelChange( -1 ) ;
  48.             break ;
  49.  
  50.         case SDL_BUTTON_WHEELUP:
  51.             CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseWheelChange( +1 ) ;
  52.             break ;
  53.  
  54.         default:
  55.             cout << "handle_mouse_down ignored '" << static_cast<int>( button )
  56.                 << "'" << endl ;
  57.             break ;
  58.  
  59.     }
  60.  
  61. }
  62.  
  63.  
  64. void handle_mouse_up( Uint8 button )
  65. {
  66.  
  67.     switch ( button )
  68.     {
  69.  
  70.         case SDL_BUTTON_LEFT:
  71.             CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp( CEGUI::LeftButton ) ;
  72.             break ;
  73.  
  74.         case SDL_BUTTON_MIDDLE:
  75.             CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp( CEGUI::MiddleButton ) ;
  76.             break ;
  77.  
  78.         case SDL_BUTTON_RIGHT:
  79.             CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp( CEGUI::RightButton ) ;
  80.             break ;
  81.  
  82.         case SDL_BUTTON_WHEELDOWN:
  83.             break ;
  84.  
  85.         case SDL_BUTTON_WHEELUP:
  86.             break ;
  87.  
  88.         default:
  89.             cout << "handle_mouse_up ignored '" << static_cast<int>( button )
  90.                 << "'" << endl ;
  91.             break ;
  92.  
  93.     }
  94.  
  95. }
  96.  
  97.  
  98.  
  99. void inject_input( bool & must_quit )
  100. {
  101.  
  102.     SDL_Event e ;
  103.  
  104.     // Go through all available events:
  105.     while ( SDL_PollEvent( &e ) )
  106.     {
  107.  
  108.         // Route according to the event type:
  109.         switch( e.type )
  110.         {
  111.  
  112.             // Mouse section:
  113.  
  114.             case SDL_MOUSEMOTION:
  115.                 // We inject the mouse position directly here:
  116.                 CEGUI::System::getSingleton().getDefaultGUIContext().injectMousePosition(
  117.                         static_cast<float>( e.motion.x ), static_cast<float>( e.motion.y ) ) ;
  118.                 break ;
  119.  
  120.             case SDL_MOUSEBUTTONDOWN:
  121.                 handle_mouse_down( e.button.button ) ;
  122.                 break ;
  123.  
  124.             case SDL_MOUSEBUTTONUP:
  125.                 handle_mouse_up( e.button.button ) ;
  126.                 break ;
  127.  
  128.                 // Keyboard section:
  129.  
  130.             case SDL_KEYDOWN:
  131.  
  132.                 CEGUI::System::getSingleton().getDefaultGUIContext().injectKeyDown((CEGUI::Key::Scan)e.key.keysym.scancode) ;
  133.  
  134.                 /*
  135.                  * Managing the character is more difficult, we have to use a translated
  136.                  * unicode value:
  137.                  *
  138.                  */
  139.  
  140.             if ( (e.key.keysym.unicode & 0xFF80) == 0 )
  141.             {
  142.                 CEGUI::System::getSingleton().getDefaultGUIContext().injectChar(
  143.                         e.key.keysym.unicode & 0x7F ) ;
  144.             }
  145.             break ;
  146.  
  147.             case SDL_KEYUP:
  148.             CEGUI::System::getSingleton().getDefaultGUIContext().injectKeyUp( (CEGUI::Key::Scan)e.key.keysym.scancode ) ;
  149.             break ;
  150.  
  151.             // A WM quit event occured:
  152.             case SDL_QUIT:
  153.             must_quit = true ;
  154.             break ;
  155.  
  156.             case SDL_VIDEORESIZE:
  157.             CEGUI::System::getSingleton().notifyDisplaySizeChanged(
  158.                     CEGUI::Size<float>( e.resize.w, e.resize.h ) ) ;
  159.             break ;
  160.  
  161.         }
  162.  
  163.     }
  164.  
  165. }
  166.  
  167.  
  168.  
  169. void inject_time_pulse( double & last_time_pulse )
  170. {
  171.  
  172.     // Get current "run-time" in seconds:
  173.     double current_time_pulse = 0.001 * SDL_GetTicks() ;
  174.  
  175.     // Inject the time that passed since the last call:
  176.     CEGUI::System::getSingleton().injectTimePulse( static_cast<float>(
  177.                 current_time_pulse - last_time_pulse ) ) ;
  178.     CEGUI::System::getSingleton().getDefaultGUIContext().injectTimePulse( static_cast<float>(
  179.                 current_time_pulse - last_time_pulse ) ) ;
  180.  
  181.     // Records the new time as the last time:
  182.     last_time_pulse = current_time_pulse ;
  183.  
  184. }
  185.  
  186.  
  187.  
  188. void render_gui()
  189. {
  190.  
  191.     // Clears the colour buffer:
  192.     glClear( GL_COLOR_BUFFER_BIT ) ;
  193.  
  194.     // Renders the GUI:
  195.     CEGUI::System::getSingleton().renderAllGUIContexts() ;
  196.  
  197.     // Updates the screen:
  198.     SDL_GL_SwapBuffers() ;
  199.  
  200. }
  201.  
  202.  
  203. void main_loop ()
  204. {
  205.  
  206.     cout << " - entering main loop" << endl ;
  207.  
  208.     bool must_quit = false ;
  209.  
  210.     // get "run-time" in seconds
  211.     double last_time_pulse = 0.001 * static_cast<double>( SDL_GetTicks() ) ;
  212.  
  213.     while ( ! must_quit )
  214.     {
  215.  
  216.         inject_input( must_quit ) ;
  217.         inject_time_pulse( last_time_pulse ) ;
  218.         render_gui() ;
  219.  
  220.     }
  221.  
  222.     cout << " - leaving main loop" << endl ;
  223.  
  224. }
  225.  
  226.  
  227. SDL_Surface & init_SDL()
  228. {
  229.  
  230.     cout << " - initializing SDL" << endl ;
  231.  
  232.     atexit (SDL_Quit) ;
  233.  
  234.     if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
  235.     {
  236.         cerr << "Unable to initialise SDL: " << SDL_GetError() ;
  237.         exit(0) ;
  238.     }
  239.  
  240.     SDL_Surface * screen = SDL_SetVideoMode ( 600, 480, 0, SDL_OPENGL ) ;
  241.  
  242.     if ( screen == 0 )
  243.     {
  244.         cerr << "Unable to set OpenGL videomode: " << SDL_GetError() ;
  245.         SDL_Quit() ;
  246.         exit(0) ;
  247.     }
  248.  
  249.     SDL_ShowCursor( SDL_DISABLE ) ;
  250.     SDL_EnableUNICODE( 1 ) ;
  251.     SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ) ;
  252.  
  253.     return *screen ;
  254.  
  255. }
  256.  
  257.  
  258.  
  259. void set_CEGUI_paths()
  260. {
  261.  
  262.     // Initialises the required directories for the DefaultResourceProvider:
  263.  
  264.     CEGUI::DefaultResourceProvider & defaultResProvider =
  265.         * static_cast<CEGUI::DefaultResourceProvider*>
  266.         ( CEGUI::System::getSingleton().getResourceProvider() ) ;
  267.  
  268.     const string CEGUIInstallSharePath = "/home/beyer/tmp/cegui-0.8.4/datafiles/" ;
  269.  
  270.     // For each resource type, sets a corresponding resource group directory:
  271.  
  272.     cout << "Using scheme directory '" << CEGUIInstallSharePath + "schemes/"
  273.         << "'" << endl ;
  274.  
  275.     defaultResProvider.setResourceGroupDirectory( "schemes",
  276.             CEGUIInstallSharePath + "schemes/" ) ;
  277.  
  278.     defaultResProvider.setResourceGroupDirectory( "imagesets",
  279.             CEGUIInstallSharePath + "imagesets/" ) ;
  280.  
  281.     defaultResProvider.setResourceGroupDirectory( "fonts",
  282.             CEGUIInstallSharePath + "fonts/" ) ;
  283.  
  284.     defaultResProvider.setResourceGroupDirectory( "layouts",
  285.             CEGUIInstallSharePath + "layouts/" ) ;
  286.  
  287.     defaultResProvider.setResourceGroupDirectory( "looknfeels",
  288.             CEGUIInstallSharePath + "looknfeel/" ) ;
  289.  
  290.     defaultResProvider.setResourceGroupDirectory( "lua_scripts",
  291.             CEGUIInstallSharePath + "lua_scripts/" ) ;
  292.  
  293.     defaultResProvider.setResourceGroupDirectory( "schemas",
  294.             CEGUIInstallSharePath + "xml_schemas/" ) ;
  295.  
  296.     defaultResProvider.setResourceGroupDirectory( "animations",
  297.             CEGUIInstallSharePath + "animations/" ) ;
  298.  
  299.     // Sets the default resource groups to be used:
  300.     CEGUI::ImageManager::setImagesetDefaultResourceGroup( "imagesets" ) ;
  301.     CEGUI::Font::setDefaultResourceGroup( "fonts" ) ;
  302.     CEGUI::Scheme::setDefaultResourceGroup( "schemes" ) ;
  303.     CEGUI::WidgetLookManager::setDefaultResourceGroup( "looknfeels" ) ;
  304.     CEGUI::WindowManager::setDefaultResourceGroup( "layouts" ) ;
  305.     CEGUI::ScriptModule::setDefaultResourceGroup( "lua_scripts" ) ;
  306.     CEGUI::AnimationManager::setDefaultResourceGroup( "animations" ) ;
  307.  
  308.     // Set-up default group for validation schemas:
  309.     CEGUI::XMLParser * parser = CEGUI::System::getSingleton().getXMLParser() ;
  310.     if ( parser->isPropertyPresent( "SchemaDefaultResourceGroup" ) )
  311.         parser->setProperty( "SchemaDefaultResourceGroup", "schemas" ) ;
  312.  
  313. }
  314.  
  315.  
  316. WindowManager & init_CEGUI( SDL_Surface & surface )
  317. {
  318.  
  319.     cout << " - initializing CEGUI" << endl ;
  320.  
  321.     CEGUI::OpenGLRenderer::bootstrapSystem() ;
  322.  
  323.     set_CEGUI_paths() ;
  324.  
  325.     SchemeManager::getSingleton().createFromFile( "TaharezLook.scheme" ) ;
  326.  
  327.     System::getSingleton().getDefaultGUIContext().getMouseCursor().setDefaultImage("TaharezLook/MouseArrow");
  328.  
  329.     return WindowManager::getSingleton() ;
  330.  
  331. }
  332.  
  333.  
  334.  
  335. void create_gui( WindowManager & winManager )
  336. {
  337.  
  338.     cout << " - creating the GUI" << endl ;
  339.  
  340.     DefaultWindow & rootWin = * static_cast<DefaultWindow*>(
  341.             winManager.createWindow( "DefaultWindow", "Root" ) ) ;
  342.  
  343.     CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow( &rootWin ) ;
  344.  
  345.     FrameWindow & myWin = * static_cast<FrameWindow*>( winManager.createWindow(
  346.                 "TaharezLook/FrameWindow", "Demo Window" ) ) ;
  347.  
  348.     rootWin.addChild( &myWin ) ;
  349.  
  350.     myWin.setPosition( UVector2( cegui_reldim(0.25f), cegui_reldim(0.25f) ) ) ;
  351.     myWin.setSize( USize( cegui_reldim(0.5f), cegui_reldim(0.5f) ) ) ;
  352.  
  353.     myWin.setMaxSize( USize( cegui_reldim(1.0f), cegui_reldim(1.0f) ) ) ;
  354.     myWin.setMinSize( USize( cegui_reldim(0.1f), cegui_reldim(0.1f) ) ) ;
  355.  
  356.     myWin.setText( "Hello World! This is a minimal SDL+OpenGL+CEGUI test." ) ;
  357.  
  358. }
  359.  
  360.  
  361. int main( int argc, char *argv[] )
  362. {
  363.  
  364.     cout << " - starting CEGUI test" << endl ;
  365.  
  366.     SDL_Surface & screen = init_SDL() ;
  367.  
  368.     WindowManager & winManager = init_CEGUI( screen ) ;
  369.  
  370.     create_gui( winManager ) ;
  371.  
  372.     main_loop() ;
  373.  
  374.     cout << " - ending CEGUI test" << endl ;
  375.  
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement