From 741a577260ec57e1562b969c2f4e6ef214693e64 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sat, 26 Feb 2022 20:52:16 -0600 Subject: [PATCH] cleanup in component creation thanks to runtime new --- run_tree/assets/images/player.png | Bin 659 -> 4858 bytes run_tree/scenes/quick_save_new.scene | 4 ++-- src/entity/manager.onyx | 17 ++++++++++++++--- src/entity/store.onyx | 8 +------- src/game.onyx | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/run_tree/assets/images/player.png b/run_tree/assets/images/player.png index 62c16d72631c6c5aa5a748c169a816d5fbe061b5..567f12f350531a44b19fb39e8795df30cf45ca55 100644 GIT binary patch literal 4858 zcmeHLc~}$I77t=Y0xBYlBFY%?iAqQ^Nyzeuf=PfVVF`Hx+A5P|0wdYT3?xvj3hEQl zidz+)`%)r)0TsX3fBe4J|4hC~X6`xXch33U zb5HI~ZggbWFsc`oLZJ+kg-aCR+l9P_4gsG&oxKQ!Vjq^Nj3*R`1u~h9T1q9`)Xc#i=Xp~pAt^_+sT%kG&8s0nGQ!^b=6)^lL@z#w(S?7DrY!+=8l$C zw|ZY+x-+BVdPB=h;jh1Wctpuub=-5Gf7f%rpT3$|o+NFw-&J?Sm{O5 zr&Zkv*%%wK*KSebw}o(h{dB*dZ>-p}yE#Zz=D#f){~`c=d9o_s`po}chob(xqV?$A z$S+5vj@%N)_&2*_yx+Y+ZV$w%j2>$1BAtQt`sqJX<0<=J`fhG?o?+GS8nfC*URXc=LRQ$Z7upl;_+h6>zFP$);%#9C6a8<8 zmbI2I`?_Lcy>MPAdqLJGk6N>&QvDW1TE3IuHw}!=^78*U)EG~Xd`FMMN`bBoiQ7V3$^sWdHBzTxkq)A zaF2gpB+eBz=mZZGiDkifZCkecK3(-0kMfFra^9_{bEY=!U*VNfJh!BEt`{$L*z((o zW-a~rk>)RzH|xf~D0^)9{Kwg0#c7XrHEjPb{c+{yyqmj!5C5&;_O;8&G4~y3c?(Yz zg&fQ*-uXn3n=@u*D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851bba 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; -- 2.25.1