01-use engines and load Resources

Source: Internet
Author: User
Tags drawtext random seed blizzard

1. The game engine is used to write public code for games, which can simplify game development.

If you don't need an engine, you need to create your own window and message processing functions, etc. See the http://blog.csdn.net/a8887396/article/details/9247419

A simple engine has seven functions:

Game initialization game starts game ends game activation game exciting game draw game cycle

2 game cycle

The average number of frames is 15-20.

3. Start the project


(1) create a Win32 Empty Project

(2) create an empty blizzard in the header file and the source file respectively. H and blizzard. CPP file, and then select Add existing item to add the game engine header file gameengine. h and the source file gameengine. CPP is added.

(3) Add resources

Right-click the resource file and choose "Add Resource"> "select icon"> "import ".

Import the two icons.

The resource file blizzard. RC and header file resource. h are automatically generated.

The resource view shows that the IDs of these two icons are large and smaller as those of the desktop icons. The title bar icons idi_icon1 idi_idcon2 are their resource IDs which can be used after name change.

(4) use the engine to write a simple animation

Blizzard. h

#pragma once#include <windows.h>#include "resource.h"#include "GameEngine.h"GameEngine *g_pGame;

Contains a pre-compilation specified, indicating to only contain once

Others are several header files.

There is also a global variable for the engine pointer

As mentioned before, the engine has seven major features which are defined in gameengine. h.

BOOL GameInitialize(HINSTANCE hInstance);void GameStart(HWND hWindow);void GameEnd();void GameActivate(HWND hWindow);void GameDeactivate(HWND hWindow);void GamePaint(HDC hDC);void GameCycle();

But we need to implement it when writing a game.

So what is implemented in blizzard. cpp is that they

# Include "blizzard. H "bool gameinitialize (hinstance) {g_pgame = new gameengine (hinstance, text (" blizzard "), text (" blizzard "), idi_icon1, idi_icon2 ); if (g_pgame = NULL) {return false;} g_pgame-> setframerate (15); // return true when gamecycle () is run 15 times per second;} void gamestart (hwnd hwindow) {srand (gettickcount (); // Random Seed} void gameend () {Delete g_pgame;} void gameactivate (hwnd hwindow) {HDC; rect; HDC = getdc (hwindow); getclientrect (hwindow, & rect); drawtext (HDC, text ("Here comes the blizzard"),-1, & rect, optional | dt_center | dt_vcenter); releasedc (hwindow, HDC);} void gamedeactivate (hwnd hwindow) {HDC; rect; HDC = getdc (hwindow); getclientrect (hwindow, & rect); drawtext (HDC, text ("the blizzard has passsed"),-1, & rect, dt_singleline | dt_center | dt_vcenter); releasedc (hwindow, HDC );} void gamepaint (HDC) {} void gamecycle () {HDC; hwnd hwindow = g_pgame-> getwindow (); HDC = getdc (hwindow); drawicon (HDC, Rand () % g_pgame-> getwidth (), Rand () % g_pgame-> getheight (), (hicon) (Word) getclasslong (hwindow, gcl_hicon); releasedc (hwindow, HDC );}

One by one

1 gameinitialize game Initialization

Bool gameinitialize (hinstance) {g_pgame = new gameengine (hinstance, text ("blizzard"), text ("blizzard"), idi_icon1, idi_icon2 ); if (g_pgame = NULL) {return false;} g_pgame-> setframerate (15); // return true if gamecycle () is run 15 times per second ;}

I did two things.

1) is to create a game engine object and assign the pointer to the global variable. This function is the constructor in the game engine.

G_pgame = new gameengine (hinstance, // game window handle

Text ("blizzard"), // window class name text is a macro that represents text

Text ("blizzard"), // Title bar name

Idi_icon1, // The icon displayed on the desktop.

Idi_icon2); // The icon in the title bar

2) sets the number of frames for the game.

G_pgame-> setframerate (15); this is also the function in the engine. 15 frames indicate that the gamecyle function is executed 15 times a second.

2. Start the game

Void gamestart (hwnd hwindow) {srand (gettickcount (); // Random Seed}

Set a Random Seed. Generally, random numbers are used in games.

Gettickcount () is a Windows function, and the returned value is the number of seconds from January 1, January 1, 1970 to the present.

3. the game ends.

void GameEnd(){delete g_pGame;}

A new engine object is created before resources are released, so you need to delete it at the end.

4 gameactivate game Activation

void GameActivate(HWND hWindow){HDC hDC;RECT rect;hDC = GetDC(hWindow);GetClientRect(hWindow,&rect);DrawText(hDC,TEXT("Here comes the blizzard"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);ReleaseDC(hWindow,hDC);}

What is activation?

The game is automatically activated at the beginning.

For example, if you click another window while playing the game, then you will kill it and then click the game window to activate it.

To see the effect of activation, a line of words is displayed during activation.

5 gamedeactivate: Game death

void GameDeactivate(HWND hWindow){HDC hDC;RECT rect;hDC = GetDC(hWindow);GetClientRect(hWindow,&rect);DrawText(hDC,TEXT("The blizzard has passsed"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);ReleaseDC(hWindow,hDC);}

In the same way, when you play a game, click another window, and the game window will be exhausted if you lose focus.

6 gamepaint game re-painting

Void gamepaint (HDC)
{

}

At the beginning of the game, the game will be re-painted.

Minimize the number of games. A game re-painting will be performed again when the game is re-opened.

Because the animation to be done does not draw the background, nothing is painted.

I added a MessageBox here. I found that messgebox will run twice.

I don't know much about it here. I will try again later.

7 gamecycle: The game cycle is 15 frames per second, so 15 cycles are executed per second.

void GameCycle(){HDC hDC;HWND hWindow = g_pGame->GetWindow();hDC = GetDC(hWindow);DrawIcon(hDC,rand()%g_pGame->GetWidth(),rand()%g_pGame->GetHeight(),(HICON)(WORD)GetClassLong(hWindow,GCL_HICON));ReleaseDC(hWindow,hDC);}

Here we mean to randomly draw a snow pattern on the panel, that is, our icon.

Someone asked where winmain is? In the source file of the game engine.

Code http://download.csdn.net/detail/a8887396/5756331

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.