About Store Forum Documentation Contact



Post Reply 
func pointer drawPrepare
Author Message
Mardok Offline
Member

Post: #1
func pointer drawPrepare
Please check below sample code A) and tell me what do you think about this.
Whether this method is correct to draw meshes?


A)
Code:
######Vehicle.h#####
STRUCT(Vehicle, Game::Item)
//{

void (*drawPreparePtr) ();

virtual void create(Game::ObjParams &obj);
virtual UInt drawPrepare();
};
######EO Vehicle.h#####

#####Vehicle.cpp#####
void Vehicle::create(Game::ObjParams &obj)
{
(..)

    switch(VEH_TYPE)
    {
        case VEH_POLICE:
            {
                drawPreparePtr = &Vehicle::PoliceDrawPrepare;
            };break;

        case VEH_TAXI:
            {
                drawPreparePtr = &Vehicle::TaxiDrawPrepare;
            };break;
    };

(..)
};


UInt Vehicle::drawPrepare()
{
     ( (*this).*drawPreparePtr ) ();
     return 0;
};


void Vehicle::PoliceDrawPrepare()
{
     mesh->draw.blablbalblablabla
};


void Vehicle::TaxiDrawPrepare()
{
     mesh->draw.blablbalblablabla
};
#####EO Vehicle.cpp#####

OR without pointers, only switch() in drawPrepare

B)
Code:
UInt Vehicle::drawPrepare()
{

    switch(VEH_TYPE)
    {
        case VEH_POLICE:
            {
                mesh-> draw.sadasdasdasdas
            };break;

        case VEH_TAXI:
            {
                mesh->draw.asdasdasdasdas
            };break;

     return 0;
};


Which method is better?
If i have many different vehicles then switch - case is very big and illegible, so pointer is good choice?
01-15-2011 01:33 PM
Find all posts by this user Quote this message in a reply
BlackHornet Offline
Member

Post: #2
RE: func pointer drawPrepare
Better would be using two extended classes any polymorphism:

Code:
struct Vehicle
{
  virtual void drawPrepare() = 0; // abstract here, have to be implemented in subclasses!
};
Code:
STRUCT(Police, Vehicle)
  void drawPrepare()
  {
    mesh-> draw.policeblabla
  }
};
Code:
STRUCT(Taxi, Vehicle)
  void drawPrepare()
  {
    mesh-> draw.taxiblabla
  }
};

Urbanity Online: http://www.facebook.com/pages/Urbanity-Online/162454237136358
Join the Esenthel IRC Channel @ Freenode: http://webchat.freenode.net/?channels=##Esenthel
(This post was last modified: 01-15-2011 03:04 PM by BlackHornet.)
01-15-2011 02:49 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #3
RE: func pointer drawPrepare
yeah, sorted in terms of performance:
1. polymorphism
2. function pointer
3. switch

but to be honest performance difference would be very very small, so just use the one which has best readability and is easy to modify
01-15-2011 03:23 PM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #4
RE: func pointer drawPrepare
(01-15-2011 02:49 PM)BlackHornet Wrote:  Better would be using two extended classes any polymorphism:

Yes, I thought about it but :

Now In my Chr i have only one pointer (*controlledVeh) to Vehicle class and if i'll do like you said then i'll need pointers to any car ex-classes Police, Taxi, Truck (it's bad idea), ... or one pointer and using CAST non stop.

Pointer to Vehicle should be only one so:

Example:
Code:
Vehicle *controlledVeh;

if( Kb.bp(KB_Z) ) controlledVeh->GetIn();
if( Kb.b(KB_W) )  controlledVeh->Accelerate();

if i'll do like you said then:
Code:
Game::Obj *controlledVeh;

if( Kb.b(KB_Z) )CAST(Vehicle,conrolledVeh)->GetIn();
if( Kb.b(KB_W) )CAST(Vehicle,conrolledVeh)->Accelerate();


Using CAST often in my Chr::Update don't slow down my application?
for ex this:

Code:
if( Kb.b(KB_W) )CAST(Vehicle,conrolledVeh)->Accelerate();
????? Is safe? is faster than

Code:
if( Kb.b(KB_W) ) controlledVeh->Accelerate();

If CAST don't slow down my application then i'll stay with polymorphism.

thx for all answers

Esenthel - engine is awesome !
(This post was last modified: 01-15-2011 04:28 PM by Mardok.)
01-15-2011 04:04 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #5
RE: func pointer drawPrepare
If the parent class Vehicle has the functions GetIn() and Accelarate() you dont need to CAST at all. Because all the children classes has the parent functions aswell. Or use virtual functions if you need to add some more functionality to those parent functions.

There is always evil somewhere, you just have to look for it properly.
01-15-2011 04:12 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: func pointer drawPrepare
Code:
struct Vehicle
{
  virtual void drawPrepare() = 0; // abstract here, have to be implemented in subclasses!
};
Code:
STRUCT(Police, Vehicle)
  void drawPrepare()
  {
    mesh-> draw.policeblabla
  }
};
Code:
STRUCT(Taxi, Vehicle)
  void drawPrepare()
  {
    mesh-> draw.taxiblabla
  }
};

struct Chr
{
Vehicle *vehicle;
};
01-15-2011 04:21 PM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #7
RE: func pointer drawPrepare
Woah, great it's powerfull

I'm still learning :]

With your help my game this year will earn millions:]
(This post was last modified: 01-15-2011 04:47 PM by Mardok.)
01-15-2011 04:35 PM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #8
RE: func pointer drawPrepare
Sorry, but i still have a little question:

If i create ex classes ( Police and Taxi ) based on Vehicle then: is it possible to store those objects in one container ?

Code:
Game::ObjMemx<Vehicle> Vehicles;

I have this error: 'Vehicle' : cannot instantiate abstract class ...


whether i'll need two ?

Code:
Game::ObjMemx<Taxi> Taxis;
Game::ObjMemx<Police> Polices;

No errors and work correct but i would like have only one container named Vehicles (for objects Taxi and Police based on Vehicle) is it possible?



:/ I think it's imposible...
http://social.msdn.microsoft.com/forums/...2411effa8f
(This post was last modified: 01-15-2011 11:15 PM by Mardok.)
01-15-2011 10:20 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #9
RE: func pointer drawPrepare
in this case you'd need to use multiple containers
01-15-2011 11:43 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #10
RE: func pointer drawPrepare
@Esenthel what the hell?! You can use multiple objects that inheritance from the same parent class in 1 container... the parent is abstract because you added the = 0; behind the drawPrepare() function.... why just dont make the parent class virtual and add you functionality there?? Cause in every kind of vehicle the drawPrepare() is the same anyway..

There is always evil somewhere, you just have to look for it properly.
(This post was last modified: 01-16-2011 12:38 AM by Dynad.)
01-16-2011 12:09 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #11
RE: func pointer drawPrepare
in containers at 1 time, you can store only 1 type of class
01-16-2011 12:27 AM
Find all posts by this user Quote this message in a reply
Post Reply