Archived

This topic is now archived and is closed to further replies.

SelectObject Failure?

This topic is 5583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


    
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites