Show
Ignore:
Timestamp:
06/02/07 18:23:07 (8 years ago)
Author:
robert
Message:

Ported across from SimpleViewer? to using Viewer

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgkeyboardmouse/osgkeyboardmouse.cpp

    r6734 r6817  
    11// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL. 
    22// 
    3 // Simple example of use of osgViewer::GraphicsWindow + SimpleViewer 
     3// Simple example of use of osgViewer::GraphicsWindow + Viewer 
    44// example that provides the user with control over view position with basic picking. 
    55 
     
    1818#include <osgGA/StateSetManipulator> 
    1919 
    20 #include <osgViewer/SimpleViewer> 
    21 #include <osgViewer/GraphicsWindow> 
     20#include <osgViewer/Viewer> 
    2221 
    2322#include <osgFX/Scribe> 
     
    101100}; 
    102101 
    103 class ExitHandler : public osgGA::GUIEventHandler  
    104 { 
    105 public:  
    106  
    107     ExitHandler(): 
    108         _done(false) {} 
    109          
    110     bool done() const { return _done; }     
    111  
    112     bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) 
    113     { 
    114         switch(ea.getEventType()) 
    115         { 
    116             case(osgGA::GUIEventAdapter::KEYUP): 
    117             { 
    118                 if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Escape) 
    119                 { 
    120                     _done = true; 
    121                 } 
    122                 return false; 
    123             } 
    124             case(osgGA::GUIEventAdapter::CLOSE_WINDOW): 
    125             case(osgGA::GUIEventAdapter::QUIT_APPLICATION): 
    126             { 
    127                 _done = true; 
    128             } 
    129             default: break; 
    130         } 
    131          
    132         return false; 
    133     } 
    134      
    135     bool _done; 
    136 }; 
    137  
    138  
    139  
    140102// class to handle events with a pick 
    141103class PickHandler : public osgGA::GUIEventHandler  
     
    152114    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) 
    153115    { 
    154         osgViewer::SimpleViewer* viewer = dynamic_cast<osgViewer::SimpleViewer*>(&aa); 
     116        osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa); 
    155117        if (!viewer) return false; 
    156118 
     
    219181    } 
    220182 
    221     void pick(const osgGA::GUIEventAdapter& ea, osgViewer::SimpleViewer* viewer) 
     183    void pick(const osgGA::GUIEventAdapter& ea, osgViewer::Viewer* viewer) 
    222184    { 
    223185        osg::Node* scene = viewer->getSceneData(); 
     
    392354    } 
    393355 
    394     gw->realize(); 
    395     gw->makeCurrent(); 
    396  
    397356    // create the view of the scene. 
    398     osgViewer::SimpleViewer viewer; 
     357    osgViewer::Viewer viewer; 
     358    viewer.getCamera()->setGraphicsContext(gc.get()); 
     359    viewer.getCamera()->setViewport(0,0,800,600); 
    399360    viewer.setSceneData(loadedModel.get()); 
    400361     
    401     viewer.setEventQueue(gw->getEventQueue()); 
    402     viewer.getEventQueue()->windowResize(traits->x,traits->y,traits->width,traits->height); 
    403  
    404362    // create a tracball manipulator to move the camera around in response to keyboard/mouse events 
    405363    viewer.setCameraManipulator( new osgGA::TrackballManipulator ); 
    406364 
    407     osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator; 
    408     statesetManipulator->setStateSet(viewer.getSceneView()->getGlobalStateSet()); 
     365    osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(viewer.getCamera()->getStateSet()); 
    409366    viewer.addEventHandler(statesetManipulator.get()); 
    410367 
     
    412369    viewer.addEventHandler(new PickHandler()); 
    413370 
    414  
    415     // add the exit handler' 
    416     ExitHandler* exitHandler = new ExitHandler; 
    417     viewer.addEventHandler(exitHandler); 
    418  
    419     viewer.init(); 
     371    viewer.realize(); 
    420372 
    421373    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.) 
    422     while( gw->isRealized() && !exitHandler->done()) 
    423     { 
    424         gw->checkEvents(); 
    425          
    426         if (gw->isRealized() && !exitHandler->done()) 
    427         {       
    428             viewer.frame(); 
    429  
    430             // Swap Buffers 
    431             gw->swapBuffers(); 
    432         } 
     374    while(!viewer.done()) 
     375    { 
     376        viewer.frame(); 
    433377    } 
    434378