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;
@ -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);
}
}