Tenth. Project Setup and Window initialization

Source: Internet
Author: User

Tenth. Project Setup and Window initialization
This chapter focuses on the basic knowledge needed to build a rendering engine. This includes creating a Visual Studio project, implementing a game loop, and eventually displaying a window on the display.
A New Beginning
This chapter formally begins with C + + programming (the chapter in this book that uses C + + to focus on the DirectX API), and it is important not to expect to quickly learn to render some scenes to the screen. Simply put, it takes some time to learn. The first step is to take the time to build some infrastructure, and the benefits of doing so are reflected in the reuse of code in multiple projects. If you prefer to use the existing code in this book companion site and skip some chapters to start the actual rendering work directly, you can. However, if you are interested in how the underlying system framework is developed, you will find the content of the next few chapters very useful.
In addition, learning the content of these chapters requires that you are already familiar with C + + programming and Visual Studio use, but do not need to be proficient in writing Windows applications. As a result, the setup of the project will be explained at the beginning so that you can complete the project Setup yourself in later chapters.
Projet Setup
The rendering engine to be developed in the next few chapters is divided into two Visual Studio projects, including a library project and a game project. The library project contains common code for any number of games or rendering applications. And the game project is the code that contains the specific application.
Directory Structure
It is a good idea to build a directory structure for all the projects. Table 10.1 lists the directory structure of the companion code for this book.
Table 10.1 Project Directory Structure
Project Creation
To use this directory structure, first create an empty Visual Studio solution (Visual Studio Solution) in the build directory. Then right-click on the solution in the Solution Explorer Anel and on the pop-up menu, select add-->new Project, the Add New Project Wizard starts.
10.1, select the Win32 project template and fill in the project's Namt and location options.
Figure 10.1 The Visual Studio, Add New Project Wizard. Select OK to start the Win32 Application Wizard (Win32 Application Settings Wizard). For the Library project, select Empty Project (10.2) in the Application type selection static library,additional options. In the settings shown in Figure 10.2, precompiled headers is disabled, and if you want to use precompiled headers (for faster compilation), you only need to tick this option.
Figure 10.2 The Visual Studio Win32 application Wizard.
Follow the same steps again to create a game project, but select the Windows Applicaiton item in the application type.
Project Build Order
Next, set the correct compilation order, right-click the solution or one of the project in the Solution Explorer panel, and select in the pop-up menu Project Dependencies。 Library project should be set as a dependency on game project so that the configuration can compile the library project (shown in 10.3) before compiling game project.
Figure 10.3 The Visual Studio Project Dependencies dialog box.
Note
An alternative to using the Project Dependencies dialog box, manually set up library project as a game project item by using the Visual Studio 2013 Common PropertiesIn the Referencesdialog box. With this approach, you need to designate library project as the reference of the game project. To learn more about project references, you can browse the MSDN online documentation.
Include Directories
To enable projects to find the DirectX header file at compile time, you need to specify some file paths in the project configuration. For some, right-click the Library project in the Solution Explorer panel and select it from the pop-up menu propertieesOpen the property Pages. Switch to configuration propertiees (config property)-->c/c++-->general, and then refer to Figure 10.4, compiling Additional Include Directories。 Be sure to configure both in the debut and release options.
Figure 10.4 The Visual Studio Additional Include Directories dialog box.
After you have installed the Windows SDK, you can use the macro $ (windowssdk_includepath). The other two paths represent the paths of the EFFECTS11 and DIRECTXTK libraries. Unzip the EFFECTS11 and DIRECTXTK into the external directory, or modify the installation path of the include directory for these libraries.

Note
Review the discussion in chapter three about the effects 11 and DirectX Tool Kit libraries. Both libraries are published in the form of source code and need to be compiled themselves before use. As mentioned above, you need to put the compiled library file into the external directory, or the game and the library projects other directories that can be accessed directly.

Repeat these path configurations in game project and add a path:
$ (SolutionDir): \source\library
This configuration allows source files in game project to directly include the header files (header file) in library project.

Warning
Unless project already contains at least one. cpp file, otherwise Configuration Propertiees the C + + Options page belowis not displayed. If you create a project yourself (rather than using the book Companion code directly), then library project does not yet contain any. cpp files, so the C + + Options page is hidden. In order to use this option to display, you can create a temporary. cpp format empty file in library project. When you start adding code, you can delete the temporary file.

Linking Libraries
Start configuring link Options below, open the property Pages for game project, go to Configuration properties-->linker-->inputOptions page. Refer to Table 10.2, edit Additional DependenciesPart of the dependent library file. Note the difference between the debug and Release configuration items. In the Debug configuration option, the EFFECTS11 and library file names have a letter D at the end, indicating that these are compiled in debug mode. This is a common way to convert debug and release library files into the same output directory. Figure 10.5 Shows the dialog box for setting additional dependencies in Visual Studio.
Table 10.2 Game Project Input Libraries
Figure 10.5 The Visual Studio Additional Dependencies dialog box.
As with the specified header file containing the path, you also specify the containing path of the library file in Visual Studio. These paths are determined by Additional Library DirectoriesThe edit item specifies that the option is located in the Property Pages dialog box Configuration properties-->linker-->generalThe Options page. Edit the path so that it is the same as shown in Figure 10.6. As before, the debug and release options are configured, and note that the debug and release versions of the DIRECTXTK library are located in different paths.
Figure 10.6 The Visual Studio Additional Library Directories dialog box.
Note: The Include Directories and library in the Configuration Properties-->vc++directories Options page of Visual Studio 2013 The directories option already contains $ (windowssdk_includepath), so when you configure the path above, the path can no longer be added.
Final Project Settings
Just add a little bit more configuration and you can start compiling projects. Because library project and game project are independent of each other (allowing multiple game projects to be used), the library compiled output needs to be stored under a directory shared by multiple projects. All projects All solution directory is a common directory, you can put the compiled generated. lib file under a directory of that path, such as $ (SolutionDir): \lib. You can configure the output directory of the library project as the directory, or copy the. lib file to the directory through a post-build event. A build event is just a series of DOS batch commands that can be triggered before or after compilation. Choose Configuration properties-->build EventsYou can see these settings, and then in Command LineEdit a specific event in the Figure 10.7 shows a post-build event, which is used to copy the compiled output. lib file to a directory that game project can access directly (already configured in game project).
Figure 10.7 The Visual Studio post-build event/command Line dialog boxes.
If you want to use a different output file name (such as Libraryd.lib) under the Debug compilation option of the library project, you need to be in the Properties Pages dialog box
Configuration properties-->ganeralModify in the Options page Target NameItem
Finally, all the code in the next section uses Unicode strings. Need to be Configuration properties-->generalOn the Options page, Character SetItem selection Use Unicode Character Set

Note
These configurations can be simplified by folding the library and game projects, but it is not easy to reuse common code. The examples in the next few chapters are based on these configurations, which allow each example to run independently.
When you create your own project, you can use your preferred configuration.

Application startup Now that the project is ready to be configured, you can start adding code. First add a Program.cpp file in game project that contains the application's entry point WinMain () function. Windows applications use WinMain () as the entry function instead of the traditional main () function.
Listing 10.1 lists the underlying code for all subsequent chapter examples.
List 10.1 The Program.cpp File
#include <memory> #include "GameException.h" #include "Game.h" #if defined (DEBUG) | | Defined (_DEBUG) #define _crtdbg_map_alloc#include <stdlib.h> #include <crtdbg.h> #endifusing namespace Library;int WINAPI WinMain (hinstance instance, hinstance previousinstance, LPSTR commandLine, int showcommand) {#if Defined (DEBUG) | Defined (_DEBUG)    _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df); #endif    std::unique_ptr< Game> Game (new Game (instance, L "Renderingclass", L "real-time 3D Rendering", Showcommand));    Try    {        game->run ();    }    catch (Gameexception ex)    {        MessageBox (Game->windowhandle (), EX.WHATW (). C_STR (), Game->windowtitle (). C_str (), mb_abortretryignore);    }    return 0;}

The code contains the header file <memory&gt, which is used to support the STD::UNIQUE_PTR smart pointer, which indicates that the pointing object is disposed when the pointer leaves the scope. A further two include files are used to support the game and Gameexception classes created in library project. The game class will be introduced later. The class gameexception is an extension to std::exception. The code for these two classes is not listed here and can be found on the companion web site.
Next, the conditional compilation option contains the header file <crtdbg.h&gt, which is used to support the CRT debug library. When the debug library is available, any memory leaks will be listed in the output panel of Visual Studio when the program exits. Calling the function _CrtSetDbgFlag () will enable the Debug library.
In front of the WinMain () function declaration, there is a using namespace directive. The rendering engine code written in this book is encapsulated in a C + + namespace named library (for example, class game and Class Gameexception). All the code about the example is in game project and is encapsulated in the rendering namespace. If you are not familiar with namespaces, it is advisable to familiarize yourself with them; namespaces are part of light side of the force (meaning the good side)
The parameters of the WinMain () function are primarily related to window initialization, and the window initialization is processed in the game class. Therefore, these parameter values are passed as constructor parameters for the game class. The window initialization related code will be discussed shortly. Using the commandline parameter, you can pass information to the Ingress function from the DOS command line or from Windows shortcut startup.
The code in the WinMain () function is very small. The Game::run () function is called through the instance object of the game class, which is encapsulated in a try/catch block, and a message box pops up when an exception occurs in the Game:run () call. Run () is primarily the execution of the application's Game Loop


Note
The code written in this book is primarily for the Windows platform, and in the underlying code you will find many references specifically for Windows. You will also encounter Visual Studio language extensions with various names, and readers who use other programming environments may not be comfortable with this. I chose this approach to avoid cross-platform complexity because the focus of this book is to tell rendering. With just a little bit of modification, the framework used in this book can be applied to both Windows RT and the Xbox One platform. If you modify a bit more, you can also become a standalone graphic API (which can support DirectX or OpenGL).

Tenth. Project Setup and Window initialization

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.