Loading and blitting bitmaps
by Matthew Allen
MXF Entertainment
updated: 2/12/00
I'll just rape this tutorial from a few posts I made on GameDev.net. Ya know, GameDev kicks ass! For more bitmap blitting and loading functions, look at the notTETRIS source.
The files ddutil.cpp and ddutil.h can be found here.
Words of wisdom:
posted November 04, 1999 09:23 PM
Ok, so you have DirectDraw set up and you want to load a bitmap and display it on the screen, right? It's really easy, there's only a couple of steps you need to follow:
- Create a surface for the bitmap.
- Load the bitmap into the surface.
- Blit the bitmap onto the back buffer.
- Flip the back buffer to the primary surface. (you can do this, right?)
1. and 2.
Luckily, Microsoft made a function that creates an offscreen surface and loads a bitmap into it: DDLoadBitmap(). To use this, though, you need to get the files "ddutil.cpp" and "ddutil.h" from the DirectDraw SDK. If you don't have them and want em, I can send them to you.
Now, after you've included the ddutil.h file, call DDLoadBitmap() like this:
LPDIRECTDRAWSURFACE4 lpDDSBitmap = NULL;
if ( ( lpDDSBitmap = DDLoadBitmap( lpDD, "bitmap.bmp", 0, 0) ) == NULL )
return FALSE;
Now the bitmap is loaded into lpDDSBitmap!
3.
To blit the bitmap onto the back buffer, call BltFast():
RECT rcFrom;
rcFrom.left = 0;
rcFrom.top = 0;
rcFrom.right = BITMAP_WIDTH;
rcFrom.bottom = BITMAP_HEIGHT;
if ( FAILED( lpDDSBack->BltFast( 0, 0, lpDDSBitmap, &rcFrom, DDBLTFAST_WAIT ) ) )
return FALSE;
What the DDLoadBitmap() function does is creates a HBITMAP and a DirectDraw sufrace, uses a Windows function (LoadImage()) to load the bitmap from a resource or a file into the HBITMAP, then blits the bitmap from the HBITMAP to the DirectDraw Surface using another Windows function (StretchBlt()).
If you need any more help, send me and email or post to this thread.
I would suggest that you look over the function really well before you use it so you know what it's doing...
posted November 08, 1999 02:44 PM
To make a couple of back buffers, you have to make the primary surface with the "two back buffers" flag:
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 2;
if ( FAILED( lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL ) ) )
return FALSE;
Now the primary surface has been created with two back buffers, but you need to call GetAttachedSurface() to actually be able to access the back buffer:
ZeroMemory( &ddscaps, sizeof( ddscaps ) );
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
if ( FAILED( lpDDSPrimary->GetAttachedSurface( &ddscaps, &lpDDSBack ) ) )
return FALSE;
You have to call GetAttachedSurface instead of CreatSurface because the back buffer has to be "a part of" another surface. DirectDraw needs to know which surface each backbuffer belongs to. You really ahve two back buffers (dwBackBufferCount) but you only need to draw onto one of them. The other one is kindof a "middlebuffer". This is how it works: you write to the back buffer, call "flip", the back buffer goes to the middlebuffer, call flip again and the middle buffer goes to the primary surface. This may seem slower, but it really speeds things up. Read the part in Inside DirectX about back buffers.
Now that you have a back buffer and a primary surface, what you need to do is:
- create another surface
- load a bitmap into it
- blit that onto the back buffer
- 4. flip the surfaces
I could send you a file that came with the directx SDK, ddutil.cpp, that create a surface and loads a bitmap into it. So, you call that and then blit that onto the back buffer with BltFast(), a DirectDraw function (look at the help file). After that, call Flip() (also, look at the help file).
You should have it call BltFast() and Flip() in a loop, say in WinMain() after it processes a message.
I really hope that is what you wanted because it took a damn long time to write.
If it doesn't help, post back here. Or I suppose you could get on ICQ...
posted November 08, 1999 02:51 PM
Here's some code to grind on:
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
MSG message;
if ( sys_Init( hInstance, nCmdShow ) == FALSE )
return FALSE;
while ( TRUE )
{
while ( PeekMessage( &message, NULL, 0, 0, PM_NOREMOVE ) == TRUE )
{
if ( GetMessage( &message, NULL, 0, 0 ) )
{
TranslateMessage( &message );
DispatchMessage( &message );
}
else
return TRUE;
}
int xTo = 0, yTo = 0;
if ( FAILED( lpDDSBack->BltFast( xTo, yTo, lpDDSBitmap, NULL, DDBLTFAST_WAIT ) ) )
return FALSE;
if ( FAILED( lpDDSPrimary->Flip( NULL, DDFLIP_WAIT ) ) )
return FALSE;
}
return message.wParam;
}
Discuss this article in the forums
Date this article was posted to GameDev.net: 7/5/2000
(Note that this date does not necessarily correspond to the date the article was written)
See Also:
DirectDraw
© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!
|