Archived

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

Dynamic pointers in C

This topic is 5783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 objects

for(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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ok. I see. I think. I don''t understand "it can handle 100, with values for x,y" - 100 what? coordinates, like

struct sprite {
int x[100];
int y[100];
};


or 100 sprites? (eg. sprite array[100]; )

Either way, I would isolate the iterator from the rotate and draw functions. I would write those to work for any sprite and use a completely different function to locate a particular sprite to be passed to rotate and draw.

Share this post


Link to post
Share on other sites
why not get createSprite to return a ''handle'' to the caller then use that in subsequent calls. In your case the handle would either be an int into the array - or what I did with an Ogl texture manager which was to pass a pointer to the data structure back (if you''re using a linked list). ie your calling code would be something like this
  
int hBall;

hBall = createSprite("ball.bmp");

/* and then elsewhere in the code */

rotateSprite(hBall, 25); // why the hell rotate the actual sprite anyway

drawSprite(hBall, 5, 5);


btw. I personally feel that you shouldn''t have a routine to rotate the actual sprite, but rather you should have a routine that can draw the sprite at an angle. eg:
  
drawSpriteRotated(hBall, 5, 5, 25); // draw the ball at xy = (5,5) and at angle 25 deg.


Hope this helps somehow

Share this post


Link to post
Share on other sites
I can see where your coming from, and I like it !.

But..

It''s having to use the

''int hball; ''

at the start. Ideally I''d like the user to be able to use any label they want, perhaps having a dynamic INT in the create process. But it seems C wont let me.

As for the combined rotate instruction.., hmm.. I had to think long & hard about it, but having separate commands means you would''nt have to keep calling it with a rotation value. It would hold a perminent varaible reference.

Share this post


Link to post
Share on other sites