📄 czdemo.cpp
字号:
//************************************************************
//游戏主程序
//作者:曾铮
//E-Mail:Noshoeman@hotmail.com || Noshoeman23@yahoo.com.cn
//时间:2004年6月起
//说明:游戏最重要的一个文件,处理游戏循环
//************************************************************
#include "stdafx.h"
#include "resource.h"
#include "CZDemo.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_CZDEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
//初始化DirectDraw环境,并实现DirectDraw功能
InitDDraw();
InitGameSurfaces();
//初始化DirectSound对象。
InitDirectSound(hWnd);
PlayMidiInit();
//加载声音。
getmoney=LoadSoundFX(hWnd,"sound/getmoney.wav");
losemoney=LoadSoundFX(hWnd,"sound/losemoney.wav");
build=LoadSoundFX(hWnd,"sound/build.wav");
getexp=LoadSoundFX(hWnd,"sound/getexp.wav");
stepsound=LoadSoundFX(hWnd,"sound/stepsound.wav");
selectsnd=LoadSoundFX(hWnd,"sound/select.wav");
suresnd=LoadSoundFX(hWnd,"sound/suresnd.wav");
success=LoadSoundFX(hWnd,"sound/success.wav");
fail=LoadSoundFX(hWnd,"sound/fail.wav");
//进入菜单状态。
mygame._SetMenu(true);
//显示健康游戏标志
CreateBitmap(pLogo,ScreenW,ScreenH,"pics/health.fim",DDSCAPS_SYSTEMMEMORY);
GL_ShowLogo();
pLogo->Release();
pLogo = NULL;//显示完后释放资源
PressKey(VK_CONTROL,1);
//显示起初菜单:
ChooseMenu(mygame);//设定游戏玩家数量。
players[0].SetRxy();
GL_GamePicShowSilence();//内调用MakeGround();
Flip();
//////////////////////////////////////////
//
//
// Main message loop:
//
//
///////////////////////////////////////////
isPlayingMidi=true;
//打开背景音乐:
if(isPlayingMidi==true)
{
//PlayMidi_1();
PlayBGMusic();
}
oldtick=tick=GetTickCount();//初始化记数
// SoundAble=false;//设置为无音效
while(true)
// while(PeekMessage(&msg, hWnd, 0, 0, PM_NOREMOVE))
{
//1.从消息队列中取得需要处理的消息。
if(PeekMessage(&msg, hWnd, 0, 0, PM_NOREMOVE))
{//PM_NORMOVE在PeekMessage取得消息后经处理后再从消息队列中删除。
if(!GetMessage(&msg, hWnd, 0, 0 ))
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//2.程序处于活动状态。
else if(g_bActive==TRUE)
// if(g_bActive==true)
{
SetCursor(NULL);
if(mygame._IsMenu()==true)//处于菜单状态,进行切换。
{
}
if(mygame._IsBegin()==true)//处于游戏进行状态。
{
tick=GetTickCount();//游戏时间提醒
if(tick>=oldtick+120000*15)
{
oldtick=tick;
char* info="您游戏时间已经过了大约三十分钟了,休息休息吧。";
MyMessage::PutMSG(SHOWMSG,info);
mymessage.RunMSG();
}
SetCursor(NULL);
GL_GameLoop(); //进入游戏主循环
}
}
//3.等待消息,程序不处于活动状态。
else
WaitMessage(); //挂起当前线程,将控制交给其他线程。
}
}
/////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_CZDEMO);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_CZDEMO;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
/////////////////////////////////////////////////////////////
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//////////////////////////////////////////////////////////////
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP,
0, 0,1024, 768, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//显示小组标志及健康提示:
void GL_ShowLogo()
{
RECT logor={0,0,ScreenW,ScreenH};
pPSur->BltFast(0,0,pLogo,&logor,DDBLTFAST_WAIT);
pBBuf->BltFast(0,0,pLogo,&logor,DDBLTFAST_WAIT);
Flip();
if(GetAsyncKeyState(VK_RETURN))
return ;
Sleep(3000);
PressKey(VK_RETURN,1);
PressKey(VK_CONTROL,1);
PressKey(VK_UP,1);
PressKey(VK_DOWN,1);
PressKey(VK_LEFT,1);
PressKey(VK_RIGHT,1);
}
//初始化玩家
int GL_InitPlayers(int index)
{
//初始化玩家。
ifstream plain;
if(index==1)
plain.open("gamedata/map1.pla");//plain:player infomation
else if(index==2)
plain.open("gamedata/map2.pla");
else
plain.open("gamedata/map3.pla");
int planum=0;//角色数量
int tempm=0;//临时金钱
int tempexp=0;//临时经验
int tx=0,ty=0;//x,y坐标
int iscomp=0;//是否为电脑
int isexist=1;//1为存在
int actualplayern=0;//实际有的角色数量
plain>>planum;
for(int j=0;j<4;j++)
{
plain>>isexist;
if(isexist)
{
players[j].existence=true;
++actualplayern;
}
else
players[j].existence=false;
plain>>tx;
players[j].x=tx*32;
plain>>ty;
players[j].y=(ty-1)*32;
plain>>tempm;
players[j].ChangeMoney(tempm);
plain>>tempexp;
players[j].ChangeExp(tempexp);
plain>>iscomp;
if(iscomp!=0)
//为玩家
players[j].iscomp=false;
else
players[j].iscomp=true;
plain>>players[j].name;
switch (j)
{
case 0:
players[j].LoadPhoto("photo/p1.fim");
break;
case 1:
players[j].LoadPhoto("photo/p2.fim");
break;
case 2:
players[j].LoadPhoto("photo/p3.fim");
break;
case 3:
players[j].LoadPhoto("photo/p4.fim");
break;
}
players[j].status=0;
players[j].orientation=GODOWN;
}
plain.close();
return actualplayern;
}
//进行游戏的初始化
int GL_InitGame(int index)
{
//初始化地图。
maps.LoadMap(index);
maps.LoadBlock(index);
//初始化建筑矩阵:
for(int i=0; i<48 ; i++)
for(int j=0; j<64 ; j++)
maps.building[j][i]=maps.path[j][i];
return GL_InitPlayers(index);
}
////////////////////////////////////////////////////////////
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
srand( (unsigned)time(NULL) );//设定随机数种
switch (message)
{
case WM_COMMAND:
break;
//应用程序运行。
case WM_ACTIVATEAPP:
g_bActive = wParam; //获取应用程序运行标志。
break;
//进入DEBUG模式。
case WM_SYSKEYDOWN:
switch(wParam)
{
case 'B':
case 'b':
{
//作弊:)
players[PlayerID].ChangeMoney(1000);
MyTool* ptool;//=new MyTool;
supercar* sc=new supercar;
ptool=(MyTool*)sc;
players[PlayerID].BuyTools(ptool,PlayerID);
remodice *rm=new remodice;
ptool=(MyTool*)rm;
players[PlayerID].BuyTools(ptool,PlayerID);
buycard *bc=new buycard;
ptool=(MyTool*)bc;
players[PlayerID].BuyTools(ptool,PlayerID);
tortcard *tc=new tortcard;
ptool=(MyTool*)tc;
players[PlayerID].BuyTools(ptool,PlayerID);
stopcard *stc=new stopcard;
ptool=(MyTool*)stc;
players[PlayerID].BuyTools(ptool,PlayerID);
compmanager *cm=new compmanager;
ptool=(MyTool*)cm;
players[PlayerID].BuyTools(ptool,PlayerID);
}
break;
case 'S':
case 's':
//作弊:)
buyinshop(PlayerID);
break;
}
break;
//系统按键控制。
case WM_KEYDOWN:
switch(wParam)
{
case VK_RETURN:
break;
case VK_ESCAPE://显示主菜单。
{
//返回后显示游戏画面。
}
break;
case VK_TAB: //暂时作为音乐开关。
{
/* if(mygame._IsBegin()==true)
{
//PlayDirectSound(bgmusic,1);
if(true==isPlayingMidi)
{
StopMidi();
isPlayingMidi=false;
}
else if(PlayMidi_1()==false)
{
RECT rect={0,0,266,198};
GL_GamePicShowSilence();//保存游戏画面
Blt(pBBuf,300,200,pMenu,rect,false);
PrintText(pBBuf,330,260,"音乐打不开!");
Flip();
PressKey(VK_RETURN,WAIT);
}
else
{
isPlayingMidi=true;
RECT rect={0,0,266,198};
GL_GamePicShowSilence();//保存游戏画面
Blt(pBBuf,300,200,pMenu,rect,false);
PrintText(pBBuf,330,260,"音乐已经打开!");
Flip();
PressKey(VK_RETURN,WAIT);
}
}
*/
}
break;
case VK_F1:
/* { //设置游戏进行开关。
if(mygame._IsBegin())
mygame._SetBegin(false);
else
mygame._SetBegin(true);
break;
*/
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
// if(mygame._IsPlaying()==true)
// {
// //restoreAll();
// GL_GamePicShowSilence();
// Flip();
// }
EndPaint(hWnd, &ps);
break;
//定时器消息,用来控制角色行走。
case WM_TIMER:
{
if(players[PlayerID].stop!=true)
{
PressKey(VK_RETURN,1);
players[PlayerID].PlayerGoEx(nextori);//使用新的寻路算法。
//在这里作图
{
//重作地图,显示人物,播放走动声音。
//steps++;
GL_GamePicShow();
Flip();
}
psteploop++;//增加小步行走计数。0~3共四个
}
}//switch WM_TIMER
if(psteploop>3 || players[PlayerID].stop==true )
{
//选择下一步方向
nextori=GetNextDirection((players[PlayerID].x+16)/32,(players[PlayerID].y+47)/32,players[PlayerID].orientation);
steps++;
psteploop=0;
{//步数大于6或者大于12就进入主循环,以重新设置步数。
if(steps>6 || steps>12)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -