coeng

view src/comp.cc @ 6:2f872a179914

first component test: - prs, xform, physics components with dependencies - topological sort of components to update them in the correct order - debug visualization component todo: remove draw() from components, doesn't make sense
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Feb 2015 07:27:12 +0200
parents 49a2e70ac455
children 8cce82794f90
line source
1 #include <stdio.h>
2 #include <map>
3 #include <string>
4 #include "comp.h"
6 static std::map<std::string, Component *(*)()> *early_comp_cons;
7 static std::map<std::string, Component *(*)()> comp_cons;
9 Component::Component()
10 {
11 name = "unknown";
12 gobj = 0;
13 }
15 Component::~Component()
16 {
17 }
19 void Component::attach(GObject *gobj)
20 {
21 this->gobj = gobj;
22 }
24 void Component::detach()
25 {
26 gobj = 0;
27 }
29 const char **Component::update_before() const
30 {
31 static const char *before[] = { 0 };
32 return before;
33 }
35 const char *Component::get_name() const
36 {
37 return name;
38 }
40 void Component::update(float dt)
41 {
42 }
44 void Component::draw() const
45 {
46 }
48 void register_component(const char *name, Component *(*cons_func)())
49 {
50 if(!early_comp_cons) {
51 early_comp_cons = new std::map<std::string, Component *(*)()>;
52 }
54 if(!(*early_comp_cons)[name]) {
55 printf("register component: %s\n", name);
56 (*early_comp_cons)[name] = cons_func;
57 }
58 }
60 Component *create_component(const char *name)
61 {
62 if(early_comp_cons) {
63 comp_cons = std::move(*early_comp_cons);
64 delete early_comp_cons;
65 early_comp_cons = 0;
66 }
68 Component *(*cons)() = comp_cons[name];
69 if(cons) {
70 return cons();
71 }
72 return 0;
73 }