cleanup in component creation thanks to runtime new
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 27 Feb 2022 02:52:16 +0000 (20:52 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 27 Feb 2022 02:52:16 +0000 (20:52 -0600)
run_tree/assets/images/player.png
run_tree/scenes/quick_save_new.scene
src/entity/manager.onyx
src/entity/store.onyx
src/game.onyx

index 62c16d72631c6c5aa5a748c169a816d5fbe061b5..567f12f350531a44b19fb39e8795df30cf45ca55 100644 (file)
Binary files a/run_tree/assets/images/player.png and b/run_tree/assets/images/player.png differ
index ce7e1f9ac764af54df6dde8519d4023f81c6b916..7851bba7bc84f0cacfd0d4251f5e4c6197f4c9dc 100644 (file)
@@ -22,9 +22,9 @@ pos.y = 356.0881
 size.x = 32.0000
 size.y = 32.0000
 :PlayerComponent
-color.r = 1.0000
+color.r = 0.0000
 color.g = 1.0000
-color.b = 1.0000
+color.b = 0.0000
 color.a = 1.0000
 holding = 0
 :MovementComponent
index d1b5787d70b400bb58733edc5efb0179f03c6df3..e6e7d1ce7f7eab328b17db949da1d07f37fe732f 100644 (file)
@@ -193,8 +193,8 @@ entity_manager_create_from_schematic :: (use this: ^Entity_Manager, schematic_na
     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) {
@@ -204,12 +204,23 @@ entity_manager_create_component :: (use this: ^Entity_Manager, $component_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);
index 3a75d037f493035ae1ef0df290d4debaa428f6ba..e8daed4a7945f67b70ad2f57df1b9d0d192fba17 100644 (file)
@@ -139,13 +139,7 @@ entity_manager_load_from_file :: (use this: ^Entity_Manager, filename: str) {
                 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);
             }
index 29d421a27ad272879c6df881a1a8c206a1737562..021459b6f734e6dcfcba8781a5394cf5ee9d3661 100644 (file)
@@ -57,7 +57,7 @@ game_draw :: () {
     immediate_flush();
     canvas_use(null);
     
-    immediate_clear(.{0, 0, 0});
+    immediate_clear(.{1, 1, 1});
 
     texture := canvas_to_texture(^scene_canvas);
     view_rect: Rect;