📄 isohex19_1.cpp
字号:
}
}
}break;
case VK_NUMPAD1:
case VK_END:
{
if(iGameState==GS_IDLE && pCurrentUnit!=NULL)//gamestate must be GS_IDLE
{
idMoveUnit=ISO_SOUTHWEST;//move to the north
POINT ptNext=TileWalker.TileWalk(pCurrentUnit->ptPosition,idMoveUnit);//check the next position
if(ptNext.x>=0 && ptNext.y>=0 && ptNext.x<MAPWIDTH && ptNext.y<MAPWIDTH)//bounds checking
{
if(mlMap[ptNext.x][ptNext.y].ulUnitList.empty())//if the map location is empty
{
iGameState=GS_STARTMOVE;//set the unit in motion
}
else
{
UNITLISTITER iter=mlMap[ptNext.x][ptNext.y].ulUnitList.begin();//get the first entry in the list
PUNITINFO pUnitInfo=*iter;//get the unit from the list
if(pUnitInfo->iTeam==pCurrentUnit->iTeam)//must be the same team
{
iGameState=GS_STARTMOVE;
}
}
}
}
}break;
case VK_NUMPAD4:
case VK_LEFT:
{
if(iGameState==GS_IDLE && pCurrentUnit!=NULL)//gamestate must be GS_IDLE
{
idMoveUnit=ISO_WEST;//move to the north
POINT ptNext=TileWalker.TileWalk(pCurrentUnit->ptPosition,idMoveUnit);//check the next position
if(ptNext.x>=0 && ptNext.y>=0 && ptNext.x<MAPWIDTH && ptNext.y<MAPWIDTH)//bounds checking
{
if(mlMap[ptNext.x][ptNext.y].ulUnitList.empty())//if the map location is empty
{
iGameState=GS_STARTMOVE;//set the unit in motion
}
else
{
UNITLISTITER iter=mlMap[ptNext.x][ptNext.y].ulUnitList.begin();//get the first entry in the list
PUNITINFO pUnitInfo=*iter;//get the unit from the list
if(pUnitInfo->iTeam==pCurrentUnit->iTeam)//must be the same team
{
iGameState=GS_STARTMOVE;
}
}
}
}
}break;
case VK_NUMPAD7:
case VK_HOME:
{
if(iGameState==GS_IDLE && pCurrentUnit!=NULL)//gamestate must be GS_IDLE
{
idMoveUnit=ISO_NORTHWEST;//move to the north
POINT ptNext=TileWalker.TileWalk(pCurrentUnit->ptPosition,idMoveUnit);//check the next position
if(ptNext.x>=0 && ptNext.y>=0 && ptNext.x<MAPWIDTH && ptNext.y<MAPWIDTH)//bounds checking
{
if(mlMap[ptNext.x][ptNext.y].ulUnitList.empty())//if the map location is empty
{
iGameState=GS_STARTMOVE;//set the unit in motion
}
else
{
UNITLISTITER iter=mlMap[ptNext.x][ptNext.y].ulUnitList.begin();//get the first entry in the list
PUNITINFO pUnitInfo=*iter;//get the unit from the list
if(pUnitInfo->iTeam==pCurrentUnit->iTeam)//must be the same team
{
iGameState=GS_STARTMOVE;
}
}
}
}
}break;
}
}break;
case WM_DESTROY://the window is being destroyed
{
//tell the application we are quitting
PostQuitMessage(0);
//handled message, so return 0
return(0);
}break;
case WM_PAINT://the window needs repainting
{
//a variable needed for painting information
PAINTSTRUCT ps;
//start painting
HDC hdc=BeginPaint(hwnd,&ps);
/////////////////////////////
//painting code would go here
/////////////////////////////
//end painting
EndPaint(hwnd,&ps);
//handled message, so return 0
return(0);
}break;
}
//pass along any other message to default message handler
return(DefWindowProc(hwnd,uMsg,wParam,lParam));
}
//////////////////////////////////////////////////////////////////////////////
//WINMAIN
//////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//assign instance to global variable
hInstMain=hInstance;
//create window class
WNDCLASSEX wcx;
//set the size of the structure
wcx.cbSize=sizeof(WNDCLASSEX);
//class style
wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
//window procedure
wcx.lpfnWndProc=TheWindowProc;
//class extra
wcx.cbClsExtra=0;
//window extra
wcx.cbWndExtra=0;
//application handle
wcx.hInstance=hInstMain;
//icon
wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
//cursor
wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
//background color
wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
//menu
wcx.lpszMenuName=NULL;
//class name
wcx.lpszClassName=WINDOWCLASS;
//small icon
wcx.hIconSm=NULL;
//register the window class, return 0 if not successful
if(!RegisterClassEx(&wcx)) return(0);
//create main window
hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
//error check
if(!hWndMain) return(0);
//if program initialization failed, then return with 0
if(!Prog_Init()) return(0);
//message structure
MSG msg;
//message pump
for(;;)
{
//look for a message
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
//there is a message
//check that we arent quitting
if(msg.message==WM_QUIT) break;
//translate message
TranslateMessage(&msg);
//dispatch message
DispatchMessage(&msg);
}
//run main game loop
Prog_Loop();
}
//clean up program data
Prog_Done();
//return the wparam from the WM_QUIT message
return(msg.wParam);
}
//////////////////////////////////////////////////////////////////////////////
//INITIALIZATION
//////////////////////////////////////////////////////////////////////////////
bool Prog_Init()
{
//create IDirectDraw object
lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
//set display mode
lpdd->SetDisplayMode(640,480,16,0,0);
//create primary surface
lpddsMain=LPDDS_CreatePrimary(lpdd,1);
//get back buffer
lpddsBack=LPDDS_GetSecondary(lpddsMain);
//create the frame buffer
lpddsFrame=LPDDS_CreateOffscreen(lpdd,640,480);
//load in the mousemap
MouseMap.Load("MouseMap.bmp");
//set up the tile plotter
TilePlotter.SetMapType(ISOMAP_DIAMOND);//diamond mode
TilePlotter.SetTileSize(MouseMap.GetWidth(),MouseMap.GetHeight());//grab width and height from mousemap
//set up tile walker to diamond mode
TileWalker.SetMapType(ISOMAP_DIAMOND);
//set up screeen space
RECT rcTemp;
SetRect(&rcTemp,0,0,640,480);
Scroller.SetScreenSpace(&rcTemp);
//load in tiles and other images
tsBack.Load(lpdd,"backgroundts.bmp");
tsUnit.Load(lpdd,"units.bmp");
tsShield.Load(lpdd,"shields.bmp");
tsPressEnter.Load(lpdd,"PressEnter.bmp");
//calculate the shield offsets
ptShieldOffset[0].x=tsUnit.GetTileList()[0].rcSrc.right-tsUnit.GetTileList()[0].ptAnchor.x;
ptShieldOffset[0].y=tsUnit.GetTileList()[0].rcSrc.top-tsUnit.GetTileList()[0].ptAnchor.y;
ptShieldOffset[1].x=tsUnit.GetTileList()[1].rcSrc.right-tsUnit.GetTileList()[1].ptAnchor.x;
ptShieldOffset[1].y=tsUnit.GetTileList()[1].rcSrc.top-tsUnit.GetTileList()[1].ptAnchor.y;
//grab tile extent from tileset
CopyRect(&rcTemp,&tsBack.GetTileList()[0].rcDstExt);
//calculate the worldspace
Scroller.CalcWorldSpace(&TilePlotter,&rcTemp,MAPWIDTH,MAPHEIGHT);
//calculate the mousemap reference point
MouseMap.CalcReferencePoint(&TilePlotter,&rcTemp);
//calculate anchor space
Scroller.CalcAnchorSpace();
//set wrap modes for scroller
Scroller.SetHWrapMode(WRAPMODE_CLIP);
Scroller.SetVWrapMode(WRAPMODE_CLIP);
//set scroller anchor to (0,0)
Scroller.GetAnchor()->x=0;
Scroller.GetAnchor()->y=0;
//attach scrolelr and tilewalker to mousemap
MouseMap.SetScroller(&Scroller);
MouseMap.SetTileWalker(&TileWalker);
//set up the map to a random tilefield
int x;
int y;
for(x=0;x<MAPWIDTH;x++)
{
for(y=0;y<MAPHEIGHT;y++)
{
mlMap[x][y].ulUnitList.clear();//clear out the list for this map location
}
}
//place the units
int team;
int count;
PUNITINFO pUnitInfo;
POINT ptTest;
for(team=0;team<2;team++)//place units for both teams
{
for(count=0;count<20;count++)//place 20 units for each team
{
//check for a valid placement
bool found=false;
while(!found)
{
//random placement
ptTest.x=rand()%MAPWIDTH;
ptTest.y=rand()%MAPHEIGHT;
//check for a unit at that position
if(mlMap[ptTest.x][ptTest.y].ulUnitList.empty())
{
//unit list at location is empty... you can place the unit
found=true;
}
else
{
//unit list is not empty, so must be occupied by the same team to be a valid location
UNITLISTITER iter=mlMap[ptTest.x][ptTest.y].ulUnitList.begin();//get iterator to beginning of list
pUnitInfo=*iter;//grab the items stored
//check for the same team
if(pUnitInfo->iTeam==team)
{
//same team, valid location
found=true;
}
}
}
//create the unit
pUnitInfo=new UnitInfo;
pUnitInfo->iTeam=team;//team
pUnitInfo->iType=rand()%2;//type
pUnitInfo->bHolding=false;//not holding position
pUnitInfo->ptPosition=ptTest;//location
//place unit in the main list
MainUnitList.push_back(pUnitInfo);
//place unit on the map
mlMap[pUnitInfo->ptPosition.x][pUnitInfo->ptPosition.y].ulUnitList.push_back(pUnitInfo);
}
}
//calculate the extent rect
RECT rcExtent;
CopyRect(&rcExtent,&tsBack.GetTileList()[0].rcDstExt);//set to background extent
UnionRect(&rcExtent,&rcExtent,&tsUnit.GetTileList()[0].rcDstExt);//union with unit extent
rcExtent.right+=tsShield.GetTileList()[0].rcDstExt.right;//adjust the extent for the width of the shield
//set up the renderer
Renderer.SetBackBuffer(lpddsBack);
Renderer.SetExtentRect(&rcExtent);
Renderer.SetFrameBuffer(lpddsFrame);
Renderer.SetMapSize(MAPWIDTH,MAPHEIGHT);
Renderer.SetMouseMap(&MouseMap);
Renderer.SetPlotter(&TilePlotter);
Renderer.SetRenderFunction(RenderFunc);
Renderer.SetScroller(&Scroller);
Renderer.SetUpdateRectCount(100);
Renderer.SetWalker(&TileWalker);
//set the initial gamestate
iGameState=GS_STARTTURN;
iCurrentTeam=0;//current team
TeamUnitList.clear();//clear out the team's unit list
//set up the selection window variables
DDPIXELFORMAT ddpf;
DDPF_Clear(&ddpf);
lpddsMain->GetPixelFormat(&ddpf);//grab pixel format
ddpf.dwRBitMask=(ddpf.dwRBitMask*3/4)&(ddpf.dwRBitMask);//calculate 3/4 red
ddpf.dwGBitMask=(ddpf.dwGBitMask*3/4)&(ddpf.dwGBitMask);//calc 3/4 green
ddpf.dwBBitMask=(ddpf.dwBBitMask*3/4)&(ddpf.dwBBitMask);//calc 3/4 blue
dwSelectWindowColor=ddpf.dwBBitMask | ddpf.dwRBitMask | ddpf.dwGBitMask;//make select window color
//calculate the cell size
RECT rcCell;
CopyRect(&rcCell,&tsUnit.GetTileList()[0].rcDstExt);
UnionRect(&rcCell,&rcCell,&tsUnit.GetTileList()[1].rcDstExt);
rcCell.right+=(tsShield.GetTileList()[0].rcDstExt.right-tsShield.GetTileList()[0].rcDstExt.left);
//cell size
ptCellSize.x=rcCell.right-rcCell.left;
ptCellSize.y=rcCell.bottom-rcCell.top;
//unit offset
ptUnitOffset.x=-rcCell.left;
ptUnitOffset.y=-rcCell.top;
//calculate select window rect
SetRect(&rcSelectWindow,0,0,ptCellSize.x*5,ptCellSize.y*4);
//center the select window
OffsetRect(&rcSelectWindow,320-rcSelectWindow.right/2,240-rcSelectWindow.bottom/2);
//update the entire screenspace
Renderer.AddRect(Scroller.GetScreenSpace());
return(true);//return success
}
//////////////////////////////////////////////////////////////////////////////
//CLEANUP
//////////////////////////////////////////////////////////////////////////////
void Prog_Done()
{
//release frame buffer
LPDDS_Release(&lpddsFrame);
//release main/back surfaces
LPDDS_Release(&lpddsMain);
//release directdraw
LPDD_Release(&lpdd);
}
//////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -