Sign in to follow this  
bberksoy

Loading Bitmaps using direct3d Surfaces seem to be too slow! Is there an Alternative?

Recommended Posts

bberksoy    122
Hi everyone, I am trying to program a simple card game for learning purposes. I have created a subroutine that Places a bitmap onto the screen at given location. I'll place the snippet on the bottom, It is actually working and simplifies loading a bitmap quite a bit. The problem is however, it is too slow. For the card game I display the number on the card and the suite for 8 cards on the screen using this method and it takes about 1.5 sec to refresh the screeen when cards are dealt again. I basically use CopyRects method. There are two problems regarding this code, first of all, it requires time to load the bitmap, I can prevent that by loading all bitmaps upon initialization. The second problem is the two for loops in this snippet that reads from bitmap and places onto the Direct3DSurface8. I can't do anything about that. Does anyone know alternative methods to display bitmaps on screen with direct3D (not directdraw)? Or is there like a build in function that loads immediately. Anything that would speed up the rendering with this code. Thanks already for your answers. BTW this snipped works only with screen format X8R8G8B8.
HRESULT DispBitmap(LPCTSTR FileName, int LocX, int LocY)
{
	RECT rct;
	HRESULT hr;
	HDC hdc = CreateCompatibleDC ( NULL ) ;
	HBITMAP hbmnew = ( HBITMAP ) LoadImage ( NULL , FileName , IMAGE_BITMAP , 0 , 0 , LR_LOADFROMFILE ) ;
	BITMAP bmp ;
	GetObject ( hbmnew , sizeof ( BITMAP ) , &bmp ) ;
	SetRect(&rct, 0, 0,  bmp.bmWidth  , bmp.bmHeight);
	HBITMAP hbmold = ( HBITMAP ) SelectObject ( hdc , hbmnew ) ;
	hr = dev->CreateImageSurface ( bmp.bmWidth  , bmp.bmHeight , pMode.Format , &Img);
	if ( FAILED ( hr ) )
		return hr;

	D3DLOCKED_RECT lockedrect ;
	Img->LockRect ( &lockedrect , &rct , 0 ) ;
	COLORREF crColor ;		
	DWORD dwColor ;	
	DWORD dwRed ;	
	DWORD dwGreen ;	
	DWORD dwBlue ;	
	DWORD dwBytesPerPixel = 4;
	BYTE* pbyBuffer = ( BYTE* ) lockedrect.pBits ;
	int iBufferPosition ;
	for ( int y = 0 ; y < bmp.bmHeight ; y ++ )
	{
		for ( int x = 0 ; x < bmp.bmWidth ; x ++ )
		{
			iBufferPosition = y * lockedrect.Pitch + x * dwBytesPerPixel ;
			crColor = GetPixel ( hdc , x , y ) ;
			dwBlue = GetBValue(crColor);
			dwGreen = GetGValue(crColor);
			dwRed = GetRValue(crColor);
			dwColor = dwRed<<16 |dwGreen<<8| dwBlue;
			memcpy ( &pbyBuffer [ iBufferPosition ] , &dwColor , dwBytesPerPixel ) ;
		}
	}
	Img->UnlockRect ( ) ;
	SelectObject ( hdc , hbmold ) ;
	DeleteObject ( hbmnew ) ;
	DeleteDC ( hdc ) ;
	dev->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &BBuff);
	POINT PtDest;
	PtDest.x=LocX;
	PtDest.y=LocY;			
	dev->BeginScene();
	dev->CopyRects(Img, &rct, 1, BBuff, &PtDest);
	dev->EndScene();
	BBuff->Release();
	Img->Release();
	return S_OK;
}

Edited by Coder: Please use [source][/source] tags for code blocks [Edited by - Coder on October 14, 2004 10:43:57 PM]

Share this post


Link to post
Share on other sites
Stevieboy    238
Hi, it looks to me as if you are basicly using win32 functions to load the bitmaps, if you have the dierectx sdk, try using directx quads instead, or the D3DXSPRITE interface. I haven't got time right now to explain any more on this, I'm sure someone else will help you more.

Share this post


Link to post
Share on other sites
bberksoy    122
Framework looks nice, however, I'm trying to learn directx, it would defeat the purpose if I used this. I'll keep that as last resort. Can someone direct me to a nice tutorial involving D3DXSPRITE

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