Advertisement
Guest User

viUnixCode

a guest
Feb 26th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. #include "viUn.h"
  2.  
  3. /* Errors */
  4. const char VI_UN_ERR_X_CON[] = "VI ERROR: Cannot connect to X Server.\n";
  5. const char VI_UN_ERR_X_VISINFO[] = "VI ERROR: No appropriate visual area found for X server.\n";
  6. const char VI_UN_ERR_X_NOT_INITIALIZED[] = "VI ERROR: X not initialized. Please initialize the X server before checking the event manager.";
  7. const char VI_UN_ERR_X_COLORMAP_FAIL[] = "VI ERROR: X failed to create colormap.";
  8. const char VI_UN_ERR_GLX_CONFIGFAIL[] = "VI ERROR: GLX failed to obtain configuration information for GLX_USE_GL, or GLX_USE_GL has returned false";
  9.  
  10.  
  11. /* Succes Confirmations */
  12. const char VI_UN_SUCCESS_VISINFO[] = "VI SUCCESS: Appropriate visual info found - %p ";
  13.  
  14. /* Title Strings */
  15. const char VI_UN_DEF_WIN_NAME[] = "( Powered by Virtual Interface ) ( Unix )";
  16.  
  17. Display*                dp;
  18. Window                  root;
  19. GLint                   attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
  20. XVisualInfo*            visualInfo;
  21. Colormap                colorMap;
  22. XSetWindowAttributes    setWindowAtt;
  23. Window                  win;
  24. GLXContext              glxContext;
  25. GLXContext              glxContext2;
  26. XWindowAttributes       xWinAtt;
  27.  
  28. void viUnInitXAndContext( void )
  29. {
  30.     dp = XOpenDisplay( NULL );
  31.  
  32.     if ( dp == NULL )
  33.     {
  34.         puts( VI_UN_ERR_X_CON );
  35.         exit( 0 );
  36.     }
  37.  
  38.     root = DefaultRootWindow( dp );
  39.  
  40.     visualInfo = glXChooseVisual( dp, 0, attributes );
  41.  
  42.     if ( !visualInfo )
  43.     {
  44.         puts( VI_UN_ERR_X_VISINFO );
  45.         exit( 0 );
  46.     }
  47.     else
  48.     {
  49.         printf( VI_UN_SUCCESS_VISINFO, ( void* )visualInfo->visualid );
  50.     }
  51.  
  52.     colorMap = XCreateColormap( dp, root, visualInfo->visual, AllocAll );
  53.  
  54.     if ( !colorMap )
  55.     {
  56.         puts( VI_UN_ERR_X_COLORMAP_FAIL );
  57.         exit( 0 );
  58.  
  59.     }
  60.  
  61.     int val;
  62.  
  63.     int success = glXGetConfig( dp, visualInfo, GLX_USE_GL, &val );
  64.  
  65.     if ( success != 0 )
  66.     {
  67.         puts( VI_UN_ERR_GLX_CONFIGFAIL );
  68.         exit( 0 );
  69.     }
  70.  
  71.     setWindowAtt.colormap = colorMap;
  72.     setWindowAtt.event_mask = ExposureMask | KeyPressMask;
  73.  
  74.     win = XCreateWindow( dp, root, 0, 0, 600, 600, 0, visualInfo->depth, InputOutput, visualInfo->visual, CWColormap | CWEventMask, &setWindowAtt );
  75.  
  76.     XMapWindow( dp, win );
  77.  
  78.     XStoreName( dp, win, VI_UN_DEF_WIN_NAME );
  79.  
  80.     glxContext = glXCreateContext( dp, visualInfo, NULL, GL_TRUE );
  81.  
  82.     glXMakeCurrent( dp, win, glxContext );
  83.  
  84. #define VI_X_INITIALIZED 1
  85.  
  86. }
  87.  
  88. void viUnInitGL( void )
  89. {
  90.  
  91. #ifndef VI_X_INITIALIZED
  92.     puts( VI_UN_ERR_X_NOT_INITIALIZED );
  93.     exit( 1 );
  94. #endif
  95.  
  96.     glEnable( GL_DEPTH_TEST );
  97. }
  98.  
  99. void viUnHandleEventMessageDraw( XEvent* event, void ( *DrawFunction )( void ) )
  100. {
  101. #ifndef VI_X_INITIALIZED
  102.     puts
  103.             ( VI_UN_ERR_X_NOT_INITIALIZED );
  104.     exit( 0 );
  105. #endif
  106.  
  107.     XNextEvent( dp, event );
  108.  
  109.     switch ( event->type )
  110.     {
  111.         case Expose:
  112.  
  113.             XGetWindowAttributes( dp, win, &xWinAtt );
  114.             glViewport( 0, 0, xWinAtt.width, xWinAtt.height );
  115.  
  116.             DrawFunction();
  117.  
  118.             glXSwapBuffers( dp, win );
  119.  
  120.             break;
  121.  
  122.         case KeyPress:
  123.  
  124.             char string[ 25 ];
  125.  
  126.             int len;
  127.  
  128.             KeySym keysym;
  129.  
  130.             len = XLookupString( &event->xkey, string, 25, &keysym, NULL );
  131.  
  132.             if ( len < 0 )
  133.             {
  134.                 if( string[ 0 ] == 'r' )
  135.                 {
  136.                         string[ 0 ] = 'n';
  137.                 }
  138.  
  139.                     fputs( string, stdout );
  140.             }
  141.  
  142.             switch( keysym )
  143.             {
  144.                 case XK_Escape:
  145.  
  146.                     glXMakeCurrent( dp, None, NULL );
  147.                     glXDestroyContext( dp, glxContext );
  148.  
  149.                     XDestroyWindow( dp, win );
  150.  
  151.                     XCloseDisplay( dp );
  152.  
  153.                     exit( 0 );
  154.  
  155.                     break;
  156.             }
  157.  
  158.  
  159.  
  160.             break;
  161.  
  162.         case KeyRelease:
  163.  
  164.             break;
  165.  
  166.     }
  167.  
  168. }
  169.  
  170. void viUnTestGL( void )
  171. {
  172.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  173.  
  174.     glLoadIdentity();
  175.  
  176.     glTranslatef( 0.0f, 0.0f, -10.0f );
  177.  
  178.     const int size = 3;
  179.  
  180.     int verts[ 4 ][ 3 ] =
  181.     {
  182.         { size, size, size },
  183.         { -size, size, size },
  184.         { -size, -size, size },
  185.         { size, -size, size }
  186.     };
  187.  
  188.     glColor4f( 1.0f, 0.0f, 0.0f, 1.0f );
  189.  
  190.     glDrawElements( GL_QUADS, 1, GL_UNSIGNED_BYTE, verts );
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement