Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "viUn.h"
- /* Errors */
- const char VI_UN_ERR_X_CON[] = "VI ERROR: Cannot connect to X Server.\n";
- const char VI_UN_ERR_X_VISINFO[] = "VI ERROR: No appropriate visual area found for X server.\n";
- const char VI_UN_ERR_X_NOT_INITIALIZED[] = "VI ERROR: X not initialized. Please initialize the X server before checking the event manager.";
- const char VI_UN_ERR_X_COLORMAP_FAIL[] = "VI ERROR: X failed to create colormap.";
- 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";
- /* Succes Confirmations */
- const char VI_UN_SUCCESS_VISINFO[] = "VI SUCCESS: Appropriate visual info found - %p ";
- /* Title Strings */
- const char VI_UN_DEF_WIN_NAME[] = "( Powered by Virtual Interface ) ( Unix )";
- Display* dp;
- Window root;
- GLint attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
- XVisualInfo* visualInfo;
- Colormap colorMap;
- XSetWindowAttributes setWindowAtt;
- Window win;
- GLXContext glxContext;
- GLXContext glxContext2;
- XWindowAttributes xWinAtt;
- void viUnInitXAndContext( void )
- {
- dp = XOpenDisplay( NULL );
- if ( dp == NULL )
- {
- puts( VI_UN_ERR_X_CON );
- exit( 0 );
- }
- root = DefaultRootWindow( dp );
- visualInfo = glXChooseVisual( dp, 0, attributes );
- if ( !visualInfo )
- {
- puts( VI_UN_ERR_X_VISINFO );
- exit( 0 );
- }
- else
- {
- printf( VI_UN_SUCCESS_VISINFO, ( void* )visualInfo->visualid );
- }
- colorMap = XCreateColormap( dp, root, visualInfo->visual, AllocAll );
- if ( !colorMap )
- {
- puts( VI_UN_ERR_X_COLORMAP_FAIL );
- exit( 0 );
- }
- int val;
- int success = glXGetConfig( dp, visualInfo, GLX_USE_GL, &val );
- if ( success != 0 )
- {
- puts( VI_UN_ERR_GLX_CONFIGFAIL );
- exit( 0 );
- }
- setWindowAtt.colormap = colorMap;
- setWindowAtt.event_mask = ExposureMask | KeyPressMask;
- win = XCreateWindow( dp, root, 0, 0, 600, 600, 0, visualInfo->depth, InputOutput, visualInfo->visual, CWColormap | CWEventMask, &setWindowAtt );
- XMapWindow( dp, win );
- XStoreName( dp, win, VI_UN_DEF_WIN_NAME );
- glxContext = glXCreateContext( dp, visualInfo, NULL, GL_TRUE );
- glXMakeCurrent( dp, win, glxContext );
- #define VI_X_INITIALIZED 1
- }
- void viUnInitGL( void )
- {
- #ifndef VI_X_INITIALIZED
- puts( VI_UN_ERR_X_NOT_INITIALIZED );
- exit( 1 );
- #endif
- glEnable( GL_DEPTH_TEST );
- }
- void viUnHandleEventMessageDraw( XEvent* event, void ( *DrawFunction )( void ) )
- {
- #ifndef VI_X_INITIALIZED
- puts
- ( VI_UN_ERR_X_NOT_INITIALIZED );
- exit( 0 );
- #endif
- XNextEvent( dp, event );
- switch ( event->type )
- {
- case Expose:
- XGetWindowAttributes( dp, win, &xWinAtt );
- glViewport( 0, 0, xWinAtt.width, xWinAtt.height );
- DrawFunction();
- glXSwapBuffers( dp, win );
- break;
- case KeyPress:
- char string[ 25 ];
- int len;
- KeySym keysym;
- len = XLookupString( &event->xkey, string, 25, &keysym, NULL );
- if ( len < 0 )
- {
- if( string[ 0 ] == 'r' )
- {
- string[ 0 ] = 'n';
- }
- fputs( string, stdout );
- }
- switch( keysym )
- {
- case XK_Escape:
- glXMakeCurrent( dp, None, NULL );
- glXDestroyContext( dp, glxContext );
- XDestroyWindow( dp, win );
- XCloseDisplay( dp );
- exit( 0 );
- break;
- }
- break;
- case KeyRelease:
- break;
- }
- }
- void viUnTestGL( void )
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glLoadIdentity();
- glTranslatef( 0.0f, 0.0f, -10.0f );
- const int size = 3;
- int verts[ 4 ][ 3 ] =
- {
- { size, size, size },
- { -size, size, size },
- { -size, -size, size },
- { size, -size, size }
- };
- glColor4f( 1.0f, 0.0f, 0.0f, 1.0f );
- glDrawElements( GL_QUADS, 1, GL_UNSIGNED_BYTE, verts );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement