[Solved] Optimization and Device

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.
Post Reply
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

[Solved] Optimization and Device

Post by Iyad »

Only a simple question :
Which is best? Passing succefully the device in each (necessary) function call or simply create a new device in each class and call it when needed?
Ex.1 :
Passing device:

Code: Select all

#include "XClass.h"
...
IrrlichtDevice* Device = createDevice...
XClass* X = new XClass();
XClass->MakeSomething(Device);
XClass->MakeSomethingElse(Device);
...
Ex.2 :
Create New Device for each class:
(in XClass.h)

Code: Select all

#ifndef XClass_H
#define XClass_H

class XClass {
  public :
     XClass(irr::IrrlichtDevice* Device_);
     virtual ~XClass();

     void MakeSomething();
     void MakeSomethingElse();
     
   private :
     irr::IrrlichtDevice* XDevice;
};

#endif /XClass_H
(In XClass.cpp)

Code: Select all

XClass::XClass(IrrlichtDevice* Device_){

XDevice = Device_;

}

void XClass::MakeSomething(){

XDevice->UseTHIS_DEVICE();

}

void XClass::MakeSomethingElse(){

XDevice->UseTHIS_DEVICE_FOR_SOMETHING_ELSE();

}
...
Thanks for your advices.
Last edited by Iyad on Sat May 29, 2010 1:20 am, edited 1 time in total.
#include <Iyad.h>
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

what you call in this case "create device for each class" is "storing the device pointer inside the class" (as you always use the same pointer in the whole class) !!! :roll:

well, storing the pointer would be a tiny little bit faster though... ;)
and in this case I would use the storing methode...

passing a pointer to the function(s) would be neccessary if you use different pointers, e.g. if you have 2 or more scene managers you'd pass the pointer of the actualy needed scene manager to the function(s)...
but as long as you only have one device (in your example) the funtion(s) only can use this one and so there's no need to pass it to the function(s), so you can store the pointer... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

Post by Iyad »

But if use alot of classes, and I'm make tons of different Device pointers in each different class, wouldn't it be using a lot of memory. Its all a question of memory.

Thanks for answering.
#include <Iyad.h>
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

no, a pointer is just a long integer value... ;)
it will cost more memory (temporaly) if you pass the pointer to each function, bc. the pointer will be copied on each call...
but even then it is always just a long integer and this is a realy minimal memory usage... ;)

remember you pass/use a pointer not the whole instance !!!
a pointer just "points" to the memory where the instance is located !!!
the instance itself is only stored once in memory !!! ;)
Last edited by Acki on Sat May 29, 2010 1:29 am, edited 4 times in total.
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

Post by Iyad »

I think I should go re-read the pointer part in my C++ book.
Thanks alot man.
#include <Iyad.h>
Post Reply