Introduction: I don't know much about DirectX, so I have limited knowledge. If I say something wrong, just correct it.
The code I use comes from the Ultimate Guide to direct x game development.
Next I will introduce the basic part of the simplest DirectX program "blank window": winmain (). Winmain () is essential in Win32 programs. I have read several different versions of winmain () function code, which are similar. Let's take one of them for example.
See the Code:
Code:
- Lresult winapi msgproc (hwnd, uint MSG, wparam, lparam)
- {
- Switch (MSG)
- {
- Case wm_destroy:
- Postquitmessage (0 );
- Return 0;
- Break;
- Case wm_keyup:
- If (wparam = vk_escape) postquitmessage (0 );
- Break;
- }
- Return defwindowproc (hwnd, MSG, wparam, lparam );
- }
- Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr using line, int show)
- {
- // Register the window class
- Wndclassex WC = {sizeof (wndclassex), cs_classdc, msgproc, 0l, 0l,
- Getmodulehandle (null), null,
- Window_class, null };
- Registerclassex (& WC );
- // Create the application's window
- Hwnd = createwindow (window_class, window_name, ws_overlappedwindow,
- 100,100,640,480, getasktopwindow (), null,
- WC. hinstance, null );
- // Initialize direct3d
- If (initialized3d (hwnd, false ))
- {
- // Show the window
- Showwindow (hwnd, sw_showdefault );
- Updatewindow (hwnd );
- // Enter the message loop
- MSG;
- Zeromemory (& MSG, sizeof (MSG ));
- While (msg. message! = Wm_quit)
- {
- If (peekmessage (& MSG, null, 0u, 0u, pm_remove ))
- {
- Translatemessage (& MSG );
- Dispatchmessage (& MSG );
- }
- Else
- Renderscene (); // temporarily ignore it.
- }
- }
- // Release any and all resources.
- Shutdown (); // temporarily ignore it.
- // Unregister our window.
- Unregisterclass (window_class, WC. hinstance );
- Return 0;
- }
There are two functions: msgproc () and winmain. Among them, the msgproc () function is used to process message queues, which is like a group of wild children waiting for the CPU aunt to take a bath. Different operations are implemented through a switch structure. The focus is on the winmain () function. The winmain () function is a little long, but the length is really nothing in front of the renderscene () function. I can use a simple graph to break down the working mechanism of the winmain () function.
First, create a window class. Note that the class names are classified into wndclass and wndclassex. Wndclass was used in early VC, and wndclassex was widely used in vc6 and later versions. From the name, we can see that this is an enhanced version of wndclass.
You can then register and use the registerclassex () function. Here, the return value is not used to determine whether the registration is correct. I hope that future programs will notice this.
You can add one at 30 rows:
Code:
- If (! Hwnd) return 0;
This can also prevent accidents.
After the display window and update window are displayed, the message loop is displayed. As shown in the figure above, if there is no message queue, it is rendered directly. If yes, it is processed differently based on the message queue. Finally, the unregisterclass () function is removed from registration ().
In addition, the msgproc () function is used on the 21st line. I once could not find its calling location. The above is my learning experience today. I hope you can give us valuable comments and suggestions.