Hi there,
I've been following this tutorial on how to render a texture with DirectX tool Kit. It's all clear.
In the example everything is created and executed inside the Game.cpp. I am still new to C++ so I was wondering, how would I transfer all the logic for rendering a sprite to a separate class. So I came Up with this:
#pragma once
#include "SpriteBatch.h"
class MyTexture
{
public:
MyTexture();
~MyTexture();
void Create();
void OnDeviceLost();
void Render();
private:
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_texture;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_texture;
std::unique_ptr<DirectX::SpriteBatch> m_spriteBatch;
DirectX::SimpleMath::Vector2 m_screenPos;
DirectX::SimpleMath::Vector2 m_origin;
UINT backBufferWidth;
UINT backBufferHeight;
};
#include "pch.h"
#include "Grid.h"
#include "SpriteBatch.h"
using namespace DirectX;
using Microsoft::WRL::ComPtr;
using namespace DirectX::SimpleMath;
MyTexture::MyTexture()
{
m_screenPos.x = backBufferWidth / 2.f;
m_screenPos.y = backBufferHeight / 2.f;
}
MyTexture::~MyTexture()
{
}
void MyTexture::Create()
{
m_spriteBatch.reset(new SpriteBatch(m_d3dContext.Get()));
ComPtr<ID3D11Resource> resource;
DX::ThrowIfFailed(
CreateWICTextureFromFile(m_d3dDevice.Get(), L"cat.png", resource.GetAddressOf(),
m_texture.ReleaseAndGetAddressOf()));
ComPtr<ID3D11Texture2D> cat;
DX::ThrowIfFailed(resource.As(&cat));
CD3D11_TEXTURE2D_DESC catDesc;
cat->GetDesc(&catDesc);
m_origin.x = float(catDesc.Width / 2);
m_origin.y = float(catDesc.Height / 2);
}
void MyTexture::OnDeviceLost()
{
m_texture.Reset();
m_spriteBatch.reset();
}
void MyTexture::Render()
{
m_spriteBatch->Begin();
m_spriteBatch->Draw(m_texture.Get(), m_screenPos, nullptr, Colors::White, 0.f, m_origin);
m_spriteBatch->End();
}
But in this class I still need a reference to the m_d3dDevice and m_d3dContext. As i said I am pretty new to C++. So what would be the best way to do this? Something like passing a pointer or something? Maybe somebody can show me some example code.
Btw in Game.h the variables are declared like this:
Microsoft::WRL::ComPtr<ID3D11Device> m_d3dDevice;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_d3dContext;