I'm trying to build a nice MVC app with OSG.  I'm getting a seg-fault whenever 
the app exits.  I realize that this is due to the 
AppController->osgViewer::Viewer->AppController reference loop (see line with 
XXXX in the runLoop() method).  The question is whether there is a "reasonable" 
way to have the AppController be the event handler for the viewer?

Lee
#include <iostream>

#include <osg/ArgumentParser>
#include <osgDB/ReadFile>

#include <osgGA/GUIEventHandler>
#include <osgGA/TrackballManipulator>

#include <osgViewer/Viewer>

class AppController : public osgGA::GUIEventHandler
{
public:
    AppController(int &argc, char *argv[])
    {
	osg::ArgumentParser arguments(&argc, argv);
	_viewer = new osgViewer::Viewer(arguments);

	_loadedModel = osgDB::readNodeFiles(arguments);

	_rootnode = new osg::Group;
	_rootnode->setName("root");
	_rootnode->addChild(_loadedModel);	

	_viewer->setSceneData( _rootnode );
    }	

    void runLoop()
    {
	_viewer->addEventHandler(this);  // XXXX  This is BAD

	osg::ref_ptr<osgGA::TrackballManipulator> tb = new osgGA::TrackballManipulator();
	_viewer->setCameraManipulator(tb);

	_viewer->setReleaseContextAtEndOfFrameHint(false);
	std::cerr << "start draw" << std::endl;

	_viewer->run();
    }


    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
    {
	if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN && ea.getKey() == 'h') {
	    std::cerr << "Hello!" << std::endl;
	    return true;
	}
	return false;
    }

private:
    osg::ref_ptr <osgViewer::Viewer> _viewer;
    osg::ref_ptr<osg::Node> _loadedModel;
    osg::ref_ptr<osg::Group> _rootnode;
};

int main(int argc, char *argv[])
{
    osg::ref_ptr<AppController> app = new AppController(argc, argv);

    app->runLoop();

    return 0;
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to