Sign in to follow this  

this bitmap function I have isn't working..

This topic is 3311 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

I pulled a function for loading bitmaps out of my textbook..and it doesn't work. The file open is failing.
unsigned char * HVSTGFX::loadBitmap(char *fileName, BITMAPINFOHEADER *bitmapinfoheader)
{
	FILE *filePtr;
	BITMAPFILEHEADER bitmapFileHeader;
	unsigned char *bitmapImage;
	int imageIdx = 0;
	unsigned char tempRGB;

	filePtr = fopen(fileName, "rb");
	if (filePtr == NULL)
	{
		MessageBox(NULL, _T("file open failed"), _T("Harvest Engine"), MB_ICONWARNING);
		return NULL;
	}

	fread (&bitmapFileHeader, sizeof (BITMAPFILEHEADER), 1, filePtr);

	if (bitmapFileHeader.bfType != BITMAP_ID)
	{
		fclose(filePtr);
		MessageBox(NULL, _T("Not a bitmap"), _T("Harvest Engine"), MB_ICONWARNING);
		return NULL;
	}

	fread(bitmapinfoheader, sizeof (BITMAPINFOHEADER), 1, filePtr);

	fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

	bitmapImage = (unsigned char*)malloc(bitmapinfoheader->biSizeImage);

	if (!bitmapImage)
	{
		free (bitmapImage);
		MessageBox(NULL, _T("memory not reserved"), _T("Harvest Engine"), MB_ICONWARNING);
		fclose(filePtr);
		return NULL;
	}

	fread(bitmapImage, 1, bitmapinfoheader->biSizeImage, filePtr);

	if (bitmapImage == NULL)
	{
		MessageBox(NULL, _T("image not loaded properly"), _T("Harvest Engine"), MB_ICONWARNING);
		fclose(filePtr);
		return NULL;
	}

	for (imageIdx = 0; imageIdx < bitmapinfoheader->biSizeImage; imageIdx += 3)
	{
		tempRGB = bitmapImage[imageIdx];
		bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
		bitmapImage[imageIdx + 2] = tempRGB;
	}

	fclose(filePtr);
	return bitmapImage;
}
This is the definition, the file open failed message box is popping up. I'm calling it like so with global variables in my windows code:
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char * test = HVSTGFX::loadBitmap("test.bmp", &bitmapInfoHeader);

Share this post


Link to post
Share on other sites
What have you actually tried yourself to fix this problem? Apart from just posting here obviously.

"test.bmp" probably isn't in the working directory. Try putting it at c:\test.bmp and using

unsigned char * test = HVSTGFX::loadBitmap("c:/test.bmp", &bitmapInfoHeader);

and see if that works.

Share this post


Link to post
Share on other sites
Quote:
Original post by grekster
What have you actually tried yourself to fix this problem? Apart from just posting here obviously.

"test.bmp" probably isn't in the working directory. Try putting it at c:\test.bmp and using

unsigned char * test = HVSTGFX::loadBitmap("c:/test.bmp", &bitmapInfoHeader);

and see if that works.


Not much other than moving the file around. Anyway, I moved it to the C drive and did what you said. Well, the messageboxes that I put into the function aren't showing up, so something went right, but it still isn't drawing the image. I've got this for my main loop:

while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{

if (GetAsyncKeyState(VK_LEFT))
xc -=0.001f;

if (GetAsyncKeyState(VK_RIGHT))
xc += 0.001f;

if (GetAsyncKeyState(VK_UP))
yc +=0.001f;

if (GetAsyncKeyState(VK_DOWN))
yc -=0.001f;

if (GetAsyncKeyState(VK_RETURN))
PostQuitMessage(0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glColor3f(1.0f,0.0f,0.0f);
glTranslatef(xc, yc, zc);
glBegin(GL_QUADS);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(.3f,0.0f,0.0f);
glVertex3f(.3f,.3f,0.0f);
glVertex3f(0.0f,.3f,0.0f);
glEnd();

glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glRasterPos2i(100,100);
glDrawPixels(bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, GL_RGB, GL_UNSIGNED_BYTE, test);

glPopMatrix();
SwapBuffers(g_HDC);

TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


The quad that I specified is working as is the keyboard input, it just isn't drawing the image anywhere.

Share this post


Link to post
Share on other sites
Obviously my subtle hint that you should try to fix this yourself failed so here we go....


TRY FIXING IT YOURSELF!

Joking aside if you don't make the effort to debug your own problems your never going to improve as a programmer. And just posting here expecting people to fix problems for you with no effort on your part is not going to help you either.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oni Sephiroth
haha, funny enough, I posted another thread about this since I thought this one died. I tried some different window coding after a few days of looking around, works fine now.


Good for you :)

Share this post


Link to post
Share on other sites

This topic is 3311 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this