Changeset 7152

Show
Ignore:
Timestamp:
07/23/07 22:37:49
Author:
robert
Message:

From Michael Hartman, "Here is an update for the closing issue with the example osgviewerMFC where the MFC rendering thread would not exit before the application and the thread would be left running in the background and the user would have to use TaskManager? to kill the process.

Changes:

MFC_OSG.cpp:

Removed pixelformatdesciptor from the class initialization.

Used setInheritedWindowPixelFormat to true so it will setup the pixelformat for the window.

Added class destructor code.

MFC_OSG.h:

Removed the ref_ptr on osgViewer
Viewer

MFC_OSG_MDIViewer.cpp:

Changed the OnDestroy? function code.

Added WaitforSingleObject? with thread handle for the MFC render handle.

MFC_OSG_MDIView.h:

Added class variable for MFC Render Thread Handle for use with the WaitforSingleObject?.
"


Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OpenSceneGraph/trunk/examples/osgviewerMFC/MFC_OSG.cpp

    r6808 r7152  
    66 
    77cOSG::cOSG(HWND hWnd) : 
    8    m_hWnd(hWnd), mDone(false)  
     8   m_hWnd(hWnd)  
    99{ 
    10     // 
    11     // We must set the pixelformat before we can create the OSG Rendering Surface 
    12     // 
    13     PIXELFORMATDESCRIPTOR pixelFormat = 
    14     { 
    15         sizeof(PIXELFORMATDESCRIPTOR), 
    16         1, 
    17         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,  
    18         PFD_TYPE_RGBA,     
    19         24, 
    20         0, 0, 0, 0, 0, 0, 0, 0, 
    21         0, 0, 0, 0, 0, 
    22         24, 
    23         0, 
    24         0, 
    25         PFD_MAIN_PLANE, 
    26         0, 
    27         0, 0, 0 
    28     }; 
     10
    2911 
    30     HDC hdc = ::GetDC(m_hWnd); 
    31     if (hdc==0) 
    32     { 
    33         ::DestroyWindow(m_hWnd); 
    34         return; 
    35     } 
     12cOSG::~cOSG() 
     13
     14    mViewer->setDone(true); 
     15    Sleep(1000); 
     16    mViewer->stopThreading(); 
    3617 
    37     int pixelFormatIndex = ::ChoosePixelFormat(hdc, &pixelFormat); 
    38     if (pixelFormatIndex==0) 
    39     { 
    40         ::ReleaseDC(m_hWnd, hdc); 
    41         ::DestroyWindow(m_hWnd); 
    42         return; 
    43     } 
    44  
    45     if (!::SetPixelFormat(hdc, pixelFormatIndex, &pixelFormat)) 
    46     { 
    47         ::ReleaseDC(m_hWnd, hdc); 
    48         ::DestroyWindow(m_hWnd); 
    49         return; 
    50     } 
     18    delete mViewer; 
    5119} 
    5220 
     
    10169 
    10270    // Create the viewer for this window 
    103     mViewer = new osgViewer::Viewer
     71    mViewer = new osgViewer::Viewer()
    10472 
    10573    // Add a Stats Handler to the viewer 
     
    12391    traits->doubleBuffer = true; 
    12492    traits->sharedContext = 0; 
     93    traits->setInheritedWindowPixelFormat = true; 
    12594    traits->inheritedWindowData = windata; 
    12695 
     
    184153    AfxMessageBox("Exit Rendering Thread"); 
    185154 
    186     // Set Done to indicate that thread has exited 
    187     osg->Done(true); 
     155    _endthread(); 
    188156} 
  • OpenSceneGraph/trunk/examples/osgviewerMFC/MFC_OSG.h

    r6864 r7152  
    1616public: 
    1717    cOSG(HWND hWnd); 
    18     ~cOSG(){}
     18    ~cOSG()
    1919 
    2020    void InitOSG(std::string filename); 
     
    3030    static void Render(void* ptr); 
    3131 
    32     osgViewer::Viewer* getViewer() { return mViewer.get(); } 
     32    osgViewer::Viewer* getViewer() { return mViewer; } 
    3333 
    3434private: 
     
    3636    std::string m_ModelName; 
    3737    HWND m_hWnd; 
    38     osg::ref_ptr<osgViewer::Viewer> mViewer; 
     38    osgViewer::Viewer* mViewer; 
    3939    osg::ref_ptr<osg::Group> mRoot; 
    4040    osg::ref_ptr<osg::Node> mModel; 
  • OpenSceneGraph/trunk/examples/osgviewerMFC/MFC_OSG_MDIView.cpp

    r6808 r7152  
    7575void CMFC_OSG_MDIView::OnDestroy() 
    7676{ 
    77     // Make sure OSG was created before we try to close it. 
    78     if(mOSG) 
    79     { 
    80         
    81         // Wait while the Viewer closes 
    82         while(!mOSG->Done()) 
    83         { 
    84             Sleep(10); // Allow others processor time 
    85         } 
    86          
    87         // Remove mOSG 
    88         delete mOSG; 
    89     } 
     77    if(mOSG != 0) delete mOSG; 
    9078 
    91     // Destroy Window 
     79    WaitForSingleObject(mThreadHandle, 1000); 
     80 
    9281    CView::OnDestroy(); 
    9382} 
     
    10493 
    10594    // Start the thread to do OSG Rendering 
    106     _beginthread(&cOSG::Render, 0, mOSG);  
     95    mThreadHandle = (HANDLE)_beginthread(&cOSG::Render, 0, mOSG);  
    10796} 
    10897 
  • OpenSceneGraph/trunk/examples/osgviewerMFC/MFC_OSG_MDIView.h

    r6643 r7152  
    3535protected: 
    3636    cOSG* mOSG; 
     37    HANDLE mThreadHandle; 
    3738 
    3839// Generated message map functions