isohex22_2.cpp

来自「一個遊戲教程」· C++ 代码 · 共 290 行

CPP
290
字号
/*****************************************************************************
IsoHex22_2.cpp
Ernest S. Pazera
28NOV2000
Start a WIN32 Application Workspace, add in this file
Requires GDICanvas.h/cpp
No other libs are required
*****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//INCLUDES
//////////////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN  

#include <windows.h>   
#include <stdlib.h>
#include "gdicanvas.h"

//////////////////////////////////////////////////////////////////////////////
//DEFINES
//////////////////////////////////////////////////////////////////////////////
//name for our window class
#define WINDOWCLASS "ISOHEX22"
//title of the application
#define WINDOWTITLE "IsoHex 22-2"

//map constants
const int MAPWIDTH=320;
const int MAPHEIGHT=320;
const int MAPSEEDS=100;
const int LANDPERC=30;

//////////////////////////////////////////////////////////////////////////////
//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

//canvas for the map
CGDICanvas gdicMap;

//////////////////////////////////////////////////////////////////////////////
//WINDOWPROC
//////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	//which message did we get?
	switch(uMsg)
	{
	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);

			//end painting
			EndPaint(hwnd,&ps);

			//draw the map
			hdc=GetDC(hWndMain);
			BitBlt(hdc,0,0,MAPWIDTH,MAPHEIGHT,gdicMap,0,0,SRCCOPY);
			ReleaseDC(hWndMain,hdc);
									
			//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_BORDER | WS_SYSMENU | WS_CAPTION| 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()
{
	//seed the random number generator
	srand(GetTickCount());

	//set the client area size
	RECT rcTemp;
	SetRect(&rcTemp,0,0,MAPWIDTH,MAPHEIGHT);//256x256 client area
	AdjustWindowRect(&rcTemp,WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,FALSE);//adjust the window size based on desired client area
	SetWindowPos(hWndMain,NULL,0,0,rcTemp.right-rcTemp.left,rcTemp.bottom-rcTemp.top,SWP_NOMOVE);//set the window width and height

	//grab the window's dc
	HDC hdc=GetDC(hWndMain);
	//make the map
	gdicMap.CreateBlank(hdc,MAPWIDTH,MAPHEIGHT);

	//clear out the map's canvas with blue
	HBRUSH hbr=CreateSolidBrush(RGB(0,0,255));
	FillRect(gdicMap,&rcTemp,hbr);
	DeleteObject(hbr);

	//select a green pen into the canvas.
	HPEN hpen=CreatePen(PS_SOLID,0,RGB(0,255,0));
	SelectObject(gdicMap,hpen);

	int x,y,count;
	//pick random location
	x=rand()%MAPWIDTH;
	y=rand()%MAPHEIGHT;
	//move to the location
	MoveToEx(gdicMap,x,y,NULL);

	//place the seeds
	for(count=0;count<MAPSEEDS;count++)
	{
		if((rand()%4)==0)
		{
			//pick random location
			x=rand()%MAPWIDTH;
			y=rand()%MAPHEIGHT;
			//move to the location
			MoveToEx(gdicMap,x,y,NULL);
			SetPixelV(gdicMap,x,y,RGB(0,255,0));
		}
		else
		{
			//line to that location
			x=x+rand()%50-25;
			y=y+rand()%50-25;
			LineTo(gdicMap,x,y);
		}
	}

	//place the rest of the land
	for(count=0;count<MAPWIDTH*MAPHEIGHT*LANDPERC/100;count++)
	{
		//pick a coord next to a land square
		bool found=false;
		do
		{
			//pick random place
			x=rand()%MAPWIDTH;
			y=rand()%MAPHEIGHT;
			//ensure the area is water
			if(GetPixel(gdicMap,x,y)!=RGB(0,0,255)) continue;
			//ensure it is next to another land area
			if(x>=0 && GetPixel(gdicMap,x-1,y)==RGB(0,255,0)) found=true;	
			if(x<(MAPWIDTH-1) && GetPixel(gdicMap,x+1,y)==RGB(0,255,0)) found=true;	
			if(y>=0 && GetPixel(gdicMap,x,y-1)==RGB(0,255,0)) found=true;	
			if(y<(MAPHEIGHT-1) && GetPixel(gdicMap,x,y+1)==RGB(0,255,0)) found=true;	
		//keep looping until you find an appropriate space
		}while(!found);
		//place pixel
		SetPixelV(gdicMap,x,y,RGB(0,255,0));
		SendMessage(hWndMain,WM_PAINT,0,0);
	}
	
	//return the window's dc
	ReleaseDC(hWndMain,hdc);

	return(true);//return success
}

//////////////////////////////////////////////////////////////////////////////
//CLEANUP
//////////////////////////////////////////////////////////////////////////////
void Prog_Done()
{
	//////////////////////////
	//clean up code goes here
	//////////////////////////
}

//////////////////////////////////////////////////////////////////////////////
//MAIN GAME LOOP
//////////////////////////////////////////////////////////////////////////////
void Prog_Loop()
{
	///////////////////////////
	//main game logic goes here
	///////////////////////////
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?