Advertisement

SelectObject Failure?

Started by August 29, 2002 04:54 PM
7 comments, last by Zorbfish 22 years, 4 months ago

    
bool BitmapToSurface(LPDIRECTDRAWSURFACE lpSurface,LPSTR szName){

	HBITMAP hBitmap;     // Handle to loaded image

	BITMAP Bitmap;       // A bitmap struct

	HDC hdcImage;   // A device context to hold data in hBitmap

	HDC hdcSurface; // Needed to BitBlt the bitmap to surface


	// Load the bitmap file into the handle


	if(!(hBitmap=(HBITMAP)LoadImage(NULL,szName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION)))
		Log.write("LoadImage failed");

	// If it failed return false


	if(hBitmap=NULL){
		Log.write("Bitmap load failed");
		return false;
	};

	hdcImage=CreateCompatibleDC(NULL);

	// Put the data in a memory dc


	if(!(SelectObject(hdcImage,&hBitmap)))
		Log.write("Select Object failed");

	// Take out the data and put it in Bitmap


	GetObject(hBitmap,sizeof(BITMAP),&Bitmap);

	// Create a offscreen surface that is the height

	// and width of the bitmap


	DDSURFACEDESC ddsd;
	ZeroMemory(&ddsd,sizeof(ddsd));
	ddsd.dwSize=sizeof(ddsd);
	ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
	ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
	ddsd.dwWidth=Bitmap.bmWidth;
	ddsd.dwHeight=Bitmap.mHeight;

	// Check if the direct draw object exists


	if(g_lpDD!=NULL){

		// Now try and create the surface


		if(g_lpDD->CreateSurface(&ddsd,&lpSurface,NULL)!=DD_OK){
			Log.write("CreateSurface failed");
			return false;
		};
	}
	else{
		Log.write("Direct Draw Object not found");
		return false;
	};

	// Get the dc for the surface so we can blit to it


	if(!(lpSurface->GetDC(&hdcSurface))){
		Log.write("GetDC Failed");
		return false;
	};

	// Try and bitblit the bitmap to the surface


	if(!(BitBlt(hdcSurface,0,0,ddsd.dwWidth,ddsd.dwHeight,hdcImage,0,0,SRCCOPY))){
		Log.write("BitBlt failed");
		return false;
	};
	
	// Now if there are still active objects

	// Delete and/or Release them


	if(hdcSurface){
		lpSurface->ReleaseDC(hdcSurface);
		DeleteDC(hdcSurface);
	};

	if(hdcImage)
		DeleteDC(hdcImage);

	if(hBitmap)
		DeleteObject(hBitmap);

	// Seems everything's ok so return true


	return true;
};
    
So when I pass the function call BitmapToSurface(g_lpCharacter,"sprite.bmp") it has always failed. I checked my log file and I notice the problem begins with the SelectObject call. [edited by - Zorbfish on August 29, 2002 5:57:44 PM]
SelectObject(hdcImage,&hBitmap)

I believe this should be SelectObject(hdcImage, hBitmap)
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
Try "SelectObject(hdcImage,hBitmap)"
Instead of "SelectObject(hdcImage,&hBitmap)"
(I''m not sure, but this function take a handle to a bitmap, not a pointer on a handle...)
Oups I''ve posted too late sorry...
Changed the "&" so its now:

  bool BitmapToSurface(LPDIRECTDRAWSURFACE lpSurface,LPSTR szName){	HBITMAP hBitmap;     // Handle to loaded image	// BITMAP Bitmap;       // A bitmap struct	HDC hdcImage;   // A device context to hold data in hBitmap	HDC hdcSurface; // Needed to BitBlt the bitmap to surface	// Load the bitmap file into the handle	if(!(hBitmap=(HBITMAP)LoadImage(NULL,szName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION)))		Log.write("LoadImage failed");	// If it failed return false	if(hBitmap=NULL){		Log.write("Bitmap load failed");		return false;	};	hdcImage=CreateCompatibleDC(NULL);	// Put the data in a memory dc	if(!SelectObject(hdcImage,hBitmap))		Log.write("Select Object failed");	// Take out the data and put it in Bitmap	// GetObject(hBitmap,sizeof(BITMAP),&Bitmap);	// Create a offscreen surface that is the height	// and width of the bitmap	DDSURFACEDESC ddsd;	ZeroMemory(&ddsd,sizeof(ddsd));	ddsd.dwSize=sizeof(ddsd);	ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;	ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;	ddsd.dwWidth=64;	ddsd.dwHeight=32;	// Check if the direct draw object exists	if(g_lpDD!=NULL){		// Now try and create the surface		if(g_lpDD->CreateSurface(&ddsd,&lpSurface,NULL)!=DD_OK){			Log.write("CreateSurface failed");			return false;		};	}	else{		Log.write("Direct Draw Object not found");		return false;	};	// Get the dc for the surface so we can blit to it	if(!(lpSurface->GetDC(&hdcSurface))){		Log.write("GetDC Failed");		return false;	};	// Try and bitblit the bitmap to the surface	if(!(BitBlt(hdcSurface,0,0,ddsd.dwWidth,ddsd.dwHeight,hdcImage,0,0,SRCCOPY))){		Log.write("BitBlt failed");		return false;	};		// Now if there are still active objects	// Delete and/or Release them	if(hdcSurface){		lpSurface->ReleaseDC(hdcSurface);		DeleteDC(hdcSurface);	};	if(hdcImage)		DeleteDC(hdcImage);	if(hBitmap)		DeleteObject(hBitmap);	// Seems everything's ok so return true	return true;};  


Still recieve a SelectObject failure


[edited by - Zorbfish on August 29, 2002 6:27:05 PM]
I think:

Put "if(hBitmap==NULL)"
instead of: "if(hBitmap=NULL)"

If yous set hBitmap to NULL, it become invalid
Advertisement
Good eye, BSP. I would have probably overlooked that over and over. Source update:


  bool BitmapToSurface(LPDIRECTDRAWSURFACE lpSurface,LPSTR szName){	HBITMAP hBitmap;     // Handle to loaded image	BITMAP Bitmap;       // A bitmap struct	HDC hdcImage;   // A device context to hold data in hBitmap	HDC hdcSurface; // Needed to BitBlt the bitmap to surface	// Load the bitmap file into the handle	if(!(hBitmap=(HBITMAP)LoadImage(NULL,szName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION)))		Log.write("LoadImage failed");	// If it failed return false	if(hBitmap==NULL){		Log.write("Bitmap load failed");		return false;	}	else{		Log.write("Bitmap load sucess");	};	hdcImage=CreateCompatibleDC(NULL);	if(hdcImage){		Log.write("HDC for the Bitmap is ready");	};	// Put the data in a memory dc	if(!SelectObject(hdcImage,hBitmap))		Log.write("Select Object failed");	// Take out the data and put it in Bitmap	if(!GetObject(hBitmap,sizeof(BITMAP),&Bitmap))		Log.write("Get object failed");	// Create a offscreen surface that is the height	// and width of the bitmap	DDSURFACEDESC ddsd;	ZeroMemory(&ddsd,sizeof(ddsd));	ddsd.dwSize=sizeof(ddsd);	ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;	ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;	ddsd.dwWidth=(int)Bitmap.bmWidth;	ddsd.dwHeight=(int)Bitmap.bmHeight;	// Check if the direct draw object exists	if(g_lpDD!=NULL){		// Now try and create the surface		if(g_lpDD->CreateSurface(&ddsd,&lpSurface,NULL)!=DD_OK){			Log.write("CreateSurface failed");			return false;		};	}	else{		Log.write("Direct Draw Object not found");		return false;	};	// Get the dc for the surface so we can blit to it	if(FAILED(lpSurface->GetDC(&hdcSurface))){		Log.write("GetDC Failed");		return false;	};	// Try and bitblit the bitmap to the surface	if(!(BitBlt(hdcSurface,0,0,ddsd.dwWidth,ddsd.dwHeight,hdcImage,0,0,SRCCOPY))){		Log.write("BitBlt failed");		return false;	};		// Now if there are still active objects	// Delete and/or Release them	if(hdcSurface){		lpSurface->ReleaseDC(hdcSurface);		DeleteDC(hdcSurface);	};	if(hdcImage)		DeleteDC(hdcImage);	if(hBitmap)		DeleteObject(hBitmap);	// Seems everything''s ok so return true	return true;};  


So far Ive fixed the two syntax errors and now I get a failure @ GetDC(); Im assuming I am doing something wrong when I set these values:

ddsd.dwWidth=Bitmap.bmWidth
ddsd.dwHeight=Bitmap.bmHeight
So what does the compiler say?
"...and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces."----------Scott Meyers, "Effective C++"
Well that''s the wierd part. The compiler says there''s nothing wrong with my code but when I execute the program ddraw starts up but all I have is a black screen (I have code for blt to backbuffer and flip so no thats not the problem)

This topic is closed to new replies.

Advertisement