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

📄 gameview.cpp

📁 看到有兄弟提出的半透明算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GameView.cpp: implementation of the CGameView class.
//
//////////////////////////////////////////////////////////////////////

#include "GameView.h"
#include "Player.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGameView::CGameView()
{

}

CGameView::CGameView(CGameDoc* pDoc)
{
	SetDocument(pDoc);
}

CGameView::~CGameView()
{

}

bool CGameView::OnInitData()
{
	m_bPause = FALSE;
	m_nGameWorld = 1;
	m_nGameStage = 1;

	// 初始化游戏数据
	InitGameData();

	m_nMapX = m_tInitStageData.nMapX;
	m_nMapY = m_tInitStageData.nMapY;
	m_tPlayer.m_nRelativePosX = m_tInitStageData.nRelativePosX;
	m_tPlayer.m_nRelativePosY = m_tInitStageData.nRelativePosY;
	m_tPlayer.m_nPosX = m_tInitStageData.nPosX;
	m_tPlayer.m_nPosY = m_tInitStageData.nPosY;
	m_tPlayer.m_nCurFaceX = m_tInitStageData.nCurFaceX;

	return true;
}

void CGameView::OnTimer()
{
	// 重新计算数据
	if (m_pDoc == NULL)
		return;
	
	// 在Doc里处理按键事件
	if (!m_pDoc->UpdateGameView())
		return;

	switch (m_nCurGameStatus)
	{
	case 0:
		{
			UpdateStartGame();
			DrawGameBmp();
		}
		break;
	case 1:
		{
			DrawGameBmp();
			UpdateGameData();
		}
		break;
	case 2:
		{
			DrawGameBmp();
			UpdateEndGame();
		}
		break;
	default:
		break;
	}
	
	CGameApp* pApp = (CGameApp*) GETAPPINSTANCE();
	if (pApp == NULL)
		return;
	if (m_nCurGameStatus != 0)
		ISHELL_SetTimerEx(pApp->m_pIShell, TIME_VIEW, &m_cbTimer);
	else
		ISHELL_SetTimerEx(pApp->m_pIShell, TIME_GAME, &m_cbTimer);
}

void CGameView::OnStartView()
{
	// 初始化对象
	if (!OnInitData())
	{
		Release();
		return;
	}
	
	m_pDoc->SetCurView(this);
	
	// 将定时器与回调函数关联
	CALLBACK_Init(&m_cbTimer, (PFNNOTIFY)HandleTimer, this);
	
	OnSetTimer();
}

void CGameView::OnSetTimer()
{
	// 设置定时器
	SetTimer(TIME_VIEW, (PFNNOTIFY)(CGameView::HandleTimer));
}

bool CGameView::SetTimer(int32 dwInterval, PFNNOTIFY pfnCallback)
{
	CGameApp* pApp = NULL;
	
	BEGIN_CHECK
	{
		// 取app指针
		CHECK_NULL(pApp, (CGameApp*) GETAPPINSTANCE());
		CHECK_ERROR(ISHELL_SetTimerEx(pApp->m_pIShell, dwInterval, &m_cbTimer));
	}
	CATCH_CHECK
	{
		return false;
	}
	END_CHECK

	return true;
}

void CGameView::OnSuspend()
{
	// 结束定时器
	CALLBACK_Cancel(&m_cbTimer);

//	m_tCharacter.nCurDoing = 1;
	// 释放资源
	
	CBaseView::OnSuspend();
}

void CGameView::OnResume()
{
	// 清屏
	IDISPLAY_ClearScreen(((CGameApp*)GETAPPINSTANCE())->m_pIDisplay);
	// 设置背景色
	IGraphics* pIGraphics = NULL;
	if (ISHELL_CreateInstance(((CGameApp*)GETAPPINSTANCE())->m_pIShell, AEECLSID_GRAPHICS, (void** )&pIGraphics) == SUCCESS)
	{
		IGRAPHICS_SetBackground(pIGraphics, 0, 0, 0);
		IGRAPHICS_ClearViewport(pIGraphics);
		IGRAPHICS_Release(pIGraphics);
	}

	CBaseView::OnResume();
}

void CGameView::HandleTimer(void* pView)
{
	((CGameView* )pView)->OnTimer();
}

void CGameView::FreeResourse()
{
	// 设置定时器结束标志
	m_pDoc->SetExitTimer(true);
	
	// 结束定时器
	CALLBACK_Cancel(&m_cbTimer);
	
	// 释放声音
//	if (m_pISoundPlayer)
//	{
//		ISOUNDPLAYER_Release(m_pISoundPlayer);
//		m_pISoundPlayer = NULL;
//	}

	ReleaseBmpRes();
}

void CGameView::ReleaseBmpRes()
{
	int i = 0;
	if (m_pIGraphics)
	{
		IGRAPHICS_Release(m_pIGraphics);
		m_pIGraphics = NULL;
	}

	for (i = 0; i < 40; i++)
	{
		if (m_pbmDbd[i])
		{
			IBITMAP_Release(m_pbmDbd[i]);
			m_pbmDbd[i] = NULL;
		}
	}

	if (m_pScreen)
	{
		IBITMAP_Release(m_pScreen);
		m_pScreen = NULL;
	}
	if (m_pMap)
	{
		FREE(m_pMap);
		m_pMap = NULL;
	}
}

void CGameView::ExitView(int nViewID)
{
	// 设置定时器结束标志
	m_pDoc->SetExitTimer(true);
	
	// 结束定时器
	CALLBACK_Cancel(&m_cbTimer);
	
	// 释放位图资源
	ReleaseBmpRes();
	
	m_pDoc->SwitchView(nViewID);
}

void CGameView::InitGameData()
{
	m_bMoveRight = FALSE;
	m_bMoveLeft = FALSE;

//	m_tPlayer.m_nRelativePosX = 50;
//	m_tPlayer.m_nPosX = m_tPlayer.m_nRelativePosX;
//	m_tPlayer.m_nRelativePosY = 119;
//	m_tPlayer.m_nPosY = m_tPlayer.m_nRelativePosY+1;
	m_tPlayer.m_nCurDoing = 0;
	m_tPlayer.m_nCurIndex = 0;
	m_tPlayer.m_nCurDirectX = 2;
	m_tPlayer.m_nCurDirectY = 2;
//	m_tPlayer.m_nCurFaceX = 1;
	m_tPlayer.m_nCurDoing = 0;
	m_tPlayer.m_nCurDoingNum = 0;
	m_tPlayer.m_nDoingTimeNum = 0;
	m_tPlayer.m_nDataStatus = 0;
	m_tPlayer.m_bContinueJump = FALSE;
	m_tPlayer.m_nRunSpeedX = PLAYER_MOVEX;
	m_tPlayer.m_bAttackable = FALSE;
	m_tPlayer.m_bAclinicMoveable = TRUE;

	m_tPlayer.m_tShadow.nIndex = 18;
	m_tPlayer.m_tShadow.nRelativeX = 55;//m_tPlayer.m_nRelativePosX;
	m_tPlayer.m_tShadow.nRelativeY = 119;//m_tPlayer.m_nRelativePosY;
	m_tPlayer.m_tShadow.nPosX = 55;//m_tPlayer.m_nPosX;
	m_tPlayer.m_tShadow.nPosY = 120;//m_tPlayer.m_nPosY;

	m_pCharacter[0] = &m_tPlayer;
	m_pCharacter[0]->m_nOrder = 0;
	m_pCharacter[0]->nType = 0;
	m_pCharacter[0]->m_pGameView = this;

//	IGraphics* pIGraphics;
//	if (ISHELL_CreateInstance(((CGameApp*)GETAPPINSTANCE())->m_pIShell,
//							AEECLSID_GRAPHICS, (void**)&pIGraphics) == SUCCESS)
//	{
//		IGRAPHICS_SetBackground(pIGraphics, 255, 255, 255);
//		IGRAPHICS_ClearViewport(pIGraphics);
//		IGRAPHICS_Release(pIGraphics);
//	}

	InitMapElementFromLocal();
	InitMapData();
	InitPlayerBmpData();
	InitGameBmp();

	m_nCurGameStatus = 0;
	InitGameGraphics();
}

void CGameView::InitMapData()
{
	int8 i = 0;
	int8 j = 0;
	uint8 czMapInfo[13] = {0};
	void * pSource = NULL;
	byte* pGround = NULL;
	uint32  nSize;
	pSource = ISHELL_LoadResDataEx (((CGameApp*)GETAPPINSTANCE())->m_pIShell, 
		EIDOLON_RES_FILE,IDS_MAP1+(m_nGameWorld-1)*4+(m_nGameStage-1), RESTYPE_IMAGE, NULL, &nSize);

	if (pSource == NULL)
	{
		ISHELL_FreeResData (((CGameApp*)GETAPPINSTANCE())->m_pIShell, pSource);
		return;
	}

	nSize = *((uint16*)pSource);
	pGround = (byte* )pSource + nSize;

	// 将原始数据读出
	MEMCPY(czMapInfo,(byte*)pGround, 4);
	m_nMapWidth = (int8)czMapInfo[0] + (int8)(czMapInfo[1] << 8);
	m_nMapHeight = (int8)czMapInfo[2] + (int8)(czMapInfo[3] << 8);
	pGround += 4;
	m_pMap = (int8*)MALLOC(m_nMapWidth*m_nMapHeight);
	MEMCPY(m_pMap, pGround, m_nMapWidth*m_nMapHeight);

	// 关卡切换数据
	pGround += m_nMapWidth*m_nMapHeight;
	MEMSET(czMapInfo, 0, 13);
	MEMCPY(czMapInfo, (byte*)pGround, 1);
	m_nChangeStageNum = (int8)czMapInfo[0];
	pGround += 1;
	for (i = 0; i < m_nChangeStageNum; i++)
	{
		MEMSET(czMapInfo, 0, 13);
		MEMCPY(czMapInfo, (byte*)pGround, 8);
		m_tChangeStage[i].nStartMapX = (int16)czMapInfo[0] + (int16)(czMapInfo[1] << 8);
		m_tChangeStage[i].nEndMapX = (int16)czMapInfo[2] + (int16)(czMapInfo[3] << 8);
		m_tChangeStage[i].nMapY = (int16)czMapInfo[4] + (int16)(czMapInfo[5] << 8);
		m_tChangeStage[i].nChangeWorld = (int8)czMapInfo[6];
		m_tChangeStage[i].nChangeStage = (int8)czMapInfo[7];
		pGround += 8;

		MEMSET(czMapInfo, 0, 13);
		MEMCPY(czMapInfo,(byte*)pGround, 13);
		m_tStageData[i].nMapX = (int16)czMapInfo[0] + (int16)(czMapInfo[1] << 8);
		m_tStageData[i].nMapY = (int16)czMapInfo[2] + (int16)(czMapInfo[3] << 8);
		m_tStageData[i].nRelativePosX = (int16)czMapInfo[4] + (int16)(czMapInfo[5] << 8);
		m_tStageData[i].nRelativePosY = (int16)czMapInfo[6] + (int16)(czMapInfo[7] << 8);
		m_tStageData[i].nPosX = (int16)czMapInfo[8] + (int16)(czMapInfo[9] << 8);
		m_tStageData[i].nPosY = (int16)czMapInfo[10] + (int16)(czMapInfo[11] << 8);
		m_tStageData[i].nCurFaceX = (int8)czMapInfo[12];
		pGround += 13;
	}


	// 游戏初始数据
	MEMSET(czMapInfo, 0, 13);
	MEMCPY(czMapInfo,(byte*)pGround, 13);
	m_tInitStageData.nMapX = (int16)czMapInfo[0] + (int16)(czMapInfo[1] << 8);
	m_tInitStageData.nMapY = (int16)czMapInfo[2] + (int16)(czMapInfo[3] << 8);
	m_tInitStageData.nRelativePosX = (int16)czMapInfo[4] + (int16)(czMapInfo[5] << 8);
	m_tInitStageData.nRelativePosY = (int16)czMapInfo[6] + (int16)(czMapInfo[7] << 8);
	m_tInitStageData.nPosX = (int16)czMapInfo[8] + (int16)(czMapInfo[9] << 8);
	m_tInitStageData.nPosY = (int16)czMapInfo[10] + (int16)(czMapInfo[11] << 8);
	m_tInitStageData.nCurFaceX = (int8)czMapInfo[12];

	ISHELL_FreeResData (((CGameApp*)GETAPPINSTANCE())->m_pIShell, pSource);
}

void CGameView::InitMapElementFromLocal()
{
	int8 i = 0;
	int8 j = 0;
	uint8 czMapInfo[6] = {0};
	void * pSource = NULL;
	byte* pGround = NULL;
	uint32  nSize;
	int8 nCount = 0;
	pSource = ISHELL_LoadResDataEx (((CGameApp*)GETAPPINSTANCE())->m_pIShell, 
		EIDOLON_RES_FILE,IDS_MAPELEMENT, RESTYPE_IMAGE, NULL, &nSize);

	if (pSource == NULL)
	{
		ISHELL_FreeResData (((CGameApp*)GETAPPINSTANCE())->m_pIShell, pSource);
		return;
	}

	nSize = *((uint16*)pSource);
	pGround = (byte* )pSource + nSize;

	MEMCPY(czMapInfo,(byte*)pGround, 1);
	nCount = (int8)(czMapInfo[0]);
	pGround += 1;
	// 将原始数据读出
	for (i = 0; i < nCount; i++)
	{
		MEMSET(czMapInfo, 0, 6);
		MEMCPY(czMapInfo,(byte*)pGround, 5);

		m_tMapElement[i].nPosX = (int16)czMapInfo[0];
		m_tMapElement[i].nPosY = (int16)czMapInfo[1];
		m_tMapElement[i].nWidth = (int8)czMapInfo[2];
		m_tMapElement[i].nHeight = (int8)czMapInfo[3];
		m_tMapElement[i].bBlock = (boolean)czMapInfo[4];

		pGround += 5;
	}

	ISHELL_FreeResData (((CGameApp*)GETAPPINSTANCE())->m_pIShell, pSource);
}

void CGameView::InitPlayerBmpData()
{
	int8 i = 0;
	int8 j = 0;
	uint8 czMapInfo[6] = {0};
	void * pSource = NULL;
	byte* pGround = NULL;
	uint32  nSize;
	int8 nCount = 0;
	int8 nIndex = 0;
	pSource = ISHELL_LoadResDataEx (((CGameApp*)GETAPPINSTANCE())->m_pIShell, 
		EIDOLON_RES_FILE,IDS_PLAYERBMP, RESTYPE_IMAGE, NULL, &nSize);

	if (pSource == NULL)
	{
		ISHELL_FreeResData (((CGameApp*)GETAPPINSTANCE())->m_pIShell, pSource);
		return;
	}

	nSize = *((uint16*)pSource);
	pGround = (byte* )pSource + nSize;

	MEMCPY(czMapInfo,(byte*)pGround, 1);
	nCount = (int8)(czMapInfo[0]);
	pGround += 1;
	// 将资源数据读出
	for (i = 0; i < nCount; i++)
	{
		MEMSET(czMapInfo, 0, 6);
		MEMCPY(czMapInfo,(byte*)pGround, 4);

		m_tBmpData[i].nResPosX = (int16)czMapInfo[0];
		m_tBmpData[i].nResPosY = (int16)czMapInfo[1];
		m_tBmpData[i].nResWidth = (int8)czMapInfo[2];
		m_tBmpData[i].nResHeight = (int8)czMapInfo[3];

		pGround += 4;
	}

	// 读出拼图数据
	MEMCPY(czMapInfo,(byte*)pGround, 1);
	nCount = (int8)(czMapInfo[0]);// 几个动作图片
	pGround += 1;
	for (i = 0; i < nCount; i++)
	{
		MEMSET(czMapInfo, 0, 6);
		MEMCPY(czMapInfo,(byte*)pGround, 1);
		nIndex = (int8)czMapInfo[0];
		m_nActionBmpNum[i] = nIndex;
		pGround += 1;
		for (j = 0; j < nIndex; j++)
		{
			MEMSET(czMapInfo, 0, 6);
			MEMCPY(czMapInfo,(byte*)pGround, 4);
			
			m_tPlayerAction[i][j].nResIndex = (int8)czMapInfo[0];
			m_tPlayerAction[i][j].nSeekPosX = (int8)czMapInfo[1];
			m_tPlayerAction[i][j].nSeekPosY = (int8)czMapInfo[2];
			m_tPlayerAction[i][j].bTransform = (boolean)czMapInfo[3];

			pGround += 4;

⌨️ 快捷键说明

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