Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / MFC

OAG Library (OpenGL) Part 1 - Setting Up the Library for an MFC Application

Rate me:
Please Sign up or sign in to vote.
4.40/5 (11 votes)
7 Aug 2011CPOL3 min read 56.3K   56  
OAG is a library written in C++. With this library, you can create OpenGL based applications.
// OAGMFCDoc.cpp : implementation of the COAGMFCDoc class
//

#include "stdafx.h"
#include "OAGMFC.h"

#include "OAGMFCDoc.h"

#include <OAGPrimitives.h>
#include <OAGTextureLoader.h>
#include <OAGFontLoader.h>
#include <OAGParser.h>

#include <XmlDocument.h>
#include <XmlWriterDocument.h>

#include "OAGMFCView.h"

//#include <OAGFont2D.h>
//#include <OAGTextureLoader.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// COAGMFCDoc

IMPLEMENT_DYNCREATE(COAGMFCDoc, CDocument)

BEGIN_MESSAGE_MAP(COAGMFCDoc, CDocument)
	ON_COMMAND(ID_FILE_OPEN, &COAGMFCDoc::OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, &COAGMFCDoc::OnFileSave)
	ON_COMMAND(ID_INSERT_TEXTURE, &COAGMFCDoc::OnInsertTexture)
	ON_COMMAND(ID_INSERT_FONT, &COAGMFCDoc::OnInsertFont)
END_MESSAGE_MAP()


// COAGMFCDoc construction/destruction

COAGMFCDoc::COAGMFCDoc()
{
	CreateLibraryObjects();
}

COAGMFCDoc::~COAGMFCDoc()
{
	UnloadLibraryObjects();
}

BOOL COAGMFCDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	//Deleting all library objects when a new document is started.
	//The Fonts must be deleted and created again,
	//if not they are not drawn properly
	UnloadLibraryObjects();
	CreateLibraryObjects();
	
	UpdateAllViews(NULL);

	return TRUE;
}

// COAGMFCDoc serialization

void COAGMFCDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}


// COAGMFCDoc diagnostics

#ifdef _DEBUG
void COAGMFCDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void COAGMFCDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// COAGMFCDoc commands

void COAGMFCDoc::CreateLibraryObjects()
{
	m_pScene = new oag::OAGScene();

	m_pTextureMappingTable = new oag::TextureMappingTable();
	m_pScene->SetTextureMappingTable( m_pTextureMappingTable );

	m_pFontMappingTable = new oag::FontMappingTable();
	m_pScene->SetFontMappingTable( m_pFontMappingTable );
}

void COAGMFCDoc::UnloadLibraryObjects()
{
	//Deletes the scene and all objects from the memory
	if ( m_pScene )
	{
		m_pScene->DeleteAllObjects();

		delete m_pScene;
		m_pScene = NULL;
	}

	if( m_pTextureMappingTable )
	{
		delete m_pTextureMappingTable;
		m_pTextureMappingTable = NULL;
	}

	if( m_pFontMappingTable )
	{
		delete m_pFontMappingTable;
		m_pFontMappingTable = NULL;
	}
}

void COAGMFCDoc::CreateNewGeometry2D(int nType)
{
	switch( nType )
	{
	case 1: //Line
		{
			oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
			pGeo->SetGeometryType( GL_LINES );
			m_pScene->AddObject( pGeo );
		}
		break;
	case 2: //Polyline
		{
			oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
			pGeo->SetGeometryType( GL_LINE_STRIP );
			m_pScene->AddObject( pGeo );
		}
		break;
	case 3: //Rectangle
		{
			oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
			pGeo->SetGeometryType( GL_QUADS );
			m_pScene->AddObject( pGeo );
		}
		break;
	case 4: //Triangle
		{
			oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
			pGeo->SetGeometryType( GL_TRIANGLES );
			m_pScene->AddObject( pGeo );
		}
		break;

	}
}


void COAGMFCDoc::OnFileOpen()
{
	CString filter;
	filter.LoadString( IDS_OAGXML );

	CFileDialog dlg(TRUE, "*.oagxml", NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filter );

	if(dlg.DoModal() == IDOK)
	{
		m_pScene->DeleteAllObjects();

		SetPathName( dlg.GetPathName() );

		POSITION pos = GetFirstViewPosition();
		COAGMFCView* pView =  (COAGMFCView*) GetNextView(pos);

		if ( pView->m_pWinGraphicContext->MakeCurrent() )
		{
			oag::OAGParser parser;
			parser.LoadFile( dlg.GetPathName().GetString(), m_pScene );
			pView->m_pWinGraphicContext->DeleteCurrent();

			UpdateAllViews(NULL);
		}

		AfxMessageBox("File Loaded");
	}
}

void COAGMFCDoc::OnFileSave()
{
#if defined(LIBXML_WRITER_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)

#define MY_ENCODING "ISO-8859-1"


	CString fileSave = GetPathName();

	if ( fileSave.IsEmpty() )
	{
		CString filter;
		filter.LoadString( IDS_OAGXML );

		CFileDialog dlg(FALSE, "*.oagxml", NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filter );

		if(	dlg.DoModal() == IDOK )
		{
			fileSave = dlg.GetPathName().GetString();
		}

	}

	if ( !fileSave.IsEmpty() )
	{
		CXmlWriterDocument writerDoc;

		CXmlNode* nodeRoot = new CXmlNode("OAGLibrary");

		CXmlNode* nodeScene = new CXmlNode("Scene");
		nodeRoot->AppendChild( nodeScene );


		CXmlNode* pNodeTables = new CXmlNode("Tables");
		nodeScene->AppendChild( pNodeTables );

		m_pScene->GetTextureMappingTable()->SaveNodeXML( pNodeTables );

		m_pScene->GetFontMappingTable()->SaveNodeXML( pNodeTables );

		CXmlNode* NodeObj = new CXmlNode("Objects");
		nodeScene->AppendChild(NodeObj);

		for( vector<int>::size_type i = 0; i < m_pScene->GetObjectCount(); ++i )
		{
			oag::OAGObject* obj = m_pScene->GetObjectAt(i);
			obj->SaveNodeXML(NodeObj);
		}

		writerDoc.WriteFile( fileSave.GetString(), nodeRoot);

		delete nodeRoot;
	}
	

#endif
}


void COAGMFCDoc::OnInsertTexture()
{
	CString filter;
	filter.LoadString( IDS_TEXTURE_FILTER );

	CFileDialog dlg(TRUE, "*.*", NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filter );

	if(dlg.DoModal() == IDOK)
	{
		CString strFileName = dlg.GetPathName().GetString();

		POSITION pos = GetFirstViewPosition();
		COAGMFCView* pView =  (COAGMFCView*) GetNextView(pos);

		if ( pView->m_pWinGraphicContext->MakeCurrent() )
		{
			oag::OAGTexture* pTexture = oag::OAGTextureLoader::LoadTextureFromDisk( strFileName.GetString() );

			if( pTexture )
			{
				pTexture->SetDefaultFilter();
				pTexture->BuildTexture();
				pTexture->SetTextureName("Image1");
				m_pScene->GetTextureMappingTable()->AddTexture( pTexture );
			}

			pView->m_pWinGraphicContext->DeleteCurrent();
		}
	}
}

void COAGMFCDoc::OnInsertFont()
{
	CString filter;
	filter.LoadString( IDS_FONT_FILTER );

	CFileDialog dlg(TRUE, "*.ttf", NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filter );

	if(dlg.DoModal() == IDOK)
	{
		CString strFileName = dlg.GetPathName().GetString();

		POSITION pos = GetFirstViewPosition();
		COAGMFCView* pView =  (COAGMFCView*) GetNextView(pos);

		if ( pView->m_pWinGraphicContext->MakeCurrent() )
		{
			oag::OAGFont* pFont = oag::OAGFontLoader::LoadFontFromDisk( strFileName.GetString(), OAG_FONT_MAP_2D );

			if( pFont )
			{
				pFont->SetFontSize(24);
				pFont->SetFontName("arial");
				m_pScene->GetFontMappingTable()->AddFont( pFont );
			}

			pView->m_pWinGraphicContext->DeleteCurrent();
		}
	}
}

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
Software Developer
Brazil Brazil
I live in Matão, a small city in Brazil. I studied as Programmer in a College for Software Development in Database.
After finishing the College I have been working with java, c# and Computer Graphics with searches for OpenGL.

Comments and Discussions