Show
Ignore:
Timestamp:
08/31/07 18:59:32 (7 years ago)
Author:
robert
Message:

Added support for running multiple text generation threads

Files:
1 modified

Legend:

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

    r7310 r7329  
    604604    // construct the viewer. 
    605605    osgViewer::Viewer viewer(arguments); 
    606  
    607     osg::ref_ptr<osg::OperationThread> operationThread; 
     606     
     607    typedef std::list< osg::ref_ptr<osg::OperationThread> > Threads; 
     608 
     609    Threads operationThreads; 
    608610    osg::ref_ptr<UpdateTextOperation> updateOperation; 
    609611 
    610     if (arguments.read("--mt")) 
     612    unsigned int numThreads = 0; 
     613    if (arguments.read("--mt", numThreads) || arguments.read("--mt")) 
    611614    { 
    612615        // construct a multi-threaded text updating test. 
     616        if (numThreads==0) numThreads = 1; 
    613617         
    614618        // create a group to add everything into. 
    615619        osg::Group* mainGroup = new osg::Group; 
    616  
    617         osg::Group* textGroup = new osg::Group; 
    618         mainGroup->addChild(textGroup); 
    619  
    620         // create the background thread 
    621         operationThread = new osg::OperationThread; 
    622          
    623         // create the operation that will run in the background and 
    624         // sync once per frame with the main viewer loop. 
    625         updateOperation = new UpdateTextOperation(textGroup); 
    626          
    627         // add the operation to the operation thread and start it. 
    628         operationThread->add(updateOperation.get()); 
    629         operationThread->startThread(); 
    630          
    631         // add the operation to the viewer to sync once per frame. 
    632         viewer.addUpdateOperation(updateOperation.get()); 
    633  
    634  
    635         // add a unit cube for the text to appear within. 
    636         osg::Geode* geode = new osg::Geode; 
    637         geode->getOrCreateStateSet()->setAttribute(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE)); 
    638         geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.5f,0.5f,0.5f),1.0))); 
    639  
    640         mainGroup->addChild(geode); 
    641          
     620         
     621        for(unsigned int i=0; i<numThreads; ++i) 
     622        { 
     623            osg::Group* textGroup = new osg::Group; 
     624            mainGroup->addChild(textGroup); 
     625 
     626            // create the background thread 
     627            osg::OperationThread* operationThread = new osg::OperationThread; 
     628             
     629            operationThreads.push_back(operationThread); 
     630 
     631            // create the operation that will run in the background and 
     632            // sync once per frame with the main viewer loop. 
     633            updateOperation = new UpdateTextOperation(textGroup); 
     634 
     635            // add the operation to the operation thread and start it. 
     636            operationThread->add(updateOperation.get()); 
     637            operationThread->startThread(); 
     638 
     639            // add the operation to the viewer to sync once per frame. 
     640            viewer.addUpdateOperation(updateOperation.get()); 
     641 
     642 
     643            // add a unit cube for the text to appear within. 
     644            osg::Geode* geode = new osg::Geode; 
     645            geode->getOrCreateStateSet()->setAttribute(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE)); 
     646            geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.5f,0.5f,0.5f),1.0))); 
     647 
     648            mainGroup->addChild(geode); 
     649        } 
     650                 
    642651        viewer.setSceneData(mainGroup);         
    643652    } 
     
    678687    viewer.run(); 
    679688     
    680     if (operationThread.valid()) 
    681     { 
    682         operationThread->cancel(); 
     689    if (!operationThreads.empty()) 
     690    { 
     691        for(Threads::iterator itr = operationThreads.begin(); 
     692            itr != operationThreads.begin(); 
     693            ++itr) 
     694        { 
     695            (*itr)->cancel(); 
     696        } 
    683697    } 
    684698}