Click here to Skip to main content
15,881,381 members
Articles / Desktop Programming / MFC

Writing a Platform and GUI Toolkit Independent OpenGL Class

Rate me:
Please Sign up or sign in to vote.
4.92/5 (33 votes)
1 Nov 2010CPOL13 min read 96.6K   7.5K   89  
Article showing how to write OS and GUI toolkit portable OpenGL view class
// This MFC Samples source code demonstrates using MFC Microsoft Office Fluent User Interface 
// (the "Fluent UI") and is provided only as referential material to supplement the 
// Microsoft Foundation Classes Reference and related electronic documentation 
// included with the MFC C++ library software.  
// License terms to copy, use or distribute the Fluent UI are available separately.  
// To learn more about our Fluent UI licensing program, please visit 
// http://msdn.microsoft.com/officeui.
//
// Copyright (C) Microsoft Corporation
// All rights reserved.

// GLViewMFCMDIAppView.cpp : implementation of the CGLViewMFCMDIAppView class
//

#include "stdafx.h"
#include "GLViewMFCMDIApp.h"

#include "GLViewMFCMDIAppDoc.h"
#include "GLViewMFCMDIAppView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CGLViewMFCMDIAppView

IMPLEMENT_DYNCREATE(CGLViewMFCMDIAppView, CView)

BEGIN_MESSAGE_MAP(CGLViewMFCMDIAppView, CView)
	ON_WM_SIZE()
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CGLViewMFCMDIAppView::OnFilePrintPreview)
END_MESSAGE_MAP()

// CGLViewMFCMDIAppView construction/destruction

CGLViewMFCMDIAppView::CGLViewMFCMDIAppView()
{
	// TODO: add construction code here
	m_pGLView = new CGLView();
}

CGLViewMFCMDIAppView::~CGLViewMFCMDIAppView()
{
	if(m_pGLView)
	{
		delete m_pGLView;
	}
}

BOOL CGLViewMFCMDIAppView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CView::PreCreateWindow(cs);
}

void CGLViewMFCMDIAppView::OnInitialUpdate()
{
	CView::OnInitialUpdate();
	if(m_pGLView)
	{
		m_pGLView->SetWindow(GetSafeHwnd());
		m_pGLView->SetupGLContext(true);
		CRect rect;
		GetClientRect(&rect);
		m_pGLView->Resize(rect.Width(), rect.Height());
	}
}
// CGLViewMFCMDIAppView drawing

void CGLViewMFCMDIAppView::OnDraw(CDC* /*pDC*/)
{
	CGLViewMFCMDIAppDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
	if(m_pGLView)
	{
		m_pGLView->RenderScene();;
	}
}


// CGLViewMFCMDIAppView printing


void CGLViewMFCMDIAppView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL CGLViewMFCMDIAppView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGLViewMFCMDIAppView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGLViewMFCMDIAppView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CGLViewMFCMDIAppView::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CGLViewMFCMDIAppView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// CGLViewMFCMDIAppView diagnostics

#ifdef _DEBUG
void CGLViewMFCMDIAppView::AssertValid() const
{
	CView::AssertValid();
}

void CGLViewMFCMDIAppView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CGLViewMFCMDIAppDoc* CGLViewMFCMDIAppView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGLViewMFCMDIAppDoc)));
	return (CGLViewMFCMDIAppDoc*)m_pDocument;
}
#endif //_DEBUG


// CGLViewMFCMDIAppView message handlers
void CGLViewMFCMDIAppView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	if(m_pGLView)
	{
		m_pGLView->Resize(cx, cy);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Mahindra & Mahindra
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Comments and Discussions