getting size of bitmaps
ok, lets say i have this:
=================================================================
ddsd.dwHeight = 10;
ddsd.dwWidth = 10;
if (lpdd->CreateSurface(&ddsd,&lpddsiface[1],NULL)!=DD_OK)
{
dd_kill();
return(0);
}
ddsd.dwHeight = 50;
ddsd.dwWidth = 50;
if (lpdd->CreateSurface(&ddsd,&lpddsiface[2],NULL)!=DD_OK)
{
dd_kill();
return(0);
}
=================================================================
now, because bolth images that i am initing here are of differant sizes...i have to declair "ddsd.whatever = whatever". but, how would i GET the size of the bitmap (height and width) so that i dont have to specificly declair the sizes anymore?
Edited by - gameprogrammerwiz on 6/29/00 5:10:15 PM
This code should do the trick...enjoy.
HBITMAP bit;
bit=(HBITMAP) LoadImage(NULL,BitmapName,IMAGE_BITMAP,0,0,
LR_DEFAULTSIZE/LR_LOADFROMFILE);
// get bitmap dimensions
BITMAP bitmap;
GetObject( bit, sizeof(BITMAP), &bitmap );
Width = bitmap.bmWidth;
Height = bitmap.bmHeight;
-Balrin
HBITMAP bit;
bit=(HBITMAP) LoadImage(NULL,BitmapName,IMAGE_BITMAP,0,0,
LR_DEFAULTSIZE/LR_LOADFROMFILE);
// get bitmap dimensions
BITMAP bitmap;
GetObject( bit, sizeof(BITMAP), &bitmap );
Width = bitmap.bmWidth;
Height = bitmap.bmHeight;
-Balrin
for (index = 0; index < num_iface; index++)
{
GetObject(lpddsiface[index], sizeof(BITMAP), &bmp);
ddsd.dwHeight = bmp.bmWidth;
ddsd.dwWidth = bmp.bmHeight;
if (lpdd->CreateSurface(&ddsd, &lpddsiface[index], NULL)!=DD_OK)
{
dd_kill();
return(0);
}
}
hmmm...it crashed
{
GetObject(lpddsiface[index], sizeof(BITMAP), &bmp);
ddsd.dwHeight = bmp.bmWidth;
ddsd.dwWidth = bmp.bmHeight;
if (lpdd->CreateSurface(&ddsd, &lpddsiface[index], NULL)!=DD_OK)
{
dd_kill();
return(0);
}
}
hmmm...it crashed
Re-read balrin's post as you have your code wrong.
one thing Balrin forgot to say is the BitmapName if the name of the file on the system.
Here is the correct code:
HBITMAP bit;
for (index = 0; index < num_iface; index++)
{
bit=(HBITMAP) LoadImage(NULL,BitmapName,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE/LR_LOADFROMFILE);
// get bitmap dimensions
BITMAP bitmap;
GetObject( bit, sizeof(BITMAP), &bitmap );
ddsd.dwHeight = bmp.bmWidth;
ddsd.dwWidth = bmp.bmHeight;
if (lpdd->CreateSurface(&ddsd, &lpddsiface[index], NULL)!=DD_OK)
{
dd_kill();
return(0);
}
// Clean up so no memory leeks
DeleteObject( bit );
}
Edited by - Steven on June 29, 2000 7:06:03 PM
one thing Balrin forgot to say is the BitmapName if the name of the file on the system.
Here is the correct code:
HBITMAP bit;
for (index = 0; index < num_iface; index++)
{
bit=(HBITMAP) LoadImage(NULL,BitmapName,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE/LR_LOADFROMFILE);
// get bitmap dimensions
BITMAP bitmap;
GetObject( bit, sizeof(BITMAP), &bitmap );
ddsd.dwHeight = bmp.bmWidth;
ddsd.dwWidth = bmp.bmHeight;
if (lpdd->CreateSurface(&ddsd, &lpddsiface[index], NULL)!=DD_OK)
{
dd_kill();
return(0);
}
// Clean up so no memory leeks
DeleteObject( bit );
}
Edited by - Steven on June 29, 2000 7:06:03 PM
ok, i understand what you guys where trying to say now.
but now i have another problem...with the loops:
=================================================================
int game_init(void)
{
int index;
HBITMAP hbm;
BITMAP bmp;
HRESULT rst;
for (index = 0; index < num_iface; index++)
{
hbm = (HBITMAP) LoadImage(NULL, lpddsiface[index], IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE/LR_LOADFROMFILE);
rst = DDCopyBitmap(lpddsiface[index], hbm, 0, 0, 0, 0);
GetObject(hbm, sizeof(BITMAP), &bmp);
ddsd.dwHeight = bmp.bmWidth;
ddsd.dwWidth = bmp.bmHeight;
if (lpdd->CreateSurface(&ddsd, &lpddsiface[index], NULL) != DD_OK)
{
dd_kill();
return(0);
}
DeleteObject(hbm);
}
}
=================================================================
here is there error that has to do with the loop and the LoadImage function:
=================================================================
C:\Windows\Desktop\project\game.cpp(11) : error C2664: 'LoadImageA' : cannot convert parameter 2 from 'struct IDirectDrawSurface *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
=================================================================
any ideas?
Edited by - gameprogrammerwiz on June 29, 2000 7:59:07 PM
but now i have another problem...with the loops:
=================================================================
int game_init(void)
{
int index;
HBITMAP hbm;
BITMAP bmp;
HRESULT rst;
for (index = 0; index < num_iface; index++)
{
hbm = (HBITMAP) LoadImage(NULL, lpddsiface[index], IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE/LR_LOADFROMFILE);
rst = DDCopyBitmap(lpddsiface[index], hbm, 0, 0, 0, 0);
GetObject(hbm, sizeof(BITMAP), &bmp);
ddsd.dwHeight = bmp.bmWidth;
ddsd.dwWidth = bmp.bmHeight;
if (lpdd->CreateSurface(&ddsd, &lpddsiface[index], NULL) != DD_OK)
{
dd_kill();
return(0);
}
DeleteObject(hbm);
}
}
=================================================================
here is there error that has to do with the loop and the LoadImage function:
=================================================================
C:\Windows\Desktop\project\game.cpp(11) : error C2664: 'LoadImageA' : cannot convert parameter 2 from 'struct IDirectDrawSurface *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
=================================================================
any ideas?
Edited by - gameprogrammerwiz on June 29, 2000 7:59:07 PM
OH! C''mon!
Your Code:
hbm = (HBITMAP) LoadImage(NULL, lpddsiface[index], IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE/LR_LOADFROMFILE);
is wrong.
LoadImage does not take a surface parameter.
The second parameter is the filename or resource name.
Correct code is:
hbm = LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION / LR_LOADFROMFILE);
OK! here is the complete code
BOOL load_bmp(LPDIRECTDRAW7 pDD, LPDIRECTDRAWSURFACE7 g_pSurface, char *filename)
{
HBITMAP hbm = 0;
BITMAP bminfo;
DDSURFACEDESC2 ddsd;
HDC hdc, bitmapdc;
if(g_pSurface != NULL) return FALSE;
hbm = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION/LR_LOADFROMFILE);
if (!hbm)
return FALSE;
if (!GetObject(hbm, sizeof(bminfo), &bminfo))
{
if (hbm)
DeleteObject(hbm);
return FALSE;
}
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS / DDSD_HEIGHT / DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = bminfo.bmWidth;
ddsd.dwHeight = bminfo.bmHeight;
if (pDD->CreateSurface(&ddsd, &g_pSurface, NULL) != DD_OK)
{
if (hbm)
DeleteObject(hbm);
return FALSE;
}
g_pSurface->Restore();
bitmapdc = CreateCompatibleDC(NULL);
SelectObject(bitmapdc, hbm);
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_HEIGHT / DDSD_WIDTH;
g_pSurface->GetSurfaceDesc(&ddsd);
g_width = ddsd.dwWidth;
g_height = ddsd.dwHeight;
if (g_pSurface->GetDC(&hdc) == DD_OK)
{
StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, bitmapdc, 0, 0,
g_width, g_height, SRCCOPY);
g_pSurface->ReleaseDC(hdc);
}
DeleteDC(bitmapdc);
DeleteObject(hbm);
return TRUE;
}
By P.P.A. Narayanan (digicrush_ii@rediff.com)
Your Code:
hbm = (HBITMAP) LoadImage(NULL, lpddsiface[index], IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE/LR_LOADFROMFILE);
is wrong.
LoadImage does not take a surface parameter.
The second parameter is the filename or resource name.
Correct code is:
hbm = LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION / LR_LOADFROMFILE);
OK! here is the complete code
BOOL load_bmp(LPDIRECTDRAW7 pDD, LPDIRECTDRAWSURFACE7 g_pSurface, char *filename)
{
HBITMAP hbm = 0;
BITMAP bminfo;
DDSURFACEDESC2 ddsd;
HDC hdc, bitmapdc;
if(g_pSurface != NULL) return FALSE;
hbm = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION/LR_LOADFROMFILE);
if (!hbm)
return FALSE;
if (!GetObject(hbm, sizeof(bminfo), &bminfo))
{
if (hbm)
DeleteObject(hbm);
return FALSE;
}
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS / DDSD_HEIGHT / DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = bminfo.bmWidth;
ddsd.dwHeight = bminfo.bmHeight;
if (pDD->CreateSurface(&ddsd, &g_pSurface, NULL) != DD_OK)
{
if (hbm)
DeleteObject(hbm);
return FALSE;
}
g_pSurface->Restore();
bitmapdc = CreateCompatibleDC(NULL);
SelectObject(bitmapdc, hbm);
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_HEIGHT / DDSD_WIDTH;
g_pSurface->GetSurfaceDesc(&ddsd);
g_width = ddsd.dwWidth;
g_height = ddsd.dwHeight;
if (g_pSurface->GetDC(&hdc) == DD_OK)
{
StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, bitmapdc, 0, 0,
g_width, g_height, SRCCOPY);
g_pSurface->ReleaseDC(hdc);
}
DeleteDC(bitmapdc);
DeleteObject(hbm);
return TRUE;
}
By P.P.A. Narayanan (digicrush_ii@rediff.com)
When you want to get the dimensions of the bitmap, why load it to memory with LoadImage which requires more overhead, when you can just read a BITMAPFILEHEADER structure and then a BITMAPINFOHEADER structure directly from the bmp file? BITMAPINFOHEADER.dwWidth and BITMAPINFOHEADER.dwHeight are the dimensions you want to know.
VBMaster
VBMaster
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement