Dynamic pointers in C

Started by
11 comments, last by pauljg 22 years, 2 months ago
Help !. At the mo I''m writing a little sprite library (for OGL in C) which uses numbers to define the sprite. IE: drawsprite 3,x,y But how about if I wanted to use labels ?, of course the actual name used would depend on the user, so I cant predefine a certain label. This is what I''d like to do: createsprite(ball); .. .. drawsprite(ball,x,y); Of course in my code, ball would still only be a pointer to a INT array, but how can I dynamically create a CHAR pointer string which can change everytime its run ? any ideas ?? or am I stuck with numbers ?
Advertisement
Change ball into a structure containing a char array to hold the string, an int array to hold the coordinates. Make the char array large enough to hold an arbitrary name (char lable[260]; ), or make lable a char pointer and mallocate space for it as needed.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
The only problem with that as far as I can see, is that I can't call it by it's name without checking the entire string everytime.

There could be hundreds of sprites all with different names.

How can I assign it a pointer if the name is not known ?.

In theory:

createsprite(ball);

I'll be passing the CHAR string to my 'createsprite' function and setting the INT values in my sprite structure. But how would I setup the name passed to it as a pointer to a position in the structure Dynamically ?


Edited by - pauljg on February 17, 2002 5:35:27 AM
In that case, maybe use a hash table, storing the key in the first element of the int array.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
So I''d only be checking the hash table code, not the full label ?.

hmmm.. its an idea, but it''s gonna have to do these checks every screen refresh which is gonna effect overall speed.
try having a look up table where you have the names and numbers side by side:

[ball] = 1
[cube] = 2

then have your program search through them:

  // loop through all your objectsfor(int i=0; i < numObjects; i++){   // if this object''s name is the one you are looking for   if(!strcmp(objectNamesArray[i], nameBeingLookedFor))       numberOfObject = i;  // grab it''s number}  


after that you have it''s number so you can proceed as normal.
If that doesn''t work try substituting

drawsprite 3,x,y

for

#define BALL 3
drawsprite BALL,x,y

------------------------------
Baldur K
Great idea, but again I dont think it would be the best option.

You see I want to use its label for other commands.. eg:

rotate ball,ANG

So, I''d be checking the lookup table for every command with every sprite.

And the define method would''nt work as I dont know what label the user wants to call it. You see its a library for C programmers, so I can''t precompile anything.

If I was writing a complete IDE or something I could use the lookup tables, but not for this project.
Yes. What is the source of the string? User input? A file?

Once the string is hashed, use the hash value to index the sprite. The difficulty with a hash is that two strings might hash the same, but there are ways around that. The plus is that a hash is faster than a look up loop under optimal conditions.

Every screen refresh? That''s a lot of checking. I assume you want to provide a lable for each sprite as it moves about?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Sorry, I''m not making it very clear.. my fault.

I''m making a 2d-3d sprite library for OGL, so every sprite must be replotted every screen refresh. (OGL must flush the buffer every loop)

I''ve got a structure called sprite, it can handle 100, with values for x,y.

So, in the users program you''d have:

createsprite(ball.bmp);

This would call my create function that would load up the texture ''ball'' and (hopefully) give it the name BALL, so I can then reference it with its label.

In the main drawing loop, you''d have:

rotatesprite(ball,25); // Rotate 25 deg
drawsprite(ball,5,5); // Draw sprite at position 5,5

all it would be doing is passing the string BALL to the various functions. When they get called they''d know that for instance BALL, is pointing to a certain index in the sprite array.
You are talking about the code actually rewriting itself for different variables declarations... not good. You probably need to use a staggered array of pointers..char *pointers[MAXVALUE] your index number is your reference.. I do not think there is a way to take a string from a file and plug it in to your code with the same variable name as the string... unless you write a program that scans your texture names and plugs in the variable names into your main code before compiling it... that seems a little dangerous though.

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

This topic is closed to new replies.

Advertisement