coeng
changeset 2:4a1c9597f4d3
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 05 Feb 2015 11:04:07 +0200 |
parents | b0d8d454c546 |
children | 66d1762eb203 |
files | .hgignore Makefile src/comp.cc src/comp.h src/comp_xform.cc src/comp_xform.h src/gameobj.cc src/test.cc |
diffstat | 8 files changed, 149 insertions(+), 23 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Thu Feb 05 11:04:07 2015 +0200 1.3 @@ -0,0 +1,4 @@ 1.4 +\.o$ 1.5 +\.swp$ 1.6 +\.d$ 1.7 +^test$
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/Makefile Thu Feb 05 11:04:07 2015 +0200 2.3 @@ -0,0 +1,19 @@ 2.4 +ccsrc = $(wildcard src/*.cc) 2.5 +obj = $(ccsrc:.cc=.o) 2.6 +bin = test 2.7 + 2.8 +CXXFLAGS = -pedantic -Wall -g 2.9 +LDFLAGS = $(libgl) -lm -lvmath 2.10 + 2.11 +ifeq ($(shell uname -s), Darwin) 2.12 + libgl = -framework OpenGL -framework GLUT -lGLEW 2.13 +else 2.14 + libgl = -lGL -lGLU -lglut -lGLEW 2.15 +endif 2.16 + 2.17 +$(bin): $(obj) 2.18 + $(CXX) -o $@ $(obj) $(LDFLAGS) 2.19 + 2.20 +.PHONY: clean 2.21 +clean: 2.22 + rm -f $(obj) $(bin)
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/comp.cc Thu Feb 05 11:04:07 2015 +0200 3.3 @@ -0,0 +1,45 @@ 3.4 +#include <map> 3.5 +#include <string> 3.6 +#include "comp.h" 3.7 + 3.8 +static std::map<std::string, Component *(*)()> comp_cons; 3.9 + 3.10 +Component::Component() 3.11 +{ 3.12 + name = "unknown"; 3.13 + gobj = 0; 3.14 + upd_prio = 0; 3.15 +} 3.16 + 3.17 +Component::~Component() 3.18 +{ 3.19 +} 3.20 + 3.21 +const char *Component::get_name() const 3.22 +{ 3.23 + return name; 3.24 +} 3.25 + 3.26 +void Component::update() 3.27 +{ 3.28 +} 3.29 + 3.30 +bool Component::operator <(const Component &c) const 3.31 +{ 3.32 + return upd_prio < c.upd_prio; 3.33 +} 3.34 + 3.35 + 3.36 +void register_component(const char *name, Component *(*cons_func)()) 3.37 +{ 3.38 + comp_cons[name] = cons_func; 3.39 +} 3.40 + 3.41 +Component *create_component(const char *name) 3.42 +{ 3.43 + Component *(*cons)() = comp_cons[name]; 3.44 + if(cons) { 3.45 + return cons(); 3.46 + } 3.47 + return 0; 3.48 +}
4.1 --- a/src/comp.h Thu Feb 05 00:38:59 2015 +0200 4.2 +++ b/src/comp.h Thu Feb 05 11:04:07 2015 +0200 4.3 @@ -2,43 +2,29 @@ 4.4 #define COMP_H_ 4.5 4.6 #include <string> 4.7 -#include <vmath/vmath.h> 4.8 4.9 class GameObject; 4.10 4.11 class Component { 4.12 protected: 4.13 - char *name; 4.14 - GameObject *parent; 4.15 + const char *name; 4.16 + GameObject *gobj; 4.17 int upd_prio; // update priority (0: normal) 4.18 4.19 public: 4.20 - Component() {} 4.21 - virtual ~Component() {} 4.22 + Component(); 4.23 + virtual ~Component(); 4.24 4.25 const char *get_name() const; 4.26 4.27 virtual void update(); 4.28 4.29 bool operator <(const Component &c) const; // for sorting based on priority 4.30 + 4.31 + friend class GameObject; 4.32 }; 4.33 4.34 -class CompXForm : public Component { 4.35 -public: 4.36 - Matrix4x4 xform; 4.37 - 4.38 - CompXForm(); 4.39 -}; 4.40 - 4.41 -class CompPRS : public Component { 4.42 -private: 4.43 - CompXForm *co_xform; // cached xform component of the parent object 4.44 - 4.45 -public: 4.46 - Vector3 pos, scale; 4.47 - Quaternion rot; 4.48 - 4.49 - void update(); 4.50 -}; 4.51 +void register_component(const char *name, Component *(*cons_func)()); 4.52 +Component *create_component(const char *name); 4.53 4.54 #endif // COMP_H_
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/comp_xform.cc Thu Feb 05 11:04:07 2015 +0200 5.3 @@ -0,0 +1,41 @@ 5.4 +#include <assert.h> 5.5 +#include "comp_xform.h" 5.6 +#include "gameobj.h" 5.7 + 5.8 +static Component *cons_xform() { return new CompXForm; } 5.9 +static Component *cons_prs() { return new CompPRS; } 5.10 + 5.11 +CompXForm::CompXForm() 5.12 +{ 5.13 + name = "xform"; 5.14 + 5.15 + register_component(name, cons_xform); 5.16 +} 5.17 + 5.18 +CompPRS::CompPRS() 5.19 +{ 5.20 + name = "prs"; 5.21 + 5.22 + register_component(name, cons_prs); 5.23 +} 5.24 + 5.25 +void CompPRS::update() 5.26 +{ 5.27 + if(!gobj) return; 5.28 + 5.29 + if(!co_xform) { 5.30 + Component *co = gobj->get_component("xform"); 5.31 + if(!co || !(co_xform = dynamic_cast<CompXForm*>(co))) { 5.32 + assert(co_xform); 5.33 + return; 5.34 + } 5.35 + } 5.36 + 5.37 + Matrix4x4 rmat = rot.get_rotation_matrix(); 5.38 + Matrix4x4 tmat, smat; 5.39 + 5.40 + tmat.set_translation(pos); 5.41 + smat.set_scaling(scale); 5.42 + 5.43 + co_xform->xform = rmat * tmat * smat; 5.44 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/comp_xform.h Thu Feb 05 11:04:07 2015 +0200 6.3 @@ -0,0 +1,28 @@ 6.4 +#ifndef COMP_XFORM_H_ 6.5 +#define COMP_XFORM_H_ 6.6 + 6.7 +#include <vmath/vmath.h> 6.8 +#include "comp.h" 6.9 + 6.10 +class CompXForm : public Component { 6.11 +public: 6.12 + Matrix4x4 xform; 6.13 + 6.14 + CompXForm(); 6.15 +}; 6.16 + 6.17 +class CompPRS : public Component { 6.18 +private: 6.19 + CompXForm *co_xform; // cached xform component of the parent object 6.20 + 6.21 +public: 6.22 + Vector3 pos, scale; 6.23 + Quaternion rot; 6.24 + 6.25 + CompPRS(); 6.26 + 6.27 + void update(); 6.28 +}; 6.29 + 6.30 + 6.31 +#endif // COMP_XFORM_H_
7.1 --- a/src/gameobj.cc Thu Feb 05 00:38:59 2015 +0200 7.2 +++ b/src/gameobj.cc Thu Feb 05 11:04:07 2015 +0200 7.3 @@ -1,3 +1,4 @@ 7.4 +#include <stdio.h> 7.5 #include <algorithm> 7.6 #include "gameobj.h" 7.7 7.8 @@ -25,6 +26,8 @@ 7.9 try { 7.10 comp.push_back(c); 7.11 comp_by_name[name] = c; 7.12 + 7.13 + c->gobj = this; 7.14 } 7.15 catch(...) { 7.16 fprintf(stderr, "failed to add component: %s\n", name);
8.1 --- a/src/test.cc Thu Feb 05 00:38:59 2015 +0200 8.2 +++ b/src/test.cc Thu Feb 05 11:04:07 2015 +0200 8.3 @@ -70,7 +70,7 @@ 8.4 { 8.5 glViewport(0, 0, x, y); 8.6 8.7 - glMatrixMode(GL_PROJECION); 8.8 + glMatrixMode(GL_PROJECTION); 8.9 glLoadIdentity(); 8.10 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); 8.11 }