OpenWalnut  1.4.0
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     m_closed( false )
00041 {
00042 #ifdef WGEMODE_MULTITHREADED
00043     // initialize context
00044     m_WindowData = wdata;
00045     try
00046     {
00047         createContext( x, y, width, height );
00048     }
00049     catch( ... )
00050     {
00051         // use our own exceptions
00052         throw WGEInitFailed( "Initialization of OpenGL graphics context failed." );
00053     }
00054 #else
00055     m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
00056             static_cast<osgViewer::GraphicsWindow*>( new osgViewer::GraphicsWindowEmbedded( x, y, width, height ) ) );
00057 #endif
00058 }
00059 
00060 WGEGraphicsWindow::~WGEGraphicsWindow()
00061 {
00062     // cleanup
00063 }
00064 
00065 osg::ref_ptr<osgViewer::GraphicsWindow> WGEGraphicsWindow::getGraphicsWindow()
00066 {
00067     return m_GraphicsWindow;
00068 }
00069 
00070 #ifdef WGEMODE_MULTITHREADED
00071 void WGEGraphicsWindow::createContext( int x, int y, int width, int height )
00072 {
00073     // Create traits for graphics contest request
00074     osg::ref_ptr<osg::DisplaySettings> ds = osg::DisplaySettings::instance();
00075     osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
00076 
00077     // set a lot of values
00078     traits->windowName = "OpenWalnut";
00079     traits->x = x;
00080     traits->y = y;
00081     traits->width = width;
00082     traits->height = height;
00083     traits->alpha = ds->getMinimumNumAlphaBits();
00084     traits->stencil = ds->getMinimumNumStencilBits();
00085     traits->doubleBuffer = true;
00086     traits->sharedContext = 0;
00087     traits->sampleBuffers = ds->getMultiSamples();
00088     traits->samples = ds->getNumMultiSamples();
00089     traits->inheritedWindowData = m_WindowData;
00090     traits->vsync = true;
00091 
00092     // finally create graphics context and window
00093     m_GraphicsContext = osg::GraphicsContext::createGraphicsContext( traits.get() );
00094 
00095     m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
00096             static_cast<osgViewer::GraphicsWindow*>( m_GraphicsContext.get() ) );
00097 
00098     // get around dearranged traits on X11 (MTCompositeViewer only)
00099     traits->x = x;
00100     traits->y = x;
00101     traits->width = width;
00102     traits->height = height;
00103 }
00104 #endif
00105 
00106 void WGEGraphicsWindow::resize( int width, int height )
00107 {
00108     m_GraphicsWindow->getEventQueue()->windowResize( 0, 0, width, height );
00109     m_GraphicsWindow->resized( 0, 0, width, height );
00110 }
00111 
00112 void WGEGraphicsWindow::close()
00113 {
00114     m_GraphicsWindow->getEventQueue()->closeWindow();
00115 }
00116 
00117 void WGEGraphicsWindow::keyEvent( KeyEvents eventType, int key )
00118 {
00119     switch( eventType )
00120     {
00121         case KEYPRESS:
00122             m_GraphicsWindow->getEventQueue()->keyPress( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
00123             break;
00124         case KEYRELEASE:
00125             m_GraphicsWindow->getEventQueue()->keyRelease( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
00126             break;
00127     }
00128 }
00129 
00130 void WGEGraphicsWindow::mouseEvent( MouseEvents eventType, int x, int y, int button )
00131 {
00132     switch( eventType )
00133     {
00134         case MOUSEPRESS:
00135             m_GraphicsWindow->getEventQueue()->mouseButtonPress( x, y, button );
00136             break;
00137         case MOUSERELEASE:
00138             m_GraphicsWindow->getEventQueue()->mouseButtonRelease( x, y, button );
00139             break;
00140         case MOUSEDOUBLECLICK:
00141             m_GraphicsWindow->getEventQueue()->mouseDoubleButtonPress( x, y, button );
00142             break;
00143         case MOUSEMOVE:
00144             m_GraphicsWindow->getEventQueue()->mouseMotion( x, y );
00145             break;
00146         case MOUSESCROLL:
00147             m_GraphicsWindow->getEventQueue()->mouseScroll2D( x, y );
00148             break;
00149     }
00150 }
00151 
00152 bool WGEGraphicsWindow::isClosed() const
00153 {
00154     return m_closed;
00155 }
00156 
00157 void WGEGraphicsWindow::setClosed( bool closed )
00158 {
00159     m_closed = closed;
00160 }
00161