store slices & spans in array
This commit is contained in:
parent
6193d80983
commit
1dd8a4d659
21
src/game.c
21
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.unarmed");
|
||||||
//e->sprite_span_name = STR("idle.two_handed");
|
//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);
|
entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED);
|
||||||
e->player_max_speed = 4.f;
|
e->player_max_speed = 4.f;
|
||||||
e->player_acceleration = 20.0f;
|
e->player_acceleration = 20.0f;
|
||||||
|
|||||||
30
src/sprite.c
30
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;
|
sheet.spans_count = ase.num_spans;
|
||||||
if (ase.num_spans > 0) {
|
if (ase.num_spans > 0) {
|
||||||
__profscope(init_spans);
|
__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));
|
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) {
|
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 string name = string_copy(arena, ase_span->name);
|
||||||
struct sprite_sheet_span *span = arena_push(arena, struct sprite_sheet_span);
|
struct sprite_sheet_span *span = &sheet.spans[index];
|
||||||
*span = (struct sprite_sheet_span) {
|
span->name = name;
|
||||||
.name = name,
|
span->start = ase_span->start;
|
||||||
.start = ase_span->start,
|
span->end = ase_span->end;
|
||||||
.end = ase_span->end
|
|
||||||
};
|
|
||||||
fixed_dict_set(arena, &sheet.spans_dict, name, span);
|
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 */
|
/* Allocate slice groups & fill originals in 2d array */
|
||||||
sheet.slice_groups_count = num_temp_slice_group_nodes;
|
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));
|
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) {
|
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->name = string_copy(arena, temp_slice_group_node->name);
|
||||||
slice_group->per_frame_count = temp_slice_group_node->per_frame_count;
|
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 x2 = ase_slice->x2;
|
||||||
f32 y2 = ase_slice->y2;
|
f32 y2 = ase_slice->y2;
|
||||||
|
|
||||||
struct v2 center = V2(x1 + ((x2 - x1) / 2.f), y1 + ((y2 - y1) / 2.f));
|
slice->rect = RECT(x1, y1, x2 - x1, y2 - y1);
|
||||||
slice->center = center;
|
slice->center = V2(x1 + ((x2 - x1) / 2.f), y1 + ((y2 - y1) / 2.f));
|
||||||
slice->dir = V2(0, -1);
|
slice->dir = V2(0, -1);
|
||||||
|
|
||||||
node->index_in_frame = index_in_frame;
|
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;
|
temp_slice_group_node->final_slice_group = slice_group;
|
||||||
fixed_dict_set(arena, &sheet.slice_groups_dict, slice_group->name, 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) */
|
/* 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 sprite_sheet_get_slice(struct sprite_sheet *sheet, struct string name, u32 frame_index)
|
||||||
{
|
{
|
||||||
struct sprite_sheet_slice res = { 0 };
|
struct sprite_sheet_slice res = { 0 };
|
||||||
|
if (sheet->slice_groups_count > 0) {
|
||||||
struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name);
|
struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name);
|
||||||
if (group) {
|
if (group) {
|
||||||
res = group->frame_slices[frame_index * group->per_frame_count];
|
res = group->frame_slices[frame_index * group->per_frame_count];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return res;
|
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 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_array res = { 0 };
|
||||||
|
if (sheet->slice_groups_count > 0) {
|
||||||
struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name);
|
struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name);
|
||||||
if (group) {
|
if (group) {
|
||||||
res.count = group->per_frame_count;
|
res.count = group->per_frame_count;
|
||||||
res.slices = &group->frame_slices[frame_index * group->per_frame_count];
|
res.slices = &group->frame_slices[frame_index * group->per_frame_count];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
struct renderer_startup_receipt;
|
struct renderer_startup_receipt;
|
||||||
struct resource_startup_receipt;
|
struct resource_startup_receipt;
|
||||||
|
struct sprite_sheet_span;
|
||||||
|
struct sprite_sheet_slice_group;
|
||||||
|
|
||||||
/* ========================== *
|
/* ========================== *
|
||||||
* Startup
|
* Startup
|
||||||
@ -58,11 +60,16 @@ struct sprite_sheet {
|
|||||||
b32 valid;
|
b32 valid;
|
||||||
struct v2 image_size;
|
struct v2 image_size;
|
||||||
struct v2 frame_size;
|
struct v2 frame_size;
|
||||||
|
|
||||||
u32 frames_count;
|
u32 frames_count;
|
||||||
struct sprite_sheet_frame *frames;
|
struct sprite_sheet_frame *frames;
|
||||||
|
|
||||||
u32 spans_count;
|
u32 spans_count;
|
||||||
|
struct sprite_sheet_span *spans;
|
||||||
struct fixed_dict spans_dict;
|
struct fixed_dict spans_dict;
|
||||||
|
|
||||||
u32 slice_groups_count;
|
u32 slice_groups_count;
|
||||||
|
struct sprite_sheet_slice_group *slice_groups;
|
||||||
struct fixed_dict slice_groups_dict;
|
struct fixed_dict slice_groups_dict;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -87,6 +94,7 @@ struct sprite_sheet_span {
|
|||||||
|
|
||||||
struct sprite_sheet_slice {
|
struct sprite_sheet_slice {
|
||||||
b32 original;
|
b32 original;
|
||||||
|
struct rect rect;
|
||||||
struct v2 center;
|
struct v2 center;
|
||||||
struct v2 dir;
|
struct v2 dir;
|
||||||
};
|
};
|
||||||
|
|||||||
22
src/user.c
22
src/user.c
@ -809,13 +809,13 @@ INTERNAL void user_update(void)
|
|||||||
pivot_rot = -v2_angle(pivot_slice.dir) - (TAU / 4.0f);
|
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 */
|
/* Apply Pivot */
|
||||||
sprite_xf = xform_rotate(sprite_xf, pivot_rot);
|
sprite_xf = xform_rotate(sprite_xf, pivot_rot);
|
||||||
sprite_xf = xform_translate(sprite_xf, v2_neg(pivot_pos));
|
sprite_xf = xform_translate(sprite_xf, v2_neg(pivot_pos));
|
||||||
sprite_xf = xform_scale(sprite_xf, sprite_size_meters);
|
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));
|
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);
|
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 */
|
/* Draw hierarchy */
|
||||||
struct entity *parent = entity_from_handle(&G.world.entity_store, ent->parent);
|
struct entity *parent = entity_from_handle(&G.world.entity_store, ent->parent);
|
||||||
if (parent->valid) {
|
if (parent->valid) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user