Advertisement

proper way to call this function

Started by February 23, 2001 06:42 PM
7 comments, last by omegasyphon 23 years, 11 months ago
i just want to know if im calling this function properly
  

LPDIRECTDRAWSURFACE7	image;

DrawHBitmap(image,"tiles.bmp",0,0,16,16);


void DrawHBitmap(IDirectDrawSurface7 *lpSurface, HBITMAP hbitmap, int x, int y, int width, int height)
{
   HDC           hdcImage;
   HDC           hdc;
   BITMAP        bm;

   if (lpSurface == NULL || hbitmap == NULL)
      return;
   lpSurface->Restore();

   hdcImage = CreateCompatibleDC(NULL);
   SelectObject(hdcImage, hbitmap);

   GetObject(hbitmap, sizeof(bm), &bm);
   width = width == 0 ? bm.bmWidth  : width;
   height = height == 0 ? bm.bmHeight : height;

   lpSurface->GetDC(&hdc);
   BitBlt(hdc, x, y, width, height, hdcImage, 0, 0, SRCCOPY);
   lpSurface->ReleaseDC(hdc);
   DeleteDC(hdcImage);
}
  
For the most part, your function is ok but there's one huge problem that'll make your compiler scream.

Decide whether you want to pass the filename for the bitmap or if you want to pass an HBITMAP structure. They are very different things! One is a string, the other is a structure full of exciting junk. For the bitmap, keep the function declaration as is, otherwise, change it to say "char *szFileName" instead of "HBITMAP hbitmap".

You can include code to load the bitmap into the HBITMAP structure but I'm not sure what it is other than that you use the LoadImage () function. Look it up.

Otherwise, I think it's fine except for a few places where addresses might need to be passed instead of values.




-Goku
SANE Productions Homepage

Edited by - Goku705 on February 23, 2001 9:04:59 PM
Advertisement
ok well i also had another question about a loading bitmap function, everytime i run my program and try to load the bitmap the program just quits upon execution, if i dont try and load the bitmap it works just fine.

  int loadbitmap(char *lpszName, int x, int y, LPDIRECTDRAWSURFACE7 lpdds){	HBITMAP hbitmap = NULL;	BITMAP bmp;	HDC hdcImage, hdcSurface;	// load bitmap resource	if (!(hbitmap = (HBITMAP)LoadImage(main_instance, lpszName,IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))		return(FALSE);	// create device context for image	if (!(hdcImage = CreateCompatibleDC(NULL)))		return(FALSE);	// select bitmap into the DC	if (!(SelectObject(hdcImage, hbitmap)))		return(FALSE);	// get surface device context	if (FAILED(lpdds->GetDC(&hdcSurface)))		return(FALSE);	// get image dimensions	GetObject(hbitmap, sizeof(BITMAP), &bmp);    int dx = bmp.bmWidth;    int dy = bmp.bmHeight;	// copy image to surface	if (!(BitBlt(hdcSurface, x, y, dx, dy, hdcImage, 0, 0, SRCCOPY)))		return(FALSE);		// get rid of all that stuff we used	lpdds->ReleaseDC(hdcSurface);	DeleteDC(hdcImage);	DeleteObject(hbitmap);	return(TRUE);}  
Have you got the image files in the right directory. Sometimes that might not be the one where the exe file is,for example if you are debugging via compiler.
God saw all that he had made. Shit happens sometimes. --the sixth day.
there in the same directory as my project but im not sure if my function is set up right to load a file or resource
im calling it like this

loadbitmap("tiles.bmp",0,0,image);
I think that in the call to LoadImage (), "main_instance" should be replaced with NULL if you want to load from file (as well as the LR_LOADFROMFILE that you''ve got). That may be it, it may not.

Failing that, everywhere you''ve got a "return (FALSE);", stick in a MessageBox () statement. Something like:

MessageBox (hwnd, "LoadImage () failed.", "Error!", MB_OK);

Put one in for each error check with appropriate messages and you''ll find out exactly what''s wrong (down to which function''s failing at least). Better yet would be to handle all of the possible error returns for each but that would be too tedious to set up.



-Goku
SANE Productions Homepage
Advertisement
well changing the main_instance to null didnt work but is this the way i add the message boxes in the function?
if so it didnt work

  int loadbitmap(char *lpszName, int x, int y, LPDIRECTDRAWSURFACE7 lpdds){	HBITMAP hbitmap = NULL;	BITMAP bmp;	HDC hdcImage, hdcSurface;	HWND hwnd;	// load bitmap resource	if (!(hbitmap = (HBITMAP)LoadImage(NULL, lpszName,IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))		return(FALSE);	// create device context for image	if (!(hdcImage = CreateCompatibleDC(NULL)))		return(MessageBox (hwnd, "LoadImage () failed.", "Error!", MB_OK));	// select bitmap into the DC	if (!(SelectObject(hdcImage, hbitmap)))		return(MessageBox (hwnd, "LoadImage () failed.", "Error!", MB_OK));	// get surface device context	if (FAILED(lpdds->GetDC(&hdcSurface)))		return(MessageBox (hwnd, "LoadImage () failed.", "Error!", MB_OK));	// get image dimensions	GetObject(hbitmap, sizeof(BITMAP), &bmp);    int dx = bmp.bmWidth;    int dy = bmp.bmHeight;	// copy image to surface	if (!(BitBlt(hdcSurface, x, y, dx, dy, hdcImage, 0, 0, SRCCOPY)))		return(MessageBox (hwnd, "LoadImage () failed.", "Error!", MB_OK));		// get rid of all that stuff we used	lpdds->ReleaseDC(hdcSurface);	DeleteDC(hdcImage);	DeleteObject(hbitmap);	return(TRUE);}  
well i ditched that function above to the one in ddutil.h and was wondering does anyone here make there own bitmap loading functions or use the one in the ddutil file?
First I used my own bitmap loading function (well, parts of it was written by GA) but then I realised that I needed Jpeg support so I got FreeImage. It works great and it''s free. the guy who programs it just wants to be in the credits if you use the programm comercially... real great tool and it allows me to concentrate on other things than writing a complicated jpeg loader so that the designers are satisfied..
cya,

Phil

Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states

RAW!
Visit Rarebyte! and no!, there are NO kangaroos in Austria (I got this question a few times over in the states ;) )

This topic is closed to new replies.

Advertisement