KeyFrameList.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:6k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // KeyFrameList.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"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrameList
- //IMPLEMENT_DYNAMIC(CKeyFrameList, CObList)
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrameList construction
- CKeyFrameList::CKeyFrameList()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- }
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrameList Destructor
- CKeyFrameList::~CKeyFrameList()
- {
- DeleteAll();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrameList function implimentation
- void CKeyFrameList::Append(CKeyFrame* pKeyFrame)
- {
- if (!pKeyFrame)
- return;
- CObList::AddTail(pKeyFrame);
- }
- // Add a key value based on the current frame's position
- BOOL CKeyFrameList::AddKeyFrame(double time, C3dObject* pObject)
- {
- if (!pObject)
- return FALSE;
- // Create new key frame
- CKeyFrame* pKeyFrame = new CKeyFrame(time, pObject);
- // Add key to list
- return AddKeyFrame(pKeyFrame);
- }
- BOOL CKeyFrameList::AddKeyFrame(CKeyFrame* pNewKeyFrame)
- {
- if (!pNewKeyFrame)
- return FALSE;
- // Insert key into list
- if (IsEmpty())
- {
- AddTail(pNewKeyFrame);
- return TRUE;
- }
- // Step through list backward
- POSITION pos = GetTailPosition();
- ASSERT(pos);
- do {
- POSITION thispos = pos;
- CKeyFrame* pKey = (CKeyFrame*) GetPrev(pos);
- if (pKey->m_dTime < pNewKeyFrame->m_dTime)
- {
- // Insert new key after this one
- InsertAfter(thispos, pNewKeyFrame);
- return TRUE;
- }
- if (pKey->m_dTime == pNewKeyFrame->m_dTime)
- {
- // User has recorded a new key with the same
- // time base as pKey, InsertAfter and Remove old.
- // Insert new key after this one
- InsertAfter(thispos, pNewKeyFrame);
- Delete(pKey);
- return TRUE;
- }
- } while (pos);
- // Put new key in at start
- AddHead(pNewKeyFrame);
- return TRUE;
- }
- void CKeyFrameList::Remove(CKeyFrame* pKeyFrame)
- {
- if (!pKeyFrame) return;
- POSITION pos = CObList::Find(pKeyFrame);
- if (pos) {
- RemoveAt(pos);
- }
- }
- void CKeyFrameList::Delete(CKeyFrame* pKeyFrame)
- {
- if (!pKeyFrame) return;
- Remove(pKeyFrame);
- delete pKeyFrame;
- }
- void CKeyFrameList::DeleteAll()
- {
- while (!IsEmpty()) {
- CKeyFrame* pKeyFrame = (CKeyFrame*) RemoveHead();
- delete pKeyFrame;
- }
- }
- double CKeyFrameList::GetLength()
- {
- if (IsEmpty()) return 0;
- CKeyFrame* pKeyFrame = (CKeyFrame*) GetTail();
- ASSERT(pKeyFrame);
- return pKeyFrame->m_dTime;
- }
- CKeyFrame* CKeyFrameList::Find(double dTime)
- {
- CKeyFrame* pKey = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pKey = GetAt(Pos);
- if(pKey->m_dTime == dTime)
- return pKey;
- pKey = GetNext(Pos);
- }
- return NULL; // end of list
- }
- CKeyFrame* CKeyFrameList::FindClosest(double dTime, int *iKeyFrameTime)
- {
- CKeyFrame* pKey = NULL;
- CKeyFrame* pKeyClosest = NULL;
- double lowest = LARGE_NUMBER;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pKey = GetAt(Pos);
- double diff = pKey->m_dTime - dTime;
- if(diff < lowest)
- pKeyClosest = pKey;
- pKey = GetNext(Pos);
- }
- if(pKeyClosest)
- {
- if(pKeyClosest->m_dTime < dTime)
- *iKeyFrameTime = -1;
- if(pKeyClosest->m_dTime == dTime)
- *iKeyFrameTime = 0;
- if(pKeyClosest->m_dTime > dTime)
- *iKeyFrameTime = 1;
- }
- return pKeyClosest; // end of list
- }
- CKeyFrame* CKeyFrameList::FindLarger(double dTime)
- {
- CKeyFrame* pKey = NULL;
- CKeyFrame* pKeyLarger = NULL;
- double lowest = LARGE_NUMBER;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pKey = GetAt(Pos);
- double diff = pKey->m_dTime - dTime;
- if(diff > 0.0)
- {
- if(diff < lowest)
- {
- pKeyLarger = pKey;
- lowest = diff;
- }
- }
- pKey = GetNext(Pos);
- }
- return pKeyLarger; // end of list
- }
- CKeyFrame* CKeyFrameList::FindSmaller(double dTime)
- {
- CKeyFrame* pKey = NULL;
- CKeyFrame* pKeySmaller = NULL;
- double lowest = -LARGE_NUMBER;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pKey = GetAt(Pos);
- double diff = pKey->m_dTime - dTime;
- if(diff < 0.0)
- {
- if(diff > lowest)
- {
- pKeySmaller = pKey;
- lowest = diff;
- }
- }
- pKey = GetNext(Pos);
- }
- return pKeySmaller; // end of list
- }
- void CKeyFrameList::Serialize(CArchive& ar, int iVersion)
- {
- CString szIndentSave;
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Objects' CKeyFrame frames
- szBuffer.Format("%stCKeyFrame List {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the number of keyframes in this list
- szBuffer.Format("%sttNumKeyFrames < %d >n", szIndent, GetNumKeys());
- ar.WriteString(szBuffer);
- // Save the current indention and indent
- // all CAnimation class procedures
- szIndentSave = szIndent;
- szIndent += _T("tt"); // Add tabs
- CKeyFrame* pKey = NULL;
- // walk the list
- POSITION Pos = GetHeadPosition();
- while (Pos) {
- pKey = GetAt(Pos);
- pKey->Serialize(ar, iVersion);
- pKey = GetNext(Pos);
- }
- // Restore indent position
- szIndent = szIndentSave; // restore indent
- szBuffer.Format("%st}n", szIndent); // end of CAnimation procedures
- ar.WriteString(szBuffer);
- }
- else
- {
- // Load all CAnimation procedures
- while(ar.ReadString(szBuffer)) {
- CKeyFrame* pKey = NULL;
- szBuffer.TrimLeft(); // Remove leading white spaces
- if(szBuffer.Compare("}") == 0) // end of CAnimKey
- break;
- if(szBuffer.Compare("CKeyFrame {") == 0) {
- pKey = new CKeyFrame(0.0, NULL);
- ASSERT(pKey);
- pKey->Serialize(ar, iVersion);
- Append(pKey);
- }
- }
- }
- }