isohex16_3.cpp
来自「一個遊戲教程」· C++ 代码 · 共 600 行 · 第 1/2 页
CPP
600 行
//calculate anchor space
Scroller.AdjustWorldSpace(MouseMap.GetWidth()/2,MouseMap.GetHeight()/2,-MouseMap.GetWidth()/2,-MouseMap.GetHeight()/2);
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
for(int x=0;x<MAPWIDTH;x++)
{
for(int y=0;y<MAPHEIGHT;y++)
{
iMap[x][y]=rand()%2;
}
}
return(true);//return success
}
//////////////////////////////////////////////////////////////////////////////
//CLEANUP
//////////////////////////////////////////////////////////////////////////////
void Prog_Done()
{
//release main/back surfaces
LPDDS_Release(&lpddsMain);
//release clipper
LPDDCLIP_Release(&lpddClip);
//release directdraw
LPDD_Release(&lpdd);
}
//////////////////////////////////////////////////////////////////////////////
//MAIN GAME LOOP
//////////////////////////////////////////////////////////////////////////////
void Prog_Loop()
{
//clear out backbuffer
DDBLTFX ddbltfx;
DDBLTFX_ColorFill(&ddbltfx,0);
lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
//move the anchor based on scrolling speed
Scroller.MoveAnchor(ptScroll.x,ptScroll.y);
//grab the mouse position
POINT ptMouse;
GetCursorPos(&ptMouse);
//map the mouse
ptCursor=MouseMap.MapMouse(ptMouse);
//clip the cursor to valid map coordinates
if(ptCursor.x<0) ptCursor.x=0;
if(ptCursor.y<0) ptCursor.y=0;
if(ptCursor.x>(MAPWIDTH-1)) ptCursor.x=MAPWIDTH-1;
if(ptCursor.y>(MAPHEIGHT-1)) ptCursor.y=MAPHEIGHT-1;
//the corner map locations of the display
POINT ptCornerUpperLeft;
POINT ptCornerUpperRight;
POINT ptCornerLowerLeft;
POINT ptCornerLowerRight;
//working variables for calculating corners
POINT ptScreen;
POINT ptWorld;
POINT ptCoarse;
POINT ptMap;
//calculate upper left corner
//screen point
ptScreen.x=Scroller.GetScreenSpace()->left;
ptScreen.y=Scroller.GetScreenSpace()->top;
//change into world coordinate
ptWorld=Scroller.ScreenToWorld(ptScreen);
//adjust by mousemap reference point
ptWorld.x-=MouseMap.GetReferencePoint()->x;
ptWorld.y-=MouseMap.GetReferencePoint()->y;
//calculate coarse coordinates
ptCoarse.x=ptWorld.x/MouseMap.GetWidth();
ptCoarse.y=ptWorld.y/MouseMap.GetHeight();
//adjust for negative remainders
if(ptWorld.x%MouseMap.GetWidth()<0) ptCoarse.x--;
if(ptWorld.y%MouseMap.GetHeight()<0) ptCoarse.y--;
//set map point to 0,0
ptMap.x=0;
ptMap.y=0;
//do eastward tilewalk
ptMap=TileWalker.TileWalk(ptMap,ISO_EAST);
ptMap.x*=ptCoarse.x;
ptMap.y*=ptCoarse.x;
//assign ptmap to corner point
ptCornerUpperLeft.x=ptMap.x;
ptCornerUpperLeft.y=ptMap.y;
//reset ptmap to 0,0
ptMap.x=0;
ptMap.y=0;
//do southward tilewalk
ptMap=TileWalker.TileWalk(ptMap,ISO_SOUTH);
ptMap.x*=ptCoarse.y;
ptMap.y*=ptCoarse.y;
//add ptmap to corner point
ptCornerUpperLeft.x+=ptMap.x;
ptCornerUpperLeft.y+=ptMap.y;
//calculate upper right corner
//screen point
ptScreen.x=Scroller.GetScreenSpace()->right;
ptScreen.y=Scroller.GetScreenSpace()->top;
//change into world coordinate
ptWorld=Scroller.ScreenToWorld(ptScreen);
//adjust by mousemap reference point
ptWorld.x-=MouseMap.GetReferencePoint()->x;
ptWorld.y-=MouseMap.GetReferencePoint()->y;
//calculate coarse coordinates
ptCoarse.x=ptWorld.x/MouseMap.GetWidth();
ptCoarse.y=ptWorld.y/MouseMap.GetHeight();
//adjust for negative remainders
if(ptWorld.x%MouseMap.GetWidth()<0) ptCoarse.x--;
if(ptWorld.y%MouseMap.GetHeight()<0) ptCoarse.y--;
//set map point to 0,0
ptMap.x=0;
ptMap.y=0;
//do eastward tilewalk
ptMap=TileWalker.TileWalk(ptMap,ISO_EAST);
ptMap.x*=ptCoarse.x;
ptMap.y*=ptCoarse.x;
//assign ptmap to corner point
ptCornerUpperRight.x=ptMap.x;
ptCornerUpperRight.y=ptMap.y;
//reset ptmap to 0,0
ptMap.x=0;
ptMap.y=0;
//do southward tilewalk
ptMap=TileWalker.TileWalk(ptMap,ISO_SOUTH);
ptMap.x*=ptCoarse.y;
ptMap.y*=ptCoarse.y;
//add ptmap to corner point
ptCornerUpperRight.x+=ptMap.x;
ptCornerUpperRight.y+=ptMap.y;
//calculate lower left corner
//screen point
ptScreen.x=Scroller.GetScreenSpace()->left;
ptScreen.y=Scroller.GetScreenSpace()->bottom;
//change into world coordinate
ptWorld=Scroller.ScreenToWorld(ptScreen);
//adjust by mousemap reference point
ptWorld.x-=MouseMap.GetReferencePoint()->x;
ptWorld.y-=MouseMap.GetReferencePoint()->y;
//calculate coarse coordinates
ptCoarse.x=ptWorld.x/MouseMap.GetWidth();
ptCoarse.y=ptWorld.y/MouseMap.GetHeight();
//adjust for negative remainders
if(ptWorld.x%MouseMap.GetWidth()<0) ptCoarse.x--;
if(ptWorld.y%MouseMap.GetHeight()<0) ptCoarse.y--;
//set map point to 0,0
ptMap.x=0;
ptMap.y=0;
//do eastward tilewalk
ptMap=TileWalker.TileWalk(ptMap,ISO_EAST);
ptMap.x*=ptCoarse.x;
ptMap.y*=ptCoarse.x;
//assign ptmap to corner point
ptCornerLowerLeft.x=ptMap.x;
ptCornerLowerLeft.y=ptMap.y;
//reset ptmap to 0,0
ptMap.x=0;
ptMap.y=0;
//do southward tilewalk
ptMap=TileWalker.TileWalk(ptMap,ISO_SOUTH);
ptMap.x*=ptCoarse.y;
ptMap.y*=ptCoarse.y;
//add ptmap to corner point
ptCornerLowerLeft.x+=ptMap.x;
ptCornerLowerLeft.y+=ptMap.y;
//calculate lower right corner
//screen point
ptScreen.x=Scroller.GetScreenSpace()->right;
ptScreen.y=Scroller.GetScreenSpace()->bottom;
//change into world coordinate
ptWorld=Scroller.ScreenToWorld(ptScreen);
//adjust by mousemap reference point
ptWorld.x-=MouseMap.GetReferencePoint()->x;
ptWorld.y-=MouseMap.GetReferencePoint()->y;
//calculate coarse coordinates
ptCoarse.x=ptWorld.x/MouseMap.GetWidth();
ptCoarse.y=ptWorld.y/MouseMap.GetHeight();
//adjust for negative remainders
if(ptWorld.x%MouseMap.GetWidth()<0) ptCoarse.x--;
if(ptWorld.y%MouseMap.GetHeight()<0) ptCoarse.y--;
//set map point to 0,0
ptMap.x=0;
ptMap.y=0;
//do eastward tilewalk
ptMap=TileWalker.TileWalk(ptMap,ISO_EAST);
ptMap.x*=ptCoarse.x;
ptMap.y*=ptCoarse.x;
//assign ptmap to corner point
ptCornerLowerRight.x=ptMap.x;
ptCornerLowerRight.y=ptMap.y;
//reset ptmap to 0,0
ptMap.x=0;
ptMap.y=0;
//do southward tilewalk
ptMap=TileWalker.TileWalk(ptMap,ISO_SOUTH);
ptMap.x*=ptCoarse.y;
ptMap.y*=ptCoarse.y;
//add ptmap to corner point
ptCornerLowerRight.x+=ptMap.x;
ptCornerLowerRight.y+=ptMap.y;
//tilewalk from corners
ptCornerUpperLeft=TileWalker.TileWalk(ptCornerUpperLeft,ISO_NORTHWEST);
ptCornerUpperRight=TileWalker.TileWalk(ptCornerUpperRight,ISO_NORTHEAST);
ptCornerLowerLeft=TileWalker.TileWalk(ptCornerLowerLeft,ISO_SOUTHWEST);
ptCornerLowerRight=TileWalker.TileWalk(ptCornerLowerRight,ISO_SOUTHEAST);
//main rendering loop
//vars for rendering loop
POINT ptCurrent;
POINT ptRowStart;
POINT ptRowEnd;
DWORD dwRowCount=0;
//set up rows
ptRowStart=ptCornerUpperLeft;
ptRowEnd=ptCornerUpperRight;
//start rendering loops
for(;;)//"infinite" loop
{
//set current point to rowstart
ptCurrent=ptRowStart;
//render a row of tiles
for(;;)//'infinite' loop
{
//check for valid point. if valid, render
if(ptCurrent.x>=0 && ptCurrent.y>=0 && ptCurrent.x<MAPWIDTH && ptCurrent.y<MAPHEIGHT)
{
//valid, so render
ptScreen=TilePlotter.PlotTile(ptCurrent);//plot tile
ptScreen=Scroller.WorldToScreen(ptScreen);//world->screen
tsBack.PutTile(lpddsBack,ptScreen.x,ptScreen.y,0);//put background tile
if(iMap[ptCurrent.x][ptCurrent.y])//check for tree
{
tsShadow.PutTile(lpddsBack,ptScreen.x,ptScreen.y,0);//put shadow
tsTree.PutTile(lpddsBack,ptScreen.x,ptScreen.y,0);//put tree
}
}
//check if at end of row. if we are, break out of inner loop
if(ptCurrent.x==ptRowEnd.x && ptCurrent.y==ptRowEnd.y) break;
//walk east to next tile
ptCurrent=TileWalker.TileWalk(ptCurrent,ISO_EAST);
}
//check to see if we are at the last row. if we are, break out of loop
if(ptRowStart.x==ptCornerLowerLeft.x && ptRowStart.y==ptCornerLowerLeft.y) break;
//move the row start and end points, based on the row number
if(dwRowCount&1)
{
//odd
//start moves SW, end moves SE
ptRowStart=TileWalker.TileWalk(ptRowStart,ISO_SOUTHWEST);
ptRowEnd=TileWalker.TileWalk(ptRowEnd,ISO_SOUTHEAST);
}
else
{
//even
//start moves SE, end moves SW
ptRowStart=TileWalker.TileWalk(ptRowStart,ISO_SOUTHEAST);
ptRowEnd=TileWalker.TileWalk(ptRowEnd,ISO_SOUTHWEST);
}
//increase the row number
dwRowCount++;
}
//flip to show the back buffer
lpddsMain->Flip(0,DDFLIP_WAIT);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?