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;
};
SelectObject Failure?
SelectObject(hdcImage,&hBitmap)
I believe this should be SelectObject(hdcImage, hBitmap)
I believe this should be SelectObject(hdcImage, hBitmap)
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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...)
Instead of "SelectObject(hdcImage,&hBitmap)"
(I''m not sure, but this function take a handle to a bitmap, not a pointer on a handle...)
Changed the "&" so its now:
Still recieve a SelectObject failure
[edited by - Zorbfish on August 29, 2002 6:27:05 PM]
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
Put "if(hBitmap==NULL)"
instead of: "if(hBitmap=NULL)"
If yous set hBitmap to NULL, it become invalid
Good eye, BSP. I would have probably overlooked that over and over. Source update:
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
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++"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement