Show
Ignore:
Timestamp:
03/08/10 22:52:07 (6 months ago)
Author:
gaul@…
Message:

more simplification tests

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/netent_demo/netent.h

    r543 r544  
    11#include <string> 
     2#include <iostream> 
     3#include <ode/ode.h> 
     4 
     5using namespace std; 
     6 
     7 
     8//see below for example 
     9#define ENTLEAF(CLASS, CLASSID, CLASSNAME) \ 
     10                virtual uint16_t get_classid() { return Ent::CLASSID; };\ 
     11                virtual const std::string get_classname() { return CLASSNAME; }; \ 
     12                static Ent* create(Game *game, uint16_t entid) { return new CLASS(game, entid); };  
     13 
     14//always declare constructor as protected! 
     15#define ENTLEAFCON(CLASS, PARENT) \ 
     16                CLASS(Game *game, uint16_t entid) : PARENT(game, entid) {}; 
     17 
     18class Game; 
    219 
    320class Ent { 
    421        protected: //do not instantiate directly 
    5                 Ent(uint16_t _entid) { 
     22                Ent(Game *_game, uint16_t _entid) { 
     23                        game = _game; 
    624                        entid = _entid; 
    725                }; 
    826        private: 
     27                Game *game; 
    928                uint16_t entid; 
    1029        public: 
     
    1534                        PlayerBall 
    1635                } Class; 
    17                 typedef Ent* (*createfct)(uint16_t); 
     36                typedef Ent* (*createfct)(Game *, uint16_t); 
    1837                uint16_t get_entid() { return entid; }; 
    1938                virtual uint16_t get_classid() = 0; 
     
    2140}; 
    2241 
    23 class Obj : public Ent { 
    24         protected:  
    25                 Obj(uint16_t entid) : Ent(entid) {}; 
     42 
     43class Game { 
     44        private: 
     45                map<uint16_t, Ent::createfct> id2ent; 
     46                map<uint16_t, Ent*> ents; 
     47                uint16_t entcnt; 
     48        public: 
     49                Game(); 
     50                virtual Ent *entcreate(uint16_t classid) { 
     51                        Ent::createfct createfct=id2ent[classid]; 
     52                        if (!createfct) { 
     53                                cout << classid << "No createfct! earth-shaking exception would be thrown in production code!" << endl; 
     54                        } 
     55                        Ent *ent = createfct(this, entcnt); 
     56                        if (!ent) { 
     57                                cout << entcnt << "No ent! earth-shaking exception would be thrown in production code!" << endl; 
     58                        } 
     59                        ents[entcnt++]=ent; 
     60                        return ent; 
     61                }; 
     62 
     63                virtual void entdelete(uint16_t entid) { 
     64                        Ent *ent=ents[entid]; 
     65                        if (!ent) { 
     66                                cout << entcnt << "No ent! earth-shaking exception would be thrown in production code!" << endl; 
     67                        } 
     68                        ents.erase(entid); 
     69                        delete ent; 
     70                }; 
     71 
     72                template <class CLASS> CLASS * entcreate(uint16_t classid) { 
     73                        CLASS *ent=dynamic_cast<CLASS *>(entcreate(classid)); 
     74                        if (!ent) { 
     75                                cout << classid << "No createfct! earth-shaking exception would be thrown in production code!" << endl; 
     76                        } 
     77                        return ent; 
     78                }; 
     79 
     80                virtual ~Game() {}; //needed in order to do dynamic_cast<ODEGame *>(Game *) 
    2681}; 
    2782 
    28 class Box : public Obj { 
    29         protected: 
    30                 Box(uint16_t entid) : Obj(entid) {}; 
     83class ODEGame : public Game { 
    3184        public: 
    32                 virtual uint16_t get_classid() { return Ent::Box; }; 
    33                 virtual const std::string get_classname() { return "Box"; }; 
    34                 static Ent* create(uint16_t entid) { return new Box(entid); }; //Ent* seems to be needed 
     85                dWorld world; 
     86                dSimpleSpace space; 
     87                ODEGame() {} ; 
    3588}; 
    3689 
    37 class Pad : public Box { 
     90//non-leaf Ent 
     91class ODEEnt : public Ent { 
     92        private: 
     93                ODEGame *odegame; 
    3894        protected: 
    39                 Pad(uint16_t entid) : Box(entid) {}; 
    40         public: 
    41                 virtual uint16_t get_classid() { return Ent::Pad; }; 
    42                 virtual const std::string get_classname() { return "Pad"; }; 
    43                 static Ent* create(uint16_t entid) { return new Pad(entid); }; 
     95                ODEEnt(Game *game, uint16_t entid) : Ent(game, entid) { 
     96                        odegame = dynamic_cast<ODEGame*>(game); 
     97                        if (!odegame) { 
     98                                //throw earth-shaking exception (since we've been cheated!!!) 
     99                                cout << "earth-shaking exception would be thrown in production code!" << endl; 
     100                        } 
     101                }; 
     102                ODEGame *getodegame() { return odegame; }; 
    44103}; 
    45104 
    46 #define ENTLEAF(CLASS, CLASSID, CLASSNAME) \ 
    47                 virtual uint16_t get_classid() { return Ent::CLASSID; };\ 
    48                 virtual const std::string get_classname() { return CLASSNAME; }; \ 
    49                 static Ent* create(uint16_t entid) { return new CLASS(entid); };  
    50  
    51 //now this is shorter (!): 
    52 class Sphere : public Ent { 
     105//example of non-leaf Ent (note the dGeom* parameter for constructor) 
     106class ODEBody : public ODEEnt { 
     107        private: 
     108                dBody body; 
     109                dGeom *geom; 
    53110        protected: 
    54                 Sphere(uint16_t entid) : Ent(entid) {}; 
    55         public: 
    56                 ENTLEAF(Sphere, Sphere, "Sphere"); 
     111                ODEBody(Game *game, uint16_t entid, dGeom *_geom) : ODEEnt(game, entid) { 
     112                        body.create(getodegame()->world); 
     113                        body.setData(this); 
     114                        geom = _geom; 
     115                        if (!geom) { //we only allow Bodys with a geometry 
     116                                //throw earth-shaking exception (since we've been cheated!!!) 
     117                                cout << entid << "earth-shaking exception would be thrown in production code!" << endl; 
     118                        } 
     119                        getodegame()->space.add(*geom); 
     120                }; 
     121                template <class GEOM> GEOM *getgeom() { 
     122                        return static_cast<GEOM *>(geom); //no way to check since dGeom isnt polymorphic 
     123                } 
     124                //TODO: 
     125                void setposition(dReal x) {}; 
    57126}; 
    58127 
    59 class PBall : public Sphere { 
     128class ODEBox : public ODEBody { 
    60129        protected: 
    61                 PBall(uint16_t entid) : Sphere(entid) {}; 
     130                ODEBox(Game *game, uint16_t entid) : ODEBody(game, entid, new dBox(1.0,1.0,1.0)) {}; 
    62131        public: 
    63                 ENTLEAF(PBall, PlayerBall, "PlayerBall"); 
     132                ENTLEAF(ODEBox, Box, "Box"); 
     133                //TODO: setbox() 
    64134}; 
     135 
     136class ODEPad : public ODEBox { 
     137        protected: 
     138                ENTLEAFCON(ODEPad, ODEBox); 
     139        public: 
     140                ENTLEAF(ODEPad, Pad, "Pad"); 
     141}; 
     142 
     143class ODESphere : public ODEBody { 
     144        protected: 
     145                ODESphere(Game *game, uint16_t entid) : ODEBody(game, entid, new dSphere(1.0)) {}; 
     146        public: 
     147                ENTLEAF(ODESphere, Sphere, "Sphere"); 
     148                void setradius(dReal r) { getgeom<dSphere>()->setRadius(r); }; 
     149                dReal getradius() { return getgeom<dSphere>()->getRadius(); }; 
     150}; 
     151 
     152class ODEPBall : public ODESphere { 
     153        protected: 
     154                ENTLEAFCON(ODEPBall, ODESphere); 
     155        public: 
     156                ENTLEAF(ODEPBall, PlayerBall, "PlayerBall"); 
     157};