isohex3_8.cpp
来自「一個遊戲教程」· C++ 代码 · 共 312 行
CPP
312 行
/*****************************************************************************
IsoHex3_8.cpp
Ernest S. Pazera
18MAY2000
Start a WIN32 Application Workspace, add in this file
Requires GDICanvas.h and GDICanvas.cpp
*****************************************************************************/
//////////////////////////////////////////////////////////////////////////////
//INCLUDES
//////////////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "GDICanvas.h"
//////////////////////////////////////////////////////////////////////////////
//DEFINES
//////////////////////////////////////////////////////////////////////////////
//name for our window class
#define WINDOWCLASS "ISOHEX3"
//title of the application
#define WINDOWTITLE "IsoHex 3-8"
//////////////////////////////////////////////////////////////////////////////
//PROTOTYPES
//////////////////////////////////////////////////////////////////////////////
bool Prog_Init();//game data initalizer
void Prog_Loop();//main game loop
void Prog_Done();//game clean up
//functions to deal with the update rectangle
void ClearUpdate();//clears the update rectangle
void AddUpdate(RECT* prcAdd);//adds the update rectangle
void RenderUpdate(HWND hwndDst, HDC hdcSrc);//renders the update
//////////////////////////////////////////////////////////////////////////////
//GLOBALS
//////////////////////////////////////////////////////////////////////////////
HINSTANCE hInstMain=NULL;//main application handle
HWND hWndMain=NULL;//handle to our main window
//gdi canvases
CGDICanvas gdicTile;
CGDICanvas gdicMask;
CGDICanvas gdicBackbuffer;
//update rectangle
RECT rcUpdate;
//////////////////////////////////////////////////////////////////////////////
//WINDOWPROC
//////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
//which message did we get?
switch(uMsg)
{
case WM_LBUTTONDOWN:
{
//borrow dc from main window
HDC hdc=GetDC(hWndMain);
//blit from the memory dc to the window's dc
BitBlt(gdicBackbuffer,LOWORD(lParam)-gdicMask.GetWidth()/2,HIWORD(lParam)-gdicMask.GetHeight()/2,gdicMask.GetWidth(),gdicMask.GetHeight(),gdicMask,0,0,SRCAND);
BitBlt(gdicBackbuffer,LOWORD(lParam)-gdicTile.GetWidth()/2,HIWORD(lParam)-gdicTile.GetHeight()/2,gdicMask.GetWidth(),gdicMask.GetHeight(),gdicTile,0,0,SRCPAINT);
//add to the update rectangle
RECT rcAdd;
SetRect(&rcAdd,LOWORD(lParam)-gdicMask.GetWidth()/2,HIWORD(lParam)-gdicMask.GetHeight()/2,LOWORD(lParam)-gdicMask.GetWidth()/2+gdicMask.GetWidth(),HIWORD(lParam)-gdicMask.GetHeight()/2+gdicMask.GetHeight());
AddUpdate(&rcAdd);
//return the borrowed dc to the system
ReleaseDC(hWndMain,hdc);
//handled, so return 0
return(0);
}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);
//add entire client are to update rectangle
RECT rcClient;
GetClientRect(hwnd,&rcClient);
AddUpdate(&rcClient);
//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_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()
{
//borrow dc from main window
HDC hdc=GetDC(hWndMain);
//load the images
gdicTile.Load(hdc,"IsoHex3_8-1.bmp");
gdicMask.Load(hdc,"IsoHex3_8-2.bmp");
//get the client rectangle
RECT rcClient;
GetClientRect(hWndMain,&rcClient);
//create a blank bitmap with the client area's dimensions
gdicBackbuffer.CreateBlank(hdc,rcClient.right,rcClient.bottom);
//clear out the blank bitmap
FillRect(gdicBackbuffer,&rcClient,(HBRUSH)GetStockObject(BLACK_BRUSH));
//clear the update region
ClearUpdate();
//return dc to system
ReleaseDC(hWndMain,hdc);
return(true);//return success
}
//////////////////////////////////////////////////////////////////////////////
//CLEANUP
//////////////////////////////////////////////////////////////////////////////
void Prog_Done()
{
//destroy the images
gdicTile.Destroy();
gdicMask.Destroy();
}
//////////////////////////////////////////////////////////////////////////////
//MAIN GAME LOOP
//////////////////////////////////////////////////////////////////////////////
void Prog_Loop()
{
//render the update
RenderUpdate(hWndMain,gdicBackbuffer);
}
//clears the update rectangle
void ClearUpdate()
{
//set the update rect to empty
SetRectEmpty(&rcUpdate);
}
//adds the update rectangle
void AddUpdate(RECT* prcAdd)
{
//if the new rectangle is empty, return without doing anything
if(IsRectEmpty(prcAdd)) return;
if(IsRectEmpty(&rcUpdate))
{
//if the rectangle is empty
//copy the new rectangle to the update rectangle
CopyRect(&rcUpdate,prcAdd);
}
else
{
//if the rectangle is not empty
//create a temporary rectangle
RECT rcTemp;
//combine the new rectangle with the old rectangle in the temporary rect
UnionRect(&rcTemp,&rcUpdate,prcAdd);
//copy the temporary rectangle to the update rect
CopyRect(&rcUpdate,&rcTemp);
}
}
//renders the update
void RenderUpdate(HWND hwndDst, HDC hdcSrc)
{
//if the update rectangle is empty, return without doing anything
if(IsRectEmpty(&rcUpdate)) return;
//borrow the dc from the destination window
HDC hdcDst=GetDC(hwndDst);
//blit the update area
BitBlt(hdcDst,rcUpdate.left,rcUpdate.top,rcUpdate.right-rcUpdate.left,rcUpdate.bottom-rcUpdate.top,hdcSrc,rcUpdate.left,rcUpdate.top,SRCCOPY);
//return the destination dc to the system
ReleaseDC(hwndDst,hdcDst);
//clear the update area
ClearUpdate();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?