// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "game.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
LPDIRECTDRAW7 pDD7; //声明DirectDraw对象
LPDIRECTDRAWSURFACE7 pPSur; //声明主绘图页
LPDIRECTDRAWSURFACE7 pBBuf; //声明后缓冲区
LPDIRECTDRAWSURFACE7 pOPla[36]; //声明幕后内存区
DDSCAPS2 caps; //声明DDSCAPS结构
DDSURFACEDESC2 desc; //声明描述结构
HRESULT result;
DDCOLORKEY key;
int arrary[640][480]; // 地图大小的数组
//用来确定不能走的区域
struct juxin
{
int x0;
int y0;
int y1;
int x1;
};
juxin aj[6];
//人物在地图上面的位置
struct renmove
{
int x1;
int y1;
int control;
};
renmove hero, whero;
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
Create(NULL,"绘图窗口",WS_POPUP);
hdc = ::CreateCompatibleDC(NULL);
hero.x1 = 180;
Injuxin(); //确定地图不能走的区域
Inarrary(); //初始化地图不能走的区域
zai = 12;
hero.y1 = 20;
tab = false;
zhi = 8;
whero.x1 = 550;
room = false;
hero.control = 0;
whero.y1 = 150;
control = 1;
///装载图片
chuli("city.bmp", 640, 480, 12);
chu("sub00.bmp", 0);
chu("npc02.bmp", 21);
chu1("Npc01.bmp");
chuli("Dialog.bmp", 640, 100, 13);
chuli("Dialog1.bmp", 640, 100, 16);
chuli("Dialog2.bmp", 640, 100, 17);
chuli("Dialog3.bmp", 640, 100, 18);
chuli("Dialog4.bmp", 640, 100, 19);
chuli("Dialog5.bmp", 640, 100, 29);
chuli("Dialog6.bmp", 640, 100, 30);
chuli("Dialog7.bmp", 640, 100, 31);
chuli("Dialog8.bmp", 640, 100, 32);
chuli("Dialog9.bmp", 640, 100, 33);
chuli("tou.bmp", 85, 85, 14);
chuli("tou3.bmp", 85, 85, 20);
chuli("toutwo.bmp", 85, 85, 34);
chuli("room.bmp", 640, 480, 15);
chuli("end.bmp", 640, 480, 35);
//使用颜色键
for (int i = 0; i < 36; i++)
{
colorkey(i);
}
num = 16;
numx = 0;
//显示人物
pBBuf->BltFast( 0 , 0 , pOPla[zai], CRect(0,0,640,480) , DDBLTFAST_WAIT);
pBBuf->BltFast(180 , 20 , pOPla[0], CRect(0,0,40,40) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
pPSur->Flip(NULL, DDFLIP_WAIT);
}
CMainFrame::~CMainFrame()
{
pDD7->Release();
pPSur->Release();
for (int i = 0; i < 36; i++)
{
pOPla[i]->Release();
}
DeleteObject(bitmap);
DeleteDC(hdc);
DeleteDC(hdc1);
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
i = 0;
i1 = 2;
i2 = 4;
i3 = 6;
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
::ShowCursor(false);
// TODO: Add your specialized creation code here
result = DirectDrawCreateEx(NULL, (VOID**)&pDD7, IID_IDirectDraw7, NULL);
//建立DirectDraw对象
if (result != DD_OK)
MessageBox("建立DirectDraw对象失败!");
result = pDD7->SetCooperativeLevel(m_hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT);
//设定协调层级
if(result !=DD_OK)
MessageBox("设定程序协调层级失败!");
result = pDD7->SetDisplayMode(640,480,16,0,DDSDM_STANDARDVGAMODE); //设定屏幕显示模式
//设定显示模式
if(result !=DD_OK)
MessageBox("设定屏幕显示模式失败!");
//建立主绘图页
memset(&desc,0,sizeof(desc));
desc.dwSize = sizeof(desc);
desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
desc.dwBackBufferCount = 1;
desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
result = pDD7->CreateSurface(&desc,&pPSur,NULL);
if(result !=DD_OK)
{
MessageBox("建立主绘图页失败!");
// return 0;
}
caps.dwCaps = DDSCAPS_BACKBUFFER;
result = pPSur->GetAttachedSurface(&caps,&pBBuf);
//连结后缓冲区
if(result !=DD_OK)
MessageBox("连接后缓冲区失败!");
memset(&desc,0,sizeof(desc));
desc.dwSize = sizeof(desc);
desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN ;
SetTimer(1, 150, NULL);
return 0;
}
void CMainFrame::OnPaint()
{
// CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CPaintDC dc(this);
// pBBuf->BltFast( 0 , 0 , pOPla, CRect(0,0,640,480) , DDBLTFAST_WAIT );
// pPSur->Flip( NULL , DDFLIP_WAIT );
// Do not call CFrameWnd::OnPaint() for painting messages
}
void CMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if (nChar == VK_ESCAPE )
{//判断是否按下 Esc 键
PostMessage(WM_CLOSE );
KillTimer(1);
}
if (nChar == VK_DOWN)
{
numx = 0;
num = 16;
hero.y1 += 10;
if ((hero .y1 == 420) && (hero.x1 <= 70) && (hero.x1 >= 50))
{
if (duihua != 0)
{
hero.y1 -= 10;
pBBuf->BltFast( 0 , 0 , pOPla[zai], CRect(0,0,640,480) , DDBLTFAST_WAIT);
pBBuf->BltFast(0, 380, pOPla[29], CRect(0, 0, 640,100) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
pBBuf->BltFast(5, 385, pOPla[14], CRect(0, 0, 85,85) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
pBBuf->BltFast(hero.x1, hero.y1, pOPla[4], CRect(0,0,40,40) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
pBBuf->BltFast(450, 160, pOPla[23], CRect(0,0,40,40) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
pPSur->Flip(NULL, DDFLIP_WAIT);
return;
}
else
{
pBBuf->BltFast( 0 , 0 , pOPla[35], CRect(0,0,640,480) , DDBLTFAST_WAIT);
pPSur->Flip(NULL, DDFLIP_WAIT);
Sleep(3000); //结束画面停留的时间,根据自己需要调整
OnClose();
//如果不想从公主房间出来后就马上结束就用注释的代码
/* zai = 12;
arrary[hero.x1][hero.y1] = 0;
Inarrary();
hero.y1 = 340;
hero.x1 = 440;
room = false;*/
}
}
hero.control = i;
if (arrary[hero.x1][hero.y1] != 0)
{
hero.y1 -= 10;
return;
}
else if (arrary[hero.x1][hero.y1] == 0)
{
if ((hero.x1 == whero.x1) && (hero.y1 + 40 == whero.y1))
{
return;
}
pBBuf->BltFast( 0 , 0 , pOPla[zai], CRect(0,0,640,480) , DDBLTFAST_WAIT);
if (tab)
{
pBBuf->BltFast(0, 380, pOPla[13], CRect(0, 0, 640,100) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
pBBuf->BltFast(5, 385, pOPla[14], CRect(0, 0, 85,85) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
}
pBBuf->BltFast(hero.x1, hero.y1, pOPla[i++], CRect(0,0,40,40) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
if (!room)
{
pBBuf->BltFast(whero.x1, whero.y1, pOPla[zhi], CRect(0,0,40,40) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
}
else
{
pBBuf->BltFast(450, 160, pOPla[23], CRect(0,0,40,40) , DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
}
}
if (i > 1)
{
i = 0;
}
}
if (nChar == VK_UP)
{
numx = 0;
num = 16;
hero.y1 -= 10;
if ((hero .y1 == 310) && (hero.x1 <= 455) && (hero.x1 >= 435))
{
zai = 15;
arrary[hero.x1][hero.y1] = 0;
Inarrary1();
h