Advertisement

Please help me simplify this code

Started by December 28, 2001 09:10 AM
27 comments, last by Keermalec 23 years, 1 month ago
I don''t think there is a way to do what you''re talking about for a variety of reasons. The biggest though is that C/C++ is compiled all the way down to machine code, meaning its not interpreted like Java, java-script, Perl, etc etc. What you are talking about is basically a type of string substitution. You would like to have:

  for(int i=0; i<5; i++)     Function("MyList"+i);  


That works fine in java-script (and I think in Java, though I''m not sure cause I''ve never tried) because its interpreted. C/C++, are not, so that won''t work. The compiler doesn''t save variable names or anything like that, it just converts them to addresses in memory.

There''s probably a way around it, but I guarantee you it isn''t easy and it would most likely have to be done at the preprocessor level. I would just do an array (that''s what they''re there for);

  LISTTYPE Lists[5];for(int i=0; i<5; i++)     Function(Lists[i]);  


That code will call "Function" on each of the lists in the array "Lists."

If that doesn''t help, let me know.

---Dan
Actually, you''re mistaken in that it is NOT a String you pass to glCallList, it''s an unsigned int (GLuint) containing a number that is the "name" of your list... so you need to store all those numbers in an array somewhere...
GLuint AllLists[5]; 

After that, you store each of your lists in the right position in the array, for instance
AllLists[0] = glGenLists(1);AllLists[1] = glGenLists(1); 

etc... write this before creating each list.
Then to call them :
for(int i=0;i&lt5i++)  glCallList(AllLists[i\); 


--
New feature - Windows users, press ALT-F4 to launch the IQ test.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
Advertisement
Jesus, Dan, and Fox, you are right, the simplest manner is to create an array such as list[5] then I can easily call whichever list I want using a simple for() loop.

I'm disappointed to hear I won't be able to manipulate the code line like a string, as Danman put it. That would be a very powerful feature. I was looking for a printf-type function which, instead of printing out to stdout, would simply use the string as a line of code, as in:

  for ( int i=0; i<num; i++) printf("function (variable%i);", num);  


which the compiler would interpret as:

  function (variable0);function (variable1);function (variable2);function (variable3);function (variable4);  


I will continue looking for such a function.

Edited by - Keermalec on December 28, 2001 2:35:48 PM
common, have an easy solution:

What I think that you want is: Take an action on a large number of lists in one line of code.

well try this then:

void function ( int num )
{
for ( int i = 0; i < num; i++ ) glCallList( list );
}

this is what the function would look like, but you also need an array like:

list[num];

Do you understand?

like

int list[10];

is an array of 10 integers, make an array of a number of lists.

hehe

-xill
This forum didnt write my code like it should be.

<center><table border=1 cellspacing=0 cellpadding=8 bgcolor="#FFFFFF" width="90%"><tr><td><font color=black face="Courier New">
void function ( int num )
{
for ( int i = 0; i < num; i++ ) glCallList ( list );
}
</font></td></tr></center>

-xill
omg this is driving me crazy!!!!!

list should be list
Advertisement
chaos...
Sure Anon, I got that and am using it right now. its only that the problem also appears for things other than display lists, like I said in my last post. btw, type [ source ] before your code and [ \ source ] after to display it right.
What you want to do is actually possible,
because I''ve already done something like that.

You want to mofdify your source code, before compiling it, but
you don''t want to do it yourself, right?

So that nearly cries for the preprocessor.
The preprocessor is used to #include files,
#define macros and ... .

So you could write a macro ( note macro calls in the source code get substituted by the macro code ) that does this job for you.

Hint: look up the ## operator.

But be careful with the preprocessor stuff anyway.

So I hope this helped you, at least a little.
i think what the original poster wanted is something like nehe''s lesson number 16 or 17 does when text is paited using glcalllist
maybe:
int* list;
list = (int*) malloc ((void*)*SIZE_OF_ARRAY);

...

glcalllists (list);

or am i mistaken?

This topic is closed to new replies.

Advertisement