Advertisement
Guest User

Untitled

a guest
Nov 30th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <X11/Xlib.h>
  4. #include <GL/gl.h>
  5. #include <GL/glx.h>
  6. #include <GL/glxext.h>
  7. #include <GL/glext.h>
  8.  
  9. struct _WGelWindowX11{
  10.     Display *XDspl;
  11.     Window XWin;
  12.     Colormap XCMap;
  13.     GLXContext glContext;
  14. };
  15.  
  16. typedef struct _WGelWindowX11 WGelWindowX11;
  17. #define WGEL_WINDOWX11(obj) ((WGelWindowX11*)obj)
  18.  
  19. int aiAttr[]={
  20.     GLX_X_RENDERABLE,   True,
  21.     GLX_X_VISUAL_TYPE,  GLX_TRUE_COLOR,
  22.     GLX_RED_SIZE,       8,
  23.     GLX_GREEN_SIZE,     8,
  24.     GLX_BLUE_SIZE,      8,
  25.     GLX_ALPHA_SIZE,     8,
  26.     GLX_DEPTH_SIZE,     24,
  27.     GLX_DOUBLEBUFFER,   True,
  28.     None
  29. };
  30.  
  31. int aiAttrGL[]={
  32.     GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  33.     GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  34.     None
  35. };
  36.  
  37. void *WGelWindowNew(void){
  38.     WGelWindowX11 *Result=malloc(sizeof(WGelWindowX11));
  39.  
  40.     Result->XDspl=XOpenDisplay(NULL);
  41.     if (!Result->XDspl){
  42.         perror("Cannot open display.\n");
  43.         return NULL;
  44.     }
  45.  
  46.     //Gettin' matchin' framebuffer configuration
  47.     int iFBCount;
  48.     GLXFBConfig *pFBConf=glXChooseFBConfig(Result->XDspl, DefaultScreen(Result->XDspl), aiAttr, &iFBCount);
  49.     if(!pFBConf){
  50.         perror("Cannot get FBConfig.\n");
  51.         return NULL;
  52.     }
  53.  
  54.     int iBestFBC=-1, iWorstFBC=-1, iBestNSampl=-1, iWorstNSampl=999,iSamplBuff, iSampl, i;
  55.     for(i=0; i<iFBCount; i++){
  56.         XVisualInfo *vi=glXGetVisualFromFBConfig(Result->XDspl, pFBConf[i]);
  57.         if (vi){
  58.             glXGetFBConfigAttrib(Result->XDspl, pFBConf[i], GLX_SAMPLE_BUFFERS, &iSamplBuff);
  59.             glXGetFBConfigAttrib(Result->XDspl, pFBConf[i], GLX_SAMPLES, &iSampl);
  60.             if (iBestFBC<0 || iSamplBuff && iSampl>iBestNSampl) iBestFBC=i, iBestNSampl=iSampl;
  61.             if (iWorstFBC<0 || !iSamplBuff || iSampl<iWorstNSampl) iWorstFBC=i, iWorstNSampl=iSampl;
  62.         }
  63.         XFree(vi);
  64.     }
  65.  
  66.     GLXFBConfig FBConfBest=pFBConf[iBestFBC];
  67.     XFree(pFBConf);
  68.  
  69.     XVisualInfo *XVslnfo=glXGetVisualFromFBConfig(Result->XDspl, FBConfBest);
  70.     XSetWindowAttributes XWinAttr;
  71.  
  72.     Result->XCMap=XCreateColormap(Result->XDspl, RootWindow(Result->XDspl, XVslnfo->screen), XVslnfo->visual, AllocNone);
  73.     XWinAttr.colormap=Result->XCMap;
  74.     XWinAttr.background_pixmap=None;
  75.     XWinAttr.border_pixel=0;
  76.     XWinAttr.event_mask=StructureNotifyMask|ExposureMask|KeyPressMask;
  77.  
  78.     Atom wmDeleteMessage=XInternAtom(Result->XDspl, "WM_DELETE_WINDOW", FALSE);
  79.  
  80.     Result->XWin=XCreateWindow(Result->XDspl, RootWindow(Result->XDspl, XVslnfo->screen), 0, 0, 300, 300, 0, XVslnfo->depth, InputOutput, XVslnfo->visual, CWBorderPixel|CWColormap|CWEventMask, &XWinAttr);
  81.     if(!Result->XWin){
  82.         perror("Cannot create window.\n");
  83.         return NULL;
  84.     }
  85.  
  86.     XFree(XVslnfo);
  87.     XStoreName(Result->XDspl, Result->XWin, "WGel Window" );
  88.     XSetWMProtocols(Result->XDspl, Result->XWin, &wmDeleteMessage, 1);
  89.  
  90.     XMapWindow(Result->XDspl, Result->XWin);
  91.  
  92.     PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs=(PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB");
  93.  
  94.     Result->glContext=glXCreateContextAttribs(Result->XDspl, FBConfBest, 0, True, aiAttrGL);
  95.     if(!Result->glContext){
  96.         perror("Cannot create GL 3.3 context\n");
  97.         return NULL;
  98.     }
  99.  
  100.     glXMakeCurrent(Result->XDspl, Result->XWin, Result->glContext);
  101.     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  102.  
  103.     return (void*)Result;}
  104.  
  105. Display *WGelX11GetDisplay(ptr window){
  106.     return WGEL_WINDOWX11(window)->XDspl;}
  107.  
  108. Window *WGelX11GetWindow(ptr window){
  109.     return &(WGEL_WINDOWX11(window)->XWin);
  110. }
  111.  
  112. int main(void){
  113.     ptr frmMain=WGelWindowNew();
  114.     Display *dpy=WGelX11GetDisplay(frmMain);
  115.     XEvent e;
  116.     while(XNextEvent(dpy, &e)>=0){
  117.         if ((!XPending(dpy))&&(e.type==Expose)){
  118.             glClear(GL_COLOR_BUFFER_BIT);
  119.             glXSwapBuffers(dpy, *WGelX11GetWindow(frmMain));
  120.         }
  121.         if (e.type==ClientMessage){
  122.             XDestroyWindow(dpy, e.xclient.window);
  123.             break;
  124.         }
  125.     }
  126.  
  127.     XCloseDisplay(dpy);
  128.  
  129.     return EXIT_SUCCESS;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement