window.cpp 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. XMapWindow( x11->display, window );
  58. glViewport( 0, 0, WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ) );
  59. camera = glm::ortho( 0.0f, (float)WidthOfScreen( x11->screen ), (float)HeightOfScreen( x11->screen ), 0.0f, -1.0f, 1.0f);
  60. }
  61. SlopWindow::~SlopWindow() {
  62. delete framebuffer;
  63. // Try to erase the window before destroying it.
  64. glClearColor( 0, 0, 0, 0 );
  65. glClear( GL_COLOR_BUFFER_BIT );
  66. display();
  67. glClearColor( 0, 0, 0, 0 );
  68. glClear( GL_COLOR_BUFFER_BIT );
  69. display();
  70. XDestroyWindow( x11->display, window );
  71. }
  72. void SlopWindow::display() {
  73. glXSwapBuffers( x11->display, window );
  74. glXWaitGL();
  75. }
  76. void SlopWindow::setCurrent() {
  77. glXMakeCurrent( x11->display, window, context ) ;
  78. }