paavohtl Claude Opus 4.6 (1M context) commited on
Commit
8304a79
Β·
1 Parent(s): 0c33b64

refactor: use stack arrays for temporary landscape buffers in DDGame ctor

Browse files

Replace wa_malloc/wa_free with stack arrays for three temporary buffers
(landscape_coords_buf, landscape_byte_buf, landscape_temp) that were
originally stack locals in the C++ code. Rename stack_local_8 to
landscape_temp for clarity.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

crates/openwa-core/src/engine/ddgame.rs CHANGED
@@ -974,13 +974,11 @@ unsafe fn init_graphics_and_resources(
974
  }
975
 
976
  // ── PCLandscape (alloc 0xB44, stdcall 11 params) ──
977
- // Allocate output buffers for landscape coordinate data (used later for coord_list)
978
- let landscape_coords_buf = wa_malloc(0x1000); // 4KB for coord output (aiStack_978)
979
- core::ptr::write_bytes(landscape_coords_buf, 0, 0x1000);
980
- let landscape_byte_buf = wa_malloc(0x100); // generous for byte output (iStack_11f9)
981
- core::ptr::write_bytes(landscape_byte_buf, 0, 0x100);
982
- let stack_local_8 = wa_malloc(0x1000); // stack local for coord count + temp data
983
- core::ptr::write_bytes(stack_local_8, 0, 0x1000);
984
 
985
  let landscape = {
986
  let alloc = wa_malloc(0xB44);
@@ -992,10 +990,10 @@ unsafe fn init_graphics_and_resources(
992
  gfx_resource,
993
  (*wrapper).display,
994
  (*game_info).landscape_data_path.as_ptr(),
995
- landscape_byte_buf,
996
  (*wrapper).gfx_mode,
997
- stack_local_8,
998
- landscape_coords_buf,
999
  &mut (*ddgame).level_width_raw as *mut u32 as *mut u8,
1000
  &mut (*ddgame).level_height_raw as *mut u32 as *mut u8,
1001
  );
@@ -1012,15 +1010,15 @@ unsafe fn init_graphics_and_resources(
1012
  // ── BitGrid at DDGame+0x380 (alloc 0x4C, memset 0x2C) ──
1013
  // Pure Rust: allocate object, call bit_grid_init, override vtable.
1014
  {
1015
- let tsm = wa_malloc(0x4C);
1016
- core::ptr::write_bytes(tsm, 0, 0x2C);
1017
- if !tsm.is_null() {
1018
  let width = (*ddgame).level_width;
1019
  let height = (*ddgame).level_height;
1020
- bit_grid_init(tsm, 1, width, height);
1021
- *(tsm as *mut u32) = rb(va::BIT_GRID_VARIANT_VTABLE);
1022
  }
1023
- (*ddgame).bit_grid = tsm;
1024
  }
1025
 
1026
  // ── 8Γ— SpriteRegion at DDGame+0x46C..0x488 ──
@@ -1170,11 +1168,11 @@ unsafe fn init_graphics_and_resources(
1170
  (*ddgame).coord_list = cl as *mut u8;
1171
 
1172
  // Populate coord_list from PCLandscape's coordinate output.
1173
- // stack_local_8[0] = coordinate count, landscape_coords_buf = pairs of (x, y).
1174
  // Original packs as: coord = x * 0x10000 + y (Fixed-point).
1175
  // Each coord_list entry is 8 bytes: [coord_value, 1]. Duplicates are skipped.
1176
- let coord_count = *(stack_local_8 as *const u32);
1177
- let coords_src = landscape_coords_buf as *const u32;
1178
  let data_ptr = data as *mut u32;
1179
  for j in 0..coord_count as usize {
1180
  let x = *coords_src.add(j * 2);
@@ -1200,10 +1198,7 @@ unsafe fn init_graphics_and_resources(
1200
  }
1201
  }
1202
 
1203
- // Free temporary landscape buffers (no longer needed after coord_list population)
1204
- crate::wa_alloc::wa_free(landscape_coords_buf);
1205
- crate::wa_alloc::wa_free(landscape_byte_buf);
1206
- crate::wa_alloc::wa_free(stack_local_8);
1207
 
1208
  // ── Loading progress ticks (2 of 4 β€” before load_resource_list) ──
1209
  call_usercall_ecx(wrapper, LOADING_PROGRESS_TICK_ADDR);
 
974
  }
975
 
976
  // ── PCLandscape (alloc 0xB44, stdcall 11 params) ──
977
+ // Temporary output buffers for landscape coordinate data (used later for coord_list).
978
+ // These were stack locals in the original code (aiStack_978, iStack_11f9).
979
+ let mut landscape_coords_buf = [0u8; 0x1000]; // 4KB for coord output
980
+ let mut landscape_byte_buf = [0u8; 0x100]; // byte output
981
+ let mut landscape_temp = [0u8; 0x1000]; // coord count + temp data
 
 
982
 
983
  let landscape = {
984
  let alloc = wa_malloc(0xB44);
 
990
  gfx_resource,
991
  (*wrapper).display,
992
  (*game_info).landscape_data_path.as_ptr(),
993
+ landscape_byte_buf.as_mut_ptr(),
994
  (*wrapper).gfx_mode,
995
+ landscape_temp.as_mut_ptr(),
996
+ landscape_coords_buf.as_mut_ptr(),
997
  &mut (*ddgame).level_width_raw as *mut u32 as *mut u8,
998
  &mut (*ddgame).level_height_raw as *mut u32 as *mut u8,
999
  );
 
1010
  // ── BitGrid at DDGame+0x380 (alloc 0x4C, memset 0x2C) ──
1011
  // Pure Rust: allocate object, call bit_grid_init, override vtable.
1012
  {
1013
+ let bit_grid = wa_malloc(0x4C);
1014
+ core::ptr::write_bytes(bit_grid, 0, 0x2C);
1015
+ if !bit_grid.is_null() {
1016
  let width = (*ddgame).level_width;
1017
  let height = (*ddgame).level_height;
1018
+ bit_grid_init(bit_grid, 1, width, height);
1019
+ *(bit_grid as *mut u32) = rb(va::BIT_GRID_VARIANT_VTABLE);
1020
  }
1021
+ (*ddgame).bit_grid = bit_grid;
1022
  }
1023
 
1024
  // ── 8Γ— SpriteRegion at DDGame+0x46C..0x488 ──
 
1168
  (*ddgame).coord_list = cl as *mut u8;
1169
 
1170
  // Populate coord_list from PCLandscape's coordinate output.
1171
+ // landscape_temp[0] = coordinate count, landscape_coords_buf = pairs of (x, y).
1172
  // Original packs as: coord = x * 0x10000 + y (Fixed-point).
1173
  // Each coord_list entry is 8 bytes: [coord_value, 1]. Duplicates are skipped.
1174
+ let coord_count = *(landscape_temp.as_ptr() as *const u32);
1175
+ let coords_src = landscape_coords_buf.as_ptr() as *const u32;
1176
  let data_ptr = data as *mut u32;
1177
  for j in 0..coord_count as usize {
1178
  let x = *coords_src.add(j * 2);
 
1198
  }
1199
  }
1200
 
1201
+ // Temporary landscape buffers (stack arrays) are dropped automatically here.
 
 
 
1202
 
1203
  // ── Loading progress ticks (2 of 4 β€” before load_resource_list) ──
1204
  call_usercall_ecx(wrapper, LOADING_PROGRESS_TICK_ADDR);