The---of Win32 game production Bizzard

Source: Internet
Author: User
Tags blizzard

Before writing a piece about the game engine design article, today uses the game engine to implement a small game (actually is not the strict meaning game), mainly is in order to feel the game engine to the game design to bring the convenience, but is not the game itself, after using the game engine you will discover, the game design will be much simpler. It gives you a framework, and all you need to do is fill in the content.

Much to say, the following is the game engine in the game specific use.

First of all to think about how to fill in the original game engine above, you need to build on the original new game header files and game implementation files, about the game engine on the content has been said, then this time only need to fill in the content.

First look at the Bizzard.h file:

Blizzard application//c++ header file-blizzard.h#pragma once//--------------------------------------//Include files//------------------ --------------------#include <Windows.h> #include <time.h> #include <tchar.h> #include "resource.h" #include "GameEngine.h"//--------------------------------------//global variable//-------------------------------------- GameEngine * g_pgame = NULL;                    Each game engine-based game requires a global game engine pointer
The pointer in this header file is blizzard access to the game engine and is therefore very important.


The resource file has nothing to say, it defines all the resource identifiers that are used in the program, and in this case it is simple to define two icons and titles and names, and of course you can add other resources, and there's no more talking.

{{no_dependencies}}//contains files generated by Microsoft Visual C + +.} For gameengine.rc use//#define Ids_app_title                   101#define idc_bizzard                     102#define                    idi_blizzard 1000#define IDI_BLIZZARD_SM                 1001//Next default values for new objects//#ifdef Apstudio_invoked#ifndef apstudio_readonly_ Symbols#define _aps_next_resource_value        103#define _aps_next_command_value         40001#define _APS_NEXT_CONTROL _value         1001#define _aps_next_symed_value           101#endif#endif

Finally, the focus of the content is the realization of the main game:

--------------------------------------//blizzard Application//c++ source program--blizzard.cpp//-------------------------------- ------//--------------------------------------//Include file//--------------------------------------#include " Blizzard.h "//--------------------------------------//Game engine function//--------------------------------------BOOL Gameinitialize (hinstance hinstance) {//create game engine G_pgame = new GameEngine (hinstance, Text ("Blizzard"), Text ("Blizzard"), Idi_blizzard, IDI_BLIZZARD_SM); if (NULL = = g_pgame) {return FALSE;}         Set the frame frequency g_pgame->setframerate (10000); 15 frames per second, there is no need to use the default (20 frames per second) to return TRUE;}        void Gamestart (HWND hwindow) {//Generate random Generator seed srand ((unsigned int) GetTickCount ()); If the game needs to call the standard rand () function to generate random numbers, you should always call the Srand () function to specify the random number generator seed}void gameend () {//cleanup game delete g_pgame;} void Gameactivate (HWND hwindow) {HDC hdc; RECT rect;//Draws text GetClientRect (Hwindow, &rect) on the game screen; hdc = GetDC (Hwindow);D Rawtext (hdc, TEXT ("Here Comes the Blizzard! "),-1, &rect, Dt_singleline | Dt_center | Dt_vcenter); ReleaseDC (HwinDow, HDC);} void Gamedeactivate (HWND hwindow) {HDC hdc; RECT rect;//draws the deactivated text GetClientRect (Hwindow, &rect) on the screen; hdc = GetDC (Hwindow);D Rawtext (hdc, TEXT ("The Blizzard has Passed. "),-1, &rect, Dt_singleline | Dt_center | Dt_vcenter); ReleaseDC (Hwindow, HDC);} void Gamepaint (HDC hdc) {//In this example, it is not necessary to call this redraw function because all the drawing work is done in the gamecycle () function, but this is rare}void gamecycle () {HDC hdc; HWND Hwindow = G_pgame->getwindow ();//random random position on the game screen draw snowflake icon hdc = GetDC (Hwindow);D Rawicon (hdc, rand ()% (g_pgame-> GetWidth ()), Rand ()% (G_pgame->getheight ()), (HICON) (WORD) Getclasslong (Hwindow, Gcl_hicon)); ReleaseDC (Hwindow, HDC);}

The game Engine code is attached here:

#pragma once//to include the file # include <windows.h> #define max_loadstring 32//windows function Declaration int WINAPI WinMain (hinstance HINSTANCE, HInstance hprevinstance, LPSTR lpcmdline, int ncmdshow); LRESULT CALLBACK WndProc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM);////game engine function declaration//The particular implementation of these functions is game-specific, BOOL Gameinitialize (HINSTANCE hinstance) must be provided by each game using the game engine, void Gamestart (HWND hwindow), void Gameend (), void Gameactivate (hwnd hwindow); void Gamedeactivate (hwnd hwindow); void Gamepaint (hdc hdc); void Gamecycle ();////gameengine Class gameengine{protected://member variable static gameengine * m_pgameengine; HINSTANCE m_hinstance; HWND M_hwindow; TCHAR m_szwindowclass[max_loadstring]; TCHAR m_sztitle[max_loadstring]; WORD M_wicon, M_wsmallicon;int m_iwidth, M_iheight;int m_iframedelay; BOOL m_bsleep;public://Constructors/destructors GameEngine (hinstance hinstance, LPTSTR szwindowclass, LPTSTR szTitle, Word wicon, Word Wsmallicon, int iwidth = 640, int iheight = 480); virtual ~gameengine ();//General method static GameEngine * Getengine () {return M_PGA MeENgine; }; BOOL Initialize (int ncmdshow); LRESULT handleevent (HWND Hwindow, UINT msg, WPARAM WPARAM, LPARAM LPARAM);//access Method HInstance getinstance () {return M_hinstan Ce }; HWND GetWindow () {return m_hwindow;}; void SetWindow (HWND hwindow) {M_hwindow = Hwindow;}; LPTSTR GetTitle () {return m_sztitle;}; WORD GetIcon () {return m_wicon;}; WORD Getsmallicon () {return m_wsmallicon;}; int getwidth () {return m_iwidth;}; int GetHeight () {return m_iheight;}; int Getframedelay () {return m_iframedelay;}; void setframerate (int iframerate) {m_iframedelay = 1000/iframerate;}; BOOL Getsleep () {return m_bsleep;}; void Setsleep (BOOL bsleep) {m_bsleep = Bsleep;};};

game engine source files:

#include "GameEngine.h" GameEngine * gameengine::m_pgameengine = null;int WINAPI WinMain (hinstance hinstance, hinstance hPrevInstance, LPSTR lpcmdline, int ncmdshow) {MSG msg;static int iticktrigger = 0;int itickcount;if (gameinitialize (hInst ance)) {//Initialize the game engine if (! Gameengine::getengine ()->initialize (nCmdShow)) {return FALSE;} Enter the main message loop while (TRUE) {if (PeekMessage (&msg, NULL, 0, 0, pm_remove)) {//Process message if (Wm_quit = = Msg.message) {break;} TranslateMessage (&msg);D ispatchmessage (&msg);} Else{if (! Gameengine::getengine ()->getsleep ()) {//check tick count to see if a cycle is over Itickcount = GetTickCount (); if (Itickcount- Iticktrigger) {Iticktrigger = Itickcount + gameengine::getengine ()->getframedelay (); Gamecycle ();}}} return (int) Msg.wparam;} Gameend (); return TRUE;} LRESULT CALLBACK WndProc (HWND Hwindow, UINT msg, WPARAM WPARAM, LPARAM LPARAM) {//All Windows messages are passed to the game engine return GameEngine: : Getengine ()->handleevent (Hwindow, MSG, WParam, LParam);} GameEngine constructor/destructor//gameengine::gameengine (hinstance HinstaNCE, LPTSTR Szwindowclass, LPTSTR szTitle, Word wicon, word wsmallicon, int iwidth, int iheight) {//Set member variables for game engine M_pgameengin E = this;m_hinstance = Hinstance;m_hwindow = Null;if (Lstrlen (Szwindowclass) > 0) {lstrcpy (M_szwindowclass, Szwindowclass);} if (Lstrlen (SzTitle) > 0) {lstrcpy (m_sztitle, szTitle);}        M_wicon = Wicon;m_wsmallicon = Wsmallicon;m_iheight = Iheight;m_iwidth = Iwidth;m_iframedelay = 20; The default is 20 frames per second m_bsleep = TRUE;} destructor Gameengine::~gameengine () {}int gameengine::initialize (int ncmdshow) {//Register window class Wndclassex wndclass; wndclass.cbsize = sizeof (wndclassex); Wndclass.style = Cs_hredraw | Cs_vredraw;wndclass.lpfnwndproc = (WNDPROC) Wndproc;wndclass.cbclsextra = 0;wndclass.cbwndextra = 0; Wndclass.hinstance = M_hinstance;wndclass.hicon = LoadIcon (M_hinstance, Makeintresource (GetIcon ())); WNDCLASS.HICONSM = LoadIcon (M_hinstance, Makeintresource (Getsmallicon ())); wndclass.hcursor = LoadCursor (NULL, IDC_ ARROW); wndclass.hbrbackground = (Hbrush) getstockobject (Black_brush); WNDCLASS.LPSzmenuname = Null;wndclass.lpszclassname = M_szwindowclass;if (! RegisterClassEx (&wndclass) {MessageBox (M_hwindow, L "Registration window failed!) ", L" warning ", MB_OK); return FALSE;} Calculates the window size and position according to the size of the game int iwindowwidth = m_iwidth + getsystemmetrics (sm_cxfixedframe) * 2; int iwindowheight = m_iheight + getsystemmetrics (sm_cyfixedframe) * 2 + getsystemmetrics (sm_cycaption); if ( Wndclass.lpszmenuname = NULL) {iwindowheight + = GetSystemMetrics (Sm_cymenu);} int ixwindowpos = (GetSystemMetrics (sm_cxscreen)-iwindowwidth)/2;int Iywindowpos = (GetSystemMetrics (SM_CYSCREEN)-IW Indowheight)/2;//Create window M_hwindow = CreateWindowEx (NULL, M_szwindowclass, M_sztitle, Ws_popupwindow | ws_caption | Ws_minimizebox, Ixwindowpos, Iywindowpos, iwindowwidth, iwindowheight, NULL, NULL, m_hinstance, NULL); if (!m_hwindow) { MessageBox (M_hwindow, L "failed to create window! ", L" warning ", MB_OK); return FALSE;} Update and display Windows ShowWindow (m_hwindow,ncmdshow); UpdateWindow (M_hwindow); return TRUE;} LRESULT gameengine::handleevent (HWND Hwindow, UINT msg, WPARAM WPARAM, LPARAMLParam) {//Pass a Windows message to the game engine member function switch (msg) {Case wm_create://set the game window and start the game Setwindow (Hwindow); Gamestart (Hwindow); Break;case wm_setfocus://activates the game and updates the Sleep state gameactivate (Hwindow); Setsleep (FALSE); Break;case wm_killfocus:gamedeactivate (Hwindow); Setsleep (TRUE); Break;case wm_paint:paintstruct PS; HDC HDC;HDC = BeginPaint (Hwindow, &ps);//Draw Game Gamepaint (HDC); EndPaint (Hwindow, &ps); Break;case wm_destroy://end the game and exit the application Gameend (); PostQuitMessage (0); break;default:break;} Return DefWindowProc (Hwindow, MSG, WParam, LParam);}

such a game, even if the production is finished, this blog is not the main game production, but the game engine to bring convenience!

Take a look at the effect:

Of course you can switch to better-looking pictures, so you can better see the snow full of the whole screen feeling!

I try to change a picture, the effect is as follows:

If you want to make the game more fun, you can make it more fun by not randomly generating it, but by generating it in a conditional way!

The---of Win32 game production Bizzard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.