3dColorList.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:7k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dColorList.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
- //////////////////////////////////////////////////////////////////
- // C3dColorList
- IMPLEMENT_DYNAMIC(C3dColorList, CObList)
- //IMPLEMENT_SERIAL(C3dColorList, CObList, 0)
- /////////////////////////////////////////////////////////////////////////////
- // C3dColorList construction
- C3dColorList::C3dColorList()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dColorList Destructor
- C3dColorList::~C3dColorList()
- {
- DeleteAll();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dColorList commands
- void C3dColorList::Remove(C3dColor* pColor)
- {
- if (!pColor) return;
- POSITION pos = CObList::Find(pColor);
- if (pos) {
- RemoveAt(pos);
- }
- }
- void C3dColorList::Delete(C3dColor* pColor)
- {
- if (!pColor) return;
- Remove(pColor);
- delete pColor;
- }
- void C3dColorList::DeleteAll()
- {
- while (!IsEmpty()) {
- C3dColor* pColor = (C3dColor*) RemoveHead();
- delete pColor;
- }
- }
- void C3dColorList::LoadColors(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, "C3dColorList::LoadColors - Color 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
- // Save the color file data..
- Serialize(ar, iFileVersion);
- }
- catch (CFileException exception) {
- // Catch any exceptions that may be thrown and
- // display for the user...
- DisplayFileIOException(&exception);
- }
- }
- void C3dColorList::SaveColors(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 color file '%s'!n", szFileName);
- AfxMessageBox(buffer, MB_OK, 0);
- #ifdef _DEBUG
- afxDump << "Unable to open file" << "n";
- #endif
- }
- CArchive ar( &f, CArchive::store );
- try
- {
- CString szBuffer;
- // Save the file version information
- ar.WriteString(szFileVersion);
- // Save the file header information
- ar.WriteString(szColorFileHeader);
- ar.WriteString(szIWDFileHeader);
- // Save the color file data..
- Serialize(ar, iFileVersion);
- }
- catch (CFileException exception) {
- // Catch any exceptions that may be thrown and
- // display for the user...
- DisplayFileIOException(&exception);
- }
- }
- void C3dColorList::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- C3dColor* pColor = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos)
- {
- pColor = GetAt(Pos);
- pColor->Serialize(ar, iVersion);
- pColor = GetNext(Pos);
- }
- }
- else
- {
- // Load or Read the Color member variables
- while(ar.ReadString(szBuffer))
- {
- // Look for the Color header...
- if(szBuffer.Compare("Color {") == 0)
- {
- C3dColor* pColor = NULL;
- pColor = C3dColor::Create();
- pColor->Serialize(ar, iVersion);
- if(!Find(pColor))
- Append(pColor);
- else
- C3dColor::Delete(pColor);
- }
- }
- }
- }
- C3dColor* C3dColorList::Find(C3dColor* pColor)
- {
- if (!pColor) return NULL;
- C3dColor* pListEntry = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pListEntry = GetAt(Pos);
- if(!pColor->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;
- }
- C3dColor* C3dColorList::Find(CString szName)
- {
- C3dColor* 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;
- }
- C3dColor* C3dColorList::LoadComboBox(CComboBox* pCombo, C3dColor* pColorSel)
- {
- if(!pCombo)
- return NULL; // Nothing to load into
- // Reset or clear the contents of the combo box
- pCombo->ResetContent();
- // Set the first color to NULL
- pCombo->AddString("<None>");
- int count = pCombo->GetCount() - 1; // zero based
- pCombo->SetItemData(count, NULL);
- // Fill the combo box with all Colors in the list
- C3dColor* pColor = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos)
- {
- pColor = GetAt(Pos);
- pCombo->AddString(pColor->m_szName);
- int count = pCombo->GetCount() - 1; // zero based
- pCombo->SetItemData(count, (unsigned long)pColor);
- pColor = GetNext(Pos);
- }
- // Select the color from our list
- pCombo->SetCurSel(0); // Set to default <None>
- if(pColorSel)
- int index = pCombo->SelectString(-1, pColorSel->m_szName);
- // int index = pCombo->FindString(-1, pColorSel->m_szName);
- // if(index == CB_ERR)
- // pCombo->SetCurSel(0);
- // else
- // pCombo->SetCurSel(index);
- // }
- // else
- // pCombo->SetCurSel(0);
- return pColor;
- }
- C3dColor* C3dColorList::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 all Colors in the list
- C3dColor* pColor = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos)
- {
- pColor = GetAt(Pos);
- pList->AddString(pColor->m_szName);
- int count = pList->GetCount() - 1; // zero based
- pList->SetItemData(count, (unsigned long)pColor);
- pColor = GetNext(Pos);
- }
- return pColor;
- }