OpenWalnut  1.3.1
WGEGraphicsWindow.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <iostream>
26 
27 #include "WGEGraphicsWindow.h"
28 
29 #include "exceptions/WGEInitFailed.h"
30 
31 WGEGraphicsWindow::WGEGraphicsWindow( osg::ref_ptr<osg::Referenced>
32  #ifdef WGEMODE_MULTITHREADED
33  wdata // this parameter is only needed on non-mac
34  #endif
35  ,
36  int x,
37  int y,
38  int width,
39  int height )
40 {
41 #ifdef WGEMODE_MULTITHREADED
42  // initialize context
43  m_WindowData = wdata;
44  try
45  {
46  createContext( x, y, width, height );
47  }
48  catch( ... )
49  {
50  // use our own exceptions
51  throw WGEInitFailed( "Initialization of OpenGL graphics context failed." );
52  }
53 #else
54  m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
55  static_cast<osgViewer::GraphicsWindow*>( new osgViewer::GraphicsWindowEmbedded( x, y, width, height ) ) );
56 #endif
57 }
58 
60 {
61  // cleanup
62 }
63 
64 osg::ref_ptr<osgViewer::GraphicsWindow> WGEGraphicsWindow::getGraphicsWindow()
65 {
66  return m_GraphicsWindow;
67 }
68 
69 #ifdef WGEMODE_MULTITHREADED
70 void WGEGraphicsWindow::createContext( int x, int y, int width, int height )
71 {
72  // Create traits for graphics contest request
73  osg::ref_ptr<osg::DisplaySettings> ds = osg::DisplaySettings::instance();
74  osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
75 
76  // ensure correct $DISPLAY variable
77  traits->readDISPLAY();
78  if( traits->displayNum < 0 )
79  {
80  traits->displayNum = 0;
81  }
82 
83  // set a lot of values
84  traits->windowName = "OpenWalnut";
85  traits->screenNum = 0; // XXX is this a good idea?
86  traits->x = x;
87  traits->y = y;
88  traits->width = width;
89  traits->height = height;
90  traits->alpha = ds->getMinimumNumAlphaBits();
91  traits->stencil = ds->getMinimumNumStencilBits();
92  // traits->windowDecoration = false;
93  traits->doubleBuffer = true;
94  traits->sharedContext = 0;
95  traits->sampleBuffers = ds->getMultiSamples();
96  traits->samples = ds->getNumMultiSamples();
97  traits->inheritedWindowData = m_WindowData;
98 
99  // finally create graphics context and window
100  m_GraphicsContext = osg::GraphicsContext::createGraphicsContext( traits.get() );
101 
102  m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
103  static_cast<osgViewer::GraphicsWindow*>( m_GraphicsContext.get() ) );
104 
105  // get around dearranged traits on X11 (MTCompositeViewer only)
106  traits->x = x;
107  traits->y = x;
108  traits->width = width;
109  traits->height = height;
110 }
111 #endif
112 
113 void WGEGraphicsWindow::resize( int width, int height )
114 {
115  m_GraphicsWindow->getEventQueue()->windowResize( 0, 0, width, height );
116  m_GraphicsWindow->resized( 0, 0, width, height );
117 }
118 
120 {
121  m_GraphicsWindow->getEventQueue()->closeWindow();
122 }
123 
124 void WGEGraphicsWindow::keyEvent( KeyEvents eventType, int key )
125 {
126  switch( eventType )
127  {
128  case KEYPRESS:
129  m_GraphicsWindow->getEventQueue()->keyPress( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
130  break;
131  case KEYRELEASE:
132  m_GraphicsWindow->getEventQueue()->keyRelease( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
133  break;
134  }
135 }
136 
137 void WGEGraphicsWindow::mouseEvent( MouseEvents eventType, int x, int y, int button )
138 {
139  switch( eventType )
140  {
141  case MOUSEPRESS:
142  m_GraphicsWindow->getEventQueue()->mouseButtonPress( x, y, button );
143  break;
144  case MOUSERELEASE:
145  m_GraphicsWindow->getEventQueue()->mouseButtonRelease( x, y, button );
146  break;
147  case MOUSEDOUBLECLICK:
148  m_GraphicsWindow->getEventQueue()->mouseDoubleButtonPress( x, y, button );
149  break;
150  case MOUSEMOVE:
151  m_GraphicsWindow->getEventQueue()->mouseMotion( x, y );
152  break;
153  case MOUSESCROLL:
154  m_GraphicsWindow->getEventQueue()->mouseScroll2D( x, y );
155  break;
156  }
157 }
158