Jump to content
  • Advertisement

Archived

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

pauljg

Dynamic pointers in C

This topic is 6057 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
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.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!