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

Source: Internet
Author: User

In this tutorial, we encapsulate the code of the previous tutorial. Encapsulate the initialization function, run function, window callback function, and shutdownwindows function into a system class.

First, choose solution, mytutoriald3d11, right-click, and select new project,

 

Create an empty project named mytutoriald3d11_1 and add the main. cpp file to the project.

The main. CPP Code is as follows:

# Include "systemclass. H"

// Main function of the application entry
Int winapi winmain (hinstance, hinstance hprevinstance, pstr pscmdline, int icmdshow)
{
Systemclass * system;
Bool result;

// Create a system object.
System = new systemclass;
If (! System)
{
Return 0;
}

// Initialize the system object.
Result = system-> initialize ();
If (result)
{
System-> Run ();
}

// Close and release the system object.
System-> Shutdown ();
Delete system;
System = 0;
Return 0;
}

 

Right-click the project mytutoriald3d11_1 and choose add> class to create a class named systemclass.

 

The systemclass. H code is as follows:

# Pragma once
// Defining this macro can reduce the size of Windows header files, so that the compiler does not compile unnecessary files and the Compilation Time is faster.
# Define win32_lean_and_mean

# Include <windows. h>

Const bool full_screen = false;
Static bool bexit = false;

Class systemclass
{
Public:
Systemclass (void );
Systemclass (const systemclass &);
~ Systemclass (void );

Bool initialize ();
Void Shutdown ();
Void run ();

Lresult callback messagehandler (hwnd, uint, wparam, lparam );
PRIVATE:
Bool frame ();
Void initializewindows (Int &, Int &);
Void shutdownwindows ();

Lpcwstr m_applicationname;
Hinstance m_hinstance;
Hwnd m_hwnd;
};

// Define static callback functions and application handles
// Because the window callback function must be specified when defining the window class, we use the static callback function wndproc.
Static lresult callback wndproc (hwnd, uint, wparam, lparam );

Static systemclass * applicationhandle = 0;

The systemclass. CPP Code is as follows:

# Include "systemclass. H"

Systemclass: systemclass (void)
{
Bexit = false;
}

Systemclass: systemclass (const systemclass &)
{

}

Systemclass ::~ Systemclass (void)
{
}

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

// Initialization window
Initializewindows (screenwidth, screenheight );

Return true;
}

Void systemclass: Shutdown ()
{
// Destroy other classes
//...

// Execute some destroy operations in the window.
Shutdownwindows ();

}

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

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

// Process messages cyclically. If wm_quit is received.
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
//....
//.....
Frame ();
If (result)
{
Done = true;
}
}

}

Return;
}

Bool systemclass: frame ()
{
Return true;
}

// Initialize the window class and create an application window
Void systemclass: initializewindows (Int & screenwidth, Int & screenheight)
{
Wndclassex WC;
Devmode dmscreensettings;
Int posx, Posy;

// Obtain the system Class Object (application handle)
Applicationhandle = this;

// 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;
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 default window position to (0, 0 ).
Posx = posy = 0;
}
Else
{
// Window mode: 800*600.
Screenwidth = 800;
Screenheight = 600;

// Window position, coordinates in the upper left corner of the posx and posy windows
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 );

}

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

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

// Destruction window
Destroywindow (m_hwnd );
M_hwnd = NULL;

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

Return;

}

Lresult callback systemclass: 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 );
}
}
}

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 applicationhandle-> messagehandler (hwnd, umessage, wparam, lparam );
}
}
}

The interface after the program is executed is the same as that after the previous execution.

Complete code can be found:

Project File mytutoriald3d11_1

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.