Archived

This topic is now archived and is closed to further replies.

branhield

loading bmps..

Recommended Posts

alright. im trying to create a small car-"game"...(not really a game but whatever...) anyways, since loading the bmp inside the render function wich is looped while(!done) will make my application run very slow...but if i load the bmp outside the loop, it will not redraw when i want to move the car, what do i do?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m not sure how your game is set up but what you should do is load your images into memory. Once in memory you can just blt them to the screen whenever you want without reloading them. You only want to load your images once, usually some sort of Game_Init() function.

Share this post


Link to post
Share on other sites
well i am loading the images to some unsigned char* variables:
world = LoadBmp("world.bmp"....);
this is done in my initialize() function....but i need to load two images; the world and a car...and if i do like this:

world = LoadBmp("world.bmp", ....);
car = LoadBmp("car.bmp", ....)

only the car will show up on the screen..know what i mean?

Share this post


Link to post
Share on other sites
Defiently a loader problem, posting some code might help, or take a look at the hundreads of loaders out there. Basically it sounds like your not allocating memory for each bitmap.

Share this post


Link to post
Share on other sites
well heres my loader function...

#define BITMAP_ID 0x4D42

unsigned char *LoadBitmap(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;

filePtr = fopen(filename, "rb");

fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
if(bitmapFileHeader.bfType != BITMAP_ID)
{
MessageBox(NULL, "Requested file is not a bitmap!", "Error",
MB_OK);

fclose(filePtr);
return NULL;
}

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

fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
if(!bitmapImage)
{
MessageBox(NULL, "Failed to allocate memory!", "Error",
MB_OK);

free(bitmapImage);
fclose(filePtr);
return NULL;
}

fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
if(bitmapImage == NULL)
{
fclose(filePtr);
return NULL;
}

for(imageIdx=0; imageIdxbiSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}

fclose(filePtr);
return bitmapImage;
}

Share this post


Link to post
Share on other sites
i noticed one thing; both bitmaps are drawn, but only 32x32 pixels from the world bitmap that is supposed to be 600x600 is drawn...32x32 is the same size as the car bmp. oh well..here goes my init() and render(), enjoy!

void Init()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

bitmapData = LoadBitmap("bana.bmp", &bitmapInfoHeader);
bitmapData2 = LoadBitmap("viper.bmp", &bitmapInfoHeader);
}

void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glRasterPos2i(carX, carY);
glDrawPixels(bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, GL_RGB,
GL_UNSIGNED_BYTE, bitmapData);

glRasterPos2i(100,100);
glDrawPixels(bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, GL_RGB,
GL_UNSIGNED_BYTE, bitmapData2);


glFlush();
SwapBuffers(g_HDC);
}

Share this post


Link to post
Share on other sites