Thread: Direct3D sprite not appearing on the screen

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Direct3D sprite not appearing on the screen

    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
    Last edited by bijan311; 12-26-2011 at 01:35 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I have no idea why it is not drawing but I should mention I would not rely on the D3DX10 sprite interface implementation. It had a serious memory leak in DirectX 9 and I doubt they have fixed it.

    Your ortho matrix appears to have bottom and top flipped. Is this on purpose?
    Last edited by VirtualAce; 12-26-2011 at 08:37 PM.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Quote Originally Posted by VirtualAce View Post
    Your ortho matrix appears to have bottom and top flipped. Is this on purpose?
    What do you mean? I double checked the example in my book and it looked right.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    As it appears in the SDK:
    D3DXMATRIX * D3DXMatrixOrthoOffCenterLH( D3DXMATRIX *pOut, FLOAT l, FLOAT r,
    FLOAT b, FLOAT t, FLOAT zn, FLOAT zf);
    As it appears in your code:
    Code:
    D3DXMatrixOrthoOffCenterLH(&matProjection, (float)viewPort.TopLeftX, (float)viewPort.Width, (float)viewPort.TopLeftY, (float)viewPort.Height,    0.1f, 10);
    You use viewPort.TopLeftY in place of b or bottom and viewPort.Height in place of t or Top. Hence it is flipped.

    In 2D positive Y is down the screen since the origin is at the top, left. In 3D left-handed coordinate systems positive Y is up and the origin is at the center of the screen. The origin in screen space that is - the origin of the world is still at 0,0,0 or at the camera position when it is moved.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Oops, I guess that was a mistake, but it still didn't solve my problem.

  6. #6
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Res//Bar.bmp
    I believe you meant "Res\\Bar.bmp" (with '\' backslashes).
    "Res/Bar.bmp" (with a single '/' slash) should also work.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. include <iostream> appearing red
    By Tachimazu in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2009, 10:59 PM
  2. Moving sprite relative to another sprite
    By Akkernight in forum Game Programming
    Replies: 5
    Last Post: 04-13-2009, 05:00 PM
  3. Menu not appearing
    By P4R4N01D in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2009, 10:27 PM
  4. Unwanted characters keep appearing
    By sundeeptuteja in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2003, 12:58 AM
  5. CDialog actions after appearing
    By phil_drew in forum Windows Programming
    Replies: 0
    Last Post: 12-06-2002, 07:13 AM