# HG changeset patch # User John Tsiombikas # Date 1423127047 -7200 # Node ID 4a1c9597f4d349db21a65724e30f943fd1edf59c # Parent b0d8d454c546275a9d66a59c07507f29248717ca foo diff -r b0d8d454c546 -r 4a1c9597f4d3 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Thu Feb 05 11:04:07 2015 +0200 @@ -0,0 +1,4 @@ +\.o$ +\.swp$ +\.d$ +^test$ diff -r b0d8d454c546 -r 4a1c9597f4d3 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Thu Feb 05 11:04:07 2015 +0200 @@ -0,0 +1,19 @@ +ccsrc = $(wildcard src/*.cc) +obj = $(ccsrc:.cc=.o) +bin = test + +CXXFLAGS = -pedantic -Wall -g +LDFLAGS = $(libgl) -lm -lvmath + +ifeq ($(shell uname -s), Darwin) + libgl = -framework OpenGL -framework GLUT -lGLEW +else + libgl = -lGL -lGLU -lglut -lGLEW +endif + +$(bin): $(obj) + $(CXX) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r b0d8d454c546 -r 4a1c9597f4d3 src/comp.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/comp.cc Thu Feb 05 11:04:07 2015 +0200 @@ -0,0 +1,45 @@ +#include +#include +#include "comp.h" + +static std::map comp_cons; + +Component::Component() +{ + name = "unknown"; + gobj = 0; + upd_prio = 0; +} + +Component::~Component() +{ +} + +const char *Component::get_name() const +{ + return name; +} + +void Component::update() +{ +} + +bool Component::operator <(const Component &c) const +{ + return upd_prio < c.upd_prio; +} + + +void register_component(const char *name, Component *(*cons_func)()) +{ + comp_cons[name] = cons_func; +} + +Component *create_component(const char *name) +{ + Component *(*cons)() = comp_cons[name]; + if(cons) { + return cons(); + } + return 0; +} diff -r b0d8d454c546 -r 4a1c9597f4d3 src/comp.h --- a/src/comp.h Thu Feb 05 00:38:59 2015 +0200 +++ b/src/comp.h Thu Feb 05 11:04:07 2015 +0200 @@ -2,43 +2,29 @@ #define COMP_H_ #include -#include class GameObject; class Component { protected: - char *name; - GameObject *parent; + const char *name; + GameObject *gobj; int upd_prio; // update priority (0: normal) public: - Component() {} - virtual ~Component() {} + Component(); + virtual ~Component(); const char *get_name() const; virtual void update(); bool operator <(const Component &c) const; // for sorting based on priority + + friend class GameObject; }; -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(); -}; +void register_component(const char *name, Component *(*cons_func)()); +Component *create_component(const char *name); #endif // COMP_H_ diff -r b0d8d454c546 -r 4a1c9597f4d3 src/comp_xform.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/comp_xform.cc Thu Feb 05 11:04:07 2015 +0200 @@ -0,0 +1,41 @@ +#include +#include "comp_xform.h" +#include "gameobj.h" + +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 b0d8d454c546 -r 4a1c9597f4d3 src/comp_xform.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/comp_xform.h Thu Feb 05 11:04:07 2015 +0200 @@ -0,0 +1,28 @@ +#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 b0d8d454c546 -r 4a1c9597f4d3 src/gameobj.cc --- a/src/gameobj.cc Thu Feb 05 00:38:59 2015 +0200 +++ b/src/gameobj.cc Thu Feb 05 11:04:07 2015 +0200 @@ -1,3 +1,4 @@ +#include #include #include "gameobj.h" @@ -25,6 +26,8 @@ try { comp.push_back(c); comp_by_name[name] = c; + + c->gobj = this; } catch(...) { fprintf(stderr, "failed to add component: %s\n", name); diff -r b0d8d454c546 -r 4a1c9597f4d3 src/test.cc --- a/src/test.cc Thu Feb 05 00:38:59 2015 +0200 +++ b/src/test.cc Thu Feb 05 11:04:07 2015 +0200 @@ -70,7 +70,7 @@ { glViewport(0, 0, x, y); - glMatrixMode(GL_PROJECION); + glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); }