.bmp loader failing
hey everyone. Please help me. I am trying to finish my .bmp bitmap loader class as part of my game engine, but it is giving me memory errors. For reading in data, I am using ''CreateFile'' and ''ReadFile''. I hope you are familiar with these two I/O functions. Anyway, here is my code:
Header File:
#ifndef _KwamiBitMapBMP_H
#define _KwamiBitMapBMP_H
#include
#include
#include
#include
class KwamiBitMapBMP
{
protected:
struct KwamiBMP
{
BITMAPFILEHEADER bitmapfileheader;
BITMAPINFOHEADER bitmapinfoheader;
unsigned short *bitmaphold;
};
HANDLE file; bool result;
public:
KwamiBitMapBMP();
void LoadBMP(HWND handle,apstring filename);
void UnloadBMP();
unsigned short *bitmapholdtemp; KwamiBMP *bmap; unsigned long *bit;
};
#endif
Source File:
#include "KwamiBitMapBMP.h"
KwamiBitMapBMP::KwamiBitMapBMP()
{
bmap->bitmapfileheader.bfSize=0;
bmap->bitmapfileheader.bfReserved1=0;
bmap->bitmapfileheader.bfReserved2=0;
bmap->bitmapfileheader.bfOffBits=0;
bmap->bitmapfileheader.bfType=0x4D42;
bmap->bitmapinfoheader.biSize=0;
bmap->bitmapinfoheader.biWidth=0;
bmap->bitmapinfoheader.biHeight=0;
bmap->bitmapinfoheader.biPlanes=1;
bmap->bitmapinfoheader.biBitCount=16;
bmap->bitmapinfoheader.biCompression=BI_RGB;
bmap->bitmapinfoheader.biSizeImage=0;
bmap->bitmapinfoheader.biXPelsPerMeter=0;
bmap->bitmapinfoheader.biYPelsPerMeter=0;
bmap->bitmapinfoheader.biClrUsed=0;
bmap->bitmapinfoheader.biClrImportant=0;
bitmapholdtemp=NULL;
bmap->bitmaphold=NULL;
}
void KwamiBitMapBMP::LoadBMP(HWND handle,apstring filename)
{
file=CreateFile(filename.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY,NULL);
if(file==INVALID_HANDLE_VALUE)
MessageBox(handle,"This file or path specified is invalid","KwamiBitMapBMP",MB_OK);
else
{
result=ReadFile(file,&bmap->bitmapfileheader,sizeof(BITMAPFILEHEADER),bit,NULL);
if(result==false)
MessageBox(handle,"Unable to read file properly.","KwamiBitMapBMP",MB_OK);
else
{
if(bmap->bitmapfileheader.bfType!=0x4D42)
MessageBox(handle, "Requested file is not a .bmp file.","KwamiBitMapBMP",MB_OK);
else
{
ReadFile(file,&bmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER),bit,NULL);
if(bmap->bitmapinfoheader.biBitCount==8)
MessageBox(handle,"The KwamiEngine does not support 8bit bitmaps","KwamiBitMapBMP",MB_OK);
else
{
UnloadBMP();
bmap->bitmaphold=(unsigned short*)malloc(bmap->bitmapinfoheader.biSizeImage);
ReadFile(file,&bmap->bitmaphold,bmap->bitmapinfoheader.biSizeImage,bit,NULL);
bitmapholdtemp=bmap->bitmaphold;
}
}
}
}
}
void KwamiBitMapBMP::UnloadBMP()
{
delete bmap->bitmaphold;
delete bitmapholdtemp;
bmap->bitmaphold=NULL;
bitmapholdtemp=NULL;
}
''unsigned long *bit'' is just a temporary pointer that I don''t use, but need for ReadFile().When I try to load in a bitmap in my client program, I get memory errors. I am not sure what I am doing wrong. Please help me. thanks a lot
Edem Attiogbe
The bitmap file format has always caused me trouble for some odd reason (both loading and saving). I recommend that you use FreeImage to load images, or D3DXCreateTextureFromFile or similar DirectX functions.
FreeImage is a free, easy to use image loading and saving library.
FreeImage
FreeImage is a free, easy to use image loading and saving library.
FreeImage
This may not be exactly what you''re looking for, but it works. It''s out of MS IDX book. You may have seen it before:
BOOL Sprite::LoadImage( LPDIRECTDRAWSURFACE lpDDS, LPSTR szImage ){
HBITMAP hbm;
HDC hdcImage = NULL;
HDC hdcSurf = NULL;
BOOL bReturn = FALSE;
DDSURFACEDESC ddsd;
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
if ( FAILED( lpDDS->GetSurfaceDesc( &ddsd ) ) )
{
goto Exit;
}
// If the pixel format isn''t some flavor of RGB,
// we can''t handle it.
if ( ( ddsd.ddpfPixelFormat.dwFlags != DDPF_RGB ) ||
( ddsd.ddpfPixelFormat.dwRGBBitCount < 16 ) )
{
OutputDebugString( "Nonpalettized RGB mode required.\n" );
goto Exit;
}
// Try loading the image.
hbm = ( HBITMAP )::LoadImage( NULL, szImage,
IMAGE_BITMAP, ddsd.dwWidth,
ddsd.dwHeight, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if ( hbm == NULL )
{
OutputDebugString("Couldn''t find the resource.\n" );
goto Exit;
}
// Create a DC and select the image into it.
hdcImage = CreateCompatibleDC( NULL );
SelectObject( hdcImage, hbm );
// Get a DC for the surface.
if ( FAILED( lpDDS->GetDC( &hdcSurf ) ) )
{
OutputDebugString( "Couldn''t get a DC.\n");
goto Exit;
}
// The BitBlt will perform format conversion as necessary.
if ( BitBlt( hdcSurf, 0, 0, ddsd.dwWidth, ddsd.dwHeight,
hdcImage, 0, 0, SRCCOPY ) == FALSE )
{
OutputDebugString( "Blt failed.\n" );
goto Exit;
}
// Success.
bReturn = TRUE;
Exit:
// Clean up everything.
if ( hdcSurf )
lpDDS->ReleaseDC( hdcSurf );
if ( hdcImage )
DeleteDC( hdcImage );
if ( hbm )
DeleteObject( hbm );
return bReturn;
}
void App::ReleaseObjects( void )
{
sprite.Destroy();
if ( lpDD != NULL )
{
if ( lpDDSPrimary != NULL )
{
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
lpDD->Release();
lpDD = NULL;
}
}
BOOL App::Fail( HWND hwnd, char *szMsg )
{
ReleaseObjects();
OutputDebugString( szMsg );
DestroyWindow( hwnd );
return FALSE;
}
BOOL Sprite::LoadImage( LPDIRECTDRAWSURFACE lpDDS, LPSTR szImage ){
HBITMAP hbm;
HDC hdcImage = NULL;
HDC hdcSurf = NULL;
BOOL bReturn = FALSE;
DDSURFACEDESC ddsd;
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
if ( FAILED( lpDDS->GetSurfaceDesc( &ddsd ) ) )
{
goto Exit;
}
// If the pixel format isn''t some flavor of RGB,
// we can''t handle it.
if ( ( ddsd.ddpfPixelFormat.dwFlags != DDPF_RGB ) ||
( ddsd.ddpfPixelFormat.dwRGBBitCount < 16 ) )
{
OutputDebugString( "Nonpalettized RGB mode required.\n" );
goto Exit;
}
// Try loading the image.
hbm = ( HBITMAP )::LoadImage( NULL, szImage,
IMAGE_BITMAP, ddsd.dwWidth,
ddsd.dwHeight, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if ( hbm == NULL )
{
OutputDebugString("Couldn''t find the resource.\n" );
goto Exit;
}
// Create a DC and select the image into it.
hdcImage = CreateCompatibleDC( NULL );
SelectObject( hdcImage, hbm );
// Get a DC for the surface.
if ( FAILED( lpDDS->GetDC( &hdcSurf ) ) )
{
OutputDebugString( "Couldn''t get a DC.\n");
goto Exit;
}
// The BitBlt will perform format conversion as necessary.
if ( BitBlt( hdcSurf, 0, 0, ddsd.dwWidth, ddsd.dwHeight,
hdcImage, 0, 0, SRCCOPY ) == FALSE )
{
OutputDebugString( "Blt failed.\n" );
goto Exit;
}
// Success.
bReturn = TRUE;
Exit:
// Clean up everything.
if ( hdcSurf )
lpDDS->ReleaseDC( hdcSurf );
if ( hdcImage )
DeleteDC( hdcImage );
if ( hbm )
DeleteObject( hbm );
return bReturn;
}
void App::ReleaseObjects( void )
{
sprite.Destroy();
if ( lpDD != NULL )
{
if ( lpDDSPrimary != NULL )
{
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
lpDD->Release();
lpDD = NULL;
}
}
BOOL App::Fail( HWND hwnd, char *szMsg )
{
ReleaseObjects();
OutputDebugString( szMsg );
DestroyWindow( hwnd );
return FALSE;
}
thanks for your help mutex and j_north. j_north, actually, I rather not use the LoadImage function. I rather write my own bitmap loader, but I know that is another alternative. mutex, I checked out that webpage, and it looks good. I just want to understand why my code isn''t working properly, so that I can learn from my mistakes. If you guys or anybody else can look at my code, that would be appriciated. once again, thanks for all your help.
Edem Attiogbe
Edem Attiogbe
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement