diff --git a/demo3d/client/bsp/demo3d_bsp.c b/demo3d/client/bsp/demo3d_bsp.c index 4d274f9..2bf7822 100644 --- a/demo3d/client/bsp/demo3d_bsp.c +++ b/demo3d/client/bsp/demo3d_bsp.c @@ -458,13 +458,7 @@ u8 demo3d_bsp_light_at(const demo3d_bsp* bsp, f32 x, f32 y, f32 z, u8 ambient) { } } - if (best_idx >= bsp->num_vertex_lights) return 255; - - u32 packed = bsp->vertex_lights[best_idx]; - u8 direct = (packed >> 24) & 0xFF; - u8 ao = (packed >> 16) & 0xFF; - f32 combined = (f32)direct + ((f32)ambient / 255.0f) * (f32)ao; - return (u8)(combined > 255.0f ? 255.0f : combined); + return demo3d_bsp_vertex_light(bsp, best_idx, ambient); } demo3d_bsp_lightmap_sample demo3d_bsp_sample_lightmap(const demo3d_bsp* bsp, u32 face_idx, f32 u, f32 v) { diff --git a/demo3d/client/bsp/demo3d_bsp.h b/demo3d/client/bsp/demo3d_bsp.h index 81ada27..b806c19 100644 --- a/demo3d/client/bsp/demo3d_bsp.h +++ b/demo3d/client/bsp/demo3d_bsp.h @@ -157,6 +157,15 @@ static inline bool demo3d_bsp_get_edge_vertex(const demo3d_bsp* bsp, i32 surfedg return *out_vert_idx < bsp->num_vertices; } +static inline u8 demo3d_bsp_vertex_light(const demo3d_bsp* bsp, u32 vert_idx, u8 ambient) { + if (!bsp->vertex_lights || vert_idx >= bsp->num_vertex_lights) return 255; + u32 packed = bsp->vertex_lights[vert_idx]; + u8 direct = (packed >> 24) & 0xFF; + u8 ao = (packed >> 16) & 0xFF; + f32 combined = (f32)direct + ((f32)ambient / 255.0f) * (f32)ao; + return (u8)(combined > 255.0f ? 255.0f : combined); +} + static inline i32 demo3d_bsp_find_leaf(const demo3d_bsp* bsp, pxl8_vec3 pos) { if (!bsp || bsp->num_nodes == 0) return -1; i32 node_id = 0; diff --git a/demo3d/client/bsp/demo3d_bsp_render.c b/demo3d/client/bsp/demo3d_bsp_render.c index 8f70b83..284c61e 100644 --- a/demo3d/client/bsp/demo3d_bsp_render.c +++ b/demo3d/client/bsp/demo3d_bsp_render.c @@ -72,14 +72,7 @@ static void collect_face_to_mesh(const demo3d_bsp* bsp, const demo3d_bsp_render_ f32 u = (pxl8_vec3_dot(pos, u_axis) + u_offset) / tex_scale; f32 v = (pxl8_vec3_dot(pos, v_axis) + v_offset) / tex_scale; - u8 light = 255; - if (bsp->vertex_lights && vert_idx < bsp->num_vertex_lights) { - u32 packed = bsp->vertex_lights[vert_idx]; - u8 direct = (packed >> 24) & 0xFF; - u8 ao = (packed >> 16) & 0xFF; - f32 combined = (f32)direct + ((f32)ambient / 255.0f) * (f32)ao; - light = (u8)(combined > 255.0f ? 255.0f : combined); - } + u8 light = demo3d_bsp_vertex_light(bsp, vert_idx, ambient); pxl8_vertex vtx = { .position = pos, diff --git a/src/asset/pxl8_ase.c b/src/asset/pxl8_ase.c index e426f8d..85423b2 100644 --- a/src/asset/pxl8_ase.c +++ b/src/asset/pxl8_ase.c @@ -245,7 +245,7 @@ static pxl8_result parse_tileset_chunk(pxl8_stream* stream, pxl8_ase_tileset* ti } if (tileset->flags & 1) { - pxl8_skip_bytes(stream, 8); // external_file_id + tileset_id + pxl8_skip_bytes(stream, 8); } if (tileset->flags & 2) { @@ -811,14 +811,8 @@ pxl8_result pxl8_ase_remap(const char* input_path, const char* output_path, cons } u32 new_last_color = compact_count > 0 ? compact_count - 1 : 0; - output_data[palette_chunk_offset + 6] = compact_count & 0xFF; - output_data[palette_chunk_offset + 7] = (compact_count >> 8) & 0xFF; - output_data[palette_chunk_offset + 8] = (compact_count >> 16) & 0xFF; - output_data[palette_chunk_offset + 9] = (compact_count >> 24) & 0xFF; - output_data[palette_chunk_offset + 14] = new_last_color & 0xFF; - output_data[palette_chunk_offset + 15] = (new_last_color >> 8) & 0xFF; - output_data[palette_chunk_offset + 16] = (new_last_color >> 16) & 0xFF; - output_data[palette_chunk_offset + 17] = (new_last_color >> 24) & 0xFF; + pxl8_pack_u32_le(output_data, palette_chunk_offset + 6, compact_count); + pxl8_pack_u32_le(output_data, palette_chunk_offset + 14, new_last_color); chunk_offset = frame_start + 16; usize output_size = file_size; @@ -897,22 +891,13 @@ pxl8_result pxl8_ase_remap(const char* input_path, const char* output_path, cons output_size - compressed_start - compressed_size); u32 new_chunk_size = chunk_size + size_diff; - new_output[chunk_offset + 0] = new_chunk_size & 0xFF; - new_output[chunk_offset + 1] = (new_chunk_size >> 8) & 0xFF; - new_output[chunk_offset + 2] = (new_chunk_size >> 16) & 0xFF; - new_output[chunk_offset + 3] = (new_chunk_size >> 24) & 0xFF; + pxl8_pack_u32_le(new_output, chunk_offset, new_chunk_size); u32 new_frame_size = frame_size + size_diff; - new_output[frame_start + 0] = new_frame_size & 0xFF; - new_output[frame_start + 1] = (new_frame_size >> 8) & 0xFF; - new_output[frame_start + 2] = (new_frame_size >> 16) & 0xFF; - new_output[frame_start + 3] = (new_frame_size >> 24) & 0xFF; + pxl8_pack_u32_le(new_output, frame_start, new_frame_size); output_size += size_diff; - new_output[0] = output_size & 0xFF; - new_output[1] = (output_size >> 8) & 0xFF; - new_output[2] = (output_size >> 16) & 0xFF; - new_output[3] = (output_size >> 24) & 0xFF; + pxl8_pack_u32_le(new_output, 0, output_size); pxl8_free(new_compressed); pxl8_free(output_data); diff --git a/src/asset/pxl8_cart.c b/src/asset/pxl8_cart.c index 4ba9cd4..fb91ad3 100644 --- a/src/asset/pxl8_cart.c +++ b/src/asset/pxl8_cart.c @@ -93,7 +93,10 @@ static bool collect_entry(void* userdata, const char* dir_path, const char* name *ctx->capacity = (*ctx->capacity == 0) ? 64 : (*ctx->capacity * 2); *ctx->paths = pxl8_realloc(*ctx->paths, *ctx->capacity * sizeof(char*)); } - (*ctx->paths)[(*ctx->count)++] = strdup(rel_path); + usize len = strlen(rel_path) + 1; + char* copy = pxl8_malloc(len); + if (copy) memcpy(copy, rel_path, len); + (*ctx->paths)[(*ctx->count)++] = copy; } return true; } @@ -342,7 +345,9 @@ const char* pxl8_cart_get_title(const pxl8_cart* cart) { void pxl8_cart_set_title(pxl8_cart* cart, const char* title) { if (!cart || !title) return; pxl8_free(cart->title); - cart->title = strdup(title); + usize len = strlen(title) + 1; + cart->title = pxl8_malloc(len); + if (cart->title) memcpy(cart->title, title, len); } pxl8_resolution pxl8_cart_get_resolution(const pxl8_cart* cart) { diff --git a/src/gfx/pxl8_anim.c b/src/gfx/pxl8_anim.c index 41cb589..4ad51dd 100644 --- a/src/gfx/pxl8_anim.c +++ b/src/gfx/pxl8_anim.c @@ -174,7 +174,11 @@ pxl8_result pxl8_anim_add_state(pxl8_anim* anim, const char* name, pxl8_anim* st } u16 idx = anim->state_machine->state_count; - anim->state_machine->states[idx].name = strdup(name); + usize name_len = strlen(name) + 1; + anim->state_machine->states[idx].name = pxl8_malloc(name_len); + if (anim->state_machine->states[idx].name) { + memcpy(anim->state_machine->states[idx].name, name, name_len); + } if (!anim->state_machine->states[idx].name) { return PXL8_ERROR_OUT_OF_MEMORY; } diff --git a/src/gfx/pxl8_blit3d.c b/src/gfx/pxl8_blit3d.c index 04a558c..61234b7 100644 --- a/src/gfx/pxl8_blit3d.c +++ b/src/gfx/pxl8_blit3d.c @@ -81,13 +81,11 @@ static u8 blend_colors( if (sf == 0.0f && df == 1.0f) return dst; - u8 sr = palette[src] & 0xFF; - u8 sg = (palette[src] >> 8) & 0xFF; - u8 sb = (palette[src] >> 16) & 0xFF; + u8 sr, sg, sb, sa; + pxl8_rgba32_unpack(palette[src], &sr, &sg, &sb, &sa); - u8 dr = palette[dst] & 0xFF; - u8 dg = (palette[dst] >> 8) & 0xFF; - u8 db = (palette[dst] >> 16) & 0xFF; + u8 dr, dg, db, da; + pxl8_rgba32_unpack(palette[dst], &dr, &dg, &db, &da); u8 out_r = (u8)pxl8_clamp_byte((i32)(sr * sf + dr * df)); u8 out_g = (u8)pxl8_clamp_byte((i32)(sg * sf + dg * df)); @@ -1401,10 +1399,8 @@ static void execute_draw( continue; } - u64 raster_start = pxl8_get_ticks_ns(); rasterize_triangle(&setup, fb, zb, r->stencil, fb_w, shader, &pip->desc, &shader_bindings, &shader_uniforms); - r->stats.raster_ns += pxl8_get_ticks_ns() - raster_start; } } } @@ -1452,9 +1448,12 @@ void pxl8_gfx_submit(pxl8_blit3d* r, pxl8_gfx_cmdbuf* cb) { case PXL8_GFX_CMD_SET_DRAW_PARAMS: r->current_draw_params = cmd->draw_params; break; - case PXL8_GFX_CMD_DRAW: + case PXL8_GFX_CMD_DRAW: { + u64 raster_start = pxl8_get_ticks_ns(); execute_draw(r, &cmd->draw); + r->stats.raster_ns += pxl8_get_ticks_ns() - raster_start; break; + } case PXL8_GFX_CMD_RESOLVE: break; } diff --git a/src/gfx/pxl8_lightmap.c b/src/gfx/pxl8_lightmap.c index 8b7f340..d3f8ef3 100644 --- a/src/gfx/pxl8_lightmap.c +++ b/src/gfx/pxl8_lightmap.c @@ -1,4 +1,5 @@ #include "pxl8_lightmap.h" +#include "pxl8_math.h" #include "pxl8_mem.h" #include @@ -101,9 +102,9 @@ void pxl8_lightmap_add_point( i32 ng = (i32)lm->data[idx + 1] + (i32)((f32)(g - 128) * contrib); i32 nb = (i32)lm->data[idx + 2] + (i32)((f32)(b - 128) * contrib); - if (nr < 0) nr = 0; if (nr > 255) nr = 255; - if (ng < 0) ng = 0; if (ng > 255) ng = 255; - if (nb < 0) nb = 0; if (nb > 255) nb = 255; + nr = pxl8_clamp_byte(nr); + ng = pxl8_clamp_byte(ng); + nb = pxl8_clamp_byte(nb); lm->data[idx + 0] = (u8)nr; lm->data[idx + 1] = (u8)ng; diff --git a/src/gfx/pxl8_mesh.c b/src/gfx/pxl8_mesh.c index 3ad0926..e29e01d 100644 --- a/src/gfx/pxl8_mesh.c +++ b/src/gfx/pxl8_mesh.c @@ -79,21 +79,21 @@ void pxl8_mesh_push_box_uv(pxl8_mesh* mesh, pxl8_vec3 center, pxl8_vec3 half, u8 }; pxl8_vec3 normals[6] = { - { 0, 0, -1}, // front - { 0, 0, 1}, // back - {-1, 0, 0}, // left - { 1, 0, 0}, // right - { 0, -1, 0}, // bottom - { 0, 1, 0}, // top + { 0, 0, -1}, + { 0, 0, 1}, + {-1, 0, 0}, + { 1, 0, 0}, + { 0, -1, 0}, + { 0, 1, 0}, }; i32 faces[6][4] = { - {0, 1, 2, 3}, // front - {5, 4, 7, 6}, // back - {4, 0, 3, 7}, // left - {1, 5, 6, 2}, // right - {4, 5, 1, 0}, // bottom - {3, 2, 6, 7}, // top + {0, 1, 2, 3}, + {5, 4, 7, 6}, + {4, 0, 3, 7}, + {1, 5, 6, 2}, + {4, 5, 1, 0}, + {3, 2, 6, 7}, }; f32 uvs[4][2] = { diff --git a/src/gfx/pxl8_particles.c b/src/gfx/pxl8_particles.c index 9b2e5e7..19a62c0 100644 --- a/src/gfx/pxl8_particles.c +++ b/src/gfx/pxl8_particles.c @@ -11,7 +11,6 @@ struct pxl8_particles { pxl8_palette* palette; pxl8_rng* rng; u32 alive_count; - u32 count; u32 max_count; f32 x, y; diff --git a/src/gfx/pxl8_tilemap.c b/src/gfx/pxl8_tilemap.c index ea25e09..9e1bf9d 100644 --- a/src/gfx/pxl8_tilemap.c +++ b/src/gfx/pxl8_tilemap.c @@ -8,19 +8,6 @@ #include "pxl8_mem.h" #include "pxl8_tilesheet.h" -struct pxl8_tilesheet { - u8* data; - bool* tile_valid; - u32 height; - u32 tile_size; - u32 tiles_per_row; - u32 total_tiles; - u32 width; - u32 ref_count; - pxl8_tile_animation* animations; - u32 animation_count; - pxl8_tile_properties* properties; -}; struct pxl8_tilemap_layer { u32 allocated_chunks; diff --git a/src/gfx/pxl8_tilesheet.c b/src/gfx/pxl8_tilesheet.c index dc9b89b..e527fd0 100644 --- a/src/gfx/pxl8_tilesheet.c +++ b/src/gfx/pxl8_tilesheet.c @@ -8,26 +8,6 @@ #include "pxl8_mem.h" #include "pxl8_tilemap.h" -struct pxl8_tilesheet { - u8* data; - bool* tile_valid; - - u32 height; - u32 tile_size; - u32 tiles_per_row; - u32 total_tiles; - u32 width; - - u32 ref_count; - - pxl8_tile_animation* animations; - u32 animation_count; - - pxl8_tile_properties* properties; - - pxl8_autotile_rule** autotile_rules; - u32* autotile_rule_counts; -}; pxl8_tilesheet* pxl8_tilesheet_create(u32 tile_size) { pxl8_tilesheet* tilesheet = pxl8_calloc(1, sizeof(pxl8_tilesheet)); diff --git a/src/gfx/pxl8_tilesheet.h b/src/gfx/pxl8_tilesheet.h index 0eef5f1..5bd15eb 100644 --- a/src/gfx/pxl8_tilesheet.h +++ b/src/gfx/pxl8_tilesheet.h @@ -6,7 +6,27 @@ typedef struct pxl8_autotile_rule pxl8_autotile_rule; typedef struct pxl8_tile_animation pxl8_tile_animation; typedef struct pxl8_tile_properties pxl8_tile_properties; -typedef struct pxl8_tilesheet pxl8_tilesheet; + +typedef struct pxl8_tilesheet { + u8* data; + bool* tile_valid; + + u32 height; + u32 tile_size; + u32 tiles_per_row; + u32 total_tiles; + u32 width; + + u32 ref_count; + + pxl8_tile_animation* animations; + u32 animation_count; + + pxl8_tile_properties* properties; + + pxl8_autotile_rule** autotile_rules; + u32* autotile_rule_counts; +} pxl8_tilesheet; #ifdef __cplusplus extern "C" { diff --git a/src/gfx/pxl8_transition.c b/src/gfx/pxl8_transition.c index aca7696..d959472 100644 --- a/src/gfx/pxl8_transition.c +++ b/src/gfx/pxl8_transition.c @@ -39,9 +39,7 @@ void pxl8_transition_destroy(pxl8_transition* transition) { f32 pxl8_transition_get_progress(const pxl8_transition* transition) { if (!transition) return 0.0f; - f32 t = transition->time / transition->duration; - if (t < 0.0f) t = 0.0f; - if (t > 1.0f) t = 1.0f; + f32 t = pxl8_clamp(transition->time / transition->duration, 0.0f, 1.0f); return transition->reverse ? 1.0f - t : t; } diff --git a/src/gui/pxl8_gui.c b/src/gui/pxl8_gui.c index dfdeada..6ea89a7 100644 --- a/src/gui/pxl8_gui.c +++ b/src/gui/pxl8_gui.c @@ -4,6 +4,7 @@ #include #include "pxl8_gfx.h" +#include "pxl8_math.h" #include "pxl8_gui_palette.h" #include "pxl8_mem.h" @@ -132,9 +133,7 @@ bool pxl8_gui_slider(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, } if (is_active && state->cursor_down) { - f32 t = (f32)(state->cursor_x - x) / (f32)w; - if (t < 0.0f) t = 0.0f; - if (t > 1.0f) t = 1.0f; + f32 t = pxl8_clamp((f32)(state->cursor_x - x) / (f32)w, 0.0f, 1.0f); f32 new_val = min_val + t * (max_val - min_val); if (new_val != *value) { *value = new_val; diff --git a/src/math/pxl8_math.h b/src/math/pxl8_math.h index 0d5553a..9f68abc 100644 --- a/src/math/pxl8_math.h +++ b/src/math/pxl8_math.h @@ -46,9 +46,6 @@ typedef struct pxl8_vec4_simd { pxl8_f32_simd x, y, z, w; } pxl8_vec4_simd; #define pxl8_pow(x, y) powf(x, y) #define pxl8_lerp(a, b, t) ((a) + ((b) - (a)) * (t)) -f32 pxl8_fast_inv_sqrt(f32 x); -f32 pxl8_fast_rcp(f32 x); - typedef union pxl8_vec2 { struct { f32 x, y; }; struct { f32 yaw, pitch; }; @@ -100,6 +97,9 @@ typedef struct pxl8_ray { extern "C" { #endif +f32 pxl8_fast_inv_sqrt(f32 x); +f32 pxl8_fast_rcp(f32 x); + u32 pxl8_hash32(u32 x); u64 pxl8_hash64(u64 x);