Sign in to follow this  
vivendi

[C++ DirectX] loading 2d image

Recommended Posts

Hey, i'm not sure if i'm doing this right at all, but i tried to load an image and display that to the screen, for creating a HUD. But for some reason my image doesn't show at all. Hopefully someone here can tell me what i'm doing wrong: I have a function which loads the image, creates a resource for it and passes that to the backbuffer:
[source language="cpp"]
HRESULT PV2DImmediate::Load2DImageFromFile ( LPCWSTR filename )
{
	HRESULT hr;

	ID3D10Resource* pD3D10Resource = NULL;
	hr = D3DX10CreateTextureFromFile ( g_pd3dDevice, filename, NULL, NULL, &pD3D10Resource, NULL );
	if ( FAILED ( hr ) )
	{
		MessageBox ( NULL, L"Failed loading image!", L"ERROR", MB_OK );
		return E_FAIL;
	}

	ID3D10Texture2D* texture2D = NULL;
	pD3D10Resource->QueryInterface(__uuidof( ID3D10Texture2D ),(LPVOID*) &texture2D);
	pD3D10Resource->Release();

	//// Get a pointer to the back buffer texture
	ID3D10Texture2D *pBackBuffer;
	hr = g_pSwapChain->GetBuffer ( 0, __uuidof ( ID3D10Texture2D ), (LPVOID*)&pBackBuffer );
	if ( FAILED ( hr ) )
	{
		MessageBox ( NULL, L"Failed allocating backbuffer!", L"ERROR", MB_OK );
		return E_FAIL;
	}

	D3D10_BOX sourceRegion;
	sourceRegion.left = 0;
	sourceRegion.right = 640;
	sourceRegion.top = 0;
	sourceRegion.bottom = 480;
	sourceRegion.front = 0;
	sourceRegion.back = 1;

	g_pd3dDevice->CopySubresourceRegion ( pBackBuffer, 0, 0, 0, 0, pD3D10Resource, 0, &sourceRegion );

	return S_OK;
}

Then in my WinMain right before the while loop where i do the PeekMessage and Render() i call that function above.
WinMain ( bla bla )
{
//...code

	PV2DImmediate 2DImg;
	2DImg.Load2DImageFromFile ( L"C:\\error-icon.png" );

    while( WM_QUIT != msg.message )
    {
        if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        else
        {
			pvCore.pvRender ( );
        }
    }

// some more code
}
Now all i get is a white window, i don't see my image at all. I'm also not doing anything in the renderloop with my 2D image... Well anyway, i hope someone here can tell me what i'm doing wrong. Thanks for any help.

Share this post


Link to post
Share on other sites
Typically you don't want to do a resource copy. Instead you want to create a shader resource view for your texture (you can use D3DX10CreateShaderResourceViewFromFile), and then draw it using triangles. The ID3DX10Sprite class allows you to easily draw textures using screen space positions.

Share this post


Link to post
Share on other sites
Sorry, this is all very new to me. I'm still not sure what to do.
Are you saying that with the code i have now i have to use D3DX10CreateShaderResourceViewFromFile()

Or do you mean that i just have to drop the code that i have now and move on to ID3DX10Sprite instead (without CreateShaderResourceViewFromFile())??

Or a combination of all of what you mentioned with my code..?

BTW, i want to load images of any size. Not just those with a size by the power of 2. 1,2,4,8,16,32,64,128,256,512 etc...

I'm not sure if that makes any difference in my case?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this