Archived

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

jwblair

#define fails! Why?

Recommended Posts

I am trying to set up a #define so that I can setup arrays according to the value given by #define. But it fails to compile. This is what I have #define MAPS 2; GLuint texture[MAPS]; When I change it to "GLuint texture[2];" it compiles. I am having the same problems with for loops and what not. Why is this happeing? Also global integers are failing too... Any help would be appreciated! PS: What I am trying to do is to set up a function that will allow me to load a number of texture maps according the value of MAPS. Thanks! John William Blair

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void

Also, I suggest using a Linked List instead of an Array to store textures .



Why? you typically have non-linear access to your textures. Linked lists would be slow.

Share this post


Link to post
Share on other sites
quote:
Original post by sjelkjd

[quote]Original post by Null and Void

Also, I suggest using a Linked List instead of an Array to store textures .



Why? you typically have non-linear access to your textures. Linked lists would be slow.

Actually, you shouldn''t be able to see the small difference between a linked list and an Array. But actually, a Linked List would be pretty much the same speed.



Cyberdrek
Headhunter Soft
A division of DLC Multimedia
Resist Windows XP''s Invasive Product Activation Technology!

Share this post


Link to post
Share on other sites
The way I set it up, I load the textures in to the linked list, and each object has a pointer to the texture that it is using. After all of the objects that are to be rendered are determined, I quicksort them (so that the least amount of state changes are required) and then render them.

It is very fast, and I doubt there is more than a millisecond lost of speed per frame. How do you think professional games store textures? The certainly don''t do it with an array.

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/

Share this post


Link to post
Share on other sites
This doesn't include my other files that are needed to actually use this though (I have a ton of .cpp files in my project, many thousands of lines of code).
    
#include "RILib/win/global.h"

LinkedList <node <TexImage> > Images;

node <TexImage> *CacheImage(char *filename) {
node <TexImage> *nimg = TextureExists(filename);
if(nimg!=NULL) {
return (nimg);
} else {
nimg = Images.AddNode();
nimg->data.path = new char[strlen(filename)+1];
if(LoadTGA(&nimg->data,filename)) {
strcpy(nimg->data.path,filename);
return (nimg);
} else {
return NULL;
}
}
}

node <TexImage> *CacheUniqueImage(char *filename) {
node <TexImage> *nimg = Images.AddNode();
nimg->data.path = new char[strlen(filename)+1];
if(LoadTGA(&nimg->data,filename)) {
strcpy(nimg->data.path,filename);
return (nimg);
} else {
return NULL;
}
}

bool LoadTGA(TexImage *texture, char *filename) {
GLubyte TGAheader[12] = {0,0,2,0,0,0,0,0,0,0,0,0};
GLubyte TGAcompare[12];
GLubyte header[6];
GLuint bytesPerPixel;
GLuint imageSize;
GLuint temp;
GLuint type=GL_RGBA;

FILE *file = fopen(filename,"rb");
if(file==NULL ||
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||
fread(header,1,sizeof(header),file)!=sizeof(header)) {
if(file==NULL) {
return false;
} else {
fclose(file);
return false;
}
}

texture->width = header[1] * 256 + header[0];
texture->height = header[3] * 256 + header[2];
if(texture->width<=0 || texture->height<=0 || (header[4]!=24 && header[4]!=32)) {
fclose(file);
return false;
}

texture->bpp = header[4];
bytesPerPixel = texture->bpp/8;
imageSize = texture->width*texture->height*bytesPerPixel;
texture->imageData = (GLubyte *) malloc(imageSize);
if(texture->imageData==NULL || fread(texture->imageData, 1, imageSize, file)!=imageSize) {
if(texture->imageData!=NULL) free(texture->imageData);
fclose(file);
return false;
}

for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) {
temp = texture->imageData[i];
texture->imageData[i] = texture->imageData[i+2];
texture->imageData[i+2] = temp;
}
fclose(file);

glGenTextures(1,&texture[0].texID);

glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(texture[0].bpp==24) {
type = GL_RGB;
}

glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);
return true;
}

unsigned short AddReference(node <TexImage> *nimg) {
nimg->data.refs++;
return nimg->data.refs;
}

unsigned short DeReference(node <TexImage> *nimg) {
nimg->data.refs--;
if(nimg->data.refs==0) {
delete [] nimg->data.path;
free(nimg->data.imageData);
glDeleteTextures(1,&nimg->data.texID);
Images.DeleteNode(nimg);
return 0;
} else {
return nimg->data.refs;
}
}

node <TexImage> *TextureExists(char *path) {
node <TexImage> *current = Images.head;
while(current!=NULL) {
if(strcmp(path,current->data.path)==0) {
return current;
}
current = current->child;
}
return NULL;
}


"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP's Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/

Edited by - Null and Void on March 15, 2001 11:42:09 PM

Share this post


Link to post
Share on other sites
Also, I have a couple of notes I forgot to mention. I stole the TGA loading code from Nehe, and all of the prototypes (externs) are in global.h (which is fairly large). The linked list class is in framework.cpp (it is huge I couldn''t post it here without feeling bad about wasting the server''s hard drive space ).

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void

The way I set it up, I load the textures in to the linked list, and each object has a pointer to the texture that it is using. After all of the objects that are to be rendered are determined, I quicksort them (so that the least amount of state changes are required) and then render them.

It is very fast, and I doubt there is more than a millisecond lost of speed per frame. How do you think professional games store textures? The certainly don''t do it with an array.


Since OpenGL stores textures as integers, i doubt they even bother, and just include it(the int) as a field with the primative they are rendering.

Share this post


Link to post
Share on other sites
Thanks for the code... If I gave you an email address could you please mail me the complete code that you have for loading textures?

That would be greatly appreciated!

for Now I will look over what is currently here...

Thanks Again!

Thanks!

John William Blair

Share this post


Link to post
Share on other sites
You''re probably write about storing a TexID for each object, I think I could implement that fairly easily actually. It would make unloading textures (using my reference system at least) slower though, I''ll have to think about that.

Jwblair, I''d rather not release my engine''s source code until it is completed, and I would have to release a good portion of it for that texturing code to work since the engine is so blended in to itself. If you go to Nehe''s you can find example code for loading textures (if you already know OpenGL, just skip to the TGA Loading tutorial, that''s what I based my above code off of).

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/

Share this post


Link to post
Share on other sites