window.cpp 3.3KB

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