I'm trying to make a game using DirectX 10, and I started by making a sprite for the health bar. The problem is the health bar isn't being drawn on the screen. Here is the relevant code.
Code:
//Creating the health bar
g_pHealthBar = new BimanBitmap(TEXT("Res//Bar.bmp"), g_pGame->GetDevice(), 10, 50);
g_pHealthBar->SetPosition(320, 240);
Code:
//The BimanBitmap functions
#include "BimanBitmap.h"
void BimanBitmap::Draw(ID3DX10Sprite* pSprite)
{
HRESULT hr = pSprite->DrawSpritesImmediate(&m_dsSprite,1, 0, 0);
if(FAILED(hr))
MessageBox(NULL, TEXT("Could not draw sprite"), TEXT("Error!"), MB_OK);
}
void BimanBitmap::SetPosition(int x, int y)
{
m_ptPos.x = x;
m_ptPos.y = y;
}
void BimanBitmap::SetVelocity(int x, int y)
{
m_ptVel.x = x;
m_ptVel.y = y;
}
void BimanBitmap::SetSize(float x, float y)
{
D3DXMatrixScaling(&m_mxScaling, x, y, 0.1f);
m_dsSprite.matWorld = (m_mxScaling * m_mxTranslation);
}
BimanBitmap::BimanBitmap(LPCWSTR szFileName, ID3D10Device* pDevice, int iWidth, int iHeight)
{
m_ptPos.x = m_ptPos.x = 0;
m_ptVel.x = m_ptVel.y = 0;
m_iWidth = iWidth;
m_iHeight = iHeight;
//Create the image
CreateImage(szFileName, pDevice);
//This sets the original position of the sprite
D3DXMatrixTranslation(&m_mxTranslation, 0, 0, 0.1f);
//This sets the size of the sprite
D3DXMatrixScaling(&m_mxScaling, (float)m_iWidth, (float)m_iHeight, 0.1f);
m_dsSprite.matWorld = (m_mxScaling * m_mxTranslation);
}
BimanBitmap::~BimanBitmap()
{
}
void BimanBitmap::Update()
{
m_ptPos.x += m_ptVel.x;
m_ptPos.y += m_ptVel.y;
//Update the position of the sprite
D3DXMatrixTranslation(&m_mxTranslation, (float)m_ptPos.x, (float)(480 - m_ptPos.y), 0.1f);
m_dsSprite.matWorld = (m_mxScaling * m_mxTranslation);
}
void BimanBitmap::CreateImage(LPCWSTR szFileName, ID3D10Device* pDevice)
{
ID3D10ShaderResourceView* pResourceView;
HRESULT hr = D3DX10CreateShaderResourceViewFromFile(pDevice, szFileName, NULL, NULL, &pResourceView, NULL);
if(FAILED(hr))
{
LPWSTR string;
wsprintf(string, TEXT("%s was not found"), szFileName);
MessageBox(NULL, string, TEXT("ERROR"), MB_OK);
}
m_dsSprite.pTexture = pResourceView;
m_dsSprite.TexCoord.x = 0;
m_dsSprite.TexCoord.y = 0;
m_dsSprite.TexSize.x = 1.0f;
m_dsSprite.TexSize.y = 1.0f;
m_dsSprite.TextureIndex = 0;
m_dsSprite.ColorModulate = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
}
Code:
//The Creating the sprite object
//Create the sprite object
hr = D3DX10CreateSprite(m_pD3DDevice, 0, &m_pSprite);
//Create the projection transform
D3DXMATRIX matProjection;
D3DXMatrixOrthoOffCenterLH(&matProjection, (float)viewPort.TopLeftX, (float)viewPort.Width, (float)viewPort.TopLeftY, (float)viewPort.Height,
0.1f, 10);
hr = m_pSprite->SetProjectionTransform(&matProjection);
Code:
//the render function
void BimanEngine::Render()
{
m_pD3DDevice->ClearRenderTargetView(m_pRenderTargetView, D3DXCOLOR(0.02f, 0.04f, 0.2f, 0.0f));
m_pFontSprite->Begin(D3DX10_SPRITE_SORT_TEXTURE);
HRESULT hr = m_pSprite->Begin(D3DX10_SPRITE_SORT_TEXTURE);
GamePaint();
m_pSprite->End();
m_pFontSprite->End();
m_pSwapChain->Present(0, 0);
}
Code:
//the game paint function
void GamePaint()
{
g_pHealthBar->Draw(g_pGame->GetSprite());
}
Thank you