Game programming notes-(I) game programming Basics

Source: Internet
Author: User

I. game programming Basics


1-Overview


1. Game Composition

The game is composed of resources such as plots, images, sounds, and texts.


2. Game Design and Production

The design and production process is roughly divided into five parts: planning, artist, sound effect, program, and test.

Planning: responsible for designing game plots, categories, And gameplay. It is the most important part of the game and directly determines whether the game is successful or not.

Artist: responsible for drawing graphics and image resources required for the game.

Sound Effects: responsible for making the sound resources required for the game.

Program: Responsible for Combining multimedia resources according to planning rules to form a final product-game.

Testing: Tests program stability and game difficulty.

I have read a book that uses the analogy of games and people. Planning is the heart, the program is the skeleton, the artist is the skin, and the sound is the clothes.

Game programming is part of the game design and production process. In my entire note, the core content discussed is the implementation of the game program.



2-game program composition


1. Composition

It is mainly composed of two parts: Logical update and screen rendering. It can also be said that the game program only does these two things.

Logic update: receives player input and updates enemy, player, and world data.

Image Rendering: displays game content in the form of images.


2. Procedure

Initialize data-Update-rendering-release resources.




3-Windows Programming Basics

All of the technologies I use are based on Windows operating systems. In 2D game programming, we use GDI (image development interface) to process graphic images. Although the implementation efficiency of GDI is low, however, compared with other development kits, it is easier to learn and understand and can be used in our learning stage. When we have a deeper understanding of game programming ideas, we can use other development kits to process graphic images, such as DirectX 3D and OpenGL.


1. program entry winmain

A simple windows program.

# Include <windows. h> int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {MessageBox (null, l "is a simple windows application! ", L" this is the title ", mb_okcancel | mb_iconinformation); Return 0 ;}

Similar to the C main function, winmain is a program entry function called by the system.

int WINAPI WinMain(  HINSTANCE hInstance,     // handle to current instance  HINSTANCE hPrevInstance, // handle to previous instance  LPSTR lpCmdLine,         // command line  int nCmdShow              // show state);

For detailed parameters, see msdn or go to the encyclopedia. When writing a program, keep this structure unchanged. The most important parameter is hinstance, which is the application instance handle and identifies the resource address of the current application. It is often used in game programming. Therefore, we often save this value for later use.


2. process for creating a Windows Application

Main function (winmain)-> registry window class (registerclassex)-> Create window (createmediawex)-> message loop (mainloop), processing Window Process (winproc ). The window process is called repeatedly in the message loop. The following is the pseudo-code of the algorithm:

WinMain(){    RegisterClassEx()    CreateWindowEx()    MainLoop()}MainLoop(){    while(true)    {        WinProc()    }}

3. Registration window class registerclassex

Window class, that is, the type of window. It is not a class in C ++ ). Tell the operating system what kind of window will be created.

Atom registerclassex (const wndclassex * lpwcx // class data); typedef struct _ wndclassex {// window class data structure uint cbsize; // the size of this structure uint style; // wndproc lpfnwndproc; // window procedure function pointer int cbclsextra; // additional parameter int cbwndextra; // additional parameter hinstance; // application instance handle hicon; // window icon hcursor; // window cursor hbrush hbrbackground; // background painter lpctstr lpszmenuname; // menu name lpctstr lpszclassname; // window class name hicon hiconsm; // window small icon} wndclassex, * pwndclassex;


4. Create window createmediawex

Hwnd createshortwex (DWORD dwexstyle, // extended window style lpctstr lpclassname, // registered class name registered window class name lptstr lpwindowname, // window name window title DWORD dwstyle, // window style int X, // horizontal position of window coordinate X int y, // vertical position of window coordinate Y int nwidth, // window width int nheight, // window height hwnd hwndparent, // handle to parent or owner window parent window hmenu, // menu handle or child identifier menu handle hinstance, // handle to application instance handle lpvoid lpparam // window-creation data additional parameter );

The return value is a window handle, which is like an address that identifies the resource location of the window.


5. Message loop mainloop

Windows programs are message-based, and all communication is implemented through message transmission.

1) Message MSG:

Typedef struct tagmsg {hwnd; // window handle uint message; // message wparam; // parameter lparam; // additional parameter DWORD time; // message generation time point pt; // mouse coordinate when the message is generated} MSG, * PMSG;

2) get the message, getmessage and peekmessage:

BOOL GetMessage(  LPMSG lpMsg,        // message information  HWND hWnd,          // handle to window  UINT wMsgFilterMin, // first message  UINT wMsgFilterMax   // last message);BOOL PeekMessage(  LPMSG lpMsg,        // message information  HWND hWnd,          // handle to window  UINT wMsgFilterMin, // first message  UINT wMsgFilterMax, // last message  UINT wRemoveMsg      // removal options);

Both get messages from the message queue. The difference is that when the message queue is empty, the processing method is different. The former is waiting, and the latter is continuing to execute. Because our games need to be constantly updated and re-painted without waiting, we need to select the latter.

3) translatemessage

Translate a message into a processing format.

BOOL TranslateMessage(  CONST MSG *lpMsg   // message information);

4) Forward the message dispatchmessage

Forward messages to the window.

LRESULT DispatchMessage(  CONST MSG *lpmsg   // message information);


6. winproc

The message processing function called by the system function in the message loop. The structure of this function must be the same as the following function type! The name can be different.

LRESULT CALLBACK WindowProc(  HWND hwnd,     // handle to window  UINT uMsg,     // message identifier  WPARAM wParam, // first message parameter  LPARAM lParam   // second message parameter);


7. Example: create a window

# Include <windows. h> // The lresult callback wndproc (hwnd, // handle to window uint umsg, // message identifier wparam, // first message parameter lparam // second message parameter) {Switch (umsg) {Case wm_destroy: // The Window destroys the message. It is generated when you press the cross button in the window. Postquitmessage (0); // send the exit program message wm_quit. Break; Case wm_lbuttondown: MessageBox (hwnd, l "processing left-click message", l "this is the title", mb_ OK); break; default: Return defwindowproc (hwnd, umsg, wparam, lparam); // call the default Window Process} return 0;} // The main function int winapi winmain (hinstance, // handle to current instance hinstance hprevinstance, // handle to previous instance lpstr lpcmdline, // command line int ncmdshow // show state) {// MessageBox (null, l "this is the message box", l "this is the title ", mb_okcance L); wndclassex wcx; // window class memset (& wcx, 0, sizeof (wndclassex); wcx. cbsize = sizeof (wndclassex); // window class size wcx. style = cs_classdc; // window class style wcx. hbrbackground = (hbrush) getstockobject (white_brush); // obtain the system paint brush (white) wcx. hcursor = loadcursor (null, idc_hand); // load the system cursor wcx. hiconsm = wcx. hicon = loadicon (null, idi_application); // load the System icon wcx. hinstance = hinstance; // application instance handle/* 'l' before the string indicates that the string is in Unicode encoding format, not the default ASCII format. If you want to change to the asii format, you can modify the project properties. */Wcx. lpszclassname = l "wndclass"; // window class name wcx. lpfnwndproc = (wndproc) wndproc; // window procedure // register the window class registerclassex (& wcx); // The window procedure hwnd = createjavaswex (0, l "wndclass ", L "this is the window title", ws_caption | ws_sysmenu | ws_minimizebox, 640,480, null, null, hinstance, null); // display the window showwindow (hwnd, sw_shownormal ); // update window, that is, sending the re-drawing message updatewindow (hwnd); MSG message; // message structure while (true) {// retrieve the message if (peekmessage (& message, nu Ll, 0, 0, pm_remove) {If (message. Message = wm_quit) // exit the program by jumping out of the loop. {Break;} // translation message translatemessage (& message); // dispatchmessage (& message);} Sleep (1); // pause ms, that is, discard CPU time slice ms to avoid wasting CPU .} Return 0 ;}

8. Create a Win32 application using a VS2008-VC9

Menu: file-> New-> project. In the displayed dialog box, select the Win32 project, as shown below:

Enter the name, select the path, click OK, and then click Next To go to the following steps,



Click windows application, select an empty project, and then complete. In this way, an empty Win32 application framework is created, and then the C ++ source file (CPP file) is added to the framework ). Write code, debug, run, OK.


If vc6.0 is used, the operation method is similar to this, but the default encoding format in vc6 is ASCII, so 'L' is not required before the string '.

Note: Both Vs and vc6 must be used to create a Win32 application project. Do not create a console application! Otherwise, the connection fails.


Modify the encoding:In the solution, right-click-> properties. In the displayed dialog box, change the character set to "use multi-Byte Character Set". Then, the program encoding will change to ASCII format. If you are a beginner, we recommend that you change the character set to "Multi-Byte Character Set ".


9. Section

Creating a window is troublesome, but most of the Code remains unchanged. Therefore, we can encapsulate the unchanged parts and use them directly when using them in the future, without having to write them again. This will achieve a permanent effect!

There are many windows APIs. For these APIs, we only need to know their usage. For specific parameters, we can go to msdn (Microsoft software development documentation) to check, therefore, we recommend that you install an msdn version.

There are many new concepts at the beginning. If you cannot understand them, you can ignore them and use them as a black box. It takes a long time to understand them.

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.