⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameview.cpp

📁 游戏框架
💻 CPP
字号:
// ***************************************************************
//  GameView   version:  1.0
//  -------------------------------------------------------------
//	File Name:	GameView.cpp
//	Created:	2007/07/18
//	Modified:	2007/07/18   16:13
//	Author:		William.Liang
//  Msn:		lwq49@msn.com
//  Email:		lwq49@21cn.com, lwq49@msn.com
//	Description:
//
//	Purpose:	
//  -------------------------------------------------------------
//  license:
//
//  The contents of this file are subject to the Mozilla Public
//  License Version 1.1 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy
//  of the License at http://www.mozilla.org/MPL/ Software dis-
//  tributed under the License is distributed on an "AS IS" 
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or im-
//  plied. See the License for the specific language governing
//  rights and limitations under the License.
//
//  The Initial Developer of the Original Code is William.Liang .
//  Copyright (C) 2007 - All Rights Reserved.
// ***************************************************************
#include "StdAfx.h"
#include ".\GameView.h"
#include "GameScene.h"
#include "GameLayer.h"

//************************************
// <p>Description: 构造函数</p>
//************************************
CGameView::CGameView(_CString szName, _SIZE siSize)
{
	SetObjectName(szName);
	if(siSize.cx == 0 && siSize.cy ==0){
		LPGDI lpGDI = g_lpDefaultResource->GetGDI();
		siSize.cx = (float)lpGDI->System_GetState(HGE_SCREENWIDTH);
		siSize.cy = (float)lpGDI->System_GetState(HGE_SCREENHEIGHT);
	}

	SetSize(siSize);
	SetObjectType(Type_View);
	m_wActionAreaCount	= 0;
	m_lpActionRect		= NULL;
}

//************************************
// <p>Description: 析构函数</p>
//************************************
CGameView::~CGameView(void)
{
	if(m_lpActionRect)
		delete[] m_lpActionRect;
	Release();
}

//设置动作区域
void CGameView::SetActionArea(_LPRECT lpRect, WORD wCount){

	m_wActionAreaCount = wCount;
	m_lpActionRect = new _RECT[wCount];
	CopyMemory(m_lpActionRect, lpRect, sizeof(_RECT)*wCount);
}

//是否激活动作区域
WORD CGameView::IsHitActionArea(float x, float y){
	if(m_wActionAreaCount==0 || m_lpActionRect==NULL) return 0;

	for(WORD i=0;i<m_wActionAreaCount;i++){
		if(m_lpActionRect[i].left<=x && m_lpActionRect[i].right>=x && m_lpActionRect[i].top<=y && m_lpActionRect[i].bottom>=y)
			return i;
	}

	return 0;
}

//************************************
// <p>Description: 创建场景</p>
// <p>Parameters:  </p>
// <p>    _CString szName</p>
// <p>    INT_PTR nPosition</p>
//
// <p>Returns:   CGameScene*</p>
//************************************
CGameScene* CGameView::Create(_CString szName, INT_PTR nPosition){
	CGameScene*	pGameScene = new CGameScene();
	pGameScene->SetObjectName(szName);
	pGameScene->SetObjectType(Type_Scene);
	pGameScene->SetSize(GetSize());

	if(pGameScene){
		if(Add(pGameScene, szName, nPosition))
			return pGameScene;
	}

	return NULL;
}

//************************************
// <p>Description: 计算容器坐标</p>
// <p>Parameters:  </p>
//
// <p>Returns:   void</p>
//************************************
void CGameView::CalcContainerPosition(){
	__super::CalcContainerPosition();

	CGameScene*		pGameScene = m_lstGameScenes.Iterators(true);
	while(pGameScene){
		pGameScene->CalcContainerPosition();
		pGameScene = m_lstGameScenes.Iterators();
	}

	return;
}

//************************************
// <p>Description: 返回场景数</p>
//
// <p>Returns:   INT_PTR</p>
//************************************
INT_PTR CGameView::GetLength(){
	return m_lstGameScenes.GetCount();
}

//************************************
// <p>Description: 返回场景</p>
// <p>Parameters:  </p>
// <p>    INT_PTR nIndex</p>
//
// <p>Returns:   CGameScene*</p>
//************************************
CGameScene* CGameView::Get(INT_PTR nIndex){
	return m_lstGameScenes.Get(nIndex);
}

//************************************
// <p>Description: 返回场景</p>
// <p>Parameters:  </p>
// <p>    LPCTSTR szKey</p>
//
// <p>Returns:   CGameScene*</p>
//************************************
CGameScene* CGameView::GetBy(LPCTSTR szKey){
	return m_lstGameScenes.GetBy(szKey);
}

//************************************
// <p>Description: 返回场景</p>
// <p>Parameters:  </p>
// <p>    INT_PTR nIndex</p>
//
// <p>Returns:   CGameScene*</p>
//************************************
CGameScene* CGameView::operator[](INT_PTR nIndex){
	return Get(nIndex);
}

//************************************
// <p>Description: 返回场景</p>
// <p>Parameters:  </p>
// <p>    LPCTSTR szKey</p>
//
// <p>Returns:   CGameScene*</p>
//************************************
CGameScene* CGameView::operator[](LPCTSTR szKey){
	return GetBy(szKey);
}

//************************************
// <p>Description: 添加场景</p>
// <p>Parameters:  </p>
// <p>    CGameScene * pGameScene</p>
// <p>    _CString szName</p>
// <p>    INT_PTR nPosition</p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameView::Add(CGameScene* pGameScene, _CString szName, INT_PTR nPosition){
	if(nPosition==LISTTEMPLATE_LAST_ITEM)
		m_lstGameScenes.Push(pGameScene, szName);
	else
		m_lstGameScenes.Insert(pGameScene, nPosition, szName);

	//关联场景
	pGameScene->SetParent(GET_OBJECTPTR_INTERFACE(this, IUnknownEx));
	//计算坐标
	pGameScene->CalcContainerPosition();
	
	return true;
}

//************************************
// <p>Description: 移除场景</p>
// <p>Parameters:  </p>
// <p>    INT_PTR nIndex</p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameView::Remove(INT_PTR nIndex){
	m_lstGameScenes.Remove(nIndex);
	return true;
}

//************************************
// <p>Description: 移除场景</p>
// <p>Parameters:  </p>
// <p>    _CString szKey</p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameView::Remove(_CString szKey){
	m_lstGameScenes.RemoveBy(szKey);
	return true;
}

//************************************
// <p>Description: 移除所有对象</p>
// <p>Parameters:  </p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameView::ClearAll(){
	m_lstGameScenes.RemoveAll();
	return true;
}

//************************************
// <p>Description: 预渲染视图</p>
// <p>Parameters:  </p>
// <p>    WPARAM wParam</p>
//
// <p>Returns:   void</p>
//************************************
void CGameView::PreRender(WPARAM wParam){
	CGameScene*		pGameScene = m_lstGameScenes.Iterators(true);
	while(pGameScene){
		//只渲染第一个可视场景
		if(pGameScene->IsVisabled()){
			pGameScene->PreRender(wParam);

			break;
		}
		pGameScene = m_lstGameScenes.Iterators();
	}
}

//************************************
// <p>Description: 渲染视图</p>
// <p>Parameters:  </p>
// <p>    WPARAM wParam</p>
//
// <p>Returns:   void</p>
//************************************
void CGameView::Render(WPARAM wParam){
	CGameScene*		pGameScene = m_lstGameScenes.Iterators(true);
	while(pGameScene){
		//只渲染第一个可视场景
		if(pGameScene->IsVisabled()){
			pGameScene->Render(wParam);

			break;
		}
		pGameScene = m_lstGameScenes.Iterators();
	}
}

//************************************
// <p>Description: 返回指定的路径下的对象</p>
//			例:defaultscene\defaultlayer\objectname
// <p>Parameters:  </p>
// <p>    LPCTSTR szPath</p>
//
// <p>Returns:   IGameObject*</p>
//************************************
IGameObject* CGameView::GetObject(LPCTSTR szPath){
	TCHAR		SubPath[3][MAX_NAME];
	LPCTSTR		pStart = szPath;
	LPCTSTR		pEnd = pStart;
	int			i = 0;
	int			Flag = 0;

	ZeroMemory(SubPath, sizeof(SubPath));
	Flag = (_tcschr(pStart, '.')!=NULL)?1:0;
	Flag = (Flag==0 && _tcschr(pStart, '\\')!=NULL)?2:Flag;
	if(Flag > 0){
		while((pEnd = strchr(pStart, Flag==1?'.':'\\'))!=NULL && i<3){
			_tcsncpy(SubPath[i], pStart, pEnd-pStart);

			pStart = pEnd+1;
			i++;
		}

		if(*pStart!=0){
			_tcsncpy(SubPath[i], pStart, MAX_NAME);
		}

		if(i==2){
			CGameScene*		pGameScene = m_lstGameScenes[SubPath[0]];
			if(pGameScene){
				CGameLayer*		pGameLayer = pGameScene->GetBy(SubPath[1]);
				if(pGameLayer){
					return pGameLayer->GetBy(SubPath[2]);
				}
			}
		}
	}
	return NULL;
}

//************************************
// <p>Description: 查找指定坐标下的对象</p>
//			主要由框架调用的内部函数
// <p>Parameters:  </p>
// <p>    float x</p>
// <p>    float y</p>
// <p>    CGameScene * * pGameScene</p>
// <p>    CGameLayer * * pGameLayer</p>
// <p>    IGameObject * * pIGameObject</p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameView::FindObject(float x, float y, CGameScene** pGameScene, CGameLayer** pGameLayer, IGameObject** pIGameObject)
{
	*pGameScene = m_lstGameScenes.Iterators(true);
	while(*pGameScene){
		//只显示第一个可视场景
		if((*pGameScene)->IsVisabled() && !(*pGameScene)->IsDisabled()){
			return (*pGameScene)->FindObject(x, y, pGameScene, pGameLayer, pIGameObject);
		}
		(*pGameScene) = m_lstGameScenes.Iterators();
	}
	return false;
}

//************************************
// <p>Description: 交换深度</p>
// <p>Parameters:  </p>
// <p>    IUnknownEx * pIObject1</p>
// <p>    IUnknownEx * pIObject2</p>
//
// <p>Returns:   void</p>
//************************************
void CGameView::SwitchDepth(IUnknownEx* pIObject1, IUnknownEx* pIObject2){
	m_lstGameScenes.Switch((CGameScene*)GET_OBJECTPTR_INTERFACE(pIObject1, IFrameObject), (CGameScene*)GET_OBJECTPTR_INTERFACE(pIObject2, IFrameObject));
}

//************************************
// <p>Description: 交换深度</p>
// <p>Parameters:  </p>
// <p>    IUnknownEx * pIObject</p>
// <p>    DWORD dwNewDepth</p>
//
// <p>Returns:   void</p>
//************************************
void CGameView::SwitchDepth(IUnknownEx* pIObject, DWORD dwNewDepth){
	CGameScene*		pGameScene = (CGameScene*)GET_OBJECTPTR_INTERFACE(pIObject, IFrameObject);

	//位置没有变化,不处理
	if(dwNewDepth == pGameScene->GetDepth()) return;

	bool			bDirection = dwNewDepth > pGameScene->GetDepth();
	INT_PTR			nOrignalPos = m_lstGameScenes.GetPosition(pGameScene);
	CGameScene*		pNextGameScene = NULL;
	if(bDirection){
		//向后搜索
		for(INT_PTR nPos=nOrignalPos+1;nPos<=GetLength();nPos++){
			pNextGameScene = m_lstGameScenes.Get(nPos);
			//后一个对象的深度比新的深度要大,则退出
			if(pNextGameScene->GetDepth()> dwNewDepth) return;

			SwitchDepth(pIObject, (IUnknownEx*)pNextGameScene->QueryInterface(IID_IUnknownEx, VER_IUnknownEx));

		}
	}
	else{
		//向前搜索
		for(INT_PTR nPos=nOrignalPos-1;nPos>=1;nPos--){
			pNextGameScene = m_lstGameScenes.Get(nPos);
			//后一个对象的深度比新的深度要小,则退出
			if(pNextGameScene->GetDepth()< dwNewDepth) return;

			SwitchDepth(pIObject, (IUnknownEx*)pNextGameScene->QueryInterface(IID_IUnknownEx, VER_IUnknownEx));

		}
	}
}

//************************************
// <p>Description: 释放对象</p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameView::Release(){
	if (IsValid()){
		CGameScene*		pGameScene = m_lstGameScenes.Iterators(true);
		while(pGameScene){
			delete pGameScene;
			pGameScene = m_lstGameScenes.Iterators();
		}
		m_lstGameScenes.RemoveAll();
		return true;
	}

	return false;
}

//************************************
// <p>Description: 接口查询</p>
// <p>Parameters:  </p>
// <p>    const IID & Guid</p>
// <p>    DWORD dwQueryVer</p>
//
// <p>Returns:   void*</p>
//************************************
void* CGameView::QueryInterface(const IID &Guid, DWORD dwQueryVer){
	QUERYINTERFACE(IFrameObject,Guid,dwQueryVer);
	QUERYINTERFACE_IUNKNOWNEX(IFrameObject,Guid,dwQueryVer);
	return NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -