coeng

view src/co_xform.cc @ 8:8cce82794f90

seems to work nicely
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Feb 2015 05:14:20 +0200
parents 2f872a179914
children
line source
1 #include <assert.h>
2 #include "co_xform.h"
3 #include "gobj.h"
5 static CoXForm reg_co_xform;
6 static CoPRS reg_co_prs;
8 static Component *cons_xform() { return new CoXForm; }
9 static Component *cons_prs() { return new CoPRS; }
11 CoXForm::CoXForm()
12 {
13 name = "xform";
15 register_component(name, cons_xform);
16 }
18 // ---- class CoPRS ----
20 CoPRS::CoPRS()
21 : scale(1, 1, 1)
22 {
23 name = "prs";
24 co_xform = 0;
26 register_component(name, cons_prs);
27 }
29 const char **CoPRS::update_before() const
30 {
31 static const char *before[] = { "xform", 0 };
32 return before;
33 }
35 void CoPRS::update(float dt)
36 {
37 if(!gobj) return;
39 if(!co_xform) {
40 if(!(co_xform = COCAST(CoXForm, gobj->get_component("xform")))) {
41 assert(co_xform);
42 return;
43 }
44 }
46 Matrix4x4 rmat = rot.get_rotation_matrix();
47 Matrix4x4 tmat, smat;
49 tmat.set_translation(pos);
50 smat.set_scaling(scale);
52 co_xform->xform = rmat * tmat * smat;
53 }
55 CoXForm *gobj_co_xform(const GObject *obj, bool nofail)
56 {
57 CoXForm *co = COCAST(CoXForm, obj->get_component("xform"));
58 if(co) return co;
60 return nofail ? &reg_co_xform : 0;
61 }
63 CoPRS *gobj_co_prs(const GObject *obj, bool nofail)
64 {
65 CoPRS *co = COCAST(CoPRS, obj->get_component("prs"));
66 if(co) return co;
68 return nofail ? &reg_co_prs : 0;
69 }