Game Development Reference
In-Depth Information
Now let's look at how we can make use of this with a renderer that will manage the
texture cache and rendering. Start off by creating a singleton in the same style as
the InputManager we created earlier, and name it Renderer .
In this class we need to manage three things: the active sprite batch for rendering,
the texture cache, and the list of sprites to be rendered. I'll refer to them as
_sb , _texCache , and _sprites , respectively. We'll also keep a reference to the
ID3D11Device1 so that we can load textures. Alongside that, add an Initialize
method that takes a ComPtr to ID3D11DeviceContext1 and ID3D11Device1 .
void
Initialise(Microsoft::WRL::ComPtr<ID3D11DeviceContext1>
context, Microsoft::WRL::ComPtr<ID3D11Device1>
device);
The initialize method itself is pretty simple; we just keep track of the device, and cre-
ate the SpriteBatch as we did in Chapter 2 , Drawing 2D Sprites .
void Renderer::Draw()
{
_sb->Begin();
for (auto sprite : _sprites)
sprite->Draw(_sb);
_sb->End();
}
The Draw method is similar to what we created before, but in this case we loop
through each of the Sprites in our vector between starting and stopping the
SpriteBatch . The big part of this class, however, is the CreateSprite method,
which we will use to create the sprite objects and ensure they're in the rendering list
for display.
std::shared_ptr<Sprite>
Renderer::CreateSprite(std::wstring &path)
Search WWH ::




Custom Search