Directx11 tutorial (1) Basic Windows Application Framework (1)

Source: Internet
Author: User

In vs2010, create a new Win32 project named mytutoriald3d11. Note: Check "create directory for solution" at the same time, and create a solution. All the project files in the subsequent tutorial are created, we are all built in this solution.

 

Check emtpy Project

Add source files-> Add new item-> main. cpp

 

To create a Windows application, follow these steps:

1. register the window class in the main function of the entry.

2. Call the createmediawex function to create a window.

3. Process Scheduling message Loops

4. Write a callback function to respond to various message events in the callback function.

In Main. cpp, gradually Add the following code:

First, add

# Include <windows. h>

In this way, we can use Windows API functions, structure, class objects, and so on.

 

// Window class name
Lpcwstr m_applicationname;
// Application instance handle
Hinstance m_hinstance;

Defines a global windows window handle, which is used to represent the main window handle of the application.
// Window handle
Hwnd m_hwnd;
// Used to determine whether to press the ESC key and press the ESC key to exit the program
Bool bexit = false;

// Initialize the window class and create an application window
Void initializewindows (Int & screenwidth, Int & screenheight );
// Call the initialization window function and other interface class initialization functions
Bool initialize ();

// Process message Loops
Void run ();

// Close the window
Void shutdownwindows ();

// These two functions are the callback functions of the window.
Static lresult callback messagehandler (hwnd, uint, wparam, lparam );
Lresult callback wndproc (hwnd, uint, wparam, lparam );

// Determine whether the window is full screen. Use different window creation parameters in full screen mode and window mode.

Const bool full_screen = false;

The following function winmain is the entry function of a Windows application.

Hinstance indicates the instance handle of the current application. It is actually a memory base address, and the system loads the executable program image to this location in the process address space.

Hprevinstance indicates the previous instance handle of the process. For example, if two windows are opened for the same program, the window opened for the first time is the window of the previous instance. This parameter is used in a 16-bit Windows system. For a 32-bit program, this parameter is always null. Currently, this parameter is retained mainly for compatibility with the 16-bit Windows system.

Pscmdline is a pointer to the application command line string, excluding the execution file name. Obtain the entire command line and use the getcommandline function.

Ncmdshow: specifies how the window is displayed, such as sw_hide (hidden) and sw_minimize (minimized). The default value is sw_show.

// Main function of the application entry
Int winapi winmain (hinstance, hinstance hprevinstance, pstr pscmdline, int icmdshow)
{

Initialize ();
Run ();
Shutdownwindows ();
Return 0;
}

This function first calls the initialization function. The initialization function sets the window height and width, and then calls the initialization function.

// Call window initialization functions and other class initialization functions
// In this example, only the window function Initialization is called.
Bool initialize ()
{
Int screenwidth = 0, screenheight = 0;

// Initialization window
Initializewindows (screenwidth, screenheight );

Return true;
}

Void initializewindows (Int & screenwidth, Int & screenheight)
{
Wndclassex WC;
Devmode dmscreensettings;
Int posx, Posy;

// Obtain the application instance handle
M_hinstance = getmodulehandle (null );

// Application name
M_applicationname = l "engine ";

// Set window parameters.
WC. Style = cs_hredraw | cs_vredraw | cs_owndc;

After the callback function is specified, Windows automatically calls the callback function to process various message events.
WC. lpfnwndproc = wndproc; // specify the callback function.
WC. cbclsextra = 0;
WC. cbwndextra = 0;
WC. hinstance = m_hinstance;
WC. hicon = loadicon (null, idi_winlogo );
WC. hiconsm = WC. hicon;
WC. hcursor = loadcursor (null, idc_arrow );
WC. hbrbackground = (hbrush) getstockobject (black_brush); // The default black window black background
WC. lpszmenuname = NULL;
WC. lpszclassname = m_applicationname;
WC. cbsize = sizeof (wndclassex );

// Register the window class
Registerclassex (& WC );

// Obtain the Windows desktop resolution
Screenwidth = getsystemmetrics (sm_cxscreen );
Screenheight = getsystemmetrics (sm_cyscreen );

// Set different resolutions based on whether the screen is full.
If (full_screen)
{
// In Full Screen mode, set the window size to Windows desktop resolution.
Memset (& dmscreensettings, 0, sizeof (dmscreensettings ));
Dmscreensettings. dmsize = sizeof (dmscreensettings );
Dmscreensettings. dmpelswidth = (unsigned long) screenwidth;
Dmscreensettings. dmpelsheight = (unsigned long) screenheight;
Dmscreensettings. dmbitsperpel = 32;
Dmscreensettings. dmfields = dm_bitsperpel | dm_pelswidth | dm_pelsheight;

// Temporarily set the display device to full screen mode. Note: When the application exits, the system default settings will be restored.
Changedisplaysettings (& dmscreensettings, cds_fullscreen );

// Set the Coordinate Position in the upper left corner of the window to (0, 0 ).
Posx = posy = 0;
}
Else
{
// Window mode: 800*600.
Screenwidth = 800;
Screenheight = 600;

// Coordinates in the upper left corner of the window, posx, Posy

Posx = (getsystemmetrics (sm_cxscreen)-screenwidth)/2;
Posy = (getsystemmetrics (sm_cyscreen)-screenheight)/2;
}

// Use different parameters for full screen and window.
If (full_screen)
{
M_hwnd = createmediawex (ws_ex_appwindow, m_applicationname, m_applicationname,
Ws_clipsiblings | ws_clipchildren | ws_popup,
Posx, Posy, screenwidth, screenheight, null, null, m_hinstance, null );
}
Else
{
M_hwnd = createmediawex (ws_ex_appwindow, m_applicationname, m_applicationname,
Ws_overlappedwindow,
Posx, Posy, screenwidth, screenheight, null, null, m_hinstance, null );
}

// Display the window and set it as the focus.
Showwindow (m_hwnd, sw_show );
Setforegroundwindow (m_hwnd );
Setfocus (m_hwnd );

// Hide the mouse.
// Showcursor (false );

Return;
}

In the run function, we respond to scheduling Windows messages and call our render function.

// Process the message
Void run ()
{
MSG;
Bool done, result = 1;

// Initialize the message structure.
Zeromemory (& MSG, sizeof (MSG ));

// Process messages cyclically

Done = false;
While (! Done)
{
// Process Windows messages.
If (peekmessage (& MSG, null, 0, 0, pm_remove ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}

// Receives the wm_quit message and exits the program.
If (msg. Message = wm_quit)
{
Done = true;
}
Else
{
Result = bexit; // If you press ESC, exit the program.

// Our rendering or other processing can be placed here
//....
//.....
If (result)
{
Done = true;
}
}

}

Return;
}

The wndproc function is a window callback function in which message processing is performed.

Lresult callback wndproc (hwnd, uint umessage, wparam, lparam)
{
Switch (umessage)
{

// Window destroy message.
Case wm_destroy:
{
Postquitmessage (0 );
Return 0;
}

// Close the message in the window.
Case wm_close:
{
Postquitmessage (0 );
Return 0;
}

// The messagehandle process processes all other messages.
Default:
{
Return messagehandler (hwnd, umessage, wparam, lparam );
}
}
}

Lresult callback messagehandler (hwnd, uint umsg, wparam, lparam)
{

Switch (umsg)
{
// Detect key messages.
Case wm_keydown:
If (wparam = vk_escape)
Bexit = true;
Return 0;
// Any other messages are sent to Windows for default processing.
Default:
{
Return defwindowproc (hwnd, umsg, wparam, lparam );
}
}
}

Shutdownwindows functions are mainly used to release some resources after the program ends.

 

Void shutdownwindows ()
{
// Display the cursor.
// Showcursor (true );

// Restore the default display settings.
If (full_screen)
{
Changedisplaysettings (null, 0 );
}

// Release the window handle.
Destroywindow (m_hwnd );
M_hwnd = NULL;

// Release the application instance.
Unregisterclass (m_applicationname, m_hinstance );
M_hinstance = NULL;

Return;
}

After the program is executed, the interface is as follows. The window is black. Press ESC and the program will exit:

Complete code can be found:

Project File mytutoriald3d11

Download Code:

Http://files.cnblogs.com/mikewolf2002/myTutorialD3D11.zip

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.