C problem with arrays
Greetings.
I am new in OpenGL world and my experience in C/C++ isn''t many, yet things are going well.
I had the following problem:
The texture names are stored in a txt file (textures.lst), and a function called LoadTexturePack will see how many textures are in the lst file. LoadTexturePack runs LoadTexture til all the textures mentioned in the txt file are loaded. The problem comes with loading the textures. I need to allocate space with AUX_RGBImageRec and with TextureImage[] (where the BMP will be loaded). I wanted to allocate only the space that was needed (I am allocating space for 30 textures). How could I do this?
#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input/Output
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The GLaux Library
GLuint texture[29];
AUX_RGBImageRec *TextureImage[30]; // Create storage space for the textures
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) { // If a file name wasn''t passed
return false; // Return false
}
File = fopen(Filename,"r"); // Else open the file
if (File) // If it exists
{
fclose(File); // Close it
return auxDIBImageLoad(Filename); // Load the bitmap and return
}
return false; // Else return false
}
int LoadTexture(int i, char *Filename)
{
int Status=FALSE; // Status Indicator
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap''s Not Found Quit
if (TextureImage=LoadBMP(Filename))
{
glGenTextures(1, &texture); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture);
// Generate The Texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
free(TextureImage->data); // Free The Texture Image Memory
free(TextureImage); // Free The Image Structure
return true;
}
MessageBox(NULL,"Couldn''t find texture",Filename,MB_ICONERROR);
return false;
}
int LoadTexturePack()
{
int c, TNumber;
TNumber = 0;
FILE *pFile;
pFile = fopen("data/textures/textures.lst","r");
char buffer[50];
if (!pFile)
{
printf("textures.lst doesn''t exists\n");
return false;
}
while ((c = getc(pFile)) != EOF) // Find how many textures there are in the lst file
{
if (c == ''\n'') // A carriage return, so… a texture
TNumber++;
}
TNumber++; // Add 1 to compensate the EOF
fseek(pFile,0,SEEK_SET); // Resets the position for fgets
for (int i = 0; i < TNumber; i++) { // Load the texture
fgets(buffer,50,pFile);
if (i != TNumber - 1)
buffer[strlen(buffer)-1] = ''\0'';
LoadTexture(0,buffer);
}
fclose(pFile); // Closes the file
return true;
}
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
LoadTexturePack();
return true;
}
Oh by the way, a quick one:
MessageBox(NULL,"Couldn''t find texture",Error,MB_ICONERROR);
How could I place the filename (char buffer[30]) here?
Thank you for your time.
</i>
This is fortunately not very hard, anyway, you have to hear it once to know it...
I assume you know how to get the total number of textures in your list:
This creates a static array:
AUX_RGBImageRec *TextureImage[30]; // Create storage space for the textures
If you want to allocate only as much mem as you need, you define the above one lke this:
AUX_RGBImageRec *TextureImage = NULL;
When you know how much textures are to be loaded, you simply allocate the amount of mem you need for the array:
TextureImage = (AUX_RGBImageRec*) malloc(sizeof(AUX_RGBImageRec)*NumberOfTextureToLoad);
Then you can use it like you did before, but be careful, there is no check, wether you are still inside your array! If you read beyond the borders, you get at best garbage and if you write, you''ll most likely end up with an access violation.
When you''re done, don''t forget to free the mem:
free(TextureImage);
Hope that helps!
Yesterday we still stood at the verge of the abyss,
today we''re a step onward!
I assume you know how to get the total number of textures in your list:
This creates a static array:
AUX_RGBImageRec *TextureImage[30]; // Create storage space for the textures
If you want to allocate only as much mem as you need, you define the above one lke this:
AUX_RGBImageRec *TextureImage = NULL;
When you know how much textures are to be loaded, you simply allocate the amount of mem you need for the array:
TextureImage = (AUX_RGBImageRec*) malloc(sizeof(AUX_RGBImageRec)*NumberOfTextureToLoad);
Then you can use it like you did before, but be careful, there is no check, wether you are still inside your array! If you read beyond the borders, you get at best garbage and if you write, you''ll most likely end up with an access violation.
When you''re done, don''t forget to free the mem:
free(TextureImage);
Hope that helps!
Yesterday we still stood at the verge of the abyss,
today we''re a step onward!
Yesterday we still stood at the verge of the abyss,today we're a step onward! Don't klick me!!!
And heres the standard addendum to that ;¬)
Malloc, Realloc and Free are available in both C and C++, but C++ also gives you New and Delete. These are pretty much the same, except they automaticly call the constructor and destructor of a class. This means its much safer to use new/delete in C++ progs.
Malloc, Realloc and Free DO give you a little more freedom though, so see if your favourite c++ book has a discussion of the topic, and make your own decision.
Dan
Malloc, Realloc and Free are available in both C and C++, but C++ also gives you New and Delete. These are pretty much the same, except they automaticly call the constructor and destructor of a class. This means its much safer to use new/delete in C++ progs.
Malloc, Realloc and Free DO give you a little more freedom though, so see if your favourite c++ book has a discussion of the topic, and make your own decision.
Dan
[size="1"]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement