OpenWalnut  1.4.0
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  m_closed( false )
41 {
42 #ifdef WGEMODE_MULTITHREADED
43  // initialize context
44  m_WindowData = wdata;
45  try
46  {
47  createContext( x, y, width, height );
48  }
49  catch( ... )
50  {
51  // use our own exceptions
52  throw WGEInitFailed( "Initialization of OpenGL graphics context failed." );
53  }
54 #else
55  m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
56  static_cast<osgViewer::GraphicsWindow*>( new osgViewer::GraphicsWindowEmbedded( x, y, width, height ) ) );
57 #endif
58 }
59 
61 {
62  // cleanup
63 }
64 
65 osg::ref_ptr<osgViewer::GraphicsWindow> WGEGraphicsWindow::getGraphicsWindow()
66 {
67  return m_GraphicsWindow;
68 }
69 
70 #ifdef WGEMODE_MULTITHREADED
71 void WGEGraphicsWindow::createContext( int x, int y, int width, int height )
72 {
73  // Create traits for graphics contest request
74  osg::ref_ptr<osg::DisplaySettings> ds = osg::DisplaySettings::instance();
75  osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
76 
77  // set a lot of values
78  traits->windowName = "OpenWalnut";
79  traits->x = x;
80  traits->y = y;
81  traits->width = width;
82  traits->height = height;
83  traits->alpha = ds->getMinimumNumAlphaBits();
84  traits->stencil = ds->getMinimumNumStencilBits();
85  traits->doubleBuffer = true;
86  traits->sharedContext = 0;
87  traits->sampleBuffers = ds->getMultiSamples();
88  traits->samples = ds->getNumMultiSamples();
89  traits->inheritedWindowData = m_WindowData;
90  traits->vsync = true;
91 
92  // finally create graphics context and window
93  m_GraphicsContext = osg::GraphicsContext::createGraphicsContext( traits.get() );
94 
95  m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
96  static_cast<osgViewer::GraphicsWindow*>( m_GraphicsContext.get() ) );
97 
98  // get around dearranged traits on X11 (MTCompositeViewer only)
99  traits->x = x;
100  traits->y = x;
101  traits->width = width;
102  traits->height = height;
103 }
104 #endif
105 
106 void WGEGraphicsWindow::resize( int width, int height )
107 {
108  m_GraphicsWindow->getEventQueue()->windowResize( 0, 0, width, height );
109  m_GraphicsWindow->resized( 0, 0, width, height );
110 }
111 
113 {
114  m_GraphicsWindow->getEventQueue()->closeWindow();
115 }
116 
117 void WGEGraphicsWindow::keyEvent( KeyEvents eventType, int key )
118 {
119  switch( eventType )
120  {
121  case KEYPRESS:
122  m_GraphicsWindow->getEventQueue()->keyPress( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
123  break;
124  case KEYRELEASE:
125  m_GraphicsWindow->getEventQueue()->keyRelease( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
126  break;
127  }
128 }
129 
130 void WGEGraphicsWindow::mouseEvent( MouseEvents eventType, int x, int y, int button )
131 {
132  switch( eventType )
133  {
134  case MOUSEPRESS:
135  m_GraphicsWindow->getEventQueue()->mouseButtonPress( x, y, button );
136  break;
137  case MOUSERELEASE:
138  m_GraphicsWindow->getEventQueue()->mouseButtonRelease( x, y, button );
139  break;
140  case MOUSEDOUBLECLICK:
141  m_GraphicsWindow->getEventQueue()->mouseDoubleButtonPress( x, y, button );
142  break;
143  case MOUSEMOVE:
144  m_GraphicsWindow->getEventQueue()->mouseMotion( x, y );
145  break;
146  case MOUSESCROLL:
147  m_GraphicsWindow->getEventQueue()->mouseScroll2D( x, y );
148  break;
149  }
150 }
151 
153 {
154  return m_closed;
155 }
156 
157 void WGEGraphicsWindow::setClosed( bool closed )
158 {
159  m_closed = closed;
160 }
161