# HG changeset patch # User John Tsiombikas # Date 1423089539 -7200 # Node ID b0d8d454c546275a9d66a59c07507f29248717ca # Parent 14e743b532896abb96d587d978305d1a6d6aa981 foo diff -r 14e743b53289 -r b0d8d454c546 src/comp.h --- a/src/comp.h Wed Feb 04 17:23:35 2015 +0200 +++ b/src/comp.h Thu Feb 05 00:38:59 2015 +0200 @@ -1,18 +1,44 @@ #ifndef COMP_H_ #define COMP_H_ +#include #include +class GameObject; + class Component { +protected: + char *name; + GameObject *parent; + int upd_prio; // update priority (0: normal) + public: Component() {} virtual ~Component() {} + + const char *get_name() const; + + virtual void update(); + + bool operator <(const Component &c) const; // for sorting based on priority +}; + +class CompXForm : public Component { +public: + Matrix4x4 xform; + + CompXForm(); }; class CompPRS : public Component { +private: + CompXForm *co_xform; // cached xform component of the parent object + public: Vector3 pos, scale; Quaternion rot; + + void update(); }; #endif // COMP_H_ diff -r 14e743b53289 -r b0d8d454c546 src/gameobj.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gameobj.cc Thu Feb 05 00:38:59 2015 +0200 @@ -0,0 +1,77 @@ +#include +#include "gameobj.h" + +GameObject::GameObject() +{ + sorted = true; +} + +GameObject::~GameObject() +{ + for(size_t i=0; iget_name(); + + if(comp_by_name.find(name) != comp_by_name.end()) { + fprintf(stderr, "component %s already exists in this game object (%p)\n", name, (void*)this); + return false; + } + + try { + comp.push_back(c); + comp_by_name[name] = c; + } + catch(...) { + fprintf(stderr, "failed to add component: %s\n", name); + return false; + } + + sorted = false; + return true; +} + +bool GameObject::remove_component(Component *c) +{ + for(size_t i=0; iget_name()); + return true; + } + } + return false; +} + +bool GameObject::delete_component(Component *c) +{ + if(remove_component(c)) { + delete c; + return true; + } + return false; +} + +Component *GameObject::get_component(const char *name) const +{ + std::map::const_iterator it; + if((it = comp_by_name.find(name)) != comp_by_name.end()) { + return it->second; + } + return 0; +} + +void GameObject::update() +{ + if(!sorted) { + std::sort(comp.begin(), comp.end()); + } + + for(size_t i=0; iupdate(); + } +} diff -r 14e743b53289 -r b0d8d454c546 src/gameobj.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gameobj.h Thu Feb 05 00:38:59 2015 +0200 @@ -0,0 +1,29 @@ +#ifndef GAMEOBJECT_H_ +#define GAMEOBJECT_H_ + +#include +#include +#include +#include "comp.h" + +class GameObject { +private: + std::vector comp; + std::map comp_by_name; + + bool sorted; + +public: + GameObject(); + ~GameObject(); + + bool add_component(Component *c); // takes ownership + bool remove_component(Component *c); + bool delete_component(Component *c); + + Component *get_component(const char *name) const; + + void update(); +}; + +#endif // GAMEOBJECT_H_