?? draw_cards_engine.cpp
字號:
// Draw_Cards_Engine.cpp: implementation of the CDrawCardsEngine class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "斗地主.h"
#include "MainFrm.h"
#include "Game.h"
#include "My_DirectInput.h"
#include "my_directdraw.h"
#include "Draw_Cards_Engine.h"
#include "Playing_Cards.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//撲克的位置;
CCardPos::CCardPos()
{
m_nX = -1; //表示沒有;
m_nY = -1;
m_bVisual = 1; //表示可見;
}
//撲克牌布局地圖結構初始化;
CCardsMap::CCardsMap()
{
for(int i=0;i<20;i++)
{
Center0[i].m_nX = Center_x0 + Card_Distance1 * i;
Center0[i].m_nY = Center_y0;
Center1[i].m_nX = Center_x1 + Card_Distance0 * i;
Center1[i].m_nY = Center_y1;
Center2[i].m_nX = Center_x2 + Card_Distance2 * i;
Center2[i].m_nY = Center_y2;
}
for(int j=0;j<20;j++)
{
Left0[j].m_nX = Left_x0;
Left0[j].m_nY = Left_y0 + Card_Distance0 * j;
Left1[j].m_nX = Left_x1;
Left1[j].m_nY = Left_y1 + Card_Distance1 * j;
Left2[j].m_nX = Left_x2;
Left2[j].m_nY = Left_y2 + Card_Distance2 * j;
}
for(int k=0;k<20;k++)
{
Right0[k].m_nX = Right_x0;
Right0[k].m_nY = Right_y0 + Card_Distance0 * k;
Right1[k].m_nX = Right_x1;
Right1[k].m_nY = Right_y1 + Card_Distance1 * k;
Right2[k].m_nX = Right_x2;
Right2[k].m_nY = Right_y2 + Card_Distance2 * k;
}
m_cLordCards[0].m_nX = Lord_Card1_x;
m_cLordCards[0].m_nY = Lord_Card1_y;
m_cLordCards[1].m_nX = Lord_Card2_x;
m_cLordCards[1].m_nY = Lord_Card2_y;
m_cLordCards[2].m_nX = Lord_Card3_x;
m_cLordCards[2].m_nY = Lord_Card3_y;
m_cDistributeCard.m_nX = Card_x;
m_cDistributeCard.m_nY = Card_y;
m_nLordCards = 0;
m_nUpCounter = 0;
}
//CDrawCardsEngine
CDrawCardsEngine::CDrawCardsEngine():BLUE( _RGB32BIT(0,0,0,255) )
{
m_bVisual = FALSE;
}
CDrawCardsEngine::~CDrawCardsEngine()
{
}
//渲染初始化;
int CDrawCardsEngine::Init()
{
char* file;
//14種牌從小到大的加載;
//3-10
char filename[50];
for(int i=0;i<=7;i++)
{
sprintf(filename,"bmp\\Cards\\%d.bmp",i+3);
lpddsbg_Cards[i] = pDraw->CreateSurface(Cards_Width*4,Cards_Height,0,BLUE);
pDraw->BitmapToSurface(lpddsbg_Cards[i],0,0,filename,Cards_Width*4,Cards_Height);
}
//J
file = "bmp\\Cards\\j.bmp";
lpddsbg_Cards[8] = pDraw->CreateSurface(Cards_Width*4,Cards_Height,0,BLUE);
pDraw->BitmapToSurface(lpddsbg_Cards[8],0,0,file,Cards_Width*4,Cards_Height);
//Q
file = "bmp\\Cards\\Q.bmp";
lpddsbg_Cards[9] = pDraw->CreateSurface(Cards_Width*4,Cards_Height,0,BLUE);
pDraw->BitmapToSurface(lpddsbg_Cards[9],0,0,file,Cards_Width*4,Cards_Height);
//K
file = "bmp\\Cards\\K.bmp";
lpddsbg_Cards[10] = pDraw->CreateSurface(Cards_Width*4,Cards_Height,0,BLUE);
pDraw->BitmapToSurface(lpddsbg_Cards[10],0,0,file,Cards_Width*4,Cards_Height);
//A
file = "bmp\\Cards\\A.bmp";
lpddsbg_Cards[11] = pDraw->CreateSurface(Cards_Width*4,Cards_Height,0,BLUE);
pDraw->BitmapToSurface(lpddsbg_Cards[11],0,0,file,Cards_Width*4,Cards_Height);
//2
file = "bmp\\Cards\\2.bmp";
lpddsbg_Cards[12] = pDraw->CreateSurface(Cards_Width*4,Cards_Height,0,BLUE);
pDraw->BitmapToSurface(lpddsbg_Cards[12],0,0,file,Cards_Width*4,Cards_Height);
//加載百搭;
file = "bmp\\Cards\\jokers.bmp";
lpddsbg_Cards[13] = pDraw->CreateSurface(Cards_Width*2,Cards_Height,0,BLUE);
pDraw->BitmapToSurface(lpddsbg_Cards[13],0,0,file,Cards_Width*2,Cards_Height);
//加載撲克背面;
file = "bmp\\Cards\\card_back1.bmp";
lpddsbg_Cards[14] = pDraw->CreateSurface(Cards_Width,Cards_Height,0,BLUE);
pDraw->BitmapToSurface(lpddsbg_Cards[14],0,0,file,Cards_Width,Cards_Height);
return 1;
}
int CDrawCardsEngine::DrawCard(int x,
int y,
int Card_m_nColor,
int Card_Value,
LPDIRECTDRAWSURFACE7 lpdds_Dest )
{
lpdds_Dest->Blt(CRect(x,
y,
x+Cards_Width,
y+Cards_Height),
lpddsbg_Cards[Card_Value - 3],
CRect(Card_m_nColor*Cards_Width,
0,
Card_m_nColor*Cards_Width + Cards_Width,
Cards_Height),
DDBLT_WAIT | DDBLT_KEYSRC,
NULL);
return 1;
}
int CDrawCardsEngine::MoveCard(int Dest_x,
int Dest_y,
int Source_x,
int Source_y,
int Card_m_nColor,
int Card_Value,
int Speed) //移動速度,以象素為單位;
// LPDIRECTDRAWSURFACE7 lpdds_Dest = lpddsback)
{
int dx,dy,dx2,dy2,x_inc,y_inc,error;
int x = Source_x,y = Source_y;
dx = Dest_x - Source_x;
dy = Dest_y - Source_y;
if(dx>=0)
{
x_inc = 1;
}
else
{
x_inc = -1;
dx = -dx;
}
if(dy >= 0)
{
y_inc = 1;
}
else
{
y_inc = -1;
dy = -dy;
}
dx2 = dx << 1;
dy2 = dy << 1;
if(dx > dy)
{
error = dy2 - dx;
for(int i=0;i<dx;i++)
{
if(i%Speed == 0)
{
::RedrawGame(NULL);
DrawCard(x,y,Card_m_nColor,Card_Value);
while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
}
if(error >= 0)
{
error-=dx2;
y+=y_inc;
}
error+=dy2;
x+=x_inc;
}//end for~
}
else
{
error = dx2 - dy;
for(int i=0;i<dy;i++)
{
if(i%Speed == 0)
{
::RedrawGame(NULL);
DrawCard(x,y,Card_m_nColor,Card_Value);
while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
}
if(error >= 0)
{
error-=dy2;
x+=x_inc;
}
error+=dx2;
y+=y_inc;
}//end for~
}
::RedrawGame(NULL);
DrawCard(x,y,Card_m_nColor,Card_Value);
while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
return 1;
}
//重新整理中間(本機)玩家的牌面;
int CDrawCardsEngine::DrawCleanUp()
{
pCenterCards->CleanUp(); //整理牌;
for(int i=0;i<pCenterCards->m_nCardsCounter;i++)
{
pCardsMap->Center0[i].m_bVisual = 0;
}
for(int j=0;j<pCenterCards->m_nCardsCounter;j++)
{
pCardsMap->Center0[j].m_bVisual = 1;
::RedrawGame(); //刷新;
}
return 1;
}
//發牌;
int CDrawCardsEngine::Dealing()
{
//分牌給三個玩家;
for(int i=0;i<17;i++)
{
//發牌給左邊玩家;
pLeftCards->m_cCards[i] = g_cAllCards[i*3 + 0];
MoveCard(Left_x0,
pCardsMap->Left0[i].m_nY,
Card_x,
Card_y,
0,
17);
pLeftCards->m_nCardsCounter++;
//發牌給中間玩家;
pCenterCards->m_cCards[i] = g_cAllCards[i*3 + 1];
MoveCard(pCardsMap->Center0[i].m_nX,
Center_y0,
Card_x,
Card_y,
pCenterCards->m_cCards[i].m_nColor,
pCenterCards->m_cCards[i].m_nValue,
100);
pCenterCards->m_nCardsCounter++;
PlaySound(MAKEINTRESOURCE(IDR_GIVE),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
//發牌給右邊玩家;
pRightCards->m_cCards[i] = g_cAllCards[i*3 + 2];
MoveCard(Right_x0,
pCardsMap->Right0[i].m_nY,
Card_x,
Card_y,
0,
17);
pRightCards->m_nCardsCounter++;
}
//測試用牌;
// int j;
// for(j=0;j<17;j++)
// {
// pCenterCards->m_cCards[j].m_nValue = 3+j/4;
// }
/*
pCenterCards->m_cCards[0].m_nValue = 4;
pCenterCards->m_cCards[1].m_nValue = 4;
pCenterCards->m_cCards[2].m_nValue = 4;
pCenterCards->m_cCards[3].m_nValue = 4;
pCenterCards->m_cCards[4].m_nValue = 8;
pCenterCards->m_cCards[5].m_nValue = 8;
pCenterCards->m_cCards[6].m_nValue = 8;
pCenterCards->m_cCards[7].m_nValue = 8;
pCenterCards->m_cCards[8].m_nValue = 11;
pCenterCards->m_cCards[9].m_nValue = 11;
pCenterCards->m_cCards[10].m_nValue = 11;
pCenterCards->m_cCards[12].m_nValue = 11;
pCenterCards->m_cCards[13].m_nValue = 3;
pCenterCards->m_cCards[14].m_nValue = 3;
pCenterCards->m_cCards[15].m_nValue = 3;
pCenterCards->m_cCards[16].m_nValue = 3;
// for(j=0;j<17;j++)
// {
// pRightCards->m_cCards[j].m_nValue = 4+j/4;
// }
*/
/*
pRightCards->m_cCards[0].m_nValue = 3;
pRightCards->m_cCards[1].m_nValue = 3;
pRightCards->m_cCards[2].m_nValue = 4;
pRightCards->m_cCards[3].m_nValue = 4;
pRightCards->m_cCards[4].m_nValue = 5;
pRightCards->m_cCards[5].m_nValue = 5;
pRightCards->m_cCards[6].m_nValue = 9;
pRightCards->m_cCards[7].m_nValue = 9;
pRightCards->m_cCards[8].m_nValue = 9;
pRightCards->m_cCards[9].m_nValue = 8;
pRightCards->m_cCards[10].m_nValue = 11;
pRightCards->m_cCards[11].m_nValue = 11;
pRightCards->m_cCards[12].m_nValue = 13;
pRightCards->m_cCards[13].m_nValue = 13;
pRightCards->m_cCards[14].m_nValue = 13;
pRightCards->m_cCards[15].m_nValue = 16;
pRightCards->m_cCards[15].m_nColor = 0;
pRightCards->m_cCards[16].m_nValue = 16;
pRightCards->m_cCards[16].m_nColor = 1;
*/
// for(j=0;j<17;j++)
// {
// pLeftCards->m_cCards[j].m_nValue = 7+j/4;
// }
// pLeftCards->m_cCards[0].m_nValue = 3;
// pLeftCards->m_cCards[1].m_nValue = 3;
// pLeftCards->m_cCards[12].m_nValue = 4;
// pLeftCards->m_cCards[13].m_nValue = 4;
//開始向上發地主牌;
//第一張地主牌;
PlaySound(MAKEINTRESOURCE(IDR_GIVE),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
MoveCard(Lord_Card1_x,
Lord_Card1_y,
Card_x,
Card_y,
0,
17,
50);
pCardsMap->m_cLordCards[0].m_bVisual = 1;
::RedrawGame();
//第二張地主牌;
PlaySound(MAKEINTRESOURCE(IDR_GIVE),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
MoveCard(Lord_Card2_x,
Lord_Card2_y,
Card_x,
Card_y,
0,
17,
50);
pCardsMap->m_cLordCards[1].m_bVisual = 1;
::RedrawGame();
//第三張地主牌;
PlaySound(MAKEINTRESOURCE(IDR_GIVE),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
MoveCard(Lord_Card3_x,
Lord_Card3_y,
Card_x,
Card_y,
0,
17,
50);
pCardsMap->m_cLordCards[2].m_bVisual = 1;
pCardsMap->m_cDistributeCard.m_bVisual = 0; //讓中間發的牌不可見;
::RedrawGame();
DrawCleanUp();
// Sleep(60000);
return 1;
}
//發地主牌;
int CDrawCardsEngine::DealingLord(int bLord)
{
pCardsMap->m_nLordCards = 2;
::RedrawGame();
if(bLord == 0)
{
//發地主牌給左邊玩家;
//第一張;
pLeftCards->m_cCards[17] = g_cAllCards[51]; //第十八張牌;
MoveCard(Left_x0,
pCardsMap->Left0[17].m_nY,
Lord_Card1_x,
Lord_Card1_y,
pLeftCards->m_cCards[17].m_nColor,
pLeftCards->m_cCards[17].m_nValue,
80);
pLeftCards->m_nCardsCounter++; //m_nCardsCounter =18
PlaySound(MAKEINTRESOURCE(IDR_GIVE),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
// ::Redraw_Game();
//第二張;
pLeftCards->m_cCards[18] = g_cAllCards[52];
MoveCard(Left_x0,
pCardsMap->Left0[18].m_nY,
Lord_Card2_x,
Lord_Card2_y,
pLeftCards->m_cCards[18].m_nColor,
pLeftCards->m_cCards[18].m_nValue,
80);
pLeftCards->m_nCardsCounter++; //m_nCardsCounter =19
PlaySound(MAKEINTRESOURCE(IDR_GIVE),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
// ::Redraw_Game();
//第三張;
pLeftCards->m_cCards[19] = g_cAllCards[53];
MoveCard(Left_x0,
pCardsMap->Left0[19].m_nY,
Lord_Card3_x,
Lord_Card3_y,
pLeftCards->m_cCards[19].m_nColor,
pLeftCards->m_cCards[19].m_nValue,
80);
pLeftCards->m_nCardsCounter++; //m_nCardsCounter =20
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -