isohex21_1.cpp
来自「一個遊戲教程」· C++ 代码 · 共 500 行
CPP
500 行
/*****************************************************************************
IsoHex21_1.cpp
Ernest S. Pazera
09SEPT2000
Start a WIN32 Application Workspace, add in this file
Requires the following libs:
ddraw.lib, dxguid.lib
Requires the following files:
DDFuncs.h.cpp, GDICanvas.h/cpp, IsoMouseMap.h/cpp,
IsoScroller.h/cpp, IsoTilePlotter.h/cpp, IsoTileWalker.h/cpp
TileSet.h/cpp. IsoHexCore.h, IsoHexDefs.h
IsoRenderer.h/cpp
*****************************************************************************/
//////////////////////////////////////////////////////////////////////////////
//INCLUDES
//////////////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "DDFuncs.h"
#include "TileSet.h"
#include "IsoHexCore.h"
#include "IsoRenderer.h"
#include <list>
//////////////////////////////////////////////////////////////////////////////
//DEFINES
//////////////////////////////////////////////////////////////////////////////
//name for our window class
#define WINDOWCLASS "ISOHEX21"
//title of the application
#define WINDOWTITLE "IsoHex 21-1"
const int MAPWIDTH=40;
const int MAPHEIGHT=40;
const int SCREENWIDTH=640;
const int SCREENHEIGHT=480;
const int UPPERLEFT=0;
const int UPPERRIGHT=1;
const int LOWERRIGHT=2;
const int LOWERLEFT=3;
//////////////////////////////////////////////////////////////////////////////
//TYPEDEFS/STRUCTS
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//PROTOTYPES
//////////////////////////////////////////////////////////////////////////////
bool Prog_Init();//game data initalizer
void Prog_Loop();//main game loop
void Prog_Done();//game clean up
//////////////////////////////////////////////////////////////////////////////
//GLOBALS
//////////////////////////////////////////////////////////////////////////////
HINSTANCE hInstMain=NULL;//main application handle
HWND hWndMain=NULL;//handle to our main window
//directdraw
LPDIRECTDRAW7 lpdd=NULL;
LPDIRECTDRAWSURFACE7 lpddsMain=NULL;
LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
LPDIRECTDRAWSURFACE7 lpddsFrame=NULL;
//tilesets
CTileSet tsBack;
CTileSet tsFringe;
//isohexcore components
CTilePlotter TilePlotter;//plotter
CTileWalker TileWalker;//walker
CScroller Scroller;//scroller
CMouseMap MouseMap;//mousemap
CRenderer Renderer;//renderer
POINT ptScroll;//keep track of how quickly we scroll
//map location structure
struct MapLocation
{
bool bLand;
int iFringe[4];
};
MapLocation mlMap[MAPWIDTH][MAPHEIGHT];//map array
//fringe lookup table
int FringeLookUp[8]={-1,0,1,1,2,3,1,1};
//rendering functionprototype
void RenderFunc(LPDIRECTDRAWSURFACE7 lpddsDst,RECT* rcClip,int xDst,int yDst,int xMap,int yMap);
void CalcFringe();//calculate entire map
void CalcFringe(int x,int y);//calculate for individual map location
void CalcFringeNeighborhood(int x,int y);//calculate for a tile neighborhood
//////////////////////////////////////////////////////////////////////////////
//WINDOWPROC
//////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
//which message did we get?
switch(uMsg)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_ESCAPE:
{
DestroyWindow(hWndMain);
return(0);
}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(SCREENWIDTH,SCREENHEIGHT,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 cursor
tsBack.Load(lpdd,"backgroundts.bmp");
tsFringe.Load(lpdd,"coastlinets.bmp");
//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++)
{
if(rand()&1)
{
mlMap[x][y].bLand=true;
}
else
{
mlMap[x][y].bLand=false;
}
}
}
//calculate the fringe
CalcFringe();
//calculate the extent rect
RECT rcExtent;
CopyRect(&rcExtent,&tsBack.GetTileList()[0].rcDstExt);//set to background extent
//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);
//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);
}
//////////////////////////////////////////////////////////////////////////////
//MAIN GAME LOOP
//////////////////////////////////////////////////////////////////////////////
void Prog_Loop()
{
//grab mouse position
POINT ptMouse;
GetCursorPos(&ptMouse);
//calc scrolling
//horizontal
ptScroll.x=0;
if(ptMouse.x<8) ptScroll.x=ptMouse.x-8;
if(ptMouse.x>SCREENWIDTH-8) ptScroll.x=ptMouse.x-(SCREENWIDTH-8);
//vertical
ptScroll.y=0;
if(ptMouse.y<8) ptScroll.y=ptMouse.y-8;
if(ptMouse.y>SCREENHEIGHT-8) ptScroll.y=ptMouse.y-(SCREENHEIGHT-8);
//scroll the frame by ptScroll
Renderer.ScrollFrame(ptScroll.x,ptScroll.y);
//update the frame
Renderer.UpdateFrame();
//flip to show the back buffer
lpddsMain->Flip(0,DDFLIP_WAIT);
}
void RenderFunc(LPDIRECTDRAWSURFACE7 lpddsDst,RECT* rcClip,int xDst,int yDst,int xMap,int yMap)
{
//check land or sea
if(mlMap[xMap][yMap].bLand)
{
//land
tsBack.ClipTile(lpddsDst,rcClip,xDst,yDst,0);
}
else
{
//sea
tsBack.ClipTile(lpddsDst,rcClip,xDst,yDst,1);
//upper left zone
if(FringeLookUp[mlMap[xMap][yMap].iFringe[UPPERLEFT]]>=0)
tsFringe.ClipTile(lpddsDst,rcClip,xDst,yDst,FringeLookUp[mlMap[xMap][yMap].iFringe[UPPERLEFT]]);
//upper right zone
if(FringeLookUp[mlMap[xMap][yMap].iFringe[UPPERRIGHT]]>=0)
tsFringe.ClipTile(lpddsDst,rcClip,xDst,yDst,FringeLookUp[mlMap[xMap][yMap].iFringe[UPPERRIGHT]]+4);
//lower right zone
if(FringeLookUp[mlMap[xMap][yMap].iFringe[LOWERRIGHT]]>=0)
tsFringe.ClipTile(lpddsDst,rcClip,xDst,yDst,FringeLookUp[mlMap[xMap][yMap].iFringe[LOWERRIGHT]]+8);
//lower left zone
if(FringeLookUp[mlMap[xMap][yMap].iFringe[LOWERLEFT]]>=0)
tsFringe.ClipTile(lpddsDst,rcClip,xDst,yDst,FringeLookUp[mlMap[xMap][yMap].iFringe[LOWERLEFT]]+12);
}
}
void CalcFringe()
{
//loop through x
for(int x=0;x<MAPWIDTH;x++)
{
//loop through y
for(int y=0;y<MAPHEIGHT;y++)
{
//calc the fringe
CalcFringe(x,y);
}
}
}
void CalcFringe(int x,int y)//calculate for individual map location
{
//range checking
if(x<0) return;
if(y<0) return;
if(x>=MAPWIDTH) return;
if(y>=MAPHEIGHT) return;
//calculate the tile neighborhood
bool Neighbor[8];
//store starting point
POINT ptStart;
ptStart.x=x;
ptStart.y=y;
//next map location
POINT ptNext;
for(int dir=0;dir<8;dir++)
{
//walk to neighbor
ptNext=TileWalker.TileWalk(ptStart,(ISODIRECTION)dir);
//range check
if(ptNext.x<0 || ptNext.y<0 || ptNext.x>=MAPWIDTH || ptNext.y>=MAPHEIGHT)
{
//out of bounds
Neighbor[dir]=0;
}
else
{
//check map location
if(mlMap[ptNext.x][ptNext.y].bLand)
{
//land
Neighbor[dir]=1;
}
else
{
//water
Neighbor[dir]=0;
}
}
}
//determine zones
mlMap[ptStart.x][ptStart.y].iFringe[UPPERLEFT]=(Neighbor[6]+2*Neighbor[7]+4*Neighbor[0])|(Neighbor[5]+4*Neighbor[1]);
mlMap[ptStart.x][ptStart.y].iFringe[UPPERRIGHT]=(Neighbor[0]+2*Neighbor[1]+4*Neighbor[2])|(Neighbor[7]+4*Neighbor[3]);
mlMap[ptStart.x][ptStart.y].iFringe[LOWERRIGHT]=(Neighbor[2]+2*Neighbor[3]+4*Neighbor[4])|(Neighbor[1]+4*Neighbor[5]);
mlMap[ptStart.x][ptStart.y].iFringe[LOWERLEFT]=(Neighbor[4]+2*Neighbor[5]+4*Neighbor[6])|(Neighbor[3]+4*Neighbor[7]);
}
void CalcFringeNeighborhood(int x,int y)//calculate for a tile neighborhood
{
//send tile(x,y) to calculate fringe function
CalcFringe(x,y);
//store center point
POINT ptCenter;
ptCenter.x=x;
ptCenter.y=y;
POINT ptNeighbor;
//loop through directions
for(int dir=0;dir<8;dir++)
{
//determine neighbor
ptNeighbor=TileWalker.TileWalk(ptCenter,(ISODIRECTION)dir);
//send neighbor to calculation function
CalcFringe(ptNeighbor.x,ptNeighbor.y);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?