3D game engine One Win32 programming

Source: Internet
Author: User


Windows programs generally wait for the user to do something, then respond and take action.
In general, the operation of the Win32 program is converted to a message in the system event queue, such as a key message wm_keydown,wm_mouseclick such as a keyboard and a mouse action message. The system message is passed to the program's local event queue, and then the message processing of the main window is passed to the WinProc () function, and after the message is processed, the program goes to the WinMain () main function, while the general main function is still in the message loop, and then waits for the new message and executes.
Win32 programs are WinMain start, the simplest of a Win32 program, starting from an empty project:

#define Win32_lean_and_mean             #include <windows.h>int WINAPI WinMain (hinstance hinstance,   hinstance hPrevInstance,   LPSTR lpcmdline,   int ncmdshow) {MessageBoxA (NULL, "Try A Try", "MY try", MB_OK | Mb_iconexclamation); return (0);}

WinMain function

MessageBox () function

In the SDK, a simple cue sound function messagebeep (UINT utype), the parameter value Utype commonly used MB_OK system default sound, of course, if you set the system sound in the computer systems to silent, you can not hear the sound.




To create a complete Windows program from an empty project, proceed as follows:
Create a Windows class.


Create an event handler WinProc


Windows class created with Windows registration: After defining the Windows class, you also register to let the Windows operating system know about the class, register it through function RegisterClassEx (), and receive a pointer to the Windows class as a parameter. Before invoking the policy function, the Windows system is unaware of the class, so it cannot be referenced with the new class name, but is registered with the actual data structure of the class that the money is stored in.


Create a window using the Windows class


Creates a primary event loop that receives Windows messages and sends them to an event handler.


Finally, a simple blank Win32 project code is as follows:

Do not load Mfc#define win32_lean_and_mean #include <Windows.h> #include <windowsx.h>//windows class name Constants Const C har* myclassname = "Winclass";//Message processing function LRESULT CALLBACK WndProc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {paintstr UCT PS; HDC Hdc;switch (msg) {Case wm_create:{//initialization code}break;case WM_PAINT:{HDC = BeginPaint (HWND,&AMP;PS);//Redraw EndPaint (hwnd, &AMP;PS);} Break;case wm_destroy:{//Release resources, close application PostQuitMessage (0);} Break;default:return (DefWindowProc (Hwnd,msg,wparam,lparam));} return (0);} Main function int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpcmdline, int ncmdshow) {wndclassex Wcex ;//Create window class HWND hwnd;//window handle MSG msg;//message//set window class wcex.cbsize = sizeof (wndclassex); wcex.style= Cs_hredraw | cs_vredraw;wcex.lpfnwndproc= wndproc;wcex.cbclsextra= 0;wcex.cbwndextra= 0;wcex.hinstance= hInstance;wcex.hIcon= LoadIcon (null,idi_application); wcex.hcursor= loadcursor (NULL, Idc_arrow); wcex.hbrbackground= (HBRUSH) (COLOR_ window+1); Wcex.lpszmenuname= Null;wcex.lpszclassnaMe= myclassname;wcex.hiconsm= LoadIcon (null,idi_application);//Register window class if (! RegisterClassEx (&wcex)) {return (0);} Create a window if (!) (hwnd = CreateWindowEx (Null,myclassname, "MY", Ws_overlappedwindow | ws_visible, 0, 0, N, a, NULL, NULL, HINSTANCE, NULL)) {return (0);} Enter the main loop while (GetMessage (&msg,null,0,0)) {TranslateMessage (&msg);D ispatchmessage (&msg);} Return to the operating system return msg.wparam;}

But this is the basic structure of the general program, for the game, to build a real-time event loop, so that it can do game logic processing, such as a game_main () function, but also real-time detection message queue message and processing.

A real-time event loop is constructed here, using the function PeekMessage () to detect if there are any messages in the message queue. If so, proceed with the other game logic and repeat the loop. The PeekMessage function is prototyped as follows:

BOOL PeekMessage (lpmsg ipmsg,//Message pointer hWnd hwnd,//window handle UINT wmsgfiltermin,//first message uint wmsgfiltermax,//last message uint wremovemsg//delete tag);
Generally the first message and the last message are set to 0, and there are three delete tags:

Pm_noremove
After the PeekMessage is processed, the message is not removed from the queue.
Pm_remove
After the PeekMessage is processed, the message is removed from the queue.
Pm_noyield
This flag causes the system not to release threads waiting for the calling program to be idle. The Pm_noyield can be combined arbitrarily into pm_noremove or pm_remove.
Pm_noremove or Pm_remove is the primary token, and if the use does not delete the message, it is necessary to cooperate with getmessage () to get the message processed. If using Pm_remove, use PeekMessage to get the message directly, the corresponding real-time event loop code is as follows:

while (true) {//Use PeekMessage to get the message if not directly into the game logic if (PeekMessage (&msg,hwnd,0,0,pm_remove)) {if (Msg.message = = Wm_quit )//If the message is Wm_quit, then end the main loop break; TranslateMessage (&msg);D ispatchmessage (&msg);} Master Game Processing Logic game_main ();}
Here the main game processing logic must have to return, that is, to generate an animation frame or execute a piece of game logic must be returned.

3D game engine One Win32 programming

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.