return entity;
}
-entity_manager_create_component :: (use this: ^Entity_Manager, $component_type: type_expr) -> ^component_type where IsComponent(^component_type) {
- comp := new(component_type, allocator=entity_allocator);
+entity_manager_create_component :: (use this: ^Entity_Manager, component_type: type_expr) -> ^Component {
+ comp := cast(^Component) new(component_type, allocator=entity_allocator);
comp.type = component_type;
if !component_vtables->has(comp.type) {
}
comp.vtable = component_vtables->get(comp.type);
+ if comp.vtable.init != null_proc {
+ comp.vtable.init(comp);
+ }
return comp;
}
entity_manager_create_and_add :: macro (this: ^Entity_Manager, entity: ^Entity, $component_type: type_expr, init_block: Code) where IsComponent(^component_type) {
- comp := this->create_component(component_type);
+ use package core.intrinsics.onyx {__initialize}
+
+ comp := cast(^component_type) this->create_component(component_type);
+
+ // The 'create_component' function does not initailize the component, as it is not
+ // a compile time known parameter and therefore cannot know how to initialize it.
+ // This 'create_and_add' function is more often used by the programmer, so initializing
+ // the component here before returning is a good idea.
+ __initialize(comp);
#insert init_block;
entity->add(comp);
current_component = current_entity.components[component_type];
} else {
- component_info := get_type_info(component_type);
-
- current_component = raw_alloc(entity_allocator, component_info.size);
- memory.set(current_component, 0, component_info.size);
- current_component.type = component_type;
- current_component.vtable = component_vtables[component_type];
-
+ current_component = scene->create_component(component_type);
debug_log(.Debug, "Adding {} to entity.", component_type);
current_entity->add(current_component);
}
immediate_flush();
canvas_use(null);
- immediate_clear(.{0, 0, 0});
+ immediate_clear(.{1, 1, 1});
texture := canvas_to_texture(^scene_canvas);
view_rect: Rect;