
Loading dynamically textures
Hi to everyone.
I'm trying to load some variables from a text file,
Here is the code:
Extracted from Nehe Lesson10, i have modified the code to fullfill my needs, here is the url.
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=10
Here is the code, first of all i would like to say i'm new on programming.
//********************************************//
// This code Works //
int numtexturas;
FILE *filein;
char oneline[255];
filein = fopen("data/texturas.txt", "rt"); // File To Load World Data From
readstr(filein,oneline);
sscanf(oneline, "TEXTURAS: %d\n", &numtexturas);
logg.Print("Nº de Texturas: %d -- OK", numtexturas);
AUX_RGBImageRec *TextureImage[2];
// This code Works //
//*********************************************//
I would like to do the following:
AUX_RGBImageRec *TextureImage[2]; -> AUX_RGBImageRec *TextureImage[numtexturas];
Change the number of Textureimage[n] with the number extracted from the text file.
I tried to do that in VC++7, but i give me several compilation errors.
Thanks you to read this post.
[edited by - eduardor2k on October 30, 2003 5:26:57 PM]
[edited by - eduardor2k on October 30, 2003 5:27:36 PM]
[edited by - eduardor2k on October 30, 2003 5:28:02 PM]

Thank you for reading this post, but if you can answer it better. :)
October 30, 2003 04:37 PM
Basically, what you''re trying to do is create a dynamically sized array. The way in which you are attempting to do it will not work. When creating an array, the value between the brackets, [n], must be a constant value. This means it must be one of the following:
- an actual number (such as 5)
- defined through the preprocessor (such as #define ARRAY_SIZE 5)
- created as a constant (const array_size = 5)
You would need to do something like the following:
AUX_RGBImageRec *TextureImage = new AUX_RGBImageRec[numTextures];
where numTextures is the number of textures you''ve loaded from the file.
After you''re done with the textures, most likely at the end of the program, you need to do the following to free the memory you''ve allocated:
delete [] TextureImage;
- an actual number (such as 5)
- defined through the preprocessor (such as #define ARRAY_SIZE 5)
- created as a constant (const array_size = 5)
You would need to do something like the following:
AUX_RGBImageRec *TextureImage = new AUX_RGBImageRec[numTextures];
where numTextures is the number of textures you''ve loaded from the file.
After you''re done with the textures, most likely at the end of the program, you need to do the following to free the memory you''ve allocated:
delete [] TextureImage;
Thank you for replying this post.
I tried to do what you said.
But when i compile the program, it gives me a lot of compilation errors.
Because of:
AUX_RGBImageRec *TextureImage = new AUX_RGBImageRec[numtexturas];
Identificator error "TextureImage".
And others messages error.
Thank you to read this post.
I tried to do what you said.
But when i compile the program, it gives me a lot of compilation errors.
Because of:
AUX_RGBImageRec *TextureImage = new AUX_RGBImageRec[numtexturas];
Identificator error "TextureImage".
And others messages error.
Thank you to read this post.
Thank you for reading this post, but if you can answer it better. :)
The best way I know to make dynamic arrays is to create a pointer to it. Example:
x = 5;
char *temp;
temp = new char[x];
This code works all the time with me. The only catch is you need to free the memory after you are finished by useing:
delete temp;
somewhere when your program is quitting.
x = 5;
char *temp;
temp = new char[x];
This code works all the time with me. The only catch is you need to free the memory after you are finished by useing:
delete temp;
somewhere when your program is quitting.
Dreams arn't just dreams, They're a whole new world to play in.
Thank you.
I will try what you said.
[edited by - eduardor2k on October 31, 2003 5:38:39 PM]
I will try what you said.
[edited by - eduardor2k on October 31, 2003 5:38:39 PM]
Thank you for reading this post, but if you can answer it better. :)
October 31, 2003 04:43 PM
holy crap, just do this (might be old school, but never fails)
AUX_RGBImageRec *TextureImage;
//then get the numtexturas from a file
TextureImage =
(AUX_RGBImageRec *)malloc(sizeof(AUX_RGBImageRec)*numtexturas);
the malloc function stands for memory allocate, & it lets you set up an array of ANYTHING dynamically & passes a pointer back to you, & that is what an array is, a pointer. Notice you pass the size of "ANYTHING" into malloc & multiply it by the number of elements in your array. you also have to cast the return value of malloc as the pointer that you need.
good luck
AUX_RGBImageRec *TextureImage;
//then get the numtexturas from a file
TextureImage =
(AUX_RGBImageRec *)malloc(sizeof(AUX_RGBImageRec)*numtexturas);
the malloc function stands for memory allocate, & it lets you set up an array of ANYTHING dynamically & passes a pointer back to you, & that is what an array is, a pointer. Notice you pass the size of "ANYTHING" into malloc & multiply it by the number of elements in your array. you also have to cast the return value of malloc as the pointer that you need.
good luck
Sorry.
I''m new on this.
I will try to practise a little bit more, before trying to solve this problem.
Thank to everyone who replied this post, it''s nice to see people interested answering our doubts.
Thanks Again.
I''m new on this.
I will try to practise a little bit more, before trying to solve this problem.
Thank to everyone who replied this post, it''s nice to see people interested answering our doubts.
Thanks Again.
Thank you for reading this post, but if you can answer it better. :)
October 31, 2003 05:00 PM
quote:
Original post by Steelrose
The best way I know to make dynamic arrays is to create a pointer to it. Example:
x = 5;
char *temp;
temp = new char[x];
This code works all the time with me. The only catch is you need to free the memory after you are finished by useing:
delete temp;
somewhere when your program is quitting.
Freeing the memory in this manner will not work. When you allocate an array, like so:
temp = new char[x];
you MUST delete it like this:
delete [] temp;
quote:
Original post by Anonymous Poster
holy crap, just do this (might be old school, but never fails)
AUX_RGBImageRec *TextureImage;
//then get the numtexturas from a file
TextureImage =
(AUX_RGBImageRec *)malloc(sizeof(AUX_RGBImageRec)*numtexturas);
the malloc function stands for memory allocate, & it lets you set up an array of ANYTHING dynamically & passes a pointer back to you, & that is what an array is, a pointer. Notice you pass the size of "ANYTHING" into malloc & multiply it by the number of elements in your array. you also have to cast the return value of malloc as the pointer that you need.
good luck
This method will work as well (you could also use realloc). However, any time that you dynamically create memory (through new, malloc, calloc, or realloc) you need to free the memory once you''re done with it. Freeing the memory is easy, but the way you free the memory is based on how you allocated it:
new must be freed with delete
malloc, calloc, or realloc must be freed with free()
new is the C++ method of allocating memory, while the others are the C methods. One of the main differences is the fact that new calls a class'' constructor (and delete the destructor). malloc, calloc, realloc and free, however, will not make these calls.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement