Advertisement

loading bitmaps with DDraw

Started by July 30, 2002 12:57 AM
15 comments, last by hibiki_konzaki 22 years, 3 months ago
It starts up and runs until escape is pressed like it should. The bitmap is ok because it loaded with my other program that uses the ddutil.cpp/h class. Here is my surface creation function, I don't think theres anything wrong with it.


  LPDIRECTDRAWSURFACE7 DDraw_Create_Surface(int width, int height,									  int mem_flags){	DDSURFACEDESC2 ddsd;	LPDIRECTDRAWSURFACE7 lpdds;	DDRAW_INIT_STRUCT(ddsd);	ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;	ddsd.dwWidth = width;	ddsd.dwHeight = height;	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | mem_flags;	if(FAILED(lpdd->CreateSurface(&ddsd,&lpdds,NULL)))		return (NULL);	DDCOLORKEY color_key;	color_key.dwColorSpaceLowValue = 0;	color_key.dwColorSpaceHighValue = 0;	lpdds->SetColorKey(DDCKEY_SRCBLT, &color_key);	return(lpdds);}   


I call this then I call the bitmap loading function in my startup code.

And here is the bitmap loading stuff.


  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);	DeleteObject(hbm);	return pdds;}void DDCopyBitmap(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__);	hdcImage = CreateCompatibleDC( NULL );	//if( hdcImage == NULL )//		ThrowEx(__LINE__,__FILE__);	SelectObject( hdcImage, hbm ); // puts bitmap handle into this	   // Compatible DC for use in StretchBlt    	if( FAILED( lpddsToCopy->GetDC( &hlpddsToCopy ))) // Get DC handle for Surface to Blt too			//	ThrowEx(__LINE__,__FILE__);		MessageBox(main_window_handle,"error","error",NULL);	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 )))	{  // SRCCOPY: Copies the source rectangle directly 	// to the destination rectangle//		ThrowEx(__LINE__,__FILE__);	}// end if	lpddsToCopy->ReleaseDC(hlpddsToCopy); // To clean up GetDC(&hlpddsToCopy)	DeleteDC(hdcImage);// To clean up memory device context	// hdcImage = CreateCompatibleDC(NULL)}  


Hibiki
Wheres the any key?





find your element
at mutedfaith.com.
<º>


[edited by - Hibiki_Konzaki on July 31, 2002 9:12:54 AM]
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
Thanks for posting the code, it really helps...


this is how you called the function:
if(!(lpddstest = DDLoadBitmap(lpdd,MAKEINTRESOURCEDB_BITMAP1),46,97)) != NULL)

Here is the prtotype:
IDirectDrawSurface7* DDLoadBitmap( IDirectDraw7* pdd, LPCSTR szBitmap, int dx, int dy)

what is MAKEINTRESOURCEDB_BITMAP1?
a define, and if so, what is it?

try
lpddstest = DDLoadBitmap(&lpdd, "bitmap.bmp",46,97);
if(FAILED(lpddstest))
error;

there are extra ()'s in the call, and they way you're checking for a null hurts my head

if this isn't it, let me know. We'll get it.

I think, therfore I am.
I think?

[edited by - glass_knife on July 31, 2002 4:15:28 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

Advertisement
Ok, I was using MAKEINT.... to try to load the bitmap as a resource. IDB_BITMAP1 is the bitmap''s define.

Ok, I change the MAKEINT... stuff to the string, and it still does the same thing. Maybe I''m doing something wrong with the blitter..


  int Game_Main(void *parms = NULL, int num_parms = 0){	Clear_Screen(lpddsback,RGB565(0,0,0));	RECT somerect = {1,1,47,98};	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);	return(1);}int Game_Init(void *parms = NULL, int num_parms = 0){	ShowCursor(false);	if (FAILED(DirectDrawCreateEx(NULL, (LPVOID*)&lpdd,								IID_IDirectDraw7, NULL)))		return(0);	if (FAILED(lpdd->SetCooperativeLevel(main_window_handle, DDSCL_EXCLUSIVE |								DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT)))		return(0);	if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,								0,0)))		return(0);	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 = 1;	if (FAILED(lpdd->CreateSurface(&ddsd,&lpddsprimary, NULL)))		return(0);	ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;	if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps,&lpddsback)))		return(0);	if (!(lpddstest = DDraw_Create_Surface(46,97,DDSCAPS_VIDEOMEMORY)))		MessageBox(main_window_handle,"error","create surface",NULL);	if(FAILED(DirectInput8Create(hinstance_app,DIRECTINPUT_VERSION,IID_IDirectInput8W, (void**)&lpdi,NULL)))		return(0);	if(FAILED(lpdi->CreateDevice(GUID_SysKeyboard,&lpdikeyboard,NULL)))		return(0); 	if(FAILED(lpdikeyboard->SetDataFormat(&c_dfDIKeyboard)))		return(0);	if(FAILED(lpdikeyboard->SetCooperativeLevel(main_window_handle,								DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))		return(0);	if(FAILED(lpdikeyboard->Acquire()))		return(0);	if(FAILED(lpddclipper = DDraw_Attach_Clipper(lpddsback,1,&list)))		return(0);	lpddstest = DDLoadBitmap(lpdd,"NiggaLeft(1).bmp",46,97);	if(FAILED(lpddstest))		MessageBox(main_window_handle,"error","load bitmap",NULL);	return(1);}  


Clear_Screen() is just a function I made that fills the given surface with the given color.

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.<º>
I see a mistake, and it's my fault...
If the load bitmap worked it returns DD_OK.
When I tried this:
lpddsBase = dd->load_bitmap_file("playingarea002.bmp");
if(FAILED(lpddsBase))
{
ERROR
}

I got the error, because Failed returns true if it's not DD_OK.
And lpddsBase is != DD_OK

I changed it to (if lpddsBase == NULL), and it loaded just
fine.

This just in...I just recreated your bug!!! My bitmaps looked all funky, and I know why.

Also, in your DDLoadBitmap, you are doing this:
DDCopyBitmap(pdds, hbm, 0, 0);
The 0,0 are the width and hight of the source and destination.
They cannot be 0; This is what caused it.

solution 1:
take out the stretchblt, and put in a blt instead;

solution 2:
call the fn() like this
DDCopyBitmap(pdds, hbm, bm.bmWidth, bm.bmHeight);

Let me know...


I think, therfore I am.
I think?

[edited by - glass_knife on July 31, 2002 6:17:40 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 changed the call to the copy_bitmap function to:

DDCopyBitmap(pdds, hbm, bm.bmWidth, bm.bmHeight);

and... it works! thanks man!

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.<º>
Glad I could help...
How much programming have you done, and what kind of game are you working 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

Advertisement
I''ve been using C++ for about a year now, I''ve made a partially completed text RPG, some stuff for TI-83 claculators (in both TI83 BASIC and Z80 Assembly) and a pong game in VC++ and DX just using filled squares and stuff. Right now I''m working on a 2-d side scrolling shooter. Basically you go around different levels and shoot people. Not really much of a story, but I''m also not a pro dev team with a million dollar budget

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.<º>

This topic is closed to new replies.

Advertisement