OpenWalnut 1.3.1
WGEGraphicsWindow.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <iostream>
00026 
00027 #include "WGEGraphicsWindow.h"
00028 
00029 #include "exceptions/WGEInitFailed.h"
00030 
00031 WGEGraphicsWindow::WGEGraphicsWindow( osg::ref_ptr<osg::Referenced>
00032      #ifdef WGEMODE_MULTITHREADED
00033         wdata  // this parameter is only needed on non-mac
00034      #endif
00035         ,
00036                                             int x,
00037                                             int y,
00038                                             int width,
00039                                             int height )
00040 {
00041 #ifdef WGEMODE_MULTITHREADED
00042     // initialize context
00043     m_WindowData = wdata;
00044     try
00045     {
00046         createContext( x, y, width, height );
00047     }
00048     catch( ... )
00049     {
00050         // use our own exceptions
00051         throw WGEInitFailed( "Initialization of OpenGL graphics context failed." );
00052     }
00053 #else
00054     m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
00055             static_cast<osgViewer::GraphicsWindow*>( new osgViewer::GraphicsWindowEmbedded( x, y, width, height ) ) );
00056 #endif
00057 }
00058 
00059 WGEGraphicsWindow::~WGEGraphicsWindow()
00060 {
00061     // cleanup
00062 }
00063 
00064 osg::ref_ptr<osgViewer::GraphicsWindow> WGEGraphicsWindow::getGraphicsWindow()
00065 {
00066     return m_GraphicsWindow;
00067 }
00068 
00069 #ifdef WGEMODE_MULTITHREADED
00070 void WGEGraphicsWindow::createContext( int x, int y, int width, int height )
00071 {
00072     // Create traits for graphics contest request
00073     osg::ref_ptr<osg::DisplaySettings> ds = osg::DisplaySettings::instance();
00074     osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
00075 
00076     // ensure correct $DISPLAY variable
00077     traits->readDISPLAY();
00078     if( traits->displayNum < 0 )
00079     {
00080         traits->displayNum = 0;
00081     }
00082 
00083     // set a lot of values
00084     traits->windowName = "OpenWalnut";
00085     traits->screenNum = 0;      // XXX is this a good idea?
00086     traits->x = x;
00087     traits->y = y;
00088     traits->width = width;
00089     traits->height = height;
00090     traits->alpha = ds->getMinimumNumAlphaBits();
00091     traits->stencil = ds->getMinimumNumStencilBits();
00092     // traits->windowDecoration = false;
00093     traits->doubleBuffer = true;
00094     traits->sharedContext = 0;
00095     traits->sampleBuffers = ds->getMultiSamples();
00096     traits->samples = ds->getNumMultiSamples();
00097     traits->inheritedWindowData = m_WindowData;
00098 
00099     // finally create graphics context and window
00100     m_GraphicsContext = osg::GraphicsContext::createGraphicsContext( traits.get() );
00101 
00102     m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
00103             static_cast<osgViewer::GraphicsWindow*>( m_GraphicsContext.get() ) );
00104 
00105     // get around dearranged traits on X11 (MTCompositeViewer only)
00106     traits->x = x;
00107     traits->y = x;
00108     traits->width = width;
00109     traits->height = height;
00110 }
00111 #endif
00112 
00113 void WGEGraphicsWindow::resize( int width, int height )
00114 {
00115     m_GraphicsWindow->getEventQueue()->windowResize( 0, 0, width, height );
00116     m_GraphicsWindow->resized( 0, 0, width, height );
00117 }
00118 
00119 void WGEGraphicsWindow::close()
00120 {
00121     m_GraphicsWindow->getEventQueue()->closeWindow();
00122 }
00123 
00124 void WGEGraphicsWindow::keyEvent( KeyEvents eventType, int key )
00125 {
00126     switch( eventType )
00127     {
00128         case KEYPRESS:
00129             m_GraphicsWindow->getEventQueue()->keyPress( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
00130             break;
00131         case KEYRELEASE:
00132             m_GraphicsWindow->getEventQueue()->keyRelease( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
00133             break;
00134     }
00135 }
00136 
00137 void WGEGraphicsWindow::mouseEvent( MouseEvents eventType, int x, int y, int button )
00138 {
00139     switch( eventType )
00140     {
00141         case MOUSEPRESS:
00142             m_GraphicsWindow->getEventQueue()->mouseButtonPress( x, y, button );
00143             break;
00144         case MOUSERELEASE:
00145             m_GraphicsWindow->getEventQueue()->mouseButtonRelease( x, y, button );
00146             break;
00147         case MOUSEDOUBLECLICK:
00148             m_GraphicsWindow->getEventQueue()->mouseDoubleButtonPress( x, y, button );
00149             break;
00150         case MOUSEMOVE:
00151             m_GraphicsWindow->getEventQueue()->mouseMotion( x, y );
00152             break;
00153         case MOUSESCROLL:
00154             m_GraphicsWindow->getEventQueue()->mouseScroll2D( x, y );
00155             break;
00156     }
00157 }
00158