From: Brendan Hansen Date: Sun, 27 Feb 2022 02:52:16 +0000 (-0600) Subject: cleanup in component creation thanks to runtime new X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=741a577260ec57e1562b969c2f4e6ef214693e64;p=bar-game.git cleanup in component creation thanks to runtime new --- diff --git a/run_tree/assets/images/player.png b/run_tree/assets/images/player.png index 62c16d7..567f12f 100644 Binary files a/run_tree/assets/images/player.png and b/run_tree/assets/images/player.png differ diff --git a/run_tree/scenes/quick_save_new.scene b/run_tree/scenes/quick_save_new.scene index ce7e1f9..7851bba 100644 --- a/run_tree/scenes/quick_save_new.scene +++ b/run_tree/scenes/quick_save_new.scene @@ -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 diff --git a/src/entity/manager.onyx b/src/entity/manager.onyx index d1b5787..e6e7d1c 100644 --- a/src/entity/manager.onyx +++ b/src/entity/manager.onyx @@ -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); diff --git a/src/entity/store.onyx b/src/entity/store.onyx index 3a75d03..e8daed4 100644 --- a/src/entity/store.onyx +++ b/src/entity/store.onyx @@ -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); } diff --git a/src/game.onyx b/src/game.onyx index 29d421a..021459b 100644 --- a/src/game.onyx +++ b/src/game.onyx @@ -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;