次の方法で共有


Set up the game project

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

The first step in assembling your game is to set up a project in Microsoft Visual Studio in such a way that you minimize the amount of code infrastructure work you need to do. You can save yourself a lot of time and hassle by using the right template and configuring the project specifically for game development. We step you through the setup and configuration of a simple game project.

Note  If you just follow this tutorial, it is easier to use the Simple3DGame sample project than create a new one. But knowing how to create your game project and what to include is a critical part of developing your first game, so we'll discuss it here.

 

Objective

  • To learn how to set up a Direct3D game project in Visual Studio.

Note  Complete sample code for this tutorial is in the Windows Store Direct3D shooting game sample.

 

Setting up the game project

You can write a game from scratch, with just a handy text editor, a few samples, and a hat full of raw brainpower. But that probably isn't the most effective use of your time. If you're new to Windows 8 development, why not let Visual Studio shoulder some of the burden? Here's what to do to get your project off to a roaring start.

(Here, we use Microsoft Visual Studio 2013.)

1. Pick the right template

A Visual Studio template is a collection of settings and code files that target a specific type of app based on the preferred language and technology. In Visual Studio 2013, you'll find a number of templates that can dramatically ease game and graphics app development. If you don't use a template, you must develop much of the basic graphics rendering and display framework yourself, which can be a bit of a chore to a new game developer.

The right template for this tutorial, of course, is the one titled Direct3D Application. In Visual Studio 2013, click File... > New Project, and then:

  1. From Templates, select Visual C++, Windows Store.
  2. In the center pane, select Direct3D App.
  3. Give your game project a name, and click OK.

This template provides you with the basic framework for a Windows Store app using DirectX with C++. Go on, build and run it with F5! Check out that powder blue screen. Take a moment and review the code that the template provides. 4 files open when you create your project:

  • <Your sample name>.h (for example, "MyAwesomeGame.h"). This is a header file for your main code file, and you declare your class definitions here. It comes with a basic class for your game, and includes the 5 functions required by all classes that implement the Windows.ApplicationModel.Core.IFrameworkView interface.
  • <Your sample name>.cpp (for example, "MyAwesomeGame.cpp"). Here you find the entry point, the main function, for your game, and the code for the classes and methods declared in <Your sample name>.h.
  • <Your sample name>Renderer.h (for example, "MyAwesomeGameRenderer.h"). This is the header file for a class that provides the rendering pipeline. It obtains, updates, and presents the Direct3D swap chain that contains your game's graphics output. It also contains the basic event handlers for common Windows Store app events, like suspend and resume, and window resizing. This class inherits from the basic view provider defined in Direct3DBase.cpp. You can modify and extend this renderer class as you see fit.
  • <Your sample name>Renderer.cpp (for example, "MyAwesomeGameRenderer.cpp"). This is the code that implements the renderer, built using the view created in Direct3DBase.cpp. The renderer creates the viewport and swap chain for your game's rendered graphics.

We talk more about the other code files in step 3. Right now, let's quickly inspect <Your sample name>.h.

ref class MyAwesomeGame : public Windows::ApplicationModel::Core::IFrameworkView
{
public:
    MyAwesomeGame();
    
    // IFrameworkView Methods
    virtual void Initialize(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView);
    virtual void SetWindow(Windows::UI::Core::CoreWindow^ window);
    virtual void Load(Platform::String^ entryPoint);
    virtual void Run();
    virtual void Uninitialize();

    // Event Handlers
protected:
    void OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowSizeChangedEventArgs^ args);

    void OnLogicalDpiChanged(Platform::Object^ sender);

    void OnActivated(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView, Windows::ApplicationModel::Activation::IActivatedEventArgs^ args);

    void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ args);

    void OnResuming(Platform::Object^ sender, Platform::Object^ args);

    void OnWindowClosed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CoreWindowEventArgs^ args);

private:
    MyAwesomeGameRenderer^ m_renderer;
    Windows::ApplicationModel::Core::CoreApplicationView^ m_applicationView;
    bool m_windowClosed;
};

You create these 5 methods, Initialize, SetWindow, Load, Run, and Uninitialize, when implementing the IFrameworkView interface that defines a view provider. These methods are run by the app singleton that is created when your game is launched, and load all your app's resources as well as connect the appropriate event handlers.

For more info about these methods, see How to set up your Windows Store app with C++ and DirectX to display a DirectX view.

Your main method is in the <Your sample name>.cpp source file. It looks like this:

[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
    auto direct3DApplicationSource = ref new Direct3DApplicationSource();
    CoreApplication::Run(direct3DApplicationSource);
    return 0;
}

Right now, it creates an instance of the Direct3D view provider from the view provider factory (Direct3DApplicationSource, defined in <Your sample name>.h), and passes it to the app singleton to run (CoreApplication::Run). This means that the starting point for your game lives in the body of the implementation of the IFrameworkView::Run method, in this case, <Your sample name>::Run. Here's the code:

void MyAwesomeGame::Run()
{
    BasicTimer^ timer = ref new BasicTimer();

    while (!m_windowClosed)
    {
        timer->Update();
        CoreWindow::Current->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
        m_renderer->Update(timer->Total, timer->Delta);
        m_renderer->Render();
        m_renderer->Present(); // This call is synchronized to the display frame rate.
    }
}

If the window for your game isn't closed, this dispatches all events, updates the timer, and renders and presents the results of your graphics pipeline. We talk about this in greater detail in Defining the game's Windows Store app framework and Assembling the rendering pipeline. At this point, you should have a sense of the basic code structure of a Windows Store DirectX game.

2. Review and update the package.appxmanifest file

The code files aren't all there is to the template. The package.appxmanifest file contains metadata about your project that are used for packaging and launching your game and for submission to the Windows Store. It also contains important info the player's system uses to provide access to the system resources the game needs to run.

Launch the Manifest Designer by double-clicking the package.appxmanifest file in Solution Explorer. You see this view:

For more info about the package.appxmanifest file and packaging, see Manifest Designer. For now, take a look at the Capabilities tab and look at the options provided.

If you don't select the capabilities that your game uses, such as access to Documents for local game saves, or the Internet for global high score board, you won't be able to access the corresponding resources or features. When you create a new game, make sure that you select the capabilities that your game needs to run!

Now, let's look at the rest of the files that come with the Direct3D Application template.

3. Review the included libraries and headers

There are a few files we haven't looked at yet. These files provide additional tools and support common to Direct3D game development scenarios.

Template Source File Description
BasicTimer.h Defines a high-resolution timer useful for gaming or interactive rendering apps.
Direct3DBase.h/.cpp Defines a basic renderer implementation that connects a Direct3D swap chain and graphics adapter to your Windows Store app using DirectX.
DirectXHelper.h Implements a single method, DX::ThrowIfFailed, that converts the error HRESULT values returned by DirectX APIs into Windows Runtime exceptions. Use this method to put a break point for debugging DirectX errors.
pch.h/.cpp Contains all the Windows system includes for the APIs used by a Direct3D app, including the DirectX 11 APIs.
SimplePixelShader.hlsl Contains the high-level shader language (HLSL) code for a very basic pixel shader.
SimpleVertexShader.hlsl Contains the high-level shader language (HLSL) code for a very basic vertex shader.

 

Next steps

At this point, you can create a Windows Store app using DirectX game project and identify the components and files provided by the Direct3D Application template.

In the next tutorial, Defining the game's Windows Store app framework, we work with a completed game and examine how it uses and extends many of the concepts and components that the template provides.