coeng
annotate 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 |
rev | line source |
---|---|
nuclear@4 | 1 #include <stdio.h> |
nuclear@2 | 2 #include <map> |
nuclear@2 | 3 #include <string> |
nuclear@2 | 4 #include "comp.h" |
nuclear@2 | 5 |
nuclear@4 | 6 static std::map<std::string, Component *(*)()> *early_comp_cons; |
nuclear@2 | 7 static std::map<std::string, Component *(*)()> comp_cons; |
nuclear@2 | 8 |
nuclear@2 | 9 Component::Component() |
nuclear@2 | 10 { |
nuclear@2 | 11 name = "unknown"; |
nuclear@2 | 12 gobj = 0; |
nuclear@2 | 13 } |
nuclear@2 | 14 |
nuclear@2 | 15 Component::~Component() |
nuclear@2 | 16 { |
nuclear@2 | 17 } |
nuclear@2 | 18 |
nuclear@6 | 19 void Component::attach(GObject *gobj) |
nuclear@6 | 20 { |
nuclear@6 | 21 this->gobj = gobj; |
nuclear@6 | 22 } |
nuclear@6 | 23 |
nuclear@6 | 24 void Component::detach() |
nuclear@6 | 25 { |
nuclear@6 | 26 gobj = 0; |
nuclear@6 | 27 } |
nuclear@6 | 28 |
nuclear@6 | 29 const char **Component::update_before() const |
nuclear@6 | 30 { |
nuclear@6 | 31 static const char *before[] = { 0 }; |
nuclear@6 | 32 return before; |
nuclear@6 | 33 } |
nuclear@6 | 34 |
nuclear@2 | 35 const char *Component::get_name() const |
nuclear@2 | 36 { |
nuclear@2 | 37 return name; |
nuclear@2 | 38 } |
nuclear@2 | 39 |
nuclear@6 | 40 void Component::update(float dt) |
nuclear@2 | 41 { |
nuclear@2 | 42 } |
nuclear@2 | 43 |
nuclear@6 | 44 void Component::draw() const |
nuclear@2 | 45 { |
nuclear@2 | 46 } |
nuclear@2 | 47 |
nuclear@2 | 48 void register_component(const char *name, Component *(*cons_func)()) |
nuclear@2 | 49 { |
nuclear@4 | 50 if(!early_comp_cons) { |
nuclear@4 | 51 early_comp_cons = new std::map<std::string, Component *(*)()>; |
nuclear@4 | 52 } |
nuclear@4 | 53 |
nuclear@4 | 54 if(!(*early_comp_cons)[name]) { |
nuclear@3 | 55 printf("register component: %s\n", name); |
nuclear@4 | 56 (*early_comp_cons)[name] = cons_func; |
nuclear@3 | 57 } |
nuclear@2 | 58 } |
nuclear@2 | 59 |
nuclear@2 | 60 Component *create_component(const char *name) |
nuclear@2 | 61 { |
nuclear@4 | 62 if(early_comp_cons) { |
nuclear@4 | 63 comp_cons = std::move(*early_comp_cons); |
nuclear@4 | 64 delete early_comp_cons; |
nuclear@4 | 65 early_comp_cons = 0; |
nuclear@4 | 66 } |
nuclear@4 | 67 |
nuclear@2 | 68 Component *(*cons)() = comp_cons[name]; |
nuclear@2 | 69 if(cons) { |
nuclear@2 | 70 return cons(); |
nuclear@2 | 71 } |
nuclear@2 | 72 return 0; |
nuclear@2 | 73 } |