bmp//array questions

M

Michael Sgier

Hello
I want to replace a windows bitmap load function through an equivalent
SDL function.
I should extract the img height etc. and assign it to the terrainTex
array. I've also
three beginner questions:
-Concerning that array why is texID used? terrainTex[0] doesn't state
the first element?
What does terrainTex[0] represent? Especially the "0".
-How do i transfer terrainTex[0] back to the calling terrain.cpp. the
original win function
returned a pointer of the bitmap data. In further functions is always
terrainTex[0] used.
-Why does glGenTextures(1, &terrainTex[0].texID) use &terrainTex and
not *terrainTex?

The declaration is:
CTexture terrainTex[5];

and the CTextureclass:
class CTexture
{
private:
long int scaledWidth;
long int scaledHeight;

unsigned char *palette;

public:
texTypes_t textureType;
unsigned char tgaImageCode; // 0 = not TGA image, 2 = color, 3 =
greyscale

int width;
int height;
int bitDepth;
unsigned int texID;

unsigned char *data;

CTexture() { data = NULL; palette = NULL; }
~CTexture() { Unload(); }

// void LoadTexture(char *filename); // That's the original function
i want to replace
//unsigned char *LoadTex(char* filename, CTexture terrainTex[0]);
//int LoadTex(char* filename);

void Unload()
{
glDeleteTextures(1, &texID);

if (data != NULL)
free(data);
if (palette != NULL)
free(palette);

data = NULL;
palette = NULL;
}
};


// load texture here instead of calling the LoadTex function
// terrainTex[0].LoadTex("pics/ground1.bmp", &terrainTex[0]);
//terrainTex[0].LoadTex("pics/ground.bmp");


SDL_Surface* img;
img = SDL_LoadBMP("pics/ground.bmp");
if (!img) {
printf("Error: %s\n", SDL_GetError());
exit(1);
}
glGenTextures(1, &terrainTex[0].texID);
glBindTexture(GL_TEXTURE_2D, terrainTex[0].texID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//or GL_CLAMP
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//or GL_CLAMP
//
// // switch (terrainTex[0].textureType)
// // {
// // case BMP:
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, terrainTex[0].width,
terrainTex[0].height,
GL_RGB, GL_UNSIGNED_BYTE, terrainTex[0].data);

The following is the original windows bmp functions:
#include "texture.h"

// LoadBitmapFile
// desc: Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *CTexture::LoadBitmapFile(char *filename, BITMAPINFOHEADER
*bitmapInfoHeader)
{
FILE *filePtr; // the file pointer
BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
unsigned char *bitmapImage; // bitmap image data
int imageIdx = 0; // image index counter
unsigned char tempRGB; // swap variable

// open filename in "read binary" mode
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return NULL;

// read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

// verify that this is a bitmap by checking for the universal bitmap id
if (bitmapFileHeader.bfType != BITMAP_ID)
{
fclose(filePtr);
return NULL;
}

// read the bitmap information header
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

// move file pointer to beginning of bitmap data
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

// allocate enough memory for the bitmap image data
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

// verify memory allocation
if (!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}

// read in the bitmap image data
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);

// make sure bitmap image data was read
if (bitmapImage == NULL)
{
fclose(filePtr);
return NULL;
}

// swap the R and B values to get RGB since the bitmap color format is
in BGR
for (imageIdx = 0; imageIdx < (int)bitmapInfoHeader->biSizeImage;
imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}

// close the file and return the bitmap image data
fclose(filePtr);
return bitmapImage;
}

/*****************************************************************************

LoadBitmapFileWithAlpha

Loads a bitmap file normally, and then adds an alpha component to use for
blending
*****************************************************************************/

unsigned char *CTexture::LoadBitmapFileWithAlpha(char *filename,
BITMAPINFOHEADER *bitmapInfoHeader)
{
unsigned char *bitmapImage = LoadBitmapFile(filename, bitmapInfoHeader);
unsigned char *bitmapWithAlpha = (unsigned char
*)malloc(bitmapInfoHeader->biSizeImage * 4 / 3);

// loop through the bitmap data
for (int src = 0, dst = 0; src < bitmapInfoHeader->biSizeImage; src
+=3, dst +=4)
{
// if the pixel is black, set the alpha to 0. Otherwise, set it to
255.
if (bitmapImage[src] == 0 && bitmapImage[src+1] == 0 &&
bitmapImage[src+2] == 0)
bitmapWithAlpha[dst+3] = 0;
else
bitmapWithAlpha[dst+3] = 0xFF;

// copy pixel data over
bitmapWithAlpha[dst] = bitmapImage[src];
bitmapWithAlpha[dst+1] = bitmapImage[src+1];
bitmapWithAlpha[dst+2] = bitmapImage[src+2];
}

free(bitmapImage);

return bitmapWithAlpha;
} // end LoadBitmapFileWithAlpha()


// LoadBMPTexture()
// desc: loads a texture of the BMP format
void CTexture::LoadBMPTexture(char *filename)
{
BITMAPINFOHEADER texInfo; // BMP header

// store BMP data in texture
data = LoadBitmapFileWithAlpha(filename, &texInfo);
if (data == NULL)
{
free(data);
}

// store texture information
width = texInfo.biWidth;
height = texInfo.biHeight;
palette = NULL;
scaledHeight = 0;
scaledWidth = 0;
textureType = BMP;
}

// LoadTexture()
// desc: loads a texture given the filename
void CTexture::LoadTexture(char *filename)
{
char *extStr;

// get extension from filename
extStr = strchr(filename, '.');
extStr++;

// set the texture type based on extension of filename
if ((strcmpi(extStr, "BMP") == 0) || (strcmpi(extStr, "bmp") == 0))
LoadBMPTexture(filename);
else if ((strcmpi(extStr, "PCX") == 0) || (strcmpi(extStr, "pcx") == 0) )
LoadPCXTexture(filename);

else if ((strcmpi(extStr, "TGA") == 0) || (strcmpi(extStr, "tga") == 0) )
LoadTGATexture(filename);
}

THANKS and regards
Michael
 
J

John Harrison

Michael said:
Hello
I want to replace a windows bitmap load function through an equivalent
SDL function.
I should extract the img height etc. and assign it to the terrainTex
array. I've also
three beginner questions:
-Concerning that array why is texID used?

I don't know, clearly it's an ID of some sort. Looks to me like
glGenTextures generates an ID (representing some textures presumably),
and returns it to the caller via a pointer passed as the second
parameter to glGenTextures.

I.e.

unsigned int someID;
glGenTextures(1, &someID);
// now someID will contain the ID of the generated textures

terrainTex[0] doesn't state
the first element?

terrainTex[0] does state the first element.
What does terrainTex[0] represent? Especially the "0".

Well 0 is the number zero. What exactly are you asking here?
terrainTex[0] is the first element of the terrainTex array.
-How do i transfer terrainTex[0] back to the calling terrain.cpp. the
original win function
returned a pointer of the bitmap data. In further functions is always
terrainTex[0] used.

That's hard to answer because you haven't explained clearly where the
terrainTex array is held, is it in some class somewhere? You could
return a pointer to it, that might be the right thing to do.
-Why does glGenTextures(1, &terrainTex[0].texID) use &terrainTex and
not *terrainTex?

&terrainTex[0].texID is a pointer to an ID, as explained above
glGenTextures will assign a value to the ID. I'm not sure why you think
*terrainTex would be better, *terrainTex[0].texID is not legal C++
because txtID is a integer. Perhaps you don't understand what the
expression &terrainTex[0].texID means. It is a pointer to the txtID that
is part of the first element of the terrainTxt array.

Hope this helps,
John
 
M

Michael Sgier

Hello John
-How do i transfer terrainTex[0] back to the calling terrain.cpp. the
original win function
returned a pointer of the bitmap data. In further functions is always
terrainTex[0] used.

That's hard to answer because you haven't explained clearly where the
terrainTex array is held, is it in some class somewhere? You could
return a pointer to it, that might be the right thing to do.

it's declared in:
class CTerrain : public CObject
{
CTexture terrainTex[5];
....
}

Here I pass terrainTex[]:
int CTexture::LoadTex(char* filename, CTexture terrainTex[])

What would that look like if i want also to return terrainTex[]
for further uses in other functions?

I'm also messing things up. I spend a lot of time searching for
functions and their declarations. I think i need some sort of
overview. What do you recommend? UML? any submittals around somewhere?
Like f.ex. terrainTex is declared in CTerrain but it's derived from
CObject...
Thanks and regards
Michael
 
J

John Harrison

Michael said:
Hello John
-How do i transfer terrainTex[0] back to the calling terrain.cpp. the
original win function
returned a pointer of the bitmap data. In further functions is always
terrainTex[0] used.


That's hard to answer because you haven't explained clearly where the
terrainTex array is held, is it in some class somewhere? You could
return a pointer to it, that might be the right thing to do.

it's declared in:
class CTerrain : public CObject
{
CTexture terrainTex[5];
...
}

Here I pass terrainTex[]:
int CTexture::LoadTex(char* filename, CTexture terrainTex[])

This is very strange. You are passing a CTexture array to a CTexture
object, doesn't that strike you as odd? Is CTexture::LoadTex a static
method? That's just about the only thing that would make sense of the above.

Try making CTexture::LoadTex static (put static in front of the
declaration of LoadTex in the header file). If that causes compile
errors then I would worry.
What would that look like if i want also to return terrainTex[]
for further uses in other functions?


You don't need to.

The CTexture array only exists in the CTerrain object. All that
CTexture::LoadText gets is a pointer to that array, so there is no need
to return the array from the CTexture::LoadTex function. Anything that
CTexture::LoadTex does to the array will be reflected in the CTerrain
object that called LoadTek.

I think you're not understanding arrays and pointers. You really need a
very good understanding of this topic to do the kind of thing you are
trying to do.

I'm also messing things up. I spend a lot of time searching for
functions and their declarations. I think i need some sort of
overview. What do you recommend? UML? any submittals around somewhere?
Like f.ex. terrainTex is declared in CTerrain but it's derived from
CObject...

It wouldn't hurt you to try and sketch out your design on paper. From
the little I've seen it looks wrong, but it's hard to offer constructive
advise.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

type declaration 1
array 1
visibility/passing of variables 5
Loading BMP problem. 7
1. Porting win to lin 2
screen capture problem 1
porting win to lin by newbie 1
streams and imported bitmaps 3

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top