coeng

changeset 5:0e5da17d589c

decided to really work on this, so first a slight rename
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Feb 2015 01:35:42 +0200
parents 49a2e70ac455
children 2f872a179914
files src/co_phys.cc src/co_phys.h src/co_xform.cc src/co_xform.h src/comp_phys.cc src/comp_phys.h src/comp_xform.cc src/comp_xform.h src/gameobj.cc src/gameobj.h src/gobj.cc src/gobj.h
diffstat 12 files changed, 218 insertions(+), 218 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/co_phys.cc	Sat Feb 14 01:35:42 2015 +0200
     1.3 @@ -0,0 +1,20 @@
     1.4 +#include "co_phys.h"
     1.5 +
     1.6 +static CoRigid reg_co_rigid;
     1.7 +
     1.8 +static Component *cons_rigid() { return new CoRigid; }
     1.9 +
    1.10 +CoRigid::CoRigid()
    1.11 +{
    1.12 +	mass = 1.0;
    1.13 +	elast = 0.5;
    1.14 +	friction = 0.0;
    1.15 +
    1.16 +	name = "rigid";
    1.17 +
    1.18 +	register_component(name, cons_rigid);
    1.19 +}
    1.20 +
    1.21 +void CoRigid::update()
    1.22 +{
    1.23 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/co_phys.h	Sat Feb 14 01:35:42 2015 +0200
     2.3 @@ -0,0 +1,17 @@
     2.4 +#ifndef COMP_PHYS_H_
     2.5 +#define COMP_PHYS_H_
     2.6 +
     2.7 +#include <vmath/vmath.h>
     2.8 +#include "comp.h"
     2.9 +
    2.10 +class CoRigid : public Component {
    2.11 +public:
    2.12 +	float mass, elast, friction;
    2.13 +	Vector3 pos, vel;
    2.14 +
    2.15 +	CoRigid();
    2.16 +
    2.17 +	void update();
    2.18 +};
    2.19 +
    2.20 +#endif	// COMP_PHYS_H_
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/co_xform.cc	Sat Feb 14 01:35:42 2015 +0200
     3.3 @@ -0,0 +1,44 @@
     3.4 +#include <assert.h>
     3.5 +#include "co_xform.h"
     3.6 +#include "gobj.h"
     3.7 +
     3.8 +static CoXForm reg_co_xform;
     3.9 +static CoPRS reg_co_prs;
    3.10 +
    3.11 +static Component *cons_xform() { return new CoXForm; }
    3.12 +static Component *cons_prs() { return new CoPRS; }
    3.13 +
    3.14 +CoXForm::CoXForm()
    3.15 +{
    3.16 +	name = "xform";
    3.17 +
    3.18 +	register_component(name, cons_xform);
    3.19 +}
    3.20 +
    3.21 +CoPRS::CoPRS()
    3.22 +{
    3.23 +	name = "prs";
    3.24 +
    3.25 +	register_component(name, cons_prs);
    3.26 +}
    3.27 +
    3.28 +void CoPRS::update()
    3.29 +{
    3.30 +	if(!gobj) return;
    3.31 +
    3.32 +	if(!co_xform) {
    3.33 +		Component *co = gobj->get_component("xform");
    3.34 +		if(!co || !(co_xform = dynamic_cast<CoXForm*>(co))) {
    3.35 +			assert(co_xform);
    3.36 +			return;
    3.37 +		}
    3.38 +	}
    3.39 +
    3.40 +	Matrix4x4 rmat = rot.get_rotation_matrix();
    3.41 +	Matrix4x4 tmat, smat;
    3.42 +
    3.43 +	tmat.set_translation(pos);
    3.44 +	smat.set_scaling(scale);
    3.45 +
    3.46 +	co_xform->xform = rmat * tmat * smat;
    3.47 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/co_xform.h	Sat Feb 14 01:35:42 2015 +0200
     4.3 @@ -0,0 +1,28 @@
     4.4 +#ifndef CO_XFORM_H_
     4.5 +#define CO_XFORM_H_
     4.6 +
     4.7 +#include <vmath/vmath.h>
     4.8 +#include "comp.h"
     4.9 +
    4.10 +class CoXForm : public Component {
    4.11 +public:
    4.12 +	Matrix4x4 xform;
    4.13 +
    4.14 +	CoXForm();
    4.15 +};
    4.16 +
    4.17 +class CoPRS : public Component {
    4.18 +private:
    4.19 +	CoXForm *co_xform;	// cached xform component of the parent object
    4.20 +
    4.21 +public:
    4.22 +	Vector3 pos, scale;
    4.23 +	Quaternion rot;
    4.24 +
    4.25 +	CoPRS();
    4.26 +
    4.27 +	void update();
    4.28 +};
    4.29 +
    4.30 +
    4.31 +#endif	// CO_XFORM_H_
     5.1 --- a/src/comp_phys.cc	Tue Feb 10 17:21:30 2015 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,20 +0,0 @@
     5.4 -#include "comp_phys.h"
     5.5 -
     5.6 -static CompRigid reg_co_rigid;
     5.7 -
     5.8 -static Component *cons_rigid() { return new CompRigid; }
     5.9 -
    5.10 -CompRigid::CompRigid()
    5.11 -{
    5.12 -	mass = 1.0;
    5.13 -	elast = 0.5;
    5.14 -	friction = 0.0;
    5.15 -
    5.16 -	name = "rigid";
    5.17 -
    5.18 -	register_component(name, cons_rigid);
    5.19 -}
    5.20 -
    5.21 -void CompRigid::update()
    5.22 -{
    5.23 -}
     6.1 --- a/src/comp_phys.h	Tue Feb 10 17:21:30 2015 +0200
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,17 +0,0 @@
     6.4 -#ifndef COMP_PHYS_H_
     6.5 -#define COMP_PHYS_H_
     6.6 -
     6.7 -#include <vmath/vmath.h>
     6.8 -#include "comp.h"
     6.9 -
    6.10 -class CompRigid : public Component {
    6.11 -public:
    6.12 -	float mass, elast, friction;
    6.13 -	Vector3 pos, vel;
    6.14 -
    6.15 -	CompRigid();
    6.16 -
    6.17 -	void update();
    6.18 -};
    6.19 -
    6.20 -#endif	// COMP_PHYS_H_
     7.1 --- a/src/comp_xform.cc	Tue Feb 10 17:21:30 2015 +0200
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,44 +0,0 @@
     7.4 -#include <assert.h>
     7.5 -#include "comp_xform.h"
     7.6 -#include "gameobj.h"
     7.7 -
     7.8 -static CompXForm reg_co_xform;
     7.9 -static CompPRS reg_co_prs;
    7.10 -
    7.11 -static Component *cons_xform() { return new CompXForm; }
    7.12 -static Component *cons_prs() { return new CompPRS; }
    7.13 -
    7.14 -CompXForm::CompXForm()
    7.15 -{
    7.16 -	name = "xform";
    7.17 -
    7.18 -	register_component(name, cons_xform);
    7.19 -}
    7.20 -
    7.21 -CompPRS::CompPRS()
    7.22 -{
    7.23 -	name = "prs";
    7.24 -
    7.25 -	register_component(name, cons_prs);
    7.26 -}
    7.27 -
    7.28 -void CompPRS::update()
    7.29 -{
    7.30 -	if(!gobj) return;
    7.31 -
    7.32 -	if(!co_xform) {
    7.33 -		Component *co = gobj->get_component("xform");
    7.34 -		if(!co || !(co_xform = dynamic_cast<CompXForm*>(co))) {
    7.35 -			assert(co_xform);
    7.36 -			return;
    7.37 -		}
    7.38 -	}
    7.39 -
    7.40 -	Matrix4x4 rmat = rot.get_rotation_matrix();
    7.41 -	Matrix4x4 tmat, smat;
    7.42 -
    7.43 -	tmat.set_translation(pos);
    7.44 -	smat.set_scaling(scale);
    7.45 -
    7.46 -	co_xform->xform = rmat * tmat * smat;
    7.47 -}
     8.1 --- a/src/comp_xform.h	Tue Feb 10 17:21:30 2015 +0200
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,28 +0,0 @@
     8.4 -#ifndef COMP_XFORM_H_
     8.5 -#define COMP_XFORM_H_
     8.6 -
     8.7 -#include <vmath/vmath.h>
     8.8 -#include "comp.h"
     8.9 -
    8.10 -class CompXForm : public Component {
    8.11 -public:
    8.12 -	Matrix4x4 xform;
    8.13 -
    8.14 -	CompXForm();
    8.15 -};
    8.16 -
    8.17 -class CompPRS : public Component {
    8.18 -private:
    8.19 -	CompXForm *co_xform;	// cached xform component of the parent object
    8.20 -
    8.21 -public:
    8.22 -	Vector3 pos, scale;
    8.23 -	Quaternion rot;
    8.24 -
    8.25 -	CompPRS();
    8.26 -
    8.27 -	void update();
    8.28 -};
    8.29 -
    8.30 -
    8.31 -#endif	// COMP_XFORM_H_
     9.1 --- a/src/gameobj.cc	Tue Feb 10 17:21:30 2015 +0200
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,80 +0,0 @@
     9.4 -#include <stdio.h>
     9.5 -#include <algorithm>
     9.6 -#include "gameobj.h"
     9.7 -
     9.8 -GameObject::GameObject()
     9.9 -{
    9.10 -	sorted = true;
    9.11 -}
    9.12 -
    9.13 -GameObject::~GameObject()
    9.14 -{
    9.15 -	for(size_t i=0; i<comp.size(); i++) {
    9.16 -		delete comp[i];
    9.17 -	}
    9.18 -}
    9.19 -
    9.20 -bool GameObject::add_component(Component *c)
    9.21 -{
    9.22 -	const char *name = c->get_name();
    9.23 -
    9.24 -	if(comp_by_name.find(name) != comp_by_name.end()) {
    9.25 -		fprintf(stderr, "component %s already exists in this game object (%p)\n", name, (void*)this);
    9.26 -		return false;
    9.27 -	}
    9.28 -
    9.29 -	try {
    9.30 -		comp.push_back(c);
    9.31 -		comp_by_name[name] = c;
    9.32 -
    9.33 -		c->gobj = this;
    9.34 -	}
    9.35 -	catch(...) {
    9.36 -		fprintf(stderr, "failed to add component: %s\n", name);
    9.37 -		return false;
    9.38 -	}
    9.39 -
    9.40 -	sorted = false;
    9.41 -	return true;
    9.42 -}
    9.43 -
    9.44 -bool GameObject::remove_component(Component *c)
    9.45 -{
    9.46 -	for(size_t i=0; i<comp.size(); i++) {
    9.47 -		if(comp[i] == c) {
    9.48 -			comp.erase(comp.begin() + i);
    9.49 -			comp_by_name.erase(c->get_name());
    9.50 -			return true;
    9.51 -		}
    9.52 -	}
    9.53 -	return false;
    9.54 -}
    9.55 -
    9.56 -bool GameObject::delete_component(Component *c)
    9.57 -{
    9.58 -	if(remove_component(c)) {
    9.59 -		delete c;
    9.60 -		return true;
    9.61 -	}
    9.62 -	return false;
    9.63 -}
    9.64 -
    9.65 -Component *GameObject::get_component(const char *name) const
    9.66 -{
    9.67 -	std::map<std::string, Component*>::const_iterator it;
    9.68 -	if((it = comp_by_name.find(name)) != comp_by_name.end()) {
    9.69 -		return it->second;
    9.70 -	}
    9.71 -	return 0;
    9.72 -}
    9.73 -
    9.74 -void GameObject::update()
    9.75 -{
    9.76 -	if(!sorted) {
    9.77 -		std::sort(comp.begin(), comp.end());
    9.78 -	}
    9.79 -
    9.80 -	for(size_t i=0; i<comp.size(); i++) {
    9.81 -		comp[i]->update();
    9.82 -	}
    9.83 -}
    10.1 --- a/src/gameobj.h	Tue Feb 10 17:21:30 2015 +0200
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,29 +0,0 @@
    10.4 -#ifndef GAMEOBJECT_H_
    10.5 -#define GAMEOBJECT_H_
    10.6 -
    10.7 -#include <vector>
    10.8 -#include <map>
    10.9 -#include <string>
   10.10 -#include "comp.h"
   10.11 -
   10.12 -class GameObject {
   10.13 -private:
   10.14 -	std::vector<Component*> comp;
   10.15 -	std::map<std::string, Component*> comp_by_name;
   10.16 -
   10.17 -	bool sorted;
   10.18 -
   10.19 -public:
   10.20 -	GameObject();
   10.21 -	~GameObject();
   10.22 -
   10.23 -	bool add_component(Component *c);	// takes ownership
   10.24 -	bool remove_component(Component *c);
   10.25 -	bool delete_component(Component *c);
   10.26 -
   10.27 -	Component *get_component(const char *name) const;
   10.28 -
   10.29 -	void update();
   10.30 -};
   10.31 -
   10.32 -#endif	// GAMEOBJECT_H_
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/gobj.cc	Sat Feb 14 01:35:42 2015 +0200
    11.3 @@ -0,0 +1,80 @@
    11.4 +#include <stdio.h>
    11.5 +#include <algorithm>
    11.6 +#include "gobj.h"
    11.7 +
    11.8 +GameObject::GameObject()
    11.9 +{
   11.10 +	sorted = true;
   11.11 +}
   11.12 +
   11.13 +GameObject::~GameObject()
   11.14 +{
   11.15 +	for(size_t i=0; i<comp.size(); i++) {
   11.16 +		delete comp[i];
   11.17 +	}
   11.18 +}
   11.19 +
   11.20 +bool GameObject::add_component(Component *c)
   11.21 +{
   11.22 +	const char *name = c->get_name();
   11.23 +
   11.24 +	if(comp_by_name.find(name) != comp_by_name.end()) {
   11.25 +		fprintf(stderr, "component %s already exists in this game object (%p)\n", name, (void*)this);
   11.26 +		return false;
   11.27 +	}
   11.28 +
   11.29 +	try {
   11.30 +		comp.push_back(c);
   11.31 +		comp_by_name[name] = c;
   11.32 +
   11.33 +		c->gobj = this;
   11.34 +	}
   11.35 +	catch(...) {
   11.36 +		fprintf(stderr, "failed to add component: %s\n", name);
   11.37 +		return false;
   11.38 +	}
   11.39 +
   11.40 +	sorted = false;
   11.41 +	return true;
   11.42 +}
   11.43 +
   11.44 +bool GameObject::remove_component(Component *c)
   11.45 +{
   11.46 +	for(size_t i=0; i<comp.size(); i++) {
   11.47 +		if(comp[i] == c) {
   11.48 +			comp.erase(comp.begin() + i);
   11.49 +			comp_by_name.erase(c->get_name());
   11.50 +			return true;
   11.51 +		}
   11.52 +	}
   11.53 +	return false;
   11.54 +}
   11.55 +
   11.56 +bool GameObject::delete_component(Component *c)
   11.57 +{
   11.58 +	if(remove_component(c)) {
   11.59 +		delete c;
   11.60 +		return true;
   11.61 +	}
   11.62 +	return false;
   11.63 +}
   11.64 +
   11.65 +Component *GameObject::get_component(const char *name) const
   11.66 +{
   11.67 +	std::map<std::string, Component*>::const_iterator it;
   11.68 +	if((it = comp_by_name.find(name)) != comp_by_name.end()) {
   11.69 +		return it->second;
   11.70 +	}
   11.71 +	return 0;
   11.72 +}
   11.73 +
   11.74 +void GameObject::update()
   11.75 +{
   11.76 +	if(!sorted) {
   11.77 +		std::sort(comp.begin(), comp.end());
   11.78 +	}
   11.79 +
   11.80 +	for(size_t i=0; i<comp.size(); i++) {
   11.81 +		comp[i]->update();
   11.82 +	}
   11.83 +}
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/gobj.h	Sat Feb 14 01:35:42 2015 +0200
    12.3 @@ -0,0 +1,29 @@
    12.4 +#ifndef GAMEOBJECT_H_
    12.5 +#define GAMEOBJECT_H_
    12.6 +
    12.7 +#include <vector>
    12.8 +#include <map>
    12.9 +#include <string>
   12.10 +#include "comp.h"
   12.11 +
   12.12 +class GameObject {
   12.13 +private:
   12.14 +	std::vector<Component*> comp;
   12.15 +	std::map<std::string, Component*> comp_by_name;
   12.16 +
   12.17 +	bool sorted;
   12.18 +
   12.19 +public:
   12.20 +	GameObject();
   12.21 +	~GameObject();
   12.22 +
   12.23 +	bool add_component(Component *c);	// takes ownership
   12.24 +	bool remove_component(Component *c);
   12.25 +	bool delete_component(Component *c);
   12.26 +
   12.27 +	Component *get_component(const char *name) const;
   12.28 +
   12.29 +	void update();
   12.30 +};
   12.31 +
   12.32 +#endif	// GAMEOBJECT_H_