window.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "window.hpp"
  2. using namespace slop;
  3. slop::SlopWindow::SlopWindow() {
  4. // Load up a opengl context
  5. static int attributeList[] = { GLX_RENDER_TYPE, GLX_RGBA_BIT,
  6. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  7. GLX_DOUBLEBUFFER, True,
  8. GLX_RED_SIZE, 1,
  9. GLX_GREEN_SIZE, 1,
  10. GLX_BLUE_SIZE, 1,
  11. GLX_ALPHA_SIZE, 1,
  12. GLX_DEPTH_SIZE, 1,
  13. None };
  14. int nelements;
  15. int render_event_base, render_error_base;
  16. if(!XRenderQueryExtension(x11->display, &render_event_base, &render_error_base)) {
  17. throw new std::runtime_error("No XRENDER extension found\n");
  18. }
  19. GLXFBConfig* fbc = glXChooseFBConfig(x11->display, DefaultScreen(x11->display), attributeList, &nelements);
  20. GLXFBConfig fbconfig;
  21. if ( !fbc ) {
  22. throw new std::runtime_error("No matching visuals available.\n");
  23. }
  24. XVisualInfo* vi ;
  25. XRenderPictFormat *pictFormat;
  26. int i;
  27. for (i=0; i<nelements; i++) {
  28. vi = glXGetVisualFromFBConfig(x11->display, fbc[i]);
  29. if (!vi) { continue; }
  30. pictFormat = XRenderFindVisualFormat(x11->display, vi->visual);
  31. if (!pictFormat) { continue; }
  32. if(pictFormat->direct.alphaMask > 0) {
  33. fbconfig = fbc[i];
  34. break;
  35. }
  36. }
  37. if (i == nelements ) {
  38. throw new std::runtime_error( "No matching visuals available" );
  39. }
  40. XSetWindowAttributes attributes;
  41. attributes.colormap = XCreateColormap(x11->display, RootWindow(x11->display, vi->screen), vi->visual, AllocNone);
  42. attributes.background_pixmap = None;
  43. attributes.border_pixel = 0;
  44. // Disable window decorations.
  45. attributes.override_redirect = True;
  46. // Make sure we know when we've been successfully destroyed later!
  47. //attributes.event_mask = StructureNotifyMask;
  48. unsigned long valueMask = CWOverrideRedirect | CWColormap | CWBackPixmap | CWBorderPixel;
  49. // Create the window
  50. window = XCreateWindow( x11->display, x11->root, 0, 0, WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ),
  51. 0, vi->depth, InputOutput,
  52. vi->visual, valueMask, &attributes );
  53. if ( !window ) {
  54. throw new std::runtime_error( "Couldn't create a GL window!" );
  55. }
  56. // Prep some hints for the window
  57. static char title[] = "slop";
  58. XWMHints* startup_state = XAllocWMHints();
  59. startup_state->initial_state = NormalState;
  60. startup_state->flags = StateHint;
  61. XTextProperty textprop;
  62. textprop.value = (unsigned char*)title;
  63. textprop.encoding = XA_STRING;
  64. textprop.format = 8;
  65. textprop.nitems = strlen( title );
  66. XSizeHints sizehints;
  67. sizehints.x = 0;
  68. sizehints.y = 0;
  69. sizehints.width = WidthOfScreen( x11->screen );
  70. sizehints.height = HeightOfScreen( x11->screen );
  71. sizehints.flags = USPosition | USSize;
  72. XClassHint classhints;
  73. char name[] = "slop";
  74. classhints.res_name = name;
  75. classhints.res_class = name;
  76. // Finally send it all over...
  77. XSetClassHint( x11->display, window, &classhints );
  78. XSetWMProperties( x11->display, window, &textprop, &textprop, NULL, 0, &sizehints, startup_state, NULL );
  79. XFree( startup_state );
  80. // Keep the window on top of all other windows.
  81. Atom stateAbove = XInternAtom(x11->display, "_NET_WM_STATE_ABOVE", False);
  82. XChangeProperty(x11->display, window, XInternAtom(x11->display, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char *) &stateAbove, 1);
  83. context = glXCreateNewContext( x11->display, fbconfig, GLX_RGBA_TYPE, 0, True );
  84. if ( !context ) {
  85. throw new std::runtime_error( "Failed to create an OpenGL context." );
  86. }
  87. setCurrent();
  88. // Finally we grab some OpenGL 3.3 stuffs.
  89. if(ogl_LoadFunctions() == ogl_LOAD_FAILED)
  90. {
  91. throw new std::runtime_error("Failed to load function pointers for OpenGL.");
  92. }
  93. framebuffer = new Framebuffer( WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ) );
  94. glViewport( 0, 0, WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ) );
  95. camera = glm::ortho( 0.0f, (float)WidthOfScreen( x11->screen ), (float)HeightOfScreen( x11->screen ), 0.0f, -1.0f, 1.0f);
  96. // Last, we actually display the window <:o)
  97. XMapWindow( x11->display, window );
  98. }
  99. slop::SlopWindow::~SlopWindow() {
  100. delete framebuffer;
  101. // Try to erase the window before destroying it.
  102. glClearColor( 0, 0, 0, 0 );
  103. glClear( GL_COLOR_BUFFER_BIT );
  104. display();
  105. glClearColor( 0, 0, 0, 0 );
  106. glClear( GL_COLOR_BUFFER_BIT );
  107. display();
  108. glXDestroyContext( x11->display, context );
  109. XDestroyWindow( x11->display, window );
  110. }
  111. void slop::SlopWindow::display() {
  112. glXSwapBuffers( x11->display, window );
  113. glXWaitGL();
  114. }
  115. void slop::SlopWindow::setCurrent() {
  116. glXMakeCurrent( x11->display, window, context ) ;
  117. }