loading bitmaps with DDraw

Started by
15 comments, last by hibiki_konzaki 21 years, 8 months ago
I need to know how to load bitmaps onto a DirectDraw7 surface. I''ve tried Google, the forums search, and everywhere newbs get roasted for because they didn''t look, but nothing has worked. I tried DDLoadBitmap(), but that didn''t work. I''ve tried that function in one of the GameDev tutorials, and that didn''t work (it came up with 102 errors then stopped because it went over 100). So if someone could suggest a site or show me a function that would help out, I''d be really happy. Thanks. Hibiki Wheres the any key?
find your element at mutedfaith.com. <º>
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
Advertisement
I don't know if it's the only way, but the way I load bitmaps onto DirectDraw surfaces is to load them into a GDI DC, obtain a DC for the surface and then BitBlt.

Something along the lines of:-


  HDC hdc, hdcMem;hdc = GetDC(hwnd);hdcMem = CreateCompatibleDC(hdc);ReleaseDC(hwnd, hdc);HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "filename.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);SelectObject(hdcMem, hBitmap);BITMAP bitmap;GetObject(hBitmap, sizeof(BITMAP), &bitmap);lpddSurface->GetDC(&hdc);BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);lpddSurface->ReleaseDC(hdc);  


Remember to delete your objects afterwards, i.e. DeleteObject(hBitmap) etc.

Also, when you create the surface you need to specify the width and height in the DDSURFACEDESC2 structure. You can get these from bitmap.bmWidth and bitmap.bmHeight.

All of the functions are in the MSDN so if you don't understand any of it, have a look.

Hope this helps.

PhilHalf

[edited by - PhilHalf on July 30, 2002 4:12:56 AM]
http://msdn.microsoft.com/library/
Hello...
When i first statred with directx, I spent the most time trying to get the damn bitmap loaded and on a surface, just to see if I could get it on the screen. After all my work, I discovered the little code snippet from microsoft. It might not be the best way, but it should work. Somewhere in all thoes Directx examples, is a file called ddutil.cpp
Here''s an enample of some of the code in that file...

//-----------------------------------------------------------------------------
// Name: DDLoadBitmap()
// Desc: Create a DirectDrawSurface from a bitmap resource.
//-----------------------------------------------------------------------------
extern "C" IDirectDrawSurface7*
DDLoadBitmap( IDirectDraw7* pdd, LPCSTR szBitmap, int dx, int dy)
{
HBITMAP hbm;
BITMAP bm;
DDSURFACEDESC2 ddsd;
IDirectDrawSurface7 *pdds;

//
// Try to load the bitmap as a resource, if that fails, try it as a file
//
hbm = (HBITMAP) LoadImage(GetModuleHandle(NULL), szBitmap, IMAGE_BITMAP, dx,
dy, LR_CREATEDIBSECTION);
if (hbm == NULL)
hbm = (HBITMAP) LoadImage(NULL, szBitmap, IMAGE_BITMAP, dx, dy,
LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if (hbm == NULL)
return NULL;
//
// Get size of the bitmap
//
GetObject(hbm, sizeof(bm), &bm);
//
// Create a DirectDrawSurface for this bitmap
//
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = bm.bmWidth;
ddsd.dwHeight = bm.bmHeight;
if (pdd->CreateSurface(&ddsd, &pdds, NULL) != DD_OK)
return NULL;
DDCopyBitmap(pdds, hbm, 0, 0, 0, 0);
DeleteObject(hbm);
return pdds;
}

I justed used this last night, and there were no problems. Make sure you''re using LPDIRECTDRAWSURFACE7, or change that part to the version you are using.
Ciao... hope this helps

I think, therfore I am.
I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Using the DDLoadBitmap, I got this error:

c:\program files\microsoft visual studio\myprojects\engine\engine.cpp(59) : error C2065: ''DDCopyBitmap'' : undeclared identifier

I included ddutil and dxutil.h/cpp from the DX8 SDK and everything is linked right. I haven''t tried to call the function yet, I just put it in my test program and tried to compile it.

Also, in my ddutil.h/cpp everything is based off a class (which I also read a tutorial about, but when I try to run the program it closes before anything happens, which is what happened when I tried the CDX lib).

Hibiki
Wheres the any key?





find your element
at mutedfaith.com.
<º>
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
The DDCopyBitmap is another fn() in the ddutil.cpp folder
Here is the stuff I use. Don't forget to add the error checking stuff, so if it doesn't work, you'll at least know what isn't woking.



    ///////////////////////////////////////////////////		load_bitmap_file()/////////////////////////////////////////////////void CDirectDraw::load_bitmap_file(LPDIRECTDRAWSURFACE7& lpdds, FILE file){  HBITMAP  hbm = NULL; // Handle to bitmap for LoadImage()  BITMAP   bm; // Bitmap struct    	//...Try to load the bitmap as a resource, if that fails, try it as a file      hbm = (HBITMAP) LoadImage(GetModuleHandle(NULL),			   file, 			   IMAGE_BITMAP,			   NULL,			   NULL,			   LR_CREATEDIBSECTION);  if (hbm == NULL){        hbm = (HBITMAP) LoadImage(NULL, // handle of instance 				  file, // file name				  IMAGE_BITMAP, //IMAGE_BITMAP 					  NULL, // Witdth of bitmap				  NULL,  // Height of bitmap					  LR_LOADFROMFILE |						  LR_CREATEDIBSECTION  }//end if  if ( hbm == NULL ){        ThrowEx(__LINE__,__FILE__);  }//end if//...put Bitmap info into bm struct       if( GetObject( hbm, sizeof(bm), &bm ) == NULL ){	DeleteObject(hbm); // From GetObject call up there 	ThrowEx(__LINE__,__FILE__);  }//...create a DirectDrawSurface for this bitmap   create_offscreen_surface(lpdds, bm.bmWidth,bm.bmHeight);//user defined fn()//...Put the bitmap we loaded on the new surface   copy_bitmap( lpdds, hbm, bm.bmWidth, bm.bmHeight );// user defined fn()  DeleteObject(hbm); // From GetObject call up there     }///////////////////////////////////////////////////	create_offscreen_surface()/////////////////////////////////////////////////void CDirectDraw::create_offscreen_surface(	LPDIRECTDRAWSURFACE7 &NewSurface,	DWORD width, 	DWORD height )			{    //...Clear memory and set flags    clear(ddsd);    ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH;    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;    ddsd.dwWidth = width;    ddsd.dwHeight = height;    if( FAILED( lpdd->CreateSurface(&ddsd, &NewSurface, NULL))){			safe_delete(NewSurface);		ThrowEx(__LINE__,__FILE__);	}// end if}///////////////////////////////////////////////////				copy_bitmap()/////////////////////////////////////////////////void CDirectDraw::copy_bitmap(		LPDIRECTDRAWSURFACE7 &lpddsToCopy,// Surface to copy too		HBITMAP hbm, // handle to bitmap to copy		DWORD width,		DWORD height )	{    HDC	hdcImage = NULL; // use in StretchBlt()    HDC	hlpddsToCopy = NULL; // use in StretchBlt()        if( hbm == NULL || lpddsToCopy == NULL ) 	ThrowEx(__LINE__,__FILE__);			//...select bitmap into a memoryDC so we can use it         hdcImage = CreateCompatibleDC( NULL ); // so we can put handle into this with SelectObject     if( hdcImage == NULL )					ThrowEx(__LINE__,__FILE__);     SelectObject( hdcImage, hbm ); // puts bitmap handle into this 					       if( FAILED( lpddsToCopy->GetDC( &hlpddsToCopy ))) // Get DC handle for Surface to Blt too		ThrowEx(__LINE__,__FILE__);				    // Put Bitmap on surface ////////////////////////		if( FAILED( StretchBlt( 		hlpddsToCopy, // handle of destination surface		0, 0,  // (x,y) of destination rect		width, // Width of destination surface		height,// Height of destination surface		hdcImage, // handle of source surface		0, 0, // (x,y) of source surface			width, // Width of source		height,	 // Height of source		SRCCOPY )))        {  		ThrowEx(__LINE__,__FILE__); 	}// end if			lpddsToCopy->ReleaseDC(hlpddsToCopy); // To clean up GetDC(&hlpddsToCopy)        DeleteDC(hdcImage);// To clean up memory device context 		       // hdcImage = CreateCompatibleDC(NULL)}//end copy_bitmap()    


Hope this helps...

I think, therfore I am.
I think?

[edited by - glass_knife on July 30, 2002 4:49:52 PM]

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Ok, I've got it to work and the error detection comes up ok, but when it shows the bitmap, it shows random windows icons like the recycle bin and other stuff like that then after about 2 seconds of that, random lines start coming up, and it's all within the bounds of where I want to blit it to.

Hibiki
Wheres the any key?





find your element
at mutedfaith.com.
<º>


[edited by - hibiki_konzaki on July 30, 2002 7:56:31 PM]
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
Please post some code so I might could figure out what''s going on.

I think, therfore I am.
I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Well, I''m using the first function you gave me and copy_bitmap, and heres my game_main function (it''s not comlpete,I''m just trying to get something to the screen before I start on the game):

Clear_Screen(lpddsback,RGB565(0,0,0));

RECT somerect = {200,200,246,298};

if(!(lpddstest = DDLoadBitmap(lpdd,MAKEINTRESOURCEDB_BITMAP1),46,97)) != NULL)
MessageBox(main_window_handle,"error","load bitmap",NULL);

if(FAILED(lpddsback->
Blt(&somerect,lpddstest,NULL,DDBLT_WAIT,NULL)))
MessageBox(main_window_handle,"error","blit",NULL);

if(FAILED(lpddsprimary->Flip(NULL,DDFLIP_WAIT)))
return(0);

Besides that theres around 450 or more lines of your functions, initialization, and windows stuff, and all that is like basic. The function that I use to create off screen surfaces is the one from TOTWGPG, exept I modified it for DX7.

Hibiki
Wheres the any key?





find your element
at mutedfaith.com.
<º>
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
If you do not correctly make an offscreen surface, or the bitmaps are not loaded correctly, the game will crash. So if everything is still running, then there is a problem with what bitmap you are loading (post code), or how you game loop is set up. If my stuff isn''t set up right, it crashes, but if it works, it always loads the bitmap I want, not windows icons.

I think, therfore I am.
I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This topic is closed to new replies.

Advertisement