isohex7_2.cpp

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

CPP
441
字号
/*****************************************************************************
IsoHex7_2.cpp
Ernest S. Pazera
24MAY2000
Start a WIN32 Application Workspace, add in this file
Needs ddraw.lib and dxguid.lib
Needs GDICanvas.h/cpp
Needs DDFuncs.h/cpp
*****************************************************************************/

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

#include <windows.h>   
#include "GDICanvas.h"
#include "ddraw.h"
#include "DDFuncs.h"

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

//////////////////////////////////////////////////////////////////////////////
//PROTOTYPES
//////////////////////////////////////////////////////////////////////////////
bool Prog_Init();//game data initalizer
void Prog_Loop();//main game loop
void Prog_Done();//game clean up

//enumeration functions
HRESULT WINAPI EnumModesCallbackCount(LPDDSURFACEDESC2 lpDDSurfaceDesc, LPVOID lpContext);
HRESULT WINAPI EnumModesCallbackList(LPDDSURFACEDESC2 lpDDSurfaceDesc,LPVOID lpContext);

//////////////////////////////////////////////////////////////////////////////
//GLOBALS
//////////////////////////////////////////////////////////////////////////////
HINSTANCE hInstMain=NULL;//main application handle
HWND hWndMain=NULL;//handle to our main window
//IDirectDraw7 Pointer
LPDIRECTDRAW7 lpdd=NULL;

//gdicanvas
CGDICanvas gdicBall;

//surfaces
LPDIRECTDRAWSURFACE7 lpddsPrime=NULL;
LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
LPDIRECTDRAWSURFACE7 lpddsBall=NULL;

//clipper
LPDIRECTDRAWCLIPPER lpddclip=NULL;

//size of the display
DWORD dwDisplayWidth=0;
DWORD dwDisplayHeight=0;

//position of the ball
POINT ptBallPosition[2];
POINT ptLastPosition[2];
//velocity of the ball
POINT ptBallVelocity[2];

//screen coordinate corresponding to the upper left of the client area
POINT ptPrimeBlt;

//paused mode
bool bPaused=false;

//////////////////////////////////////////////////////////////////////////////
//WINDOWPROC
//////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	//which message did we get?
	switch(uMsg)
	{
	case WM_MOVE:
		{
			//set primeblit point to client 0,0
			ptPrimeBlt.x=0;
			ptPrimeBlt.y=0;

			//convert to screen coordinates
			ClientToScreen(hwnd,&ptPrimeBlt);

			//handled... return 0
			return(0);
		}break;
	case WM_ACTIVATEAPP:
		{
			if(wParam)
			{
				if(bPaused)
				{
					//being activated
					bPaused=false;

					//restore all surfaces
					lpdd->RestoreAllSurfaces();

					//clear out back buffer
					DDBLTFX ddbltfx;
					DDBLTFX_ColorFill(&ddbltfx,0);
					lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);

					//reload ball images
					LPDDS_ReloadFromFile(lpddsBall,"IsoHex6_4.bmp");
				}
			}
			else
			{
				//being deactivated
				bPaused=true;
			}
		}break;
	case WM_KEYDOWN:
		{
			//check for escape key
			if(wParam==VK_ESCAPE)
			{
				DestroyWindow(hWndMain);
			}

			return(0);//handled message
		}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_BORDER | WS_CAPTION | WS_SYSMENU | WS_VISIBLE,0,0,640,480,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 direct draw object
	lpdd=LPDD_Create(hWndMain,DDSCL_NORMAL);

	//check the display bpp
	DDSURFACEDESC2 ddsd;
	DDSD_Clear(&ddsd);
	lpdd->GetDisplayMode(&ddsd);
	if(ddsd.ddpfPixelFormat.dwRGBBitCount<16)
	{
		if(MessageBox(hWndMain,"This demo is designed for hi-color modes.  Continuing in your current display mode may not look as good. Do you wish to continue?","Continue?",MB_YESNO)==IDNO)
		{
			//do not continue
			return(false);
		}
	}

	//retrieve client rectangle
	RECT rcClient;
	GetClientRect(hWndMain,&rcClient);

	//keep display width and height in global variables
	dwDisplayWidth=rcClient.right;
	dwDisplayHeight=rcClient.bottom;

	//create the primary surface with no back buffers
	lpddsPrime=LPDDS_CreatePrimary(lpdd,0);

	//retrieve back buffer
	lpddsBack=LPDDS_CreateOffscreen(lpdd,dwDisplayWidth,dwDisplayHeight);

	//do an initial clearing out of the back buffer
	DDBLTFX ddbltfx;
	DDBLTFX_ColorFill(&ddbltfx,0);

	//do a color fill
	lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL, &ddbltfx);

	//create clipper
	lpdd->CreateClipper(0,&lpddclip,NULL);

	//set clipper window
	lpddclip->SetHWnd(0,hWndMain);

	//attach clipper
	lpddsPrime->SetClipper(lpddclip);

	//load in the ball image
	gdicBall.Load(NULL,"IsoHex7_2.bmp");

	//create an offscreen surface to contain the ball
	lpddsBall=LPDDS_LoadFromFile(lpdd,"IsoHex7_2.bmp");

	//create a black color key
	LPDDS_SetSrcColorKey(lpddsBall,0);

	//initialize ball position and velocity
	ptBallPosition[0].x=0;
	ptBallPosition[0].y=0;
	ptLastPosition[0].x=0;
	ptLastPosition[0].y=0;
	ptBallPosition[1].x=0;
	ptBallPosition[1].y=0;
	ptLastPosition[1].x=0;
	ptLastPosition[1].y=0;

	ptBallVelocity[0].x=4;
	ptBallVelocity[0].y=2;
	ptBallVelocity[1].x=2;
	ptBallVelocity[1].y=4;

	//setup the primary blit position
	ptPrimeBlt.x=0;
	ptPrimeBlt.y=0;
	ClientToScreen(hWndMain,&ptPrimeBlt);

	return(true);//return success
}

//////////////////////////////////////////////////////////////////////////////
//CLEANUP
//////////////////////////////////////////////////////////////////////////////
void Prog_Done()
{	
	//clean up clipper
	LPDDCLIP_Release(&lpddclip);

	//clean up ball surface
	LPDDS_Release(&lpddsBall);

	//clean up "back buffer"
	LPDDS_Release(&lpddsBack);
		
	//clean up primary surface
	LPDDS_Release(&lpddsPrime);

	//clean up the dd pointer
	LPDD_Release(&lpdd);

	//clean up gdicanvas
	gdicBall.Destroy();

}

//////////////////////////////////////////////////////////////////////////////
//MAIN GAME LOOP
//////////////////////////////////////////////////////////////////////////////
void Prog_Loop()
{
	//if paused, return without doing anything
	if(bPaused) return;

	int index;

	for(index=0;index<2;index++)
	{


	//set up rectangle for filling
	RECT rcFill;
	SetRect(&rcFill,ptLastPosition[index].x,ptLastPosition[index].y,ptLastPosition[index].x+gdicBall.GetWidth(),ptLastPosition[index].y+gdicBall.GetHeight());

	//set up a ddbltfx
	DDBLTFX ddbltfx;
	DDBLTFX_ColorFill(&ddbltfx,0);

	//blt the color fill to the back buffer
	lpddsBack->Blt(&rcFill,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL, &ddbltfx);
	}

	for(index=0;index<2;index++)
	{
	//set up source and destination rects for blitting the ball
	RECT rcSrc;
	RECT rcDst;

	SetRect(&rcSrc,0,0,gdicBall.GetWidth(),gdicBall.GetHeight());
	CopyRect(&rcDst,&rcSrc);
	OffsetRect(&rcDst,ptBallPosition[index].x,ptBallPosition[index].y);

	//blit the ball
	lpddsBack->Blt(&rcDst,lpddsBall,&rcSrc,DDBLT_WAIT | DDBLT_KEYSRC, NULL);

	//copy current position of ball to old position
	ptLastPosition[index]=ptBallPosition[index];

	//move the ball
	ptBallPosition[index].x+=ptBallVelocity[index].x;
	ptBallPosition[index].y+=ptBallVelocity[index].y;

	//bounds checking
	//left side
	if(ptBallPosition[index].x<=0) ptBallVelocity[index].x=abs(ptBallVelocity[index].x);
	//top side
	if(ptBallPosition[index].y<=0) ptBallVelocity[index].y=abs(ptBallVelocity[index].y);
	//right side
	if(ptBallPosition[index].x>=(int)dwDisplayWidth-gdicBall.GetWidth()) ptBallVelocity[index].x=-abs(ptBallVelocity[index].x);
	//bottom side
	if(ptBallPosition[index].y>=(int)dwDisplayHeight-gdicBall.GetHeight()) ptBallVelocity[index].y=-abs(ptBallVelocity[index].y);
	}

	//copy from "back buffer" onto primary
	//declare rectangles
	RECT rcSrc;
	RECT rcDst;

	//set up source rectangle
	SetRect(&rcSrc,0,0,dwDisplayWidth,dwDisplayHeight);

	//copy source rect to destination rect
	CopyRect(&rcDst,&rcSrc);

	//offset by the screen coordinate of the window's client area
	OffsetRect(&rcDst,ptPrimeBlt.x,ptPrimeBlt.y);

	//performa the blit
	lpddsPrime->Blt(&rcDst,lpddsBack,&rcSrc,DDBLT_WAIT,NULL);
}

⌨️ 快捷键说明

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