From 1dd8a4d6596e4908cb0c25da88721ad44aa26fc0 Mon Sep 17 00:00:00 2001 From: jacob Date: Thu, 25 Jul 2024 13:04:12 -0500 Subject: [PATCH] store slices & spans in array --- src/game.c | 21 --------------------- src/sprite.c | 44 ++++++++++++++++++++++++-------------------- src/sprite.h | 8 ++++++++ src/user.c | 22 +++++++++++++++++++--- 4 files changed, 51 insertions(+), 44 deletions(-) diff --git a/src/game.c b/src/game.c index 1cab9ca0..c86c5486 100644 --- a/src/game.c +++ b/src/game.c @@ -194,27 +194,6 @@ INTERNAL void game_update(void) e->sprite_span_name = STR("idle.unarmed"); //e->sprite_span_name = STR("idle.two_handed"); -#if 0 - struct v2 sprite_pos = V2(0, 0); - f32 sprite_rot = 0; - struct v2 sprite_size = V2(0.5f, 0.5f); - // struct v2 sprite_size = sprite_sheet_size_meters(sprite_sheet_tag); - - struct v2 sprite_pivot; - { - struct v2 sprite_pivot_norm = V2(0, 0); /* Pivot x & y are each normalized about sprite dimensions. <0, 0> is center, <1, 1> is bottom right corner, etc. */ - struct v2 half_size = v2_mul(sprite_size, 0.5f); - sprite_pivot = v2_mul_v2(sprite_pivot_norm, half_size); - } - - struct xform sprite_xf = XFORM_POS(sprite_pos); - sprite_xf = xform_rotate(sprite_xf, sprite_rot); - sprite_xf = xform_translate(sprite_xf, v2_neg(sprite_pivot)); - sprite_xf = xform_scale(sprite_xf, sprite_size); - e->sprite_quad_xform = sprite_xf; -#endif - e->sprite_tint = COLOR_WHITE; - entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED); e->player_max_speed = 4.f; e->player_acceleration = 20.0f; diff --git a/src/sprite.c b/src/sprite.c index 3cea7e28..aafa14e9 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -419,16 +419,17 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str sheet.spans_count = ase.num_spans; if (ase.num_spans > 0) { __profscope(init_spans); + sheet.spans = arena_push_array_zero(arena, struct sprite_sheet_span, sheet.spans_count); sheet.spans_dict = fixed_dict_init(arena, (u64)(ase.num_spans * SHEET_SPAN_LOOKUP_TABLE_BUCKET_RATIO)); + u64 index = 0; for (struct ase_span *ase_span = ase.span_head; ase_span; ase_span = ase_span->next) { struct string name = string_copy(arena, ase_span->name); - struct sprite_sheet_span *span = arena_push(arena, struct sprite_sheet_span); - *span = (struct sprite_sheet_span) { - .name = name, - .start = ase_span->start, - .end = ase_span->end - }; + struct sprite_sheet_span *span = &sheet.spans[index]; + span->name = name; + span->start = ase_span->start; + span->end = ase_span->end; fixed_dict_set(arena, &sheet.spans_dict, name, span); + ++index; } } @@ -486,10 +487,12 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str /* Allocate slice groups & fill originals in 2d array */ sheet.slice_groups_count = num_temp_slice_group_nodes; + sheet.slice_groups = arena_push_array_zero(arena, struct sprite_sheet_slice_group, sheet.slice_groups_count); sheet.slice_groups_dict = fixed_dict_init(arena, (u64)(num_temp_slice_group_nodes * SHEET_SLICE_LOOKUP_TABLE_BUCKET_RATIO)); + u64 index = 0; for (struct temp_slice_group_node *temp_slice_group_node = temp_slice_group_head; temp_slice_group_node; temp_slice_group_node = temp_slice_group_node->next) { - struct sprite_sheet_slice_group *slice_group = arena_push_zero(arena, struct sprite_sheet_slice_group); + struct sprite_sheet_slice_group *slice_group = &sheet.slice_groups[index]; slice_group->name = string_copy(arena, temp_slice_group_node->name); slice_group->per_frame_count = temp_slice_group_node->per_frame_count; @@ -511,8 +514,8 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str f32 x2 = ase_slice->x2; f32 y2 = ase_slice->y2; - struct v2 center = V2(x1 + ((x2 - x1) / 2.f), y1 + ((y2 - y1) / 2.f)); - slice->center = center; + slice->rect = RECT(x1, y1, x2 - x1, y2 - y1); + slice->center = V2(x1 + ((x2 - x1) / 2.f), y1 + ((y2 - y1) / 2.f)); slice->dir = V2(0, -1); node->index_in_frame = index_in_frame; @@ -526,6 +529,7 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str temp_slice_group_node->final_slice_group = slice_group; fixed_dict_set(arena, &sheet.slice_groups_dict, slice_group->name, slice_group); + ++index; } /* Propogate original slices into next frames (and first slices into previous frames) */ @@ -930,25 +934,25 @@ struct sprite_sheet_span sprite_sheet_get_span(struct sprite_sheet *sheet, struc struct sprite_sheet_slice sprite_sheet_get_slice(struct sprite_sheet *sheet, struct string name, u32 frame_index) { struct sprite_sheet_slice res = { 0 }; - - struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name); - if (group) { - res = group->frame_slices[frame_index * group->per_frame_count]; + if (sheet->slice_groups_count > 0) { + struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name); + if (group) { + res = group->frame_slices[frame_index * group->per_frame_count]; + } } - return res; } struct sprite_sheet_slice_array sprite_sheet_get_slices(struct sprite_sheet *sheet, struct string name, u32 frame_index) { struct sprite_sheet_slice_array res = { 0 }; - - struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name); - if (group) { - res.count = group->per_frame_count; - res.slices = &group->frame_slices[frame_index * group->per_frame_count]; + if (sheet->slice_groups_count > 0) { + struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name); + if (group) { + res.count = group->per_frame_count; + res.slices = &group->frame_slices[frame_index * group->per_frame_count]; + } } - return res; } diff --git a/src/sprite.h b/src/sprite.h index 8bb59047..254f8765 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -5,6 +5,8 @@ struct renderer_startup_receipt; struct resource_startup_receipt; +struct sprite_sheet_span; +struct sprite_sheet_slice_group; /* ========================== * * Startup @@ -58,11 +60,16 @@ struct sprite_sheet { b32 valid; struct v2 image_size; struct v2 frame_size; + u32 frames_count; struct sprite_sheet_frame *frames; + u32 spans_count; + struct sprite_sheet_span *spans; struct fixed_dict spans_dict; + u32 slice_groups_count; + struct sprite_sheet_slice_group *slice_groups; struct fixed_dict slice_groups_dict; }; @@ -87,6 +94,7 @@ struct sprite_sheet_span { struct sprite_sheet_slice { b32 original; + struct rect rect; struct v2 center; struct v2 dir; }; diff --git a/src/user.c b/src/user.c index 949ac10c..23808599 100644 --- a/src/user.c +++ b/src/user.c @@ -809,13 +809,13 @@ INTERNAL void user_update(void) pivot_rot = -v2_angle(pivot_slice.dir) - (TAU / 4.0f); } + /* Apply entity sprite xform */ + sprite_xf = xform_mul(sprite_xf, ent->sprite_quad_xform); + /* Apply Pivot */ sprite_xf = xform_rotate(sprite_xf, pivot_rot); sprite_xf = xform_translate(sprite_xf, v2_neg(pivot_pos)); sprite_xf = xform_scale(sprite_xf, sprite_size_meters); - - /* Apply entity sprite xform */ - sprite_xf = xform_mul(sprite_xf, ent->sprite_quad_xform); } quad = quad_mul_xform(QUAD_UNIT_SQUARE_CENTERED, xform_mul(ent->world_xform, sprite_xf)); @@ -874,6 +874,22 @@ INTERNAL void user_update(void) debug_draw_xform(ent->world_xform); } +#if 0 + /* Draw slices */ + { + struct sprite_tag sprite = ent->sprite; + struct sprite_sheet *sheet = sprite_sheet_from_tag_async(sprite_frame_scope, sprite); + + for (u64 i = 0; i < sheet->slice_groups_count; ++i) { + struct sprite_sheet_slice_group *group = &sheet->slice_groups[i]; + + for (u32 j = 0; j < group->per_frame_count; ++j) { + group->frame_slices[(j * group->per_frame_count) + k]; + } + } + + } +#endif /* Draw hierarchy */ struct entity *parent = entity_from_handle(&G.world.entity_store, ent->parent); if (parent->valid) {