3dMaterialList.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:7k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dMaterialList.cpp : implementation file
- //
- // glOOP (OpenGL Object Oriented Programming library)
- // Copyright (c) Craig Fahrnbach 1997, 1998
- //
- // OpenGL is a registered trademark of Silicon Graphics
- //
- //
- // This program is provided for educational and personal use only and
- // is provided without guarantee or warrantee expressed or implied.
- //
- // Commercial use is strickly prohibited without written permission
- // from ImageWare Development.
- //
- // This program is -not- in the public domain.
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "glOOP.h"
- #include "MyFileIO.h"
- #include <stdio.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dMaterialList
- IMPLEMENT_DYNAMIC(C3dMaterialList, CObList)
- //IMPLEMENT_SERIAL(C3dMaterialList, CObList, 0)
- /////////////////////////////////////////////////////////////////////////////
- // C3dMaterialList construction
- C3dMaterialList::C3dMaterialList()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dMaterialList Destructor
- C3dMaterialList::~C3dMaterialList()
- {
- DeleteAll();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dMaterialList commands
- void C3dMaterialList::Remove(C3dMaterial* pMaterial)
- {
- if (!pMaterial) return;
- POSITION pos = CObList::Find(pMaterial);
- if (pos) {
- RemoveAt(pos);
- }
- }
- void C3dMaterialList::Delete(C3dMaterial* pMaterial)
- {
- if (!pMaterial) return;
- Remove(pMaterial);
- delete pMaterial;
- }
- void C3dMaterialList::DeleteAll()
- {
- while (!IsEmpty()) {
- C3dMaterial* pMaterial = (C3dMaterial*) RemoveHead();
- delete pMaterial;
- }
- }
- void C3dMaterialList::LoadMaterials(CString szFileName)
- {
- CFile f;
- CFileException fe;
- float fVersion;
- int iVersion;
- if(!szFileName.GetLength())
- return;
- if( !f.Open( szFileName, CFile::modeRead, &fe ) )
- {
- if(the3dEngine.m_bDisplayErrorMessages)
- {
- char buf[256];
- sprintf(buf, "C3dMaterialList::LoadMaterials - Material file read error. Could not open file: '%s'. ", szFileName);
- AfxMessageBox(buf, MB_OK, NULL);
- }
- #ifdef _DEBUG
- afxDump << "Unable to open material file" << "n";
- #endif
- return;
- }
- CArchive ar( &f, CArchive::load );
- try
- {
- CString szBuffer;
- // Read the version number
- ar.ReadString(szBuffer);
- sscanf(szBuffer, "// Version: %fn", &fVersion);
- iVersion = Roundf(fVersion*100);
- if(iVersion < 101)
- iVersion = 101; // File had no version number, so assign default
- // Load the material file data..
- Serialize(ar, iVersion);
- }
- catch (CFileException exception) {
- // Catch any exceptions that may be thrown and
- // display for the user...
- DisplayFileIOException(&exception);
- }
- }
- void C3dMaterialList::SaveMaterials(CString szFileName)
- {
- CFile f;
- CFileException fe;
- if( !f.Open( szFileName, CFile::modeCreate | CFile::modeReadWrite, &fe))
- {
- // Error! Display message to the user..
- CString buffer;
- buffer.Format("Could not open material file '%s'!n", szFileName);
- AfxMessageBox(buffer, MB_OK, 0);
- #ifdef _DEBUG
- afxDump << "Unable to open file" << "n";
- #endif
- }
- // Construct a CArchive object and specify whether it will be used for
- // loading or storing objects.
- CArchive ar( &f, CArchive::store );
- try
- {
- CString szBuffer;
- // Save the file version information
- ar.WriteString(szFileVersion);
- // Save the file header information
- ar.WriteString(szMatlFileHeader);
- ar.WriteString(szIWDFileHeader);
- // Save the material file data..
- Serialize(ar, iFileVersion);
- }
- catch (CFileException exception)
- {
- // Catch any exceptions that may be thrown and
- // display for the user...
- DisplayFileIOException(&exception);
- }
- }
- void C3dMaterialList::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- C3dMaterial* pMatl = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pMatl = GetAt(Pos);
- pMatl->Serialize(ar, iVersion);
- pMatl = GetNext(Pos);
- }
- }
- else
- {
- while(ar.ReadString(szBuffer))
- {
- // Look for the Material header...
- if(szBuffer.Compare("Material {") == 0)
- {
- C3dMaterial* pMatl = NULL;
- pMatl = C3dMaterial::Create();
- pMatl->Serialize(ar, iVersion);
- if(!Find(pMatl))
- Append(pMatl);
- else
- C3dMaterial::Delete(pMatl);
- }
- }
- }
- }
- C3dMaterial* C3dMaterialList::Find(C3dMaterial* pMatl)
- {
- if (!pMatl) return NULL;
- C3dMaterial* pListEntry = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pListEntry = GetAt(Pos);
- if(!pMatl->m_szName.Compare(pListEntry->m_szName))
- // A material with the same name has been found
- // in the material list.
- return pListEntry;
- pListEntry = GetNext(Pos);
- }
- return NULL;
- }
- C3dMaterial* C3dMaterialList::Find(CString szName)
- {
- C3dMaterial* pListEntry = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pListEntry = GetAt(Pos);
- if(!szName.Compare(pListEntry->m_szName))
- // A material with the same name has been found
- // in the material list.
- return pListEntry;
- pListEntry = GetNext(Pos);
- }
- return NULL;
- }
- C3dMaterial* C3dMaterialList::LoadComboBox(CComboBox* pCombo)
- {
- if(!pCombo)
- return NULL; // Nothing to load into
- // Reset or clear the contents of the combo box
- pCombo->ResetContent();
- // Fill the combo box with materials from this list
- C3dMaterial* pMatl = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos)
- {
- pMatl = GetAt(Pos);
- pCombo->AddString(pMatl->m_szName);
- int count = pCombo->GetCount() - 1; // zero based
- pCombo->SetItemData(count, (unsigned long)pMatl);
- pMatl = GetNext(Pos);
- }
- return pMatl;
- }
- C3dMaterial* C3dMaterialList::LoadListBox(CListBox* pList)
- {
- if(!pList)
- return NULL; // Nothing to load into
- // Reset or clear the contents of the combo box
- pList->ResetContent();
- // Fill the combo box with materials from this list
- C3dMaterial* pMatl = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos)
- {
- pMatl = GetAt(Pos);
- pList->AddString(pMatl->m_szName);
- int count = pList->GetCount() - 1; // zero based
- pList->SetItemData(count, (unsigned long)pMatl);
- pMatl = GetNext(Pos);
- }
- return pMatl;
- }