coeng

view src/comp.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 <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 GObject *Component::get_object() const
41 {
42 return gobj;
43 }
45 void Component::update(float dt)
46 {
47 }
49 void Component::draw() const
50 {
51 }
53 void register_component(const char *name, Component *(*cons_func)())
54 {
55 if(!early_comp_cons) {
56 early_comp_cons = new std::map<std::string, Component *(*)()>;
57 }
59 if(!(*early_comp_cons)[name]) {
60 printf("register component: %s\n", name);
61 (*early_comp_cons)[name] = cons_func;
62 }
63 }
65 Component *create_component(const char *name)
66 {
67 if(early_comp_cons) {
68 comp_cons = std::move(*early_comp_cons);
69 delete early_comp_cons;
70 early_comp_cons = 0;
71 }
73 Component *(*cons)() = comp_cons[name];
74 if(cons) {
75 return cons();
76 }
77 return 0;
78 }