bijan311 0 Newbie Poster

I'm trying to make a 2D game engine using Direct3D and I've come across a problem, when I try to draw a sprite to the screen it doesn't show up.

Here is what I believe is relevant code, but all the files will be attached if you want to look at them.

//Bitmap constructor
BimanBitmap::BimanBitmap(LPCWSTR szFileName, ID3D10Device* pDevice)
{
	m_ptPos.x = m_ptPos.x = 0;
	m_ptVel.x = m_ptVel.y = 0;
	//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_mxTranslation * m_mxScaling);
}
//Render function
void BimanEngine::Render()
{
	m_pD3DDevice->ClearRenderTargetView(m_pRenderTargetView, D3DXCOLOR(0.02f, 0.04f, 0.2f, 1.0f));

	m_pSpriteObject->SetProjectionTransform(&m_mxProjection);

	m_pSpriteObject->Begin(D3DX10_SPRITE_SORT_TEXTURE);

	GamePaint();

	//Make it so white parts of the image are transparent
	float fNewBlendFactor[4] = {0, 0, 0, 0};
	m_pD3DDevice->OMSetBlendState(m_pBlendState, fNewBlendFactor, 0xffffffff);

	m_pSpriteObject->Flush();
	m_pSpriteObject->End();

	m_pSwapChain->Present(0, 0);
}
//Game paint function
void GamePaint()
{
	g_pBitmap->Draw(g_pGame->GetSprite());
}

//Draw function
void BimanBitmap::Draw(ID3DX10Sprite* pSprite)
{
	HRESULT hr = pSprite->DrawSpritesBuffered(&m_dsSprite, 1);
	if(FAILED(hr))
		MessageBox(NULL, TEXT("Could not draw sprite"), TEXT("Error!"), MB_OK);
}
//InitD3D function

BOOL BimanEngine::InitD3D()
{
	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = m_iWidth;
	swapChainDesc.BufferDesc.Height = m_iHeight;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow = m_hWindow;
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;
	swapChainDesc.Windowed = TRUE;

	HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0, D3D10_SDK_VERSION, &swapChainDesc,
		&m_pSwapChain, &m_pD3DDevice);

	if(hr != S_OK)
		return FALSE;

	ID3D10Texture2D* pBackBuffer;
	m_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
	if(!pBackBuffer)
		return FALSE;
	
	m_pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &m_pRenderTargetView);
	pBackBuffer->Release();

	m_pD3DDevice->OMSetRenderTargets(1, &m_pRenderTargetView, NULL);

	hr = D3DX10CreateSprite(m_pD3DDevice, 0, &m_pSpriteObject);
	if(hr != S_OK)
		MessageBox(NULL, TEXT("Can't create the sprite object"), TEXT("ERROR!"), MB_OK);

	D3D10_BLEND_DESC blendDesc;
	blendDesc.AlphaToCoverageEnable = FALSE;
	for(int i = 0; i < 8; i++)
	  blendDesc.BlendEnable[i] = TRUE;
	blendDesc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
	blendDesc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
	blendDesc.BlendOp = D3D10_BLEND_OP_ADD;
	blendDesc.SrcBlendAlpha = D3D10_BLEND_ZERO;
	blendDesc.DestBlendAlpha = D3D10_BLEND_ZERO;
	blendDesc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
	for(int i = 0; i < 8; i++)
	  blendDesc.RenderTargetWriteMask[i] = D3D10_COLOR_WRITE_ENABLE_ALL;
	hr = m_pD3DDevice->CreateBlendState(&blendDesc, &m_pBlendState);
	if(FAILED(hr))
		return FALSE;

	D3D10_VIEWPORT viewPort;
	viewPort.TopLeftX = 0;
	viewPort.TopLeftY = 0;
	viewPort.Width = m_iWidth;
	viewPort.Height = m_iHeight;
	viewPort.MaxDepth = 1.0f;
	viewPort.MinDepth = 0.0f;
	m_pD3DDevice->RSSetViewports(1, &viewPort);

	if(FAILED(hr))
	{
		MessageBox(m_hWindow, TEXT("SetViewTransform failed"), TEXT("Error!"), MB_OK);
		return FALSE;
	}

	//This is used so a sprite doesn't go past the screen
	D3DXMatrixOrthoOffCenterLH(&m_mxProjection, (float)viewPort.TopLeftX, (float)viewPort.Width, (float)viewPort.TopLeftY, (float)viewPort.Height,
		0.1f, 10);

	hr = m_pSpriteObject->SetProjectionTransform(&m_mxProjection);
	if(FAILED(hr))
	{
		MessageBox(m_hWindow, TEXT("SetProjectionTransform failed"), TEXT("Error!"), MB_OK);
		return FALSE;
	}

	return TRUE;
}

Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.