# HG changeset patch # User John Tsiombikas # Date 1423870542 -7200 # Node ID 0e5da17d589ced306767a9ea8dd82450b8d9da33 # Parent 49a2e70ac455cfd69b51c8097facd68c6976cca4 decided to really work on this, so first a slight rename diff -r 49a2e70ac455 -r 0e5da17d589c src/co_phys.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/co_phys.cc Sat Feb 14 01:35:42 2015 +0200 @@ -0,0 +1,20 @@ +#include "co_phys.h" + +static CoRigid reg_co_rigid; + +static Component *cons_rigid() { return new CoRigid; } + +CoRigid::CoRigid() +{ + mass = 1.0; + elast = 0.5; + friction = 0.0; + + name = "rigid"; + + register_component(name, cons_rigid); +} + +void CoRigid::update() +{ +} diff -r 49a2e70ac455 -r 0e5da17d589c src/co_phys.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/co_phys.h Sat Feb 14 01:35:42 2015 +0200 @@ -0,0 +1,17 @@ +#ifndef COMP_PHYS_H_ +#define COMP_PHYS_H_ + +#include +#include "comp.h" + +class CoRigid : public Component { +public: + float mass, elast, friction; + Vector3 pos, vel; + + CoRigid(); + + void update(); +}; + +#endif // COMP_PHYS_H_ diff -r 49a2e70ac455 -r 0e5da17d589c src/co_xform.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/co_xform.cc Sat Feb 14 01:35:42 2015 +0200 @@ -0,0 +1,44 @@ +#include +#include "co_xform.h" +#include "gobj.h" + +static CoXForm reg_co_xform; +static CoPRS reg_co_prs; + +static Component *cons_xform() { return new CoXForm; } +static Component *cons_prs() { return new CoPRS; } + +CoXForm::CoXForm() +{ + name = "xform"; + + register_component(name, cons_xform); +} + +CoPRS::CoPRS() +{ + name = "prs"; + + register_component(name, cons_prs); +} + +void CoPRS::update() +{ + if(!gobj) return; + + if(!co_xform) { + Component *co = gobj->get_component("xform"); + if(!co || !(co_xform = dynamic_cast(co))) { + assert(co_xform); + return; + } + } + + Matrix4x4 rmat = rot.get_rotation_matrix(); + Matrix4x4 tmat, smat; + + tmat.set_translation(pos); + smat.set_scaling(scale); + + co_xform->xform = rmat * tmat * smat; +} diff -r 49a2e70ac455 -r 0e5da17d589c src/co_xform.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/co_xform.h Sat Feb 14 01:35:42 2015 +0200 @@ -0,0 +1,28 @@ +#ifndef CO_XFORM_H_ +#define CO_XFORM_H_ + +#include +#include "comp.h" + +class CoXForm : public Component { +public: + Matrix4x4 xform; + + CoXForm(); +}; + +class CoPRS : public Component { +private: + CoXForm *co_xform; // cached xform component of the parent object + +public: + Vector3 pos, scale; + Quaternion rot; + + CoPRS(); + + void update(); +}; + + +#endif // CO_XFORM_H_ diff -r 49a2e70ac455 -r 0e5da17d589c src/comp_phys.cc --- a/src/comp_phys.cc Tue Feb 10 17:21:30 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -#include "comp_phys.h" - -static CompRigid reg_co_rigid; - -static Component *cons_rigid() { return new CompRigid; } - -CompRigid::CompRigid() -{ - mass = 1.0; - elast = 0.5; - friction = 0.0; - - name = "rigid"; - - register_component(name, cons_rigid); -} - -void CompRigid::update() -{ -} diff -r 49a2e70ac455 -r 0e5da17d589c src/comp_phys.h --- a/src/comp_phys.h Tue Feb 10 17:21:30 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -#ifndef COMP_PHYS_H_ -#define COMP_PHYS_H_ - -#include -#include "comp.h" - -class CompRigid : public Component { -public: - float mass, elast, friction; - Vector3 pos, vel; - - CompRigid(); - - void update(); -}; - -#endif // COMP_PHYS_H_ diff -r 49a2e70ac455 -r 0e5da17d589c src/comp_xform.cc --- a/src/comp_xform.cc Tue Feb 10 17:21:30 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -#include -#include "comp_xform.h" -#include "gameobj.h" - -static CompXForm reg_co_xform; -static CompPRS reg_co_prs; - -static Component *cons_xform() { return new CompXForm; } -static Component *cons_prs() { return new CompPRS; } - -CompXForm::CompXForm() -{ - name = "xform"; - - register_component(name, cons_xform); -} - -CompPRS::CompPRS() -{ - name = "prs"; - - register_component(name, cons_prs); -} - -void CompPRS::update() -{ - if(!gobj) return; - - if(!co_xform) { - Component *co = gobj->get_component("xform"); - if(!co || !(co_xform = dynamic_cast(co))) { - assert(co_xform); - return; - } - } - - Matrix4x4 rmat = rot.get_rotation_matrix(); - Matrix4x4 tmat, smat; - - tmat.set_translation(pos); - smat.set_scaling(scale); - - co_xform->xform = rmat * tmat * smat; -} diff -r 49a2e70ac455 -r 0e5da17d589c src/comp_xform.h --- a/src/comp_xform.h Tue Feb 10 17:21:30 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -#ifndef COMP_XFORM_H_ -#define COMP_XFORM_H_ - -#include -#include "comp.h" - -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; - - CompPRS(); - - void update(); -}; - - -#endif // COMP_XFORM_H_ diff -r 49a2e70ac455 -r 0e5da17d589c src/gameobj.cc --- a/src/gameobj.cc Tue Feb 10 17:21:30 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -#include -#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; - - c->gobj = this; - } - 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 49a2e70ac455 -r 0e5da17d589c src/gameobj.h --- a/src/gameobj.h Tue Feb 10 17:21:30 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -#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_ diff -r 49a2e70ac455 -r 0e5da17d589c src/gobj.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gobj.cc Sat Feb 14 01:35:42 2015 +0200 @@ -0,0 +1,80 @@ +#include +#include +#include "gobj.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; + + c->gobj = this; + } + 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 49a2e70ac455 -r 0e5da17d589c src/gobj.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gobj.h Sat Feb 14 01:35:42 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_