• 12
• 9
• 10
• 13
• 10

# this bitmap function I have isn't working..

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

## 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;
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;
}

{
fclose(filePtr);
MessageBox(NULL, _T("Not a bitmap"), _T("Harvest Engine"), MB_ICONWARNING);
return NULL;
}

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

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 on other sites
Mostly it means the file can't be located. But maybe you simply don't have read permission?!

##### 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

and see if that works.

##### Share on other sites
Quote:
 Original post by greksterWhat 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 usingunsigned 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 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 on other sites
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.

##### Share on other sites
Quote:
 Original post by Oni Sephirothhaha, 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 :)