Some simple organization questions.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Some simple organization questions.

Post by twentytortures »

So I'm still pretty new to C++ and have a few questions.

I don't like how all the irrlicht examples are organized. I prefer to have my projects broken up into different functions and files. When trying to do this to some irrlicht examples I have problems when trying to pass variables into functions.

My question:
Would it be bad to use a lot of global variables? At the moment I only have two globals. Would it be bad if I made the majority of my irrlicht related things declared globally?

Thanks.

EDIT: Also, how do I display a number as a string? Do I have to convert it to a string?
Delee
Posts: 18
Joined: Sat Apr 14, 2007 8:36 am

Post by Delee »

What problems do you have passing variables to functions?

It is generally considered bad practice to have a lot of global variables. There are many memory problems involved. In the end, it depends on your organisation of the program - sometimes, you cannot avoid a few global variables. In C++, I think it is common to use data structures to store information and pass a reference to that.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Post by twentytortures »

It's kind of hard for me to describe. Do you know of any examples that shows something like example 4 organized into separate functions?
Rytz
Posts: 249
Joined: Wed Oct 25, 2006 6:05 am
Location: IL, USA
Contact:

Post by Rytz »

twentytortures wrote:It's kind of hard for me to describe. Do you know of any examples that shows something like example 4 organized into separate functions?
One approach would be to create pointers to all of the objects created in "main" in the "MyEventReceiver" class.

That way when you create a "MyEventReceiver" object you can reference all of your different Irrlicht objects by just passing around the "MyEventReceiver" object (pointer).

Whether this is good design or not is up for speulation I guess. If you're not catching my drift I can show you an example.
Image
Delee
Posts: 18
Joined: Sat Apr 14, 2007 8:36 am

Post by Delee »

Not really... for examples I generally prefer everything to be in one function as it is easier to follow. Do you have problems passing Irrlicht objects?

Code: Select all

IrrlichtDevice *device       = createDevice(EDT_DIRECT3D9, dimension2d<s32>(640, 480), 16, false, false, false, &receiver);
IVideoDriver   *videoDriver  = device -> getVideoDriver();
ISceneManager  *sceneManager = device -> getSceneManager();

DoSomething(device, videoDriver, sceneManager);

-

void DoSomething(IrrlichtDevice *device, IVideoDriver *videoDriver, ISceneManager *sceneManager) {
	...
}
Or ...

Code: Select all

vector3df veloc    = ...;
matrix4   position = ...;

Create(veloc, position);

-

void Create(vector3df velocity, matrix4 position) {
	...
}
I hope that helps you out... it is hard to give specific examples without knowing what the problem is.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Better use 'const vector3df&' to avoid passing by copy, though this is only a little performance overhead, no real loss of functionality.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Post by twentytortures »

I usually like to pass data into variables by using a '&' pointer like this:

Code: Select all

void someFunction(int &a)
{
     a = 2;
}
Rather than do it this way:

Code: Select all

int someFunction()
{
     int a;
     a = 2;
     return a;
}
But irrlicht objects are declared using the '*' pointer, does this still work the same way? I.e. would this function work the same as the one above?:

Code: Select all

void someFunction(int *a)
{
     a = 2;
}
I can post a more specific example when I have some more free time.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

yes it works this way, but you have to do it this way:

Code: Select all

void someFunction(int *a)
{
     &a = 2;
}
otherwise you set the pointer (int*) to point to the address 2 and not set the value to 2 !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Acki wrote:yes it works this way, but you have to do it this way
tss, tss... *slaps Acki* - this way is the wrong way ;-)
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

i always use this and it works perfect :|

Code: Select all

void someFunction(int *a) 
{ 
     a = 2; 
}
thephoenix
Posts: 19
Joined: Wed Jan 25, 2006 7:44 pm

Post by thephoenix »

Code: Select all

void someFunction(int *a)
{
     *a = 2;
}
That should be right. ;)
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

thephoenix no. pointer value is adress in a memmory. *a = 2; makes a to point to adress 2 in a memmory and this adress is not valid. do not do that at home ;)
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

no, *a=2 means (the memory value pointed to by a) = 2.
If you don't have anything nice to say, don't say anything at all.
thephoenix
Posts: 19
Joined: Wed Jan 25, 2006 7:44 pm

Post by thephoenix »

Huh?

I definately wanted to do what Luben said.
roxaz:
*var <- that's dereferencing, the same is done by class->somefunction(), which can be written (*class).somefuntion()
With that you'll sure change the value, and not the pointer.
I mean, that's what *I* am using, I don't know your Compiler ;)
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

aaaaaa..... :? whatever. as long as it works for me good there is no problem :?
Post Reply