Setting up the stage
Let's begin by starting up Visual Studio 2012 (the Express Edition is fine) and creating a new Direct3D App project. You can find this project in the same window by navigating to New Project | Templates | Visual C++ | Windows Store. Once Visual Studio has finished creating the project, we need to delete the following files:
CubeRenderer.h
CubeRenderer.cpp
SimpleVertexShader.hlsl
SimplePixelShader.hlsl
Once these files have been removed, we need to remove any references to the files that we just removed. Now create a new header (Game.h
) and code (Game.cpp
) file and add the following class declaration and some stub functions into Game.h
and Game.cpp
, respectively. Once you've done that, search for any references to CubeRenderer
and replace them with Game
. To compile this you'll also need to ensure that any #include
statements that previously pointed to CubeRenderer.h
now point to Game.h
.
Note
Remember that Microsoft introduced the C++/CX (Component Extensions) to help you write C++ code that works with WinRT. Make sure that you're creating the right type of class.
#pragma once #include "Direct3DBase.h" ref class Game sealed : public Direct3DBase { public: Game(); virtual void Render() override; void Update(float totalTime, float deltaTime); };
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Here we have the standard constructor, as well as an overridden Render()
function from the Direct3DBase
base class. Alongside, we have a new function named Update()
, which will be explained in more detail when we cover the game loop later in this chapter.
We add the following stub methods in Game.cpp to allow this to compile:
#include "pch.h" #include "Game.h" Game::Game(void) { } void Game::Render() { } void Game::Update(float totalTime, float deltaTime) { }
Once these are in place you can compile to ensure everything works fine, before we move on to the rest of the basic game structure.
Applications and windows
Windows 8 applications use a different window system compared to the Win32 days. When the application starts, instead of describing a window that needs to be created, you provide an implementation of IFrameworkView that allows your application to respond to events such as resume, suspend, and resize.
When you implement this interface, you also have control over the system used to render to the screen, just as if you had created a Win32 window. In this book we will only use DirectX to create the visuals for the screen; however, Microsoft provides another option that can be used in conjunction with DirectX (or on its own). XAML is the user interface system that provides everything from controls to media to animations. If you want to avoid creating a user interface yourself, this would be your choice. However, because XAML uses DirectX for rendering, some extra steps need to be taken to ensure that the two work together properly. This is beyond the scope of the book, but I strongly recommend you look into taking those extra steps if you want to take advantage of an incredibly powerful user interface system.
The most important methods for us right now are Initialize
, SetWindow
, and Run
, which can all be found in the Chapter1
class (Chapter1.h
) if you're following along with the sample code. These three methods are where we will hook up the code for the game. As of now, the template has already included some code referring to the CubeRenderer
. To compile the code we need to replace any references to CubeRenderer
inside our Chapter1
class with a reference to Game
.