PDA

View Full Version : question about using functions and structures...



adityagaddam
09-29-2002, 01:55 PM
ok..

I have a structure..say call it "3dcube"

I have a fucntion..lets call it "makecube(parameters)"

now how can I write the function so that when the user says this:

Makecube(cube1);

the function will make a new "instance" (or whatever they are called) of the struct "3dcube" with the variable name "cube1" (from the MakeCube function)

I hope you guys understood my question..

Thanks,
Aditya

tinak
09-29-2002, 02:35 PM
Not sure whether that way is possible but I would do this as an example:

struct 3dCube
{
various variables
};

void MakeCube(3dCube* newcube)
{
newcube->variable1 = value;
etc...
}

void CreateObjects()
{
3dCube Cube1;
CreateCube(&Cube1);
3dCube Cube2;
CreateCube(&Cube2);
etc...
}

Whether this is something along the lines of what you want I don't know but you still have to create the cube variable before the routine. I don't know of any way of generating a variable after passing it to a function. Perhaps a bit too complex for my knowledge limit so far..

Also, there is the object orientated approach with classes which are similarly created to structs. Whether this allows what you want instead of structs I don't know.

I'm sure class experts can point you in an object orientated direction there.

Tina

adityagaddam
09-29-2002, 02:44 PM
thanks tina.. I was using that workaround.. but I just wanted to wait ans aks to see if a struct "3dcube" can be declared like:

3dcube Cube1;

where "Cube1" would be a name given by the user of the function rather than being declared a struct first...

This is kind of what I want:

struct 3dcube
{
int x;
int y;
int z;
};

void MakeCube(char cubename)
{
3dcube cubename;
}


And in the design mode.. the author would be able to say

MakeCube(Cube1);

And this would create a struct named Cube1 with the struct format of the struct 3dcube.. is it more clear now?

Again, thanks for at least trying to answer my really unclear question,


-Aditya

nexusone
09-29-2002, 04:22 PM
It almost sounds like you want to write a real time drawing program.
Are you wanting to have this as a routine like say glut or glu primitives (sphere, cube).
Or are you wanting to add objects as the user is running the program?

I am still a bit unclear as to what you wanted.

Just like in you now have to add glut.h to your programs to use it.
You would also have to define some type of structure for your routine.

I not sure maybe you could do it the way you want with a pointer... but I have to think on it.



[This message has been edited by nexusone (edited 09-29-2002).]

Deiussum
09-29-2002, 06:30 PM
If you are using C++, you could use the STL map to create a list of objects, and be able to easily access them with a name. It's usage would be something like so:




#include <string>
#include <map>
#include "Object.h"

struct Object
{
// whatever
};
using namespace std;

typedef map<string, Object*> ObjectMap;

ObjectMap g_oObjects;

void CreateObject(string sName)
{
Object* pObj = new Object;

g_oObjects.push_back(sName, pObj);
}

Object* GetObject(string sName)
{
return g_oObjects[sName];
}

adityagaddam
09-30-2002, 02:19 AM
Thanks
I am not really that advanced in c++, but I will look into deciphering that http://www.opengl.org/discussion_boards/ubb/biggrin.gif

Thanks again, and if anyone can give me a simpler solution please try to do so,

-Aditya

Gavin
09-30-2002, 03:21 AM
How are you proposing to refer to each struct? Just by name? You need to know its address in memory to access its variables etc. You could make it a bit more dynamic buy using pointers so that your createcube func returns a pointer to a struct. or maybe you should be looking at someother structures like linked lists or....... where you could make a fun addcube

Robbo
09-30-2002, 03:43 AM
Hey guys, I hate to be a terrible bore, but where is the:

pObj = new THING;

#ifdef __DEBUG
assert ( pObj );
#endif

if ( !pObj )
{
return ( false );
}
else
{
...
}

nexusone
09-30-2002, 04:05 AM
I think the answer is no you can not define a structure per your example.

If you tell us more on what function you are trying to have your program do, we could help point out a solution.

http://www.opengl.org/discussion_boards/ubb/smile.gif



Originally posted by adityagaddam:
Thanks
I am not really that advanced in c++, but I will look into deciphering that http://www.opengl.org/discussion_boards/ubb/biggrin.gif

Thanks again, and if anyone can give me a simpler solution please try to do so,

-Aditya

adityagaddam
10-01-2002, 02:32 AM
Ok this is some pseudo/real code that will probably explain it better...

My files:
1. primitives.h - contains all the functions and stuff for making all the 2d and 3d primitives.

2. main.cpp - this is ofcourse my main file and will bring all the headers and the code together


ok this code should go into primitives.h:

struct 3dcube
{
int x;
int y;
int z;
};


void makecube(char cubename, int x, int y, int z)
{

3dcube cubename (from the function parameters above);

cubename.x = x;
cubename.y = y;
cubename.z = z;


}

Then I should be able to use this in my rendering routine in main.cpp:

makecube(cube1, 0,0,0);

that call to the function should make a 3dcube struct called "cube1"....

is that more clear... if you guys can understand what I asked now, please correct the above pseudo/real code to the correct syntax, if something is not in the correct syntax... Sorry if I am unclear but I dont know how to describe my problem in detail.. hope the above will help you guys to answer my question.

Thanks,
Aditya Gaddam

Deiussum
10-01-2002, 04:48 AM
You can't do that the way you want to, and honestly, I fail to see the reason you would need to.

Your pseudocode just uses a local instance of that struct, which is going to be destroyed when the method exits anyway.

If you think that by using a dynamic name like that is somehow going to let you store multiple instances with different names, then I hate to break it to you, but it don't work like that. You'll need to use some container object to keep a collection, and if you want to name each, also store a name for each. My example code for using an STL map is one way you could do this.

nexusone
10-01-2002, 11:59 AM
Yes... your varaible structures would be put in as your example: primitives.h

Now as for your routines your can have like a primitives.c (your primitive code) or you can create a library file (.lib) to put them in. Note that in C code is not ok to put the function code in the .h files, but in C++ it is ok from what I understand.

primitives.h

typedef struct
{
int x;
int y;
int z;
}cube3d; ///Note You can not have a variable or structure that starts with a number.

primitives.c

void make3dcube(cube3d *cube, int x, int y, int z )
{
cube.x = x;
cube.y = y;
cube.z = z;
}

// main.c

#include "primitives.h"
static cube3d My_cube; // make a place to store the new cube

void main()
{

make3dcube( My_cube, x, y, z);

}

How here is a big question, do you want the cube to be drawn when you call make3dcube?
or you going to have a routine called draw3Ddcube(cube)?


Originally posted by adityagaddam:
Ok this is some pseudo/real code that will probably explain it better...

My files:
1. primitives.h - contains all the functions and stuff for making all the 2d and 3d primitives.

2. main.cpp - this is ofcourse my main file and will bring all the headers and the code together


ok this code should go into primitives.h:

struct 3dcube
{
int x;
int y;
int z;
};


void makecube(char cubename, int x, int y, int z)
{

3dcube cubename (from the function parameters above);

cubename.x = x;
cubename.y = y;
cubename.z = z;


}

Then I should be able to use this in my rendering routine in main.cpp:

makecube(cube1, 0,0,0);

that call to the function should make a 3dcube struct called "cube1"....

is that more clear... if you guys can understand what I asked now, please correct the above pseudo/real code to the correct syntax, if something is not in the correct syntax... Sorry if I am unclear but I dont know how to describe my problem in detail.. hope the above will help you guys to answer my question.

Thanks,
Aditya Gaddam

Deiussum
10-01-2002, 12:09 PM
I think you mis-understood what he wanted. Look at his pseudo-code again.

void makecube(char cubename, int x, int y, int z)
{

3dcube cubename (from the function parameters above);

cubename.x = x;
cubename.y = y;
cubename.z = z;


}


It seems to me that maybe he thinks that if you use the same name of an object in two different places, you are accessing the same object. This is NOT the case if each of the objects is in a different scope. (e.g. two different functions.)

*Aaron*
10-01-2002, 03:12 PM
You can't do this with a function, but you can do it with a macro:


#define makecube(cubename,x,y,z)\
Cube3D cubename = {x,y,z};Of course, the variable 'cubename' would be subject to the same scope rules as other variables. If you want it to be global, you have to call the macro outside of all functions.

Having said this, I don't see the point of using it, and I think you are confusing C/C++ with an interpreted language (Basic, Matlab, etc.). The argument 'cubename' in the above macro is an identifier, not a character string. If you want to get the name of a variable at run time, how do you expect to know how to reference it in your code?

Also, you also can't call the struct 3dCube because identifiers can't begin with a number.


[This message has been edited by *Aaron* (edited 10-01-2002).]

nexusone
10-01-2002, 05:07 PM
I think you missed that he also asked the correct way to do it.

I tried to give an example of how you may create something close. Since he can not do it his way.


Originally posted by Deiussum:
I think you mis-understood what he wanted. Look at his pseudo-code again.

void makecube(char cubename, int x, int y, int z)
{

3dcube cubename (from the function parameters above);

cubename.x = x;
cubename.y = y;
cubename.z = z;


}


It seems to me that maybe he thinks that if you use the same name of an object in two different places, you are accessing the same object. This is NOT the case if each of the objects is in a different scope. (e.g. two different functions.)

Keermalec
10-02-2002, 01:46 AM
Would the following answer your query?

struct cube
{
int x;
int y;
int z;
};

cube* MakeCube ( int X, int Y, int Z )
{
cube *newcube;
newcube.x = X;
newcube.y = Y;
newcude.z = Z;
return newcube;
}

void DrawCube(cube *c)
{
glBegin(GL_QUADS); // right face
glVertex3f(c.x/2,-c.y/2,-c.z/2);
glVertex3f(c.x/2,-c.y/2,c.z/2);
glVertex3f(c.x/2,c.y/2,c.z/2);
glVertex3f(c.x/2,c.y/2,-c.z/2);
glEnd;
glBegin(GL_QUADS); // back face
...
}
// Then your main would look like this

int main ( void)
{
cube *my_cube;
my_cube = MakeCube(1,1,1);
// Insert loop function
DrawCube(my_cube);
// On exiting
free(my_cube)
return 0;
}

adityagaddam
10-02-2002, 02:15 AM
wow! thanks for all your replies guys... I think some of you guys finally understood my stupid and unclear question.. but I haven't gotten what I really need.. at least I dont think so. but I will look at all your solutions. I am currently using the workaround of first declaring a new "cube3d" struct and then passing it as a parameter to the function Makecube(cube3d cube);

Thanks again and if someone does come up with something please post it here.. otherwise, dont waste your time guys.. it is not that important.. i just wanted my engine to be as code-free as possible(for the end user.. not me of course http://www.opengl.org/discussion_boards/ubb/biggrin.gif)

-Aditya

nexusone
10-02-2002, 02:35 AM
Now you need to be careful, just making it simple may limit is usefulness also.

Let's take your idea, GLUT does already does make programming a openGL program easy. But there are limits to what you can do with glut objects.

I think if you are looking to make it simple for the programmer, make commands like this:

add_cube(name, size, x, y, z); adds a cube scene
add_triange(name, size, x, y, z); adds a triangle scene
add_textured_cube(name, char texture_name, x, y, z)


example code:
int add_cube( char *name[], int s, int x, int y, int z)
{
int id_tag;

id_tag = add_object(); // our rendering engine would assign a id number.
// We create an object with the id_tag number, tell it to make a cube.
// Now the object cube is added to our que of objects to be rendered.
create_object( id_tag, CUBE, s, x, y, z);

return( id_tag )// return the address of the new object
}

// then you can have calls like this
Object_move( id_tag, x, y, z);
Object_scale( id_tag, s);
// You could also use a name, instead of id tag
// Look something like this:
Object_move( "name", x, y, z) or

char name[30]; // use variables to store names
Object_move( name, x, y, z)


// How your engine would have a processing routine
Object_engine()
{

if (id_tag_index != 0) Check for objects to process
{
for(i = 0; i < id_tag_index; i++)
{
draw_objects( i ); // draw each object that has been added.
}
Update_display();
}

Hope this gives you an idea....


Originally posted by adityagaddam:
wow! thanks for all your replies guys... I think some of you guys finally understood my stupid and unclear question.. but I haven't gotten what I really need.. at least I dont think so. but I will look at all your solutions. I am currently using the workaround of first declaring a new "cube3d" struct and then passing it as a parameter to the function Makecube(cube3d cube);

Thanks again and if someone does come up with something please post it here.. otherwise, dont waste your time guys.. it is not that important.. i just wanted my engine to be as code-free as possible(for the end user.. not me of course http://www.opengl.org/discussion_boards/ubb/biggrin.gif)

-Aditya

[This message has been edited by nexusone (edited 10-02-2002).]

[This message has been edited by nexusone (edited 10-02-2002).]

Gavin
10-02-2002, 07:17 AM
keermalec

cube* MakeCube ( int X, int Y, int Z )
{
cube *newcube;
newcube.x = X;
newcube.y = Y;
newcude.z = Z;
return newcube;
}

that code hurts!
either do
cube newcube;
or
cube *newcube;
newcube = (cube*) malloc(sizeof(cube));

and also

newcube->x = 123;

Deiussum
10-02-2002, 09:44 AM
He wouldn't want to do "cube newcube;", unless the struct was returned, rather than the pointer to the struct. If you pass back a pointer to a local variable that is not dynamically allocated, you're asking for trouble.

Turbo_Pascal
10-02-2002, 11:41 AM
What i understand is that you want to use severals 3dcube variables but the exact count is defined at runtime, where the final user is able to add more and more new 3dcube variables.

Yes, this is perfectly posible but not in the way that you are trying to do using string names for bind the variables with the code, the "C" language is compiled mean that at run time all variables names used in the code have not meaning anymore, those names are used when the compiler is bulding the EXE file.

For defining new variables at runtime you need to declare a dinamic array of type 3dcube, each time the user want to add a new 3dcube variable then add a new element and keep count how much elements the array has.
In the render rutine you have to draw all elements in the array.


good luck.

Turbo Pascal.

adityagaddam
10-03-2002, 02:15 AM
thanks guys! I will try your idea Turbo Pascal...and thanks for your id_tag idea.. I had something else(something really stupid) in mind for the rendering process.

-Aditya

nexusone
10-03-2002, 04:15 AM
No problem....

http://www.opengl.org/discussion_boards/ubb/smile.gif



Originally posted by adityagaddam:
thanks guys! I will try your idea Turbo Pascal...and thanks for your id_tag idea.. I had something else(something really stupid) in mind for the rendering process.

-Aditya

valmian
10-03-2002, 10:53 AM
Can structures have private or protected members or is it just classes? http://www.opengl.org/discussion_boards/ubb/smile.gif

Robbo
10-03-2002, 10:56 AM
Structures are just classes where all the members are public by default. So of course, you can still use the private\protected\public keywords therein.

valmian
10-03-2002, 11:00 AM
Is there are virtual functions in normal C?
Can structures inherit each other in normal C?

pleopard
10-03-2002, 11:42 AM
Originally posted by valmian:
Is there are virtual functions in normal C?
Can structures inherit each other in normal C?



No

*Aaron*
10-03-2002, 03:31 PM
Also, in C++ structs and classes can have member functions; in C they can only have data members (and there are no classes). This, inheritance, and overloading are the main differences between the two.