merge sort typo

This commit is contained in:
jacob 2025-01-16 14:33:13 -06:00
parent d2cfeed161
commit 6c5183fe0a
2 changed files with 447 additions and 437 deletions

View File

@ -18,6 +18,7 @@
#include "atomic.h"
#include "collider.h"
#include "rng.h"
#include "log.h"
struct bind_state {
b32 is_held; /* Is this bind held down this frame */
@ -433,7 +434,7 @@ INTERNAL void debug_draw_movement(struct entity *ent)
* Sort entities
* ========================== */
INTERNAL SORT_COMPARE_FUNC_DEF(sort_entities, arg_a, arg_b, udata)
INTERNAL SORT_COMPARE_FUNC_DEF(entity_draw_order_cmp, arg_a, arg_b, udata)
{
(UNUSED)udata;
struct entity *a = *(struct entity **)arg_a;
@ -914,6 +915,8 @@ INTERNAL void user_update(void)
u64 sorted_count = 0;
{
/* Copy valid entities */
{
__profscope(copy_sprites_for_sorting);
for (u64 entity_index = 0; entity_index < store->reserved; ++entity_index) {
struct entity *ent = &store->entities[entity_index];
if (entity_is_valid_and_active(ent)) {
@ -921,16 +924,22 @@ INTERNAL void user_update(void)
++sorted_count;
}
}
}
/* Sort */
merge_sort(sorted, sorted_count, sizeof(*sorted), sort_entities, NULL);
logf_info("Sorting %F sprites", FMT_UINT(sorted_count));
{
__profscope(sort_sprites);
merge_sort(sorted, sorted_count, sizeof(*sorted), entity_draw_order_cmp, NULL);
}
}
/* ========================== *
* Draw entities
* ========================== */
{
__profscope(draw_entities);
for (u64 sorted_index = 0; sorted_index < sorted_count; ++sorted_index) {
__profscope(user_entity_iter);
struct entity *ent = sorted[sorted_index];
if (!entity_is_valid_and_active(ent)) continue;
//if (sprite_tag_is_nil(ent->sprite)) continue;
@ -1028,7 +1037,7 @@ INTERNAL void user_update(void)
draw_solid_arrow_line(G.viewport_canvas, start, end, 3, 10, RGBA_32_F(1, 1, 1, 0.5));
}
#if 0
#if 0
/* Draw slices */
if (!sprite_tag_is_nil(ent->sprite)) {
struct sprite_sheet *sheet = sprite_sheet_from_tag_async(sprite_frame_scope, sprite);
@ -1069,7 +1078,7 @@ INTERNAL void user_update(void)
}
}
}
#endif
#endif
/* Draw collider */
if (entity_has_prop(ent, ENTITY_PROP_PHYSICAL_DYNAMIC)) {
@ -1097,14 +1106,14 @@ INTERNAL void user_update(void)
end = xform_mul_v2(G.world_view, end);
draw_solid_line(G.viewport_canvas, start, end, thickness, color);
}
#if 0
#if 0
/* Draw support point at focus dir */
{
struct v2 p = collider_support_point(&collider, xf, ent->control.focus);
p = xform_mul_v2(G.world_view, p);
draw_solid_circle(G.viewport_canvas, p, 3, COLOR_RED, 10);
}
#endif
#endif
}
/* Draw contact constraint */
@ -1115,19 +1124,19 @@ INTERNAL void user_update(void)
(UNUSED)e0;
(UNUSED)e1;
#if 1
#if 1
/* Draw contact points */
{
f32 radius = 5;
for (u32 i = 0; i < data->num_points; ++i) {
struct phys_contact_point point = data->points[i];
#if 0
#if 0
struct v2 p0 = xform_mul_v2(e0_xf, contact.point_local_e0);
struct v2 p1 = xform_mul_v2(e1_xf, contact.point_local_e1);
struct v2 point = v2_add(p0, v2_mul(v2_sub(p1, p0), 0.5f));
#else
#else
struct v2 dbg_pt = point.dbg_pt;
#endif
#endif
/* Draw point */
{
//u32 color = contact.persisted ? RGBA_32_F(1, 1, 0, 0.50) : RGBA_32_F(1, 0, 0, 0.50);
@ -1146,7 +1155,7 @@ INTERNAL void user_update(void)
struct v2 end = xform_mul_v2(G.world_view, v2_add(dbg_pt, v2_mul(v2_norm(data->normal), len)));
draw_solid_arrow_line(G.viewport_canvas, start, end, arrow_thickness, arrow_height, color);
}
#if 0
#if 0
/* Draw contact info */
{
struct font *disp_font = font_load_async(STR("res/fonts/fixedsys.ttf"), 12.0f);
@ -1177,13 +1186,13 @@ INTERNAL void user_update(void)
draw_text(G.viewport_canvas, disp_font, v2_add(v2_round(xform_mul_v2(G.world_view, dbg_pt)), V2(0, offset_px)), text);
}
}
#endif
#endif
}
}
}
/* Draw collision debug */
#if COLLIDER_DEBUG
#if COLLIDER_DEBUG
if (entity_has_prop(ent, ENTITY_PROP_COLLISION_DEBUG)) {
struct phys_collision_debug *data = &ent->collision_debug_data;
struct collider_collision_points_result collider_res = data->res;
@ -1195,7 +1204,7 @@ INTERNAL void user_update(void)
(UNUSED)e1_collider;
/* Draw closest points */
#if 0
#if 0
{
f32 radius = 4;
u32 color = RGBA_32_F(1, 1, 0, 0.5);
@ -1204,7 +1213,7 @@ INTERNAL void user_update(void)
draw_solid_circle(G.viewport_canvas, a, radius, color, 10);
draw_solid_circle(G.viewport_canvas, b, radius, color, 10);
}
#endif
#endif
/* Draw clipping */
{
@ -1244,11 +1253,11 @@ INTERNAL void user_update(void)
}
}
#if COLLIDER_DEBUG_DETAILED_DRAW_MENKOWSKI
#if COLLIDER_DEBUG_DETAILED_DRAW_MENKOWSKI
struct xform e0_xf = data->xf0;
struct xform e1_xf = data->xf1;
#if 0
#if 0
/* Only draw points with large separation */
b32 should_draw = false;
for (u32 i = 0; i < data->num_points; ++i) {
@ -1257,12 +1266,12 @@ INTERNAL void user_update(void)
break;
}
}
#else
#else
b32 should_draw = true;
#endif
#endif
if (should_draw) {
#if 0
#if 0
/* Test info */
{
struct font *disp_font = font_load_async(STR("res/fonts/fixedsys.ttf"), 12.0f);
@ -1284,7 +1293,7 @@ INTERNAL void user_update(void)
draw_text(G.viewport_canvas, disp_font, v2_add(v2_round(xform_mul_v2(G.world_view, V2(0, 0))), V2(0, offset_px)), text);
}
}
#endif
#endif
/* Draw menkowski */
{
@ -1369,10 +1378,10 @@ INTERNAL void user_update(void)
}
}
}
#endif
#endif
}
#endif
#endif
#endif
#endif
/* Draw hierarchy */
if (entity_has_prop(parent, ENTITY_PROP_ACTIVE) && !parent->is_root) {
@ -1400,6 +1409,7 @@ INTERNAL void user_update(void)
arena_temp_end(temp);
}
}
}
/* Draw crosshair or show cursor */
if (!G.debug_camera) {

View File

@ -55,15 +55,15 @@ INLINE u128 hash_fnv128(u128 seed, struct buffer buff)
#define SORT_COMPARE_FUNC_DEF(name, arg_a, arg_b, arg_udata) i32 name(void *arg_a, void *arg_b, void *arg_udata)
typedef SORT_COMPARE_FUNC_DEF(sort_compare_func, a, b, udata);
INLINE void merge_sort_internal(void *left, void *right, void *items, u64 left_count, u64 right_count, u64 item_size, sort_compare_func *callback, void *udata)
INLINE void merge_sort_internal(u8 *left, u8 *right, u8 *items, u64 left_count, u64 right_count, u64 item_size, sort_compare_func *callback, void *udata)
{
u64 i = 0;
u64 l = 0;
u64 r = 0;
while (l < left_count && r < right_count) {
u8 *dst = ((u8 *)items) + (i * item_size);
u8 *left_item = ((u8 *)left) + (l * item_size);
u8 *right_item = ((u8 *)right) + (r * item_size);
u8 *dst = items + (i * item_size);
u8 *left_item = left + (l * item_size);
u8 *right_item = right + (r * item_size);
++i;
if (callback(left_item, right_item, udata) > 0) {
MEMCPY(dst, left_item, item_size);
@ -77,18 +77,18 @@ INLINE void merge_sort_internal(void *left, void *right, void *items, u64 left_c
u64 left_remaining_bytes = (left_count - l) * item_size;
u64 right_remaining_bytes = (right_count - r) * item_size;
if (left_remaining_bytes > 0) {
u8 *dst = ((u8 *)items) + (i * item_size);
u8 *src = ((u8 *)left) + (l * item_size);
u8 *dst = items + (i * item_size);
u8 *src = left + (l * item_size);
MEMCPY(dst, src, left_remaining_bytes);
i += left_count - l;
l = left_remaining_bytes;
l = left_count;
}
if (right_remaining_bytes > 0) {
u8 *dst = ((u8 *)items) + (i * item_size);
u8 *src = ((u8 *)right) + (r * item_size);
u8 *dst = items + (i * item_size);
u8 *src = right + (r * item_size);
MEMCPY(dst, src, right_remaining_bytes);
i += right_count - r;
l = right_remaining_bytes;
r = right_count;
}
}
@ -109,7 +109,7 @@ INLINE void merge_sort(void *items, u64 item_count, u64 item_size, sort_compare_
merge_sort(left, left_count, item_size, callback, udata);
merge_sort(right, right_count, item_size, callback, udata);
merge_sort_internal(left, right, items, left_count, right_count, item_size, callback, udata);
merge_sort_internal(left, right, (u8 *)items, left_count, right_count, item_size, callback, udata);
scratch_end(scratch);
}
}