I have an MFC based Windows desktop SDI application built with the Doc-View framework and which displays modern OpenGL based graphics in the MainFrame.
All the common frequently encountered/used OpenGL operations are managed by small library of dedicated classes (e.g Camera, PipelineManager, ShaderManager, Quaternion, etc).
In addition to the project default View class (MFCAPPView) I have created a second View class (SecondaryView).
I want to be able to switch/toggle the default project View class to the second View class while the application is running, via a Mainmenu option 'Switch View' (i.e. make the second View class to be the active view).
Both View classes have an OnCreate()
, OnDraw()
and OnDestroy()
.
The initialisation of the OpenGL (i.e. loading of shaders specific for the View, etc) is done in OnCreate()
.
int MFCAPPView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
{
return -1;
}
//initialize the screen, create OpenGL context based on arguments
m_screen.Initialize(GetSafeHwnd(), 32, 3, 0, false);
//create the shader manager
m_shaderManager = std::make_unique<GLShaderManager>();
m_shaderManager->CreateMainShader();
m_shaderManager->CreateTextShader();
m_shaderManager->CreateLightShader();
.
.
.
}
The OnDraw()
takes the form:
void CMFCAPPView::OnDraw(CDC*)
{
CMFCAPPDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
m_screen.ActivateContext();
.
.
.
}
And the OnDestroy()
complete code is:
void CMFCAPPView::OnDestroy()
{
//disable the rendering context
m_screen.DeactivateContext();
//close down window
m_screen.ShutDown();
//call the parent class 'CView' to destroy the window
CView::OnDestroy();
}
In the App class header I define:
CView* SwitchView();
CView* m_defaultView = NULL;
CView* m_secondaryView = NULL;
The initial basic code I have for the 'Switch View' event handler is based on Microsoft sample code for Switching Function: link
CView* CMFCAPPApp::SwitchView()
{
CView* pActiveView = ((CFrameWnd*)m_pMainWnd)->GetActiveView();
CView* pNewView = NULL;
if (pActiveView == m_defaultView)
{
dynamic_cast<CMFCAPPView*>(pActiveView)->OnDestroy();
pActiveView = m_secondaryView;
dynamic_cast<SecondaryView*>(pActiveView)->OnCreate();
pNewView = m_secondaryView;
}
else
pNewView = m_secondaryView;
.
.
.
}
When I try to use the 'Switch View' menu option nothing happens at all. At very least I would have expected that when the OnDestroy()
is called that the default view OpenGL context would be deactivated and I would then see just a blank and black View.
What am I missing or not doing correctly?