From 2a638dffe4b7b7c161e6a589b8d2f8abea2e2330 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Dec 2023 15:25:58 -0500 Subject: [PATCH] Day 47 complete Not much code, some fixes. --- project/codegen/engine_gen.cpp | 6 +- project/codegen/engine_postbuild_gen.cpp | 2 + project/codegen/platform_gen.cpp | 2 + project/engine/engine.cpp | 114 ++++++++++++++--------- project/engine/gen/physics.hpp | 12 +-- project/handmade.hpp | 2 +- project/platform/intrinsics.hpp | 20 +++- project/platform/macros.hpp | 3 + 8 files changed, 108 insertions(+), 53 deletions(-) diff --git a/project/codegen/engine_gen.cpp b/project/codegen/engine_gen.cpp index 8b83f3c..1f3bc83 100644 --- a/project/codegen/engine_gen.cpp +++ b/project/codegen/engine_gen.cpp @@ -5,6 +5,8 @@ #define GEN_BENCHMARK #define GEN_ENFORCE_STRONG_CODE_TYPES #include "dependencies/gen.hpp" +#undef min +#undef max #undef ccast #undef pcast #undef rcast @@ -319,7 +321,7 @@ Code gen__phys2( StrC type ) String sym_vec = String::fmt_buf( GlobalAllocator, "Vec2_%s", type.Ptr ); String sym_pos = String::fmt_buf( GlobalAllocator, "Pos2_%s", type.Ptr ); String sym_dir = String::fmt_buf( GlobalAllocator, "Dir2_%s", type.Ptr); - String sym_dist = String::fmt_buf( GlobalAllocator, "Dist2_%s", type.Ptr ); + String sym_dist = String::fmt_buf( GlobalAllocator, "Dist_%s", type.Ptr ); String sym_vel = String::fmt_buf( GlobalAllocator, "Vel2_%s", type.Ptr ); String sym_accel = String::fmt_buf( GlobalAllocator, "Accel2_%s", type.Ptr ); @@ -549,9 +551,9 @@ int gen_main() physics_header.print( gen_phys2( f32 ) ); physics_header.print( parse_global_body( code( + using Dist = Dist_f32; using Pos2 = Pos2_f32; using Dir2 = Dir2_f32; - using Dist2 = Dist2_f32; using Vel2 = Vel2_f32; using Accel2 = Accel2_f32; ))); diff --git a/project/codegen/engine_postbuild_gen.cpp b/project/codegen/engine_postbuild_gen.cpp index a87ecf0..383378c 100644 --- a/project/codegen/engine_postbuild_gen.cpp +++ b/project/codegen/engine_postbuild_gen.cpp @@ -13,6 +13,8 @@ #undef do_once #undef do_once_start #undef do_once_end +#undef min +#undef max using namespace gen; #include diff --git a/project/codegen/platform_gen.cpp b/project/codegen/platform_gen.cpp index 9b49bf5..83962db 100644 --- a/project/codegen/platform_gen.cpp +++ b/project/codegen/platform_gen.cpp @@ -4,6 +4,8 @@ #define GEN_BENCHMARK #define GEN_ENFORCE_STRONG_CODE_TYPES #include "dependencies/gen.hpp" +#undef min +#undef max using namespace gen; #define path_gen "./gen/" diff --git a/project/engine/engine.cpp b/project/engine/engine.cpp index f7362e7..f30b6d5 100644 --- a/project/engine/engine.cpp +++ b/project/engine/engine.cpp @@ -332,6 +332,8 @@ Bitmap load_bmp( platform::ModuleAPI* platform_api, Str file_path ) BitmapHeaderPacked* header = pcast(BitmapHeaderPacked*, file.data); assert( header->compression == 3 ); + + u32 alpha_mask = ~(header->red_mask | header->green_mask | header->blue_mask); // TODO(Ed) : Do not directly assign this, allocate the pixels to somewhere in game or engine persistent. result.pixels = rcast(u32*, rcast(Byte*, file.data) + header->bitmap_offset); @@ -339,20 +341,27 @@ Bitmap load_bmp( platform::ModuleAPI* platform_api, Str file_path ) result.height = header->height; result.bits_per_pixel = header->bits_per_pixel; - u32 red_shift = 0; - u32 green_shift = 0; - u32 blue_shift = 0; - u32 alpha_shift = 0; - - b32 red_found = bitscan_forward( & red_shift, header->red_mask ); - b32 green_found = bitscan_forward( & green_shift, header->green_mask ); - b32 blue_found = bitscan_forward( & blue_shift, header->blue_mask ); - b32 alpha_found = bitscan_forward( & alpha_shift, ~(header->red_mask | header->green_mask | header->blue_mask) ); + u32 red_scan = 0; + u32 green_scan = 0; + u32 blue_scan = 0; + u32 alpha_scan = 0; + b32 red_found = bitscan_forward( & red_scan, header->red_mask ); + b32 green_found = bitscan_forward( & green_scan, header->green_mask ); + b32 blue_found = bitscan_forward( & blue_scan, header->blue_mask ); + b32 alpha_found = bitscan_forward( & alpha_scan, alpha_mask ); + assert( red_found ); assert( green_found ); assert( blue_found ); assert( alpha_found ); + +#if 1 + s32 blue_shift = 0 - scast( s32, blue_scan ); + s32 green_shift = 8 - scast( s32, green_scan ); + s32 red_shift = 16 - scast( s32, red_scan ); + s32 alpha_shift = 24 - scast( s32, alpha_scan ); +#endif u32* src = result.pixels; for ( s32 y = 0; y < header->width; ++ y ) @@ -369,10 +378,17 @@ Bitmap load_bmp( platform::ModuleAPI* platform_api, Str file_path ) Pixel* px = rcast(Pixel*, src); - u32 alpha = (( *src >> alpha_shift ) & 0xFF) << 24; - u32 red = (( *src >> red_shift ) & 0xFF) << 16; - u32 green = (( *src >> green_shift ) & 0xFF) << 8; - u32 blue = (( *src >> blue_shift ) & 0xFF) << 0; + #if 0 + u32 alpha = (( *src >> alpha_scan ) & 0xFF) << 24; + u32 red = (( *src >> red_scan ) & 0xFF) << 16; + u32 green = (( *src >> green_scan ) & 0xFF) << 8; + u32 blue = (( *src >> blue_scan ) & 0xFF) << 0; + #else + u32 alpha = rotate_left( *src & alpha_mask, alpha_shift ); + u32 red = rotate_left( *src & header->red_mask, red_shift ); + u32 green = rotate_left( *src & header->green_mask, green_shift ); + u32 blue = rotate_left( *src & header->blue_mask, blue_shift ); + #endif *src = alpha | red | green | blue; ++ src; @@ -417,9 +433,6 @@ s32 get_entity( hh::GameState* gs ) inline void player_init( hh::Player* player, hh::GameState* gs ) { - player->controller.xpad_id = 0; - player->controller.ds_pad_id = 0; - hh::PlayerState* state = & player->state; s32 entity_id = get_entity( gs ); assert( entity_id ); @@ -433,7 +446,7 @@ void player_init( hh::Player* player, hh::GameState* gs ) entity->position.rel_pos.x = 0.f; entity->position.rel_pos.y = 0.f; - entity->move_velocity = {}; + entity->velocity = {}; state->mid_jump = false; state->jump_time = 0.f; @@ -555,18 +568,29 @@ void update_player( hh::Player* player, f32 delta_time, World* world, hh::GameSt player_move_vec.x = scast(f32, actions->player_x_move_digital ); player_move_vec.y = scast(f32, actions->player_y_move_digital ); } - + + // vec_x_adjusted = desired_vec_len / curr_vec_len * curr_vec_x + + // Allows for more ganular movement with the analog + if (0) + { + f32 accel_scale = magnitude( player_move_vec ); + if ( accel_scale > 1.f ) + accel_scale = 1.f; + move_accel *= accel_scale; + } + Dir2 player_direction = cast( Dir2, player_move_vec ); Accel2 player_move_accel = scast( Accel2, player_direction ) * move_accel; // TODO(Ed) : ODE! - Accel2 friction = pcast( Accel2, entity->move_velocity) * 9.f; + Accel2 friction = pcast( Accel2, entity->velocity) * 9.f; player_move_accel -= friction; - entity->move_velocity += player_move_accel * 0.5f; - new_player_pos += entity->move_velocity; + entity->velocity += player_move_accel * 0.5f; + new_player_pos += entity->velocity; - // Old collision implmentation + // Collision #if 1 { b32 collision_nw = false; @@ -642,10 +666,10 @@ void update_player( hh::Player* player, f32 delta_time, World* world, hh::GameSt //} // The 2x multiplier allows for the the "bounce off" velocity to occur instead of the player just looking like they impacted the wall and stopped - entity->move_velocity -= cast( Vel2, 1.f * scalar_product( Vec2( entity->move_velocity ), wall_vector ) * wall_vector ); + entity->velocity -= cast( Vel2, 1.f * scalar_product( Vec2( entity->velocity ), wall_vector ) * wall_vector ); new_player_pos = { old_pos.rel_pos.x, old_pos.rel_pos.y }; - new_player_pos += entity->move_velocity; + new_player_pos += entity->velocity; } TileMapPos new_pos = { @@ -662,16 +686,20 @@ void update_player( hh::Player* player, f32 delta_time, World* world, hh::GameSt old_pos.tile_x, old_pos.tile_y, old_pos.tile_z }; new_pos = recannonicalize_position( tile_map, new_pos ); - - s32 min_tile_x = 0; - s32 min_tile_y = 0; + + s32 min_tile_x = min( old_pos.tile_x, new_pos.tile_x ); + s32 min_tile_y = min( old_pos.tile_y, new_pos.tile_y ); + + s32 max_tile_x = max( old_pos.tile_x, new_pos.tile_x ); + s32 max_tile_y = max( old_pos.tile_y, new_pos.tile_y ); TileMapPos best_position = old_pos; - Dist2 best_distance2 = cast(Dist2, magnitude_squared( entity->move_velocity ) ); + + f32 min_interp_delta = 1.0f; - for ( s32 tile_y = 0; tile_y <= min_tile_y; ++ tile_y ) + for ( s32 tile_y = min_tile_y; tile_y <= max_tile_x; ++ tile_y ) { - for ( s32 tile_x = 0; tile_x <= min_tile_x; ++ tile_x ) + for ( s32 tile_x = min_tile_x; tile_x <= max_tile_x; ++ tile_x ) { TileMapPos test_tile_pos = centered_tile_point( tile_x, tile_y, new_pos.tile_z ); s32 tile_value = TileMap_get_tile_value( tile_map, test_tile_pos ); @@ -684,14 +712,16 @@ void update_player( hh::Player* player, f32 delta_time, World* world, hh::GameSt Vec2 max_corner = 0.5f * tile_xy_in_meters; TileMapPos rel_new_player_pos = subtract( test_tile_pos, new_pos ); - Vec2 test_pos = closest_point_in_rectangle( min_corner, max_corner, rel_new_player_pos ); - f32 test_dist2 = ; - if ( best_distance2 > test_dst ) - { - best_position = ; - best_distance2 = ; - } + // TODO(Ed) : Test all four walls and take the minimum Z + /* + ts = wx -p0x * 1/dx + + wall_x - start_pos_x * 1 / distance_interval_x + + result = wall.x - rel_new_player_pos.rel_pos.x * 1 / entity->velocity; + */ + test_wall( min_corner.x, rel_new_player_pos.rel_pos.x, Vec2 { min_corner.y, max_corner.y }, ); } } } @@ -721,13 +751,13 @@ void update_player( hh::Player* player, f32 delta_time, World* world, hh::GameSt using hh::FacingDirection_Left; using hh::FacingDirection_Right; - if ( is_nearly_zero( entity->move_velocity.x ) && is_nearly_zero( entity->move_velocity.y ) ) + if ( is_nearly_zero( entity->velocity.x ) && is_nearly_zero( entity->velocity.y ) ) { // Note(Ed): Leave facing directiona alone } - else if ( abs( entity->move_velocity.x ) > abs( entity->move_velocity.y ) ) + else if ( abs( entity->velocity.x ) > abs( entity->velocity.y ) ) { - if ( entity->move_velocity.x > 0 ) + if ( entity->velocity.x > 0 ) { entity->facing_direction = FacingDirection_Right; } @@ -736,9 +766,9 @@ void update_player( hh::Player* player, f32 delta_time, World* world, hh::GameSt entity->facing_direction = FacingDirection_Left; } } - else if ( abs( entity->move_velocity.x ) < abs( entity->move_velocity.y ) ) + else if ( abs( entity->velocity.x ) < abs( entity->velocity.y ) ) { - if ( entity->move_velocity.y >= 0 ) + if ( entity->velocity.y >= 0 ) { entity->facing_direction = FacingDirection_Back; } diff --git a/project/engine/gen/physics.hpp b/project/engine/gen/physics.hpp index ac44c21..5616ced 100644 --- a/project/engine/gen/physics.hpp +++ b/project/engine/gen/physics.hpp @@ -136,13 +136,13 @@ inline Pos2_f32& operator/=( Pos2_f32& v, f32 s ) return v; } -using Dist2_f32 = f32; +using Dist_f32 = f32; -inline Dist2_f32 distance( Pos2_f32 a, Pos2_f32 b ) +inline Dist_f32 distance( Pos2_f32 a, Pos2_f32 b ) { - f32 x = b.x - a.x; - f32 y = b.y - a.y; - Dist2_f32 result = sqrt( x * x + y * y ); + f32 x = b.x - a.x; + f32 y = b.y - a.y; + Dist_f32 result = sqrt( x * x + y * y ); return result; } @@ -495,8 +495,8 @@ inline Dir2_f32 direction( Accel2_f32 accel ) return result; } +using Dist = Dist_f32; using Pos2 = Pos2_f32; using Dir2 = Dir2_f32; -using Dist2 = Dist2_f32; using Vel2 = Vel2_f32; using Accel2 = Accel2_f32; diff --git a/project/handmade.hpp b/project/handmade.hpp index 7b91148..f5fa7a1 100644 --- a/project/handmade.hpp +++ b/project/handmade.hpp @@ -109,7 +109,7 @@ struct Entity EFacingDirection facing_direction; engine::TileMapPos position; - Vel2 move_velocity; + Vel2 velocity; }; struct PlayerState diff --git a/project/platform/intrinsics.hpp b/project/platform/intrinsics.hpp index 9bf23e9..e036cba 100644 --- a/project/platform/intrinsics.hpp +++ b/project/platform/intrinsics.hpp @@ -10,13 +10,15 @@ inline f32 abs( f32 value ) { - return fabsf(value); + f32 result = fabsf(value); + return result; } inline f32 sqrt( f32 value ) { - return sqrtf(value); + f32 result = sqrtf(value); + return result; } inline @@ -113,3 +115,17 @@ b32 bitscan_forward( u32* index, u32 value ) #endif return found; } + +inline +u32 rotate_left( u32 mask, s32 shift ) +{ + u32 result = _rotl( mask, shift); + return result; +} + +inline +u32 rotate_right( u32 mask, s32 shift ) +{ + u32 result = _rotl( mask, shift ); + return result; +} diff --git a/project/platform/macros.hpp b/project/platform/macros.hpp index 9cb49f9..00a2cf7 100644 --- a/project/platform/macros.hpp +++ b/project/platform/macros.hpp @@ -38,6 +38,9 @@ #define gigabytes( x ) ( megabytes( x ) * ( s64 )( 1024 ) ) #define terabytes( x ) ( gigabytes( x ) * ( s64 )( 1024 ) ) +#define max( a, b ) ( (a > b) ? (a) : (b) ) +#define min( a, b ) ( (a < b) ? (a) : (b) ) + // TODO(Ed) : Move to debug header eventually #if Build_Development