473,385 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

bmp//array questions

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
Sep 17 '05 #1
3 4566
Michael Sgier wrote:
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
Sep 17 '05 #2
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
Sep 18 '05 #3
Michael Sgier wrote:
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
Sep 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Joe Six-Pack | last post by:
Hi, Im having problems in randomly selecting an element in an array that has not been selected before.. in other words, I have an array of answers to questions, then I want to select 5, with one...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: Robert | last post by:
Hi, This might be a strange question but i would like to know how to return an array from a function. Do you have to use pointers for this? Thanks in advance, Robert
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
3
by: kurrent | last post by:
i'm still new to php and programming and was hoping to get some help on a few questions i haven't had success in answering myself. I successfully created my first very simple script to accomplish a...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
2
by: Imran | last post by:
Hello all, I am trying to pass 2D array to a func, as follows, it is showing compiler error. void helloptr(int **a){ a = 0xB; } int main(int argc, char* argv){
7
by: lawpoop | last post by:
Hello all - Is there a way to get a nested array in a $_POST variable? I have a form where there are several questions, each one corresponding to a database row. On submission of the form, I...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.