Archived

This topic is now archived and is closed to further replies.

Mr Teatime

Help with some basic direct3D code.

Recommended Posts

Mr Teatime    122

  
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <dxerr9.h>
#include "D3DApp.h"

/* Direct3D Globals */
LPDIRECT3D9		g_pD3D;
LPDIRECT3DDEVICE9	g_pd3dDevice;
LPDIRECT3DTEXTURE9	g_ptexBall;
D3DXIMAGE_INFO		g_BallSrcInfo;
LPD3DXSPRITE		g_pd3dxSprite;

HRESULT InitD3D( HWND hWndMain )
{
	
        g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
	if( g_pD3D == NULL )
		return E_FAIL;

	/* Identify default adapter and get displaymode */
	D3DADAPTER_IDENTIFIER9 adapter;
	UINT uiAdapters = g_pD3D->GetAdapterCount();
	HRESULT result = g_pD3D->GetAdapterIdentifier( 0, NULL, &adapter );
	D3DDISPLAYMODE d3ddm;

	if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT,  &d3ddm ) ) )
		return E_FAIL;

	/* Declare and fill out a present parameters structure */
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory( &d3dpp, sizeof( d3dpp ) );
	d3dpp.SwapEffect		= D3DSWAPEFFECT_FLIP;
	d3dpp.BackBufferWidth		= BUFFER_WIDTH;
	d3dpp.BackBufferHeight		= BUFFER_HEIGHT;
	d3dpp.BackBufferCount		= 1;
	d3dpp.Windowed			= TRUE;
	d3dpp.BackBufferFormat		= d3ddm.Format;
	d3dpp.Flags			= D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;


  /* Define capabilites and create a device */
	D3DCAPS9 devCaps;
	HRESULT hr = g_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &devCaps );
	if( FAILED( hr ) )
	{
		DXTRACE_ERR( "Error getting device capabilities", hr );
		return E_FAIL;
	}

	DWORD dwBehaviourFlags;
	if( devCaps.VertexProcessingCaps != 0 )
		dwBehaviourFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		dwBehaviourFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	
	hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL, hWndMain,  D3DCREATE_HARDWARE_VERTEXPROCESSING,
			&d3dpp, &g_pd3dDevice );

	if( FAILED( hr ) )
	{
		DXTRACE_ERR( "Error Creating device", hr );
		return E_FAIL;
	}

	hr = D3DXCreateSprite( g_pd3dDevice, &g_pd3dxSprite );
	if( FAILED( hr ) )
	{
		DXTRACE_ERR( "Error creating sprite interface", hr );
		return E_FAIL;
	}
    return S_OK;
}

void CleanUp()
{
	if( g_pd3dDevice )
		g_pd3dDevice->Release();

	if( g_pD3D )
		g_pD3D->Release();

	if( g_ptexBall )
		g_ptexBall->Release();

	if( g_pd3dxSprite )
		g_pd3dxSprite->Release();

}

void GetTextureFromFile()
{
	HRESULT hr = D3DXCreateTextureFromFileEx( g_pd3dDevice, "ball.bmp", 0, 0, 1, 0,
				D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
				NULL, &g_BallSrcInfo, NULL, &g_ptexBall ); 

    //  HRESULT hr = D3DXCreateTextureFromFile( g_pd3dDevice, "ball.bmp", &g_ptexBall ); 
}

void Render()
{
	/* Clear the screen, then begin rendering the scene */
	g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0x00), 1.0f, 0L );
	g_pd3dDevice->BeginScene();

	IDirect3DSurface9 *pSurface;
	HRESULT hr = g_pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurface );

	D3DLOCKED_RECT lockedRect;
	hr = pSurface->LockRect( &lockedRect, NULL, NULL );

	UINT *pBackBuf = (UINT*)lockedRect.pBits;
	
    D3DXVECTOR2 pos;
	pos.x = 320;
	pos.y = 240;

	g_pd3dxSprite->GetDevice( &g_pd3dDevice );
	g_pd3dxSprite->Begin();
	g_pd3dxSprite->Draw( g_ptexBall, NULL, NULL,
					NULL, NULL, &pos, 0xFFFFFFFF );
	g_pd3dxSprite->End();


	pSurface->UnlockRect();
	pSurface->Release();

    g_pd3dDevice->EndScene();
	g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
  
First of all, I wasnt sure whether to put this question in the beginners forum, the game programming forum or this one, so apologies in advance if this is off-topic for this forum. Anyway, Ive just started programming in directX and basically all im trying to do is blit a ball texture (which originates from a bmp) onto the backbuffer, then flip the front and backbuffers to display it. When I run the program however, the window is simply blank and the ball isnt displayed, Ive been trying to get it to display for quite some time, but cant seem to work out what it is that im doing wrong.

Share this post


Link to post
Share on other sites
flaXen    122
Trying to upload a bitmap to the backbuffer isn''t a good idea. It''s slow and probably won''t work with anything but the COPY swap effect. If you wish to display a full-screen bitmap, create a texture and upload the bitmap to that, then display the bitmap using a quad.

If your bitmap dimensions aren''t power-of-2, consider blitting it into a larger (next highest power-of-2) texture. If that''s out of the question because of memory constraints (ie 1280x960 = 2048x1024), then consider breaking the graphic into smaller power-of-two textures & quads: 1280x960 = 1024x1024 and 256x1024 (saves 768x1024!). 1280x1024 would take the same amount of memory as 1280x960 since 1280x64 is wasted at 1280x960.

Share this post


Link to post
Share on other sites