// canvasFrame.cpp : implementation file
//
#include "stdafx.h"
#include "canvasr.h"
#include "canvasFrame.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct bullet //飛機的子彈
{
int x; //貼圖的 X 座標
int y; //貼圖的 Y 座標
int hitx; //碰撞點的 X 座標
int hity; //碰撞點的 Y 座標
BOOL exist; //子彈是否存在
};
struct cartridge //怪物的子彈
{
int x;
int y;
int hitx;
int hity;
BOOL exist;
};
struct ship //飛機
{
int x; //貼圖的 X 座標
int y; //貼圖的 Y 座標
RECT r[3]; //飛機的區塊
};
struct monster //怪物
{
int x; //貼圖的 X 座標
int y; //貼圖的 Y 座標
int type; //怪物的類型
int draw; //控制貼圖速度的變數
RECT r[3]; //怪物的區塊
POINT p[4]; //怪物的碰撞點
BOOL exist; //怪物是否存在
};
/////////////////////////////////////////////////////////////////////////////
// canvasFrame
IMPLEMENT_DYNCREATE(canvasFrame, CFrameWnd)
//宣告 DirectDraw 變數
LPDIRECTDRAW7 DD;
LPDIRECTDRAWSURFACE7 DDSur;
LPDIRECTDRAWSURFACE7 DDBuf;
LPDIRECTDRAWSURFACE7 DDPla[10];
DDSCAPS2 DDcaps;
DDSURFACEDESC2 DDde;
HRESULT result;
DDCOLORKEY key;
//宣告 DirectSound 變數
LPDIRECTSOUND DS;
LPDIRECTSOUNDBUFFER DSPri;
LPDIRECTSOUNDBUFFER DSBuf[6];
WAVEFORMATEX DSfmt;
WAVEFORMATEX wfmt;
DSBUFFERDESC DSde;
MMCKINFO ckRiff;
MMCKINFO ckInfo;
MMRESULT mmresult;
HMMIO hmmio;
DWORD wsize;
LPVOID pAudio;
DWORD bytesAudio;
//宣告 DirectInput 變數
LPDIRECTINPUT7 DI;
LPDIRECTINPUTDEVICE7 DIms;
DIMOUSESTATE2 DImstate;
//宣告其它變數
HDC hdc,dhdc;
HBITMAP bitmap;
int i,j,m,n;
int bgx;
int bcount,mcount,ccount;
int t,f; //控制螢幕貼圖
int score=0; //分數
BOOL press,hit,over;
cartridge c[100]; //怪物子彈陣列
bullet b[100]; //飛機子彈陣列
monster mon[50]; //怪物陣列
ship ship;
char str[20];
canvasFrame::canvasFrame()
{
Create(NULL,"繪圖視窗",WS_POPUP);
hdc = ::CreateCompatibleDC(NULL);
::ShowCursor(false);
CreateDDPla(640,480,"load.bmp",9);
DDBuf->BltFast( 0 , 0 , DDPla[9], CRect(0,0,640,480) , DDBLTFAST_WAIT);
DDSur->Flip( NULL , DDFLIP_WAIT ); //顯示載入中圖案
CreateDDPla(640,480,"bgbmp.bmp",0);
CreateDDPla(100,74,"ship.bmp",1);
CreateDDPla(10,10,"bullet.bmp",2);
CreateDDPla(80,59,"mon0.bmp",3);
CreateDDPla(60,56,"mon1.bmp",4);
CreateDDPla(79,51,"mon2.bmp",5);
CreateDDPla(10,10,"cartridge.bmp",6);
CreateDDPla(80,80,"hit.bmp",7);
CreateDDPla(640,159,"over.bmp",8);
CreateDSBuf("s0.wav",0);
CreateDSBuf("s1.wav",1);
CreateDSBuf("s2.wav",2);
CreateDSBuf("s3.wav",3);
ColorKey(1);
ColorKey(2);
ColorKey(3);
ColorKey(4);
ColorKey(5);
ColorKey(6);
ColorKey(7);
ColorKey(8);
ship.x = 540;
ship.y = 200;
DSBuf[0]->Play(0,0,1); //播放背景音樂
}
canvasFrame::~canvasFrame()
{
delete bitmap;
delete hdc;
delete dhdc;
DD->Release();
DDSur->Release();
for(i=0;i<=9;i++)
DDPla[i]->Release();
DI->Release();
DIms->Release();
DS->Release();
DSPri->Release();
for(i=0;i<=2;i++)
DSBuf[i]->Release();
}
BEGIN_MESSAGE_MAP(canvasFrame, CFrameWnd)
//{{AFX_MSG_MAP(canvasFrame)
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// canvasFrame message handlers
int canvasFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
InitDD();
InitDS();
InitDI();
SetTimer(1,0,NULL);
return 0;
}
//建立幕後暫存區並載入圖檔的自訂函式
void canvasFrame::CreateDDPla(int width,int height,char* filename,int num)
{
DDde.dwWidth = width;
DDde.dwHeight = height;
result = DD->CreateSurface(&DDde, &DDPla[num], NULL);
if(result !=DD_OK)
{
MessageBox("建立幕後暫存區失敗!");
return;
}
bitmap = (HBITMAP)::LoadImage(NULL,filename,IMAGE_BITMAP,width,height,LR_LOADFROMFILE);
if(bitmap==NULL)
{
MessageBox("無法載入點陣圖!");
return;
}
::SelectObject(hdc,bitmap);
DDPla[num]->GetDC( &dhdc );
::BitBlt( dhdc , 0 , 0 ,width,height, hdc , 0 , 0 , SRCCOPY );
DDPla[num]->ReleaseDC(dhdc);
}
void canvasFrame::ColorKey(int num)
{
key.dwColorSpaceHighValue = 0;
key.dwColorSpaceLowValue = 0;
DDPla[num]->SetColorKey(DDCKEY_SRCBLT,&key);
}
void canvasFrame::Generate()
{
switch(rand()%3)
{
case 0:
for(i=0;i<50;i++)
{
if(!mon[i].exist)
{
mon[i].type = 0;
mon[i].x = 0;
mon[i].y = rand()%420;
mon[i].exist = true;
mon[i].r[0].left = mon[i].x + 28;
mon[i].r[0].top = mon[i].y + 7;
mon[i].r[0].right = mon[i].x + 72;
mon[i].r[0].bottom = mon[i].y + 34;
mon[i].r[1].left = mon[i].x;
mon[i].r[1].top = mon[i].y + 23;
mon[i].r[1].right = mon[i].x + 49;
mon[i].r[1].bottom = mon[i].y + 59;
mon[i].r[2].left = mon[i].x + 24;
mon[i].r[2].top = mon[i].y + 50;
mon[i].r[2].right = mon[i].x + 48;
mon[i].r[2].bottom = mon[i].y + 59;
mon[i].p[0].x = mon[i].x +46;
mon[i].p[0].y = mon[i].y + 8;
mon[i].p[1].x = mon[i].x + 71;
mon[i].p[1].y = mon[i].y + 22;
mon[i].p[2].x = mon[i].x + 36;
mon[i].p[2].y = mon[i].y + 57;
mon[i].p[3].x = mon[i].x + 1;
mon[i].p[3].y = mon[i].y + 31;
mcount++;
break;
}
}
break;
case 1:
for(i=0;i<50;i++)
{
if(!mon[i].exist)
{
mon[i].type = 1;
mon[i].x = 0;
mon[i].y = rand()%420;
mon[i].exist = true;
mon[i].r[0].left = mon[i].x + 14;
mon[i].r[0].top = mon[i].y + 4;
mon[i].r[0].right = mon[i].x + 57;
mon[i].r[0].bottom = mon[i].y + 26;
mon[i].r[1].left = mon[i].x+1;
mon[i].r[1].top = mon[i].y + 14;
mon[i].r[1].right = mon[i].x + 43;
mon[i].r[1].bottom = mon[i].y + 41;
mon[i].r[2].left = mon[i].x + 5;
mon[i].r[2].top = mon[i].y + 34;
mon[i].r[2].right = mon[i].x + 22;
mon[i].r[2].bottom = mon[i].y + 55;
mon[i].p[0].x = mon[i].x +52;
mon[i].p[0].y = mon[i].y + 18;
mon[i].p[1].x = mon[i].x + 20;
mon[i].p[1].y = mon[i].y + 4;
mon[i].p[2].x = mon[i].x + 3;
mon[i].p[2].y = mon[i].y + 22;
mon[i].p[3].x = mon[i].x + 14;
mon[i].p[3].y = mon[i].y + 50;
mcount++;
break;
}
}
break;
case 2:
for(i=0;i<50;i++)
{
if(!mon[i].exist)
{
mon[i].type = 2;
mon[i].x = 580;
mon[i].y = rand()%420;
mon[i].exist = true;
mon[i].r[0].left = mon[i].x + 1;
mon[i].r[0].top = mon[i].y + 8;
mon[i].r[0].right = mon[i].x + 78;
mon[i].r[0].bottom = mon[i].y + 37;
mon[i].r[1].left = mon[i].x+8;
mon[i].r[1].top = mon[i].y + 36;
mon[i].r[1].right = mon[i].x + 52;
mon[i].r[1].bottom = mon[i].y + 50;
mon[i].r[2].left = mon[i].x + 20;
mon[i].r[2].top = mon[i].y;
mon[i].r[2].right = mon[i].x + 37;
mon[i].r[2].bottom = mon[i].y + 8;
mon[i].p[0].x = mon[i].x +4;
mon[i].p[0].y = mon[i].y + 21;
mon[i].p[1].x = mon[i].x + 31;
mon[i].p[1].y = mon[i].y + 3;
mon[i].p[2].x = mon[i].x + 72;
mon[i].p[2].y = mon[i].y + 24;
mon[i].p[3].x = mon[i].x + 36;
mon[i].p[3].y = mon[i].y + 45;
mcount++;
break;
}
}
break;
}
}
void canvasFrame::OnTimer(UINT nIDEvent)
{
if(!over)
{
t++;
if(score>10000)
f = 20;
else if(score<=10000 && score>5500)
f = 30;
else if(score<=5500 && score >2000)
f = 40;
else
f = 50;
if(t%f==0)
{
Generate(); //產生怪物