isohex6_2.cpp
来自「一個遊戲教程」· C++ 代码 · 共 482 行
CPP
482 行
/*****************************************************************************
IsoHex6_2.cpp
Ernest S. Pazera
21MAY2000
Start a WIN32 Application Workspace, add in this file
Needs ddraw.lib and dxguid.lib
Needs GDICanvas.h/cpp
*****************************************************************************/
//////////////////////////////////////////////////////////////////////////////
//INCLUDES
//////////////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "GDICanvas.h"
#include "ddraw.h"
//////////////////////////////////////////////////////////////////////////////
//DEFINES
//////////////////////////////////////////////////////////////////////////////
//name for our window class
#define WINDOWCLASS "ISOHEX6"
//title of the application
#define WINDOWTITLE "IsoHex 6-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;
//display mode structure
struct DisplayMode
{
DWORD dwWidth;
DWORD dwHeight;
DWORD dwBPP;
};
//display mode enumeration variables
DWORD dwDisplayModeCount=0;
DisplayMode* DisplayModeList=NULL;
//gdicanvas
CGDICanvas gdicBall;
//surfaces
LPDIRECTDRAWSURFACE7 lpddsPrime=NULL;
LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
LPDIRECTDRAWSURFACE7 lpddsBall=NULL;
//size of the display
DWORD dwDisplayWidth=0;
DWORD dwDisplayHeight=0;
//position of the ball
POINT ptBallPosition;
POINT ptLastPosition[2];
//velocity of the ball
POINT ptBallVelocity;
//////////////////////////////////////////////////////////////////////////////
//WINDOWPROC
//////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
//which message did we get?
switch(uMsg)
{
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_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()
{
//error code
HRESULT hr;
//initialize the dd pointer
hr=DirectDrawCreateEx(NULL,(void**)&lpdd,IID_IDirectDraw7,NULL);
//example for error handling
if(FAILED(hr))
{
//initialization failed
return(false);
}
//set the cooperative level
lpdd->SetCooperativeLevel(hWndMain,DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
//enumerate the displaymodes
dwDisplayModeCount=0;
lpdd->EnumDisplayModes(0,NULL,NULL,EnumModesCallbackCount);
DisplayModeList=new DisplayMode[dwDisplayModeCount];
dwDisplayModeCount=0;
lpdd->EnumDisplayModes(0,NULL,NULL,EnumModesCallbackList);
//pick a display mode
DisplayMode TestMode;
TestMode.dwWidth=0;
TestMode.dwHeight=0;
TestMode.dwBPP=0;
DWORD index;
bool found=false;
for(index=0;(index<dwDisplayModeCount);index++)
{
if(DisplayModeList[index].dwBPP==16)
{
if(DisplayModeList[index].dwWidth>TestMode.dwWidth)
{
TestMode.dwWidth=DisplayModeList[index].dwWidth;
TestMode.dwHeight=DisplayModeList[index].dwHeight;
TestMode.dwBPP=DisplayModeList[index].dwBPP;
found=true;
}
}
}
if(!found)
{
return(false);
}
//set the display mode
hr=lpdd->SetDisplayMode(TestMode.dwWidth,TestMode.dwHeight,TestMode.dwBPP,0,0);
//keep display width and height in global variables
dwDisplayWidth=TestMode.dwWidth;
dwDisplayHeight=TestMode.dwHeight;
//create the primary surface with a single back buffer
//clear out a DDSURFACEDESC2
DDSURFACEDESC2 ddsd;
memset(&ddsd,0,sizeof(DDSURFACEDESC2));
ddsd.dwSize=sizeof(DDSURFACEDESC2);
//set the flags for primary surface
ddsd.dwFlags=DDSD_BACKBUFFERCOUNT | DDSD_CAPS;
//set back buffer count
ddsd.dwBackBufferCount=1;
//set caps
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
//create the surface
lpdd->CreateSurface(&ddsd,&lpddsPrime,NULL);
//clear out a DDSCAPS2
DDSCAPS2 ddsCaps;
memset(&ddsCaps,0,sizeof(DDSCAPS2));
//set the caps
ddsCaps.dwCaps=DDSCAPS_BACKBUFFER;
//retrieve back buffer
lpddsPrime->GetAttachedSurface(&ddsCaps,&lpddsBack);
//do an initial clearing out of the back buffer
//clean out a DDBLTFX
DDBLTFX ddbltfx;
memset(&ddbltfx,0,sizeof(DDBLTFX));
ddbltfx.dwSize=sizeof(DDBLTFX);
//set the fill color to black
ddbltfx.dwFillColor=0;
//do a color fill
lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL, &ddbltfx);
//load in the ball image
gdicBall.Load(NULL,"IsoHex6_2.bmp");
//create an offscreen surface to contain the ball
//clear out ddsd
memset(&ddsd,0,sizeof(DDSURFACEDESC2));
ddsd.dwSize=sizeof(DDSURFACEDESC2);
//set ddsd flags
ddsd.dwFlags=DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
//set width and height
ddsd.dwWidth=gdicBall.GetWidth();
ddsd.dwHeight=gdicBall.GetHeight();
//set caps
ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
//create surface
lpdd->CreateSurface(&ddsd,&lpddsBall,NULL);
//grab dc from offscreen surface
HDC hdcSurf;
lpddsBall->GetDC(&hdcSurf);
//blit ball to surface
BitBlt(hdcSurf,0,0,gdicBall.GetWidth(),gdicBall.GetHeight(),gdicBall,0,0,SRCCOPY);
//release dc
lpddsBall->ReleaseDC(hdcSurf);
//initialize ball position and velocity
ptBallPosition.x=0;
ptBallPosition.y=0;
ptLastPosition[0].x=0;
ptLastPosition[0].y=0;
ptLastPosition[1].x=0;
ptLastPosition[1].y=0;
ptBallVelocity.x=16;
ptBallVelocity.y=8;
return(true);//return success
}
//////////////////////////////////////////////////////////////////////////////
//CLEANUP
//////////////////////////////////////////////////////////////////////////////
void Prog_Done()
{
//clean up ball surface
if(lpddsBall)
{
lpddsBall->Release();
lpddsBall=NULL;
}
//clean up primary surface(this will clean up the back buffer, also)
if(lpddsPrime)
{
lpddsPrime->Release();
lpddsPrime=NULL;
lpddsBack=NULL;
}
//clean up the dd pointer
if(lpdd)
{
lpdd->Release();
lpdd=NULL;
}
//clean up gdicanvas
gdicBall.Destroy();
//get rid of enumeration stuff
delete [] DisplayModeList;
}
//////////////////////////////////////////////////////////////////////////////
//MAIN GAME LOOP
//////////////////////////////////////////////////////////////////////////////
void Prog_Loop()
{
//set up rectangle for filling
RECT rcFill;
SetRect(&rcFill,ptLastPosition[0].x,ptLastPosition[0].y,ptLastPosition[0].x+gdicBall.GetWidth(),ptLastPosition[0].y+gdicBall.GetHeight());
//set up a ddbltfx
DDBLTFX ddbltfx;
memset(&ddbltfx,0,sizeof(DDBLTFX));
ddbltfx.dwSize=sizeof(DDBLTFX);
//set up a color fill of black
ddbltfx.dwFillColor=0;
//blt the color fill to the back buffer
lpddsBack->Blt(&rcFill,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL, &ddbltfx);
//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.x,ptBallPosition.y);
//blit the ball
lpddsBack->Blt(&rcDst,lpddsBall,&rcSrc,DDBLT_WAIT, NULL);
//copy current position of ball to old position
ptLastPosition[0]=ptLastPosition[1];
ptLastPosition[1]=ptBallPosition;
//move the ball
ptBallPosition.x+=ptBallVelocity.x;
ptBallPosition.y+=ptBallVelocity.y;
//bounds checking
//left side
if(ptBallPosition.x<=0) ptBallVelocity.x=abs(ptBallVelocity.x);
//top side
if(ptBallPosition.y<=0) ptBallVelocity.y=abs(ptBallVelocity.y);
//right side
if(ptBallPosition.x>=(int)dwDisplayWidth-gdicBall.GetWidth()) ptBallVelocity.x=-abs(ptBallVelocity.x);
//bottom side
if(ptBallPosition.y>=(int)dwDisplayHeight-gdicBall.GetHeight()) ptBallVelocity.y=-abs(ptBallVelocity.y);
//flip surfaces
lpddsPrime->Flip(NULL,DDFLIP_WAIT);
}
//enumeration-count
HRESULT WINAPI EnumModesCallbackCount(
LPDDSURFACEDESC2 lpDDSurfaceDesc,
LPVOID lpContext
)
{
//increment the count variable
dwDisplayModeCount++;
//continue the enumeration
return(DDENUMRET_OK);
}
//enumeration-list
HRESULT WINAPI EnumModesCallbackList(
LPDDSURFACEDESC2 lpDDSurfaceDesc,
LPVOID lpContext
)
{
//copy applicable information to the list
DisplayModeList[dwDisplayModeCount].dwWidth=lpDDSurfaceDesc->dwWidth;
DisplayModeList[dwDisplayModeCount].dwHeight=lpDDSurfaceDesc->dwHeight;
DisplayModeList[dwDisplayModeCount].dwBPP=lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount;
//increment the count variable
dwDisplayModeCount++;
//continue the enumeration
return(DDENUMRET_OK);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?