12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "window.hpp"
  2. SlopWindow::SlopWindow() {
  3. XVisualInfo visual;
  4. XMatchVisualInfo(x11->display, DefaultScreen(x11->display), 32, TrueColor, &visual);
  5. XSetWindowAttributes attributes;
  6. attributes.colormap = XCreateColormap( x11->display, x11->root, visual.visual, AllocNone );
  7. attributes.background_pixmap = None;
  8. attributes.border_pixel = 0;
  9. // Disable window decorations.
  10. attributes.override_redirect = True;
  11. // Make sure we know when we've been successfully destroyed later!
  12. //attributes.event_mask = StructureNotifyMask;
  13. unsigned long valueMask = CWOverrideRedirect | CWColormap | CWBackPixmap | CWBorderPixel;
  14. // Create the window
  15. window = XCreateWindow( x11->display, x11->root, 0, 0, WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ),
  16. 0, visual.depth, InputOutput,
  17. visual.visual, valueMask, &attributes );
  18. if ( !window ) {
  19. throw new std::runtime_error( "Couldn't create a GL window!" );
  20. }
  21. // Prep some hints for the window
  22. static char title[] = "slop";
  23. XWMHints* startup_state = XAllocWMHints();
  24. startup_state->initial_state = NormalState;
  25. startup_state->flags = StateHint;
  26. XTextProperty textprop;
  27. textprop.value = (unsigned char*)title;
  28. textprop.encoding = XA_STRING;
  29. textprop.format = 8;
  30. textprop.nitems = strlen( title );
  31. XSizeHints sizehints;
  32. sizehints.x = 0;
  33. sizehints.y = 0;
  34. sizehints.width = WidthOfScreen( x11->screen );
  35. sizehints.height = HeightOfScreen( x11->screen );
  36. sizehints.flags = USPosition | USSize;
  37. XClassHint classhints;
  38. char name[] = "slop";
  39. classhints.res_name = name;
  40. classhints.res_class = name;
  41. // Finally send it all over...
  42. XSetClassHint( x11->display, window, &classhints );
  43. XSetWMProperties( x11->display, window, &textprop, &textprop, NULL, 0, &sizehints, startup_state, NULL );
  44. XFree( startup_state );
  45. // Load up a opengl context
  46. context = glXCreateContext( x11->display, &visual, 0, True );
  47. if ( !context ) {
  48. throw new std::runtime_error( "Failed to create an OpenGL context." );
  49. }
  50. setCurrent();
  51. // Finally we grab some OpenGL 3.3 stuffs.
  52. if(ogl_LoadFunctions() == ogl_LOAD_FAILED)
  53. {
  54. throw new std::runtime_error("Failed to load function pointers for OpenGL.");
  55. }
  56. framebuffer = new Framebuffer( WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ) );
  57. glViewport( 0, 0, WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ) );
  58. camera = glm::ortho( 0.0f, (float)WidthOfScreen( x11->screen ), (float)HeightOfScreen( x11->screen ), 0.0f, -1.0f, 1.0f);
  59. // Make it so all input falls through
  60. XRectangle rect;
  61. rect.x = rect.y = rect.width = rect.height = 0;
  62. XShapeCombineRectangles( x11->display, window, ShapeInput, 0, 0, &rect, 1, ShapeSet, 0);
  63. // Last, we actually display the window <:o)
  64. XMapWindow( x11->display, window );
  65. }
  66. SlopWindow::~SlopWindow() {
  67. delete framebuffer;
  68. // Try to erase the window before destroying it.
  69. glClearColor( 0, 0, 0, 0 );
  70. glClear( GL_COLOR_BUFFER_BIT );
  71. display();
  72. glClearColor( 0, 0, 0, 0 );
  73. glClear( GL_COLOR_BUFFER_BIT );
  74. display();
  75. glXDestroyContext( x11->display, context );
  76. XDestroyWindow( x11->display, window );
  77. }
  78. void SlopWindow::display() {
  79. glXSwapBuffers( x11->display, window );
  80. glXWaitGL();
  81. }
  82. void SlopWindow::setCurrent() {
  83. glXMakeCurrent( x11->display, window, context ) ;
  84. }