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

📄 gtgame.cpp

📁 这个例子告诉你2d Arpg游戏中的动态阴影如何生成
💻 CPP
字号:
// gtGame.cpp: implementation of the gtGame class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "gtGame.h"
#include "CDInput.h"
#include "resource.h"
#include "log.h"
#include <stdio.h>
#define ENABLE_TRACE

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

//BEGIN_MAP_MESSAGE 
const WMessageFunc GtGame::messageEntries[]={
	{0,0},
MAP_MESSAGE(WM_LBUTTONDOWN,OnLButtonDown)
MAP_MESSAGE(WM_DESTROY,OnDestroy)
MAP_MESSAGE(WM_KEYDOWN,OnKeyDown)
END_MAP_MESSAGE()

CDIKeyBoard	theKeyBoard;
CDIMouse	theMouse;

GtGame::GtGame(HINSTANCE hInstance,int nCmdShow) : hInstance(hInstance) , nCmdShow(nCmdShow)
{

}

GtGame::~GtGame()
{

}


ATOM GtGame::MyRegisterClass()
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= 0;
	wcex.lpfnWndProc	= (WNDPROC)GtGame::_ProcFn;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= NULL;
	wcex.hbrBackground	= NULL;
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "raomiao";
	wcex.hIconSm		=LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

/* 
函数名称:	Create 
函数全名: gtGame::MainLoop 
函数权限: public 
返回类型: void
函数版本: 1.0 
函数功能: 
游戏主消息循环 
*/
void GtGame::Create() 
{ 
   this->MyRegisterClass();

   this->GameHwnd = CreateWindow("raomiao", "Chess", WS_OVERLAPPEDWINDOW,0,0,
								  GetSystemMetrics( SM_CXSCREEN ),
								  GetSystemMetrics( SM_CYSCREEN ),
								  NULL,NULL,hInstance,NULL);

   if (!this->GameHwnd)
   {
	   MessageBox(NULL,"错误","初始化失败",MB_OK);
		return ;
   }

   ShowWindow(this->GameHwnd, nCmdShow);
   UpdateWindow(this->GameHwnd);

   SetWindowLong(GameHwnd, GWL_USERDATA, (LONG) this );
 //  surface.Init();

    InitEasyDraw(this->GameHwnd,false,800,600);
  // surface.CreateImageSurface("move.bmp",1024,768,DDBLTFAST_NOCOLORKEY);
 //  surface.SetSurfaceColorKey(0xf81f);
 //  surface1.CreateImageSurface("iron_barrier_02.bmp",117,195,DDBLTFAST_NOCOLORKEY);
//  surface1.SetSurfaceColorKey(0xf81f);
  map.Init("my.bmp",20,20);
//  build.Init("iron_barrier_02.bmp",0,0);
  hero.Init("my2.bmp",200,200);
//  CDInput::InitDInput(this->hInstance);
   theKeyBoard.Init(this->GameHwnd,this->hInstance);
   theMouse.Init(this->GameHwnd,this->hInstance);
   timer.UpdateFps();
   	 font=CreateFont(15,8,0,0,FW_EXTRALIGHT
		 ,0,0,0,DEFAULT_CHARSET,   
					 OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,   
						DEFAULT_PITCH,"楷体_GB2312");
//   Easydraw=new EasyDraw();
//	Easydraw->InitDDraw(GameHwnd,800,600);
} 


LRESULT CALLBACK GtGame::_ProcFn ( HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam ) 
{
	GtGame *wnd=(GtGame*)GetWindowLong(hwnd,GWL_USERDATA);
	if (wnd) {
		const WMessageFunc *mf=wnd->GetMessageEntries();
		do {
			int i;
			for (i=1;mf[i].func;i++) {
				if (mf[i].id==uMsg) {
					if ((wnd->*(mf[i].func))(wParam,lParam)==-1)
						return 0;
					else break;
				}
			}
			mf=(const WMessageFunc*)(mf[0].id);
		} while (mf!=0);
	}
	return DefWindowProc(hwnd,uMsg, wParam, lParam);
} 

/* 
函数名称: MainLoop 
函数全名: gtGame::MainLoop 
函数权限: public 
返回类型: int 
函数版本: 1.0 
函数功能: 
游戏主消息循环 

  调用: 
  [API]PeekMessage 
  [API]TranslateMessage 
  [API]DispatchMessage 
  [API]WaitMessage 
*/ 
int GtGame::MainLoop() 
{ 
	MSG msg; 
	bool bIsQuit = false; 
	while ( !bIsQuit ) 
	{ 
		if(::PeekMessage(&msg, NULL, 0,0,PM_REMOVE)) 
		{ 
			if(msg.message == WM_QUIT) 
			{ 
				bIsQuit = true; 
			} 
			::TranslateMessage( &msg ); 
			::DispatchMessage( &msg ); 
		}
		else
		{
			RECT rect={0,0,1024,768};
			RECT rect1={0,0,153,117};
			theMouse.UpdateMouse(this->GameHwnd);
			map.MoveMap();
			map.ShowMap();
			if(theMouse.IsLPress())
			{
				MessageBox(this->GameHwnd,"dsf","",MB_OK);
			}
			hero.ShowBuilding(map.view);
			timer.UpdateFps();
			HDC hdc;
			::SelectObject(hdc,font);
			//	char text="雕花笼·绝代双骄前传·主题曲";
			if (GetEasyDrawPointer()->GetBackSurface()->GetDC(&hdc) == DD_OK)
			{
				::SelectObject(hdc,font);
				SetBkMode(hdc,TRANSPARENT);
				SetTextColor(hdc, RGB(255, 255, 255));
				char str[128];
				sprintf(str,"FPS:%d/s",timer.GetFps());
				TextOut(hdc,10,10, str,strlen(str));
				GetEasyDrawPointer()->GetBackSurface()->ReleaseDC(hdc);
			}
			::ReleaseDC(this->GameHwnd,hdc);
			GetEasyDrawPointer()->Filp();
			Sleep(0);
		//	Sleep(10);
		//	Sleep(50);
		}
	} 
	
	return msg.wParam; 
} 

int GtGame::OnLButtonDown(DWORD w,DWORD l)
{
	RECT rect={0,0,200,200};
	surface.CopySurfaceToBmp("test.bmp",&rect);
	return 0;
}

int GtGame::OnDestroy(DWORD w,DWORD l)
{
	PostQuitMessage(0);	
	return 0;
}

int GtGame::OnKeyDown(DWORD w,DWORD l)
{
	if(::GetKeyState(VK_ESCAPE) & 0x80) 
	{ 
		::SendMessage(this->GameHwnd, WM_CLOSE, 0, 0 ); 
	} 
	return 0; 
}
/* 
End-Of-File "gtGame.cpp" 
*/ 

⌨️ 快捷键说明

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