paavohtl Claude Opus 4.6 (1M context) commited on
Commit
8a23377
·
1 Parent(s): ccfa5da

feat: port stippled and tiled blit — remove fallthrough in slots 19/20

Browse files

Port DDDisplay__BlitStippled (per-pixel checkerboard XOR pattern) and
DDDisplay__BlitTiled (horizontal sprite tiling) to pure Rust, eliminating
fallthrough to the original WA code for these modes.

Slot 19 (BlitSprite) now only falls through for bitmap sprites.
Slot 20 (DrawScaledSprite) has no fallthrough at all.

Adds 6 snapshot tests (4 stippled + 2 tiled) validated against WA ground
truth, and consolidates snapshot capture into a single OPENWA_CAPTURE_SNAPSHOTS
env var.

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

crates/openwa-core/src/address.rs CHANGED
@@ -834,6 +834,9 @@ pub mod va {
834
  global G_SKIP_TO_MAIN_MENU = 0x007A_083D;
835
  global G_AUTO_NETWORK_FLAG = 0x007A_083F;
836
  global G_DD_DISPLAY_WRAPPER = 0x0079_D6D4;
 
 
 
837
  global G_FONT_ARRAY = 0x007A_0F58;
838
  global G_MAIN_MENU_ACTIVE = 0x007C_0A20;
839
  global G_CWINAPP = 0x007C_03D0;
 
834
  global G_SKIP_TO_MAIN_MENU = 0x007A_083D;
835
  global G_AUTO_NETWORK_FLAG = 0x007A_083F;
836
  global G_DD_DISPLAY_WRAPPER = 0x0079_D6D4;
837
+ /// Stipple checkerboard parity — toggled (XOR 1) each render frame in GameRender.
838
+ /// Used by DDDisplay__BlitStippled to alternate the checkerboard pattern.
839
+ global G_STIPPLE_PARITY = 0x007A_087C;
840
  global G_FONT_ARRAY = 0x007A_0F58;
841
  global G_MAIN_MENU_ACTIVE = 0x007C_0A20;
842
  global G_CWINAPP = 0x007C_03D0;
crates/openwa-core/src/display/dd_display.rs CHANGED
@@ -861,7 +861,7 @@ pub unsafe extern "thiscall" fn draw_via_callback(
861
  /// - Default: normal blit (color table mode for transparency)
862
  /// - 0x200000: additive blend via color_add_table LUT
863
  /// - 0x4000000: color blend via color_blend_table LUT
864
- /// - 0x8000000 / 0x10000000: stippled (checkerboard) blit — NOT YET PORTED, falls through
865
  ///
866
  /// Bit 20 controls the blend mode: clear = ColorTable (transparency), set = Copy (opaque).
867
  ///
@@ -903,9 +903,20 @@ pub unsafe fn draw_scaled_sprite(
903
  // Blend mode from flag bit 20: clear = 1 (ColorTable), set = 0 (Copy)
904
  let blend_mode = (!(flags >> 20)) & 1;
905
 
906
- // Stippled modes — not yet ported
907
  if (flags & 0x8000000) != 0 || (flags & 0x10000000) != 0 {
908
- return DrawScaledSpriteResult::Unhandled;
 
 
 
 
 
 
 
 
 
 
 
909
  }
910
 
911
  // Determine color table pointer from flags
@@ -965,10 +976,20 @@ pub enum DrawScaledSpriteResult {
965
  color_table: *const u8,
966
  blit_flags: u32,
967
  },
 
 
 
 
 
 
 
 
 
 
 
 
968
  /// Already handled (e.g. zero-size, early out).
969
  Handled,
970
- /// Unhandled mode — caller should fall through to original WA function.
971
- Unhandled,
972
  }
973
 
974
  /// Port of DDDisplay::DrawPixelStrip (vtable slot 15, 0x56BE10).
 
861
  /// - Default: normal blit (color table mode for transparency)
862
  /// - 0x200000: additive blend via color_add_table LUT
863
  /// - 0x4000000: color blend via color_blend_table LUT
864
+ /// - 0x8000000 / 0x10000000: stippled (checkerboard) blit
865
  ///
866
  /// Bit 20 controls the blend mode: clear = ColorTable (transparency), set = Copy (opaque).
867
  ///
 
903
  // Blend mode from flag bit 20: clear = 1 (ColorTable), set = 0 (Copy)
904
  let blend_mode = (!(flags >> 20)) & 1;
905
 
906
+ // Stippled modes (checkerboard blit)
907
  if (flags & 0x8000000) != 0 || (flags & 0x10000000) != 0 {
908
+ let stipple_mode: u32 = if (flags & 0x10000000) != 0 { 1 } else { 0 };
909
+ return DrawScaledSpriteResult::Stippled {
910
+ layer: (*gfx).layer_0,
911
+ dst_x,
912
+ dst_y,
913
+ width,
914
+ height,
915
+ sprite,
916
+ src_x,
917
+ src_y,
918
+ stipple_mode,
919
+ };
920
  }
921
 
922
  // Determine color table pointer from flags
 
976
  color_table: *const u8,
977
  blit_flags: u32,
978
  },
979
+ /// Stippled (checkerboard) blit — caller should use blit_stippled_raw.
980
+ Stippled {
981
+ layer: *mut DisplayBitGrid,
982
+ dst_x: i32,
983
+ dst_y: i32,
984
+ width: i32,
985
+ height: i32,
986
+ sprite: *mut DisplayBitGrid,
987
+ src_x: i32,
988
+ src_y: i32,
989
+ stipple_mode: u32,
990
+ },
991
  /// Already handled (e.g. zero-size, early out).
992
  Handled,
 
 
993
  }
994
 
995
  /// Port of DDDisplay::DrawPixelStrip (vtable slot 15, 0x56BE10).
crates/openwa-core/src/display/sprite_blit.rs CHANGED
@@ -551,6 +551,137 @@ pub fn pixel_grid_from_indexed(width: u32, height: u32, pixels: &[u8]) -> PixelG
551
  grid
552
  }
553
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
554
  #[cfg(test)]
555
  mod tests {
556
  use super::*;
@@ -1610,4 +1741,81 @@ mod tests {
1610
  0,
1611
  );
1612
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1613
  }
 
551
  grid
552
  }
553
 
554
+ // ---------------------------------------------------------------------------
555
+ // Stippled blit (checkerboard pattern)
556
+ // ---------------------------------------------------------------------------
557
+
558
+ /// Blit a sprite with a checkerboard (stippled) pattern.
559
+ ///
560
+ /// Port of DDDisplay__BlitStippled (0x56AEF0). Draws every other pixel
561
+ /// in a checkerboard pattern, creating a dithered transparency effect.
562
+ ///
563
+ /// The checkerboard is determined by: `(dst_x ^ parity ^ dst_y ^ mode) & 1`.
564
+ /// - `parity` alternates 0/1 each frame (g_StippleParity at 0x7A087C)
565
+ /// - `mode` is 0 or 1, inverting the pattern between the two stippled flag bits
566
+ ///
567
+ /// Only non-zero source pixels are drawn (transparent = 0).
568
+ /// Pixels outside the destination clip rect are skipped.
569
+ pub fn blit_stippled(
570
+ dst: &mut PixelGrid,
571
+ src: &BlitSource<'_>,
572
+ dst_x: i32,
573
+ dst_y: i32,
574
+ width: i32,
575
+ height: i32,
576
+ src_x: i32,
577
+ src_y: i32,
578
+ stipple_mode: u32,
579
+ parity: u32,
580
+ ) {
581
+ if width <= 0 || height <= 0 {
582
+ return;
583
+ }
584
+
585
+ // Source-to-destination offset (for mapping dst coords back to src)
586
+ let src_offset_x = src_x - dst_x;
587
+
588
+ for row in 0..height {
589
+ let sy = src_y + row;
590
+ let dy = (dst_y - src_y) + sy; // = dst_y + row
591
+
592
+ // Clip: skip rows outside destination
593
+ if dy < dst.clip_top as i32 || dy >= dst.clip_bottom as i32 {
594
+ continue;
595
+ }
596
+
597
+ for col in 0..width {
598
+ let dx = dst_x + col;
599
+
600
+ // Clip: skip columns outside destination
601
+ if dx < dst.clip_left as i32 || dx >= dst.clip_right as i32 {
602
+ continue;
603
+ }
604
+
605
+ // Checkerboard test — matching WA's XOR pattern
606
+ if (dx as u32 ^ parity ^ dy as u32 ^ stipple_mode) & 1 == 0 {
607
+ continue;
608
+ }
609
+
610
+ // Read source pixel (with bounds check matching get_pixel_clipped)
611
+ let sx = src_offset_x + dx;
612
+ if sx < 0 || sx >= src.width as i32 || sy < 0 || sy >= src.height as i32 {
613
+ continue;
614
+ }
615
+
616
+ let pixel = src.data[sy as usize * src.row_stride as usize + sx as usize];
617
+ if pixel != 0 {
618
+ dst.data[dy as usize * dst.row_stride as usize + dx as usize] = pixel;
619
+ }
620
+ }
621
+ }
622
+ }
623
+
624
+ // ---------------------------------------------------------------------------
625
+ // Tiled blit (horizontal tiling)
626
+ // ---------------------------------------------------------------------------
627
+
628
+ /// Blit a sprite tiled horizontally across a destination region.
629
+ ///
630
+ /// Port of DDDisplay__BlitTiled (0x56B000). Tiles the sprite from
631
+ /// `clip_left` to `clip_right` by repeatedly calling `blit_sprite_rect`.
632
+ ///
633
+ /// The `initial_x` is wrapped to the largest value <= `clip_left`
634
+ /// using the tiling width, then blits are emitted rightward.
635
+ ///
636
+ /// `flags` is the blit flags word (orientation + blend mode) passed through
637
+ /// to each individual `blit_sprite_rect` call.
638
+ pub fn blit_tiled(
639
+ dst: &mut PixelGrid,
640
+ src: &BlitSource<'_>,
641
+ initial_x: i32,
642
+ dst_y: i32,
643
+ tile_width: i32,
644
+ tile_height: i32,
645
+ clip_left: i32,
646
+ clip_right: i32,
647
+ color_table: Option<&[u8; 256]>,
648
+ flags: u32,
649
+ ) {
650
+ if tile_width <= 0 || tile_height <= 0 {
651
+ return;
652
+ }
653
+
654
+ let orientation = BlitOrientation::from_flags(flags);
655
+ let blend = BlitBlend::from_flags(flags);
656
+
657
+ // Wrap x to largest value <= clip_left (matching WA's wrapping loop)
658
+ let mut x = initial_x;
659
+ while x < clip_left {
660
+ x += tile_width;
661
+ }
662
+ while x > clip_left {
663
+ x -= tile_width;
664
+ }
665
+
666
+ // Tile rightward until past clip_right
667
+ while x < clip_right {
668
+ blit_sprite_rect(
669
+ dst,
670
+ src,
671
+ x,
672
+ dst_y,
673
+ tile_width,
674
+ tile_height,
675
+ 0,
676
+ 0,
677
+ color_table,
678
+ orientation,
679
+ blend,
680
+ );
681
+ x += tile_width;
682
+ }
683
+ }
684
+
685
  #[cfg(test)]
686
  mod tests {
687
  use super::*;
 
1741
  0,
1742
  );
1743
  }
1744
+
1745
+ // ---------------------------------------------------------------
1746
+ // Stippled blit snapshot tests
1747
+ // ---------------------------------------------------------------
1748
+
1749
+ /// Run a stippled blit and compare against WA snapshot.
1750
+ fn run_stippled_snapshot_test(snap_name: &str, stipple_mode: u32, parity: u32) {
1751
+ let path = test_asset_path("sprite_transparent_test.gif");
1752
+ let (sprite, _) = load_gif_indexed(&path);
1753
+ let src = BlitSource::from(&sprite);
1754
+ let sw = sprite.width as i32;
1755
+ let sh = sprite.height as i32;
1756
+
1757
+ let canvas_w = (sw + 32) as u32;
1758
+ let canvas_h = (sh + 32) as u32;
1759
+ let mut dst = PixelGrid::new(canvas_w, canvas_h);
1760
+ dst.data.fill(77); // background matching DLL capture
1761
+
1762
+ blit_stippled(&mut dst, &src, 16, 16, sw, sh, 0, 0, stipple_mode, parity);
1763
+
1764
+ assert_matches_snapshot(&dst, snap_name);
1765
+ }
1766
+
1767
+ #[test]
1768
+ fn snap_stippled_mode0_par0() {
1769
+ run_stippled_snapshot_test("stippled_mode0_par0", 0, 0);
1770
+ }
1771
+ #[test]
1772
+ fn snap_stippled_mode0_par1() {
1773
+ run_stippled_snapshot_test("stippled_mode0_par1", 0, 1);
1774
+ }
1775
+ #[test]
1776
+ fn snap_stippled_mode1_par0() {
1777
+ run_stippled_snapshot_test("stippled_mode1_par0", 1, 0);
1778
+ }
1779
+ #[test]
1780
+ fn snap_stippled_mode1_par1() {
1781
+ run_stippled_snapshot_test("stippled_mode1_par1", 1, 1);
1782
+ }
1783
+
1784
+ // ---------------------------------------------------------------
1785
+ // Tiled blit snapshot tests
1786
+ // ---------------------------------------------------------------
1787
+
1788
+ /// Run a tiled blit and compare against WA snapshot.
1789
+ fn run_tiled_snapshot_test(snap_name: &str, flags: u32, bg_fill: u8) {
1790
+ let path = test_asset_path("sprite_transparent_test.gif");
1791
+ let (sprite, _) = load_gif_indexed(&path);
1792
+ let src = BlitSource::from(&sprite);
1793
+ let sw = sprite.width as i32;
1794
+ let sh = sprite.height as i32;
1795
+
1796
+ // Match DLL capture: tile_w = sw * 4 + 32
1797
+ let canvas_w = (sw as u32) * 4 + 32;
1798
+ let canvas_h = (sh + 32) as u32;
1799
+ let mut dst = PixelGrid::new(canvas_w, canvas_h);
1800
+ dst.data.fill(bg_fill);
1801
+
1802
+ let clip_left = 0i32;
1803
+ let clip_right = canvas_w as i32;
1804
+
1805
+ blit_tiled(
1806
+ &mut dst, &src, 8, // initial_x matching DLL capture
1807
+ 16, sw, sh, clip_left, clip_right, None, flags,
1808
+ );
1809
+
1810
+ assert_matches_snapshot(&dst, snap_name);
1811
+ }
1812
+
1813
+ #[test]
1814
+ fn snap_tiled_transparent() {
1815
+ run_tiled_snapshot_test("tiled_transparent", 0x0000_0001, 77);
1816
+ }
1817
+ #[test]
1818
+ fn snap_tiled_copy() {
1819
+ run_tiled_snapshot_test("tiled_copy", 0x0000_0000, 0);
1820
+ }
1821
  }
crates/openwa-dll/src/replacements/bitgrid.rs CHANGED
@@ -21,7 +21,7 @@ use openwa_core::rebase::rb;
21
  ///
22
  /// Creates a test BitGrid, calls WA line functions with known inputs,
23
  /// saves pixel data to `testdata/snapshots/`. Activated by
24
- /// `OPENWA_CAPTURE_LINE_SNAPSHOTS=1`.
25
  pub unsafe fn capture_line_snapshots() {
26
  use openwa_core::bitgrid::DisplayBitGrid;
27
  use std::fs;
@@ -244,7 +244,7 @@ pub unsafe fn capture_line_snapshots() {
244
  /// Creates source and destination BitGrids, loads test GIF images,
245
  /// calls WA's blit with various orientations and blend modes,
246
  /// saves pixel output to `testdata/snapshots/`. Activated by
247
- /// `OPENWA_CAPTURE_BLIT_SNAPSHOTS=1`.
248
  pub unsafe fn capture_blit_snapshots() {
249
  use openwa_core::bitgrid::DisplayBitGrid;
250
  use std::fs;
@@ -435,6 +435,310 @@ pub unsafe fn capture_blit_snapshots() {
435
  ));
436
  }
437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
  /// Decoded indexed image (no WA dependencies).
439
  struct IndexedImage {
440
  width: u32,
@@ -656,6 +960,129 @@ pub(crate) unsafe extern "cdecl" fn blit_impl(
656
  result as u32
657
  }
658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
659
  /// Call the original WA blit function via trampoline.
660
  unsafe fn call_original_blit(
661
  dst: *mut openwa_core::bitgrid::DisplayBitGrid,
 
21
  ///
22
  /// Creates a test BitGrid, calls WA line functions with known inputs,
23
  /// saves pixel data to `testdata/snapshots/`. Activated by
24
+ /// `OPENWA_CAPTURE_SNAPSHOTS=1`.
25
  pub unsafe fn capture_line_snapshots() {
26
  use openwa_core::bitgrid::DisplayBitGrid;
27
  use std::fs;
 
244
  /// Creates source and destination BitGrids, loads test GIF images,
245
  /// calls WA's blit with various orientations and blend modes,
246
  /// saves pixel output to `testdata/snapshots/`. Activated by
247
+ /// `OPENWA_CAPTURE_SNAPSHOTS=1`.
248
  pub unsafe fn capture_blit_snapshots() {
249
  use openwa_core::bitgrid::DisplayBitGrid;
250
  use std::fs;
 
435
  ));
436
  }
437
 
438
+ // =========================================================================
439
+ // Stippled & tiled blit snapshot capture
440
+ // =========================================================================
441
+
442
+ /// Capture stippled and tiled blit snapshots from WA's native functions.
443
+ ///
444
+ /// Creates synthetic BitGrids and a minimal fake DDDisplay context (render_lock=1
445
+ /// so AcquireRenderLock is a no-op), then calls the original WA functions.
446
+ ///
447
+ /// Activated by `OPENWA_CAPTURE_SNAPSHOTS=1`.
448
+ pub unsafe fn capture_stippled_tiled_snapshots() {
449
+ use openwa_core::bitgrid::DisplayBitGrid;
450
+ use openwa_core::rebase::rb;
451
+ use std::fs;
452
+ use std::io::Write;
453
+
454
+ let _ = log_line("[BitGrid] STIPPLED/TILED SNAPSHOT: Starting capture");
455
+
456
+ const PROJECT_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../..");
457
+
458
+ // Load test image
459
+ let img_data = match fs::read(format!(
460
+ "{PROJECT_ROOT}/testdata/assets/sprite_transparent_test.gif"
461
+ )) {
462
+ Ok(d) => d,
463
+ Err(e) => {
464
+ let _ = log_line(&format!(
465
+ "[BitGrid] STIPPLED/TILED SNAPSHOT: Failed to read test image: {e}"
466
+ ));
467
+ return;
468
+ }
469
+ };
470
+ let img = match decode_gif_indexed(&img_data) {
471
+ Some(i) => i,
472
+ None => {
473
+ let _ = log_line("[BitGrid] STIPPLED/TILED SNAPSHOT: Failed to decode test image");
474
+ return;
475
+ }
476
+ };
477
+
478
+ let dir = format!("{PROJECT_ROOT}/testdata/snapshots");
479
+ let _ = fs::create_dir_all(&dir);
480
+ let mut count = 0u32;
481
+
482
+ let sw = img.width as i32;
483
+ let sh = img.height as i32;
484
+
485
+ // Create source BitGrid
486
+ let src = DisplayBitGrid::alloc(8, img.width, img.height);
487
+ if src.is_null() {
488
+ let _ = log_line("[BitGrid] STIPPLED/TILED SNAPSHOT: Failed to allocate source");
489
+ return;
490
+ }
491
+ for y in 0..img.height {
492
+ let src_row = (y * img.width) as usize;
493
+ let dst_row = (*src).data.add((y * (*src).row_stride) as usize);
494
+ core::ptr::copy_nonoverlapping(
495
+ img.pixels.as_ptr().add(src_row),
496
+ dst_row,
497
+ img.width as usize,
498
+ );
499
+ }
500
+ (*src).clip_left = 0;
501
+ (*src).clip_top = 0;
502
+ (*src).clip_right = img.width;
503
+ (*src).clip_bottom = img.height;
504
+
505
+ // Save grid helper
506
+ let save_grid = |grid: *mut DisplayBitGrid, name: &str| {
507
+ let w = (*grid).width;
508
+ let h = (*grid).height;
509
+ let stride = (*grid).row_stride;
510
+ let data = (*grid).data;
511
+ let data_size = (stride * h) as usize;
512
+ let path = format!("{dir}/{name}.bin");
513
+ if let Ok(mut file) = fs::File::create(&path) {
514
+ let _ = file.write_all(&w.to_le_bytes());
515
+ let _ = file.write_all(&h.to_le_bytes());
516
+ let _ = file.write_all(&stride.to_le_bytes());
517
+ let _ = file.write_all(core::slice::from_raw_parts(data, data_size));
518
+ }
519
+ };
520
+
521
+ // ---------------------------------------------------------------
522
+ // Stippled blit capture
523
+ // ---------------------------------------------------------------
524
+ // DDDisplay__BlitStippled (0x56AEF0):
525
+ // usercall: EAX = height, ESI set internally from stack param
526
+ // 8 stack params (RET 0x20):
527
+ // this, dst_x, dst_y, width, sprite, src_x, src_y, stipple_mode
528
+ // Accesses this+0x3D98 (render_lock) and this+0x3D9C (layer_0).
529
+ //
530
+ // We create a minimal fake DDDisplay context with render_lock=1
531
+ // (so AcquireRenderLock skips) and layer_0 pointing to our dst grid.
532
+ {
533
+ let dw = (sw + 32) as u32;
534
+ let dh = (sh + 32) as u32;
535
+ let dst = DisplayBitGrid::alloc(8, dw, dh);
536
+ if dst.is_null() {
537
+ let _ = log_line("[BitGrid] STIPPLED/TILED SNAPSHOT: Failed to allocate stipple dst");
538
+ } else {
539
+ let dst_data = (*dst).data;
540
+ let dst_size = ((*dst).row_stride * dh) as usize;
541
+
542
+ // Fake DDDisplay context — only fields at 0x3D98 and 0x3D9C matter.
543
+ const FAKE_SIZE: usize = 0x3DA0;
544
+ let fake_display: Vec<u8> = vec![0u8; FAKE_SIZE];
545
+ let fake_ptr = fake_display.as_ptr() as *mut u8;
546
+
547
+ // render_lock = 1 (skip AcquireRenderLock)
548
+ *(fake_ptr.add(0x3D98) as *mut u32) = 1;
549
+ // layer_0 = our destination grid
550
+ *(fake_ptr.add(0x3D9C) as *mut *mut DisplayBitGrid) = dst;
551
+
552
+ // Save and set g_StippleParity to known values
553
+ let parity_ptr = rb(openwa_core::address::va::G_STIPPLE_PARITY) as *mut u32;
554
+ let saved_parity = *parity_ptr;
555
+
556
+ // Test cases: (name, dst_x, dst_y, width, height, src_x, src_y, stipple_mode, parity, bg)
557
+ #[allow(clippy::type_complexity)]
558
+ let test_cases: &[(&str, u32, i32, i32, i32, i32, i32, u32, u32, u8)] = &[
559
+ // Mode 0 with parity 0
560
+ ("stippled_mode0_par0", 16, 16, sw, sh, 0, 0, 0, 0, 77),
561
+ // Mode 0 with parity 1 (inverted checkerboard)
562
+ ("stippled_mode0_par1", 16, 16, sw, sh, 0, 0, 0, 1, 77),
563
+ // Mode 1 with parity 0
564
+ ("stippled_mode1_par0", 16, 16, sw, sh, 0, 0, 1, 0, 77),
565
+ // Mode 1 with parity 1
566
+ ("stippled_mode1_par1", 16, 16, sw, sh, 0, 0, 1, 1, 77),
567
+ ];
568
+
569
+ let blit_stippled_addr = rb(0x0056_AEF0);
570
+
571
+ for &(suffix, dx, dy, w, h, sx, sy, mode, parity, bg) in test_cases {
572
+ core::ptr::write_bytes(dst_data, bg, dst_size);
573
+ *parity_ptr = parity;
574
+
575
+ wa_blit_stippled(
576
+ fake_ptr,
577
+ dx,
578
+ dy,
579
+ w,
580
+ src,
581
+ sx,
582
+ sy,
583
+ mode,
584
+ h,
585
+ blit_stippled_addr,
586
+ );
587
+
588
+ save_grid(dst, suffix);
589
+ count += 1;
590
+ }
591
+
592
+ // Restore parity
593
+ *parity_ptr = saved_parity;
594
+
595
+ let destructor = (*(*dst).vtable).destructor;
596
+ destructor(dst, 1);
597
+ }
598
+ }
599
+
600
+ // ---------------------------------------------------------------
601
+ // Tiled blit capture
602
+ // ---------------------------------------------------------------
603
+ // DDDisplay__BlitTiled tiles BlitSpriteRect horizontally.
604
+ // Rather than calling the original (which needs complex DDDisplay setup),
605
+ // we replicate the tiling loop using wa_blit_sprite_rect (original blit).
606
+ {
607
+ // Wide destination for visible tiling
608
+ let tile_w = (sw as u32) * 4 + 32;
609
+ let tile_h = (sh + 32) as u32;
610
+ let dst = DisplayBitGrid::alloc(8, tile_w, tile_h);
611
+ if dst.is_null() {
612
+ let _ = log_line("[BitGrid] STIPPLED/TILED SNAPSHOT: Failed to allocate tiled dst");
613
+ } else {
614
+ let dst_data = (*dst).data;
615
+ let dst_size = ((*dst).row_stride * tile_h) as usize;
616
+
617
+ let orig = ORIG_BLIT.load(core::sync::atomic::Ordering::Relaxed);
618
+ let blit_target = if orig != 0 {
619
+ orig
620
+ } else {
621
+ rb(openwa_core::address::va::BLIT_SPRITE_RECT)
622
+ };
623
+
624
+ // Tile modes: blend mode 1 (color table / transparency, no color table = skip zero)
625
+ // with orient = Normal (0)
626
+ let flags: u32 = 0x0000_0001; // blend=1 (color_table transparency), orient=Normal
627
+
628
+ // Test: tile sprite across the full width, starting from x=0
629
+ // Clip rect = full destination
630
+ let clip_left: i32 = 0;
631
+ let clip_right: i32 = tile_w as i32;
632
+ let dst_y: i32 = 16;
633
+
634
+ // Replicate BlitTiled wrapping logic from the original:
635
+ // while (x < clip_left) x += width;
636
+ // while (x > clip_left) x -= width;
637
+ // This wraps x to the largest value <= clip_left.
638
+ // Start from an arbitrary offset to test partial-tile clipping.
639
+ let mut start_x: i32 = 8;
640
+ while start_x < clip_left {
641
+ start_x += sw;
642
+ }
643
+ while start_x > clip_left {
644
+ start_x -= sw;
645
+ }
646
+
647
+ // Tiled with transparent blend
648
+ core::ptr::write_bytes(dst_data, 77, dst_size);
649
+ let mut x = start_x;
650
+ while x < clip_right {
651
+ wa_blit_sprite_rect(
652
+ dst,
653
+ x,
654
+ dst_y,
655
+ sw,
656
+ sh,
657
+ src,
658
+ 0,
659
+ 0,
660
+ core::ptr::null(),
661
+ flags,
662
+ blit_target,
663
+ );
664
+ x += sw;
665
+ }
666
+ save_grid(dst, "tiled_transparent");
667
+ count += 1;
668
+
669
+ // Tiled with copy mode (flags=0)
670
+ core::ptr::write_bytes(dst_data, 0, dst_size);
671
+ let mut x = start_x;
672
+ while x < clip_right {
673
+ wa_blit_sprite_rect(
674
+ dst,
675
+ x,
676
+ dst_y,
677
+ sw,
678
+ sh,
679
+ src,
680
+ 0,
681
+ 0,
682
+ core::ptr::null(),
683
+ 0x0000_0000, // copy mode
684
+ blit_target,
685
+ );
686
+ x += sw;
687
+ }
688
+ save_grid(dst, "tiled_copy");
689
+ count += 1;
690
+
691
+ let destructor = (*(*dst).vtable).destructor;
692
+ destructor(dst, 1);
693
+ }
694
+ }
695
+
696
+ // Free source
697
+ let destructor = (*(*src).vtable).destructor;
698
+ destructor(src, 1);
699
+
700
+ let _ = log_line(&format!(
701
+ "[BitGrid] STIPPLED/TILED SNAPSHOT: Saved {count} snapshots to {dir}/"
702
+ ));
703
+ }
704
+
705
+ /// Call WA's DDDisplay__BlitStippled (0x56AEF0).
706
+ ///
707
+ /// Usercall: EAX = height, 8 stdcall params (RET 0x20).
708
+ /// `target` is the rebased runtime address.
709
+ #[unsafe(naked)]
710
+ unsafe extern "cdecl" fn wa_blit_stippled(
711
+ _this: *mut u8,
712
+ _dst_x: u32,
713
+ _dst_y: i32,
714
+ _width: i32,
715
+ _sprite: *mut openwa_core::bitgrid::DisplayBitGrid,
716
+ _src_x: i32,
717
+ _src_y: i32,
718
+ _stipple_mode: u32,
719
+ _height: i32,
720
+ _target: u32,
721
+ ) {
722
+ core::arch::naked_asm!(
723
+ "push esi",
724
+ "push edi",
725
+ "mov eax, [esp + 44]", // height (param 9, offset: 8 + 9*4 = 44 from original ESP, +8 for our pushes)
726
+ "mov edi, [esp + 48]", // target (param 10)
727
+ "push dword ptr [esp + 40]", // stipple_mode
728
+ "push dword ptr [esp + 40]", // src_y
729
+ "push dword ptr [esp + 40]", // src_x
730
+ "push dword ptr [esp + 40]", // sprite
731
+ "push dword ptr [esp + 40]", // width
732
+ "push dword ptr [esp + 40]", // dst_y
733
+ "push dword ptr [esp + 40]", // dst_x
734
+ "push dword ptr [esp + 40]", // this
735
+ "call edi", // RET 0x20 cleans 8 params (32 bytes)
736
+ "pop edi",
737
+ "pop esi",
738
+ "ret",
739
+ );
740
+ }
741
+
742
  /// Decoded indexed image (no WA dependencies).
743
  struct IndexedImage {
744
  width: u32,
 
960
  result as u32
961
  }
962
 
963
+ /// Stippled blit on raw BitGrid pointers.
964
+ ///
965
+ /// Wraps the raw pointers into PixelGrid/BlitSource and calls
966
+ /// `sprite_blit::blit_stippled`.
967
+ pub(crate) unsafe fn blit_stippled_raw(
968
+ dst: *mut openwa_core::bitgrid::DisplayBitGrid,
969
+ src: *mut openwa_core::bitgrid::DisplayBitGrid,
970
+ dst_x: i32,
971
+ dst_y: i32,
972
+ width: i32,
973
+ height: i32,
974
+ src_x: i32,
975
+ src_y: i32,
976
+ stipple_mode: u32,
977
+ parity: u32,
978
+ ) {
979
+ use openwa_core::display::sprite_blit::{blit_stippled, BlitSource};
980
+
981
+ let src_data =
982
+ core::slice::from_raw_parts((*src).data, ((*src).row_stride * (*src).height) as usize);
983
+ let blit_src = BlitSource {
984
+ data: src_data,
985
+ width: (*src).width,
986
+ height: (*src).height,
987
+ row_stride: (*src).row_stride,
988
+ };
989
+
990
+ let dst_stride = (*dst).row_stride;
991
+ let dst_h = (*dst).height;
992
+ let dst_w = (*dst).width;
993
+ let dst_data_len = (dst_stride * dst_h) as usize;
994
+
995
+ let mut dst_grid = openwa_core::display::line_draw::PixelGrid {
996
+ data: Vec::new(),
997
+ width: dst_w,
998
+ height: dst_h,
999
+ row_stride: dst_stride,
1000
+ clip_left: (*dst).clip_left,
1001
+ clip_top: (*dst).clip_top,
1002
+ clip_right: (*dst).clip_right,
1003
+ clip_bottom: (*dst).clip_bottom,
1004
+ };
1005
+ let mut real_data = Vec::from_raw_parts((*dst).data, dst_data_len, dst_data_len);
1006
+ core::mem::swap(&mut dst_grid.data, &mut real_data);
1007
+
1008
+ blit_stippled(
1009
+ &mut dst_grid,
1010
+ &blit_src,
1011
+ dst_x,
1012
+ dst_y,
1013
+ width,
1014
+ height,
1015
+ src_x,
1016
+ src_y,
1017
+ stipple_mode,
1018
+ parity,
1019
+ );
1020
+
1021
+ core::mem::swap(&mut dst_grid.data, &mut real_data);
1022
+ core::mem::forget(real_data);
1023
+ }
1024
+
1025
+ /// Tiled blit on raw BitGrid pointers.
1026
+ ///
1027
+ /// Wraps the raw pointers into PixelGrid/BlitSource and calls
1028
+ /// `sprite_blit::blit_tiled`.
1029
+ pub(crate) unsafe fn blit_tiled_raw(
1030
+ dst: *mut openwa_core::bitgrid::DisplayBitGrid,
1031
+ src: *mut openwa_core::bitgrid::DisplayBitGrid,
1032
+ initial_x: i32,
1033
+ dst_y: i32,
1034
+ tile_width: i32,
1035
+ tile_height: i32,
1036
+ clip_left: i32,
1037
+ clip_right: i32,
1038
+ flags: u32,
1039
+ ) {
1040
+ use openwa_core::display::sprite_blit::{blit_tiled, BlitSource};
1041
+
1042
+ let src_data =
1043
+ core::slice::from_raw_parts((*src).data, ((*src).row_stride * (*src).height) as usize);
1044
+ let blit_src = BlitSource {
1045
+ data: src_data,
1046
+ width: (*src).width,
1047
+ height: (*src).height,
1048
+ row_stride: (*src).row_stride,
1049
+ };
1050
+
1051
+ let dst_stride = (*dst).row_stride;
1052
+ let dst_h = (*dst).height;
1053
+ let dst_w = (*dst).width;
1054
+ let dst_data_len = (dst_stride * dst_h) as usize;
1055
+
1056
+ let mut dst_grid = openwa_core::display::line_draw::PixelGrid {
1057
+ data: Vec::new(),
1058
+ width: dst_w,
1059
+ height: dst_h,
1060
+ row_stride: dst_stride,
1061
+ clip_left: (*dst).clip_left,
1062
+ clip_top: (*dst).clip_top,
1063
+ clip_right: (*dst).clip_right,
1064
+ clip_bottom: (*dst).clip_bottom,
1065
+ };
1066
+ let mut real_data = Vec::from_raw_parts((*dst).data, dst_data_len, dst_data_len);
1067
+ core::mem::swap(&mut dst_grid.data, &mut real_data);
1068
+
1069
+ blit_tiled(
1070
+ &mut dst_grid,
1071
+ &blit_src,
1072
+ initial_x,
1073
+ dst_y,
1074
+ tile_width,
1075
+ tile_height,
1076
+ clip_left,
1077
+ clip_right,
1078
+ None, // color_table — tiled mode doesn't use one
1079
+ flags,
1080
+ );
1081
+
1082
+ core::mem::swap(&mut dst_grid.data, &mut real_data);
1083
+ core::mem::forget(real_data);
1084
+ }
1085
+
1086
  /// Call the original WA blit function via trampoline.
1087
  unsafe fn call_original_blit(
1088
  dst: *mut openwa_core::bitgrid::DisplayBitGrid,
crates/openwa-dll/src/replacements/display.rs CHANGED
@@ -55,7 +55,6 @@ unsafe extern "thiscall" fn headless_destructor(
55
  mod originals {
56
  use std::sync::atomic::AtomicU32;
57
  pub static BLIT_SPRITE: AtomicU32 = AtomicU32::new(0);
58
- pub static DRAW_SCALED_SPRITE: AtomicU32 = AtomicU32::new(0);
59
  }
60
 
61
  /// Rust port of DDDisplay::BlitSprite (slot 19, 0x56B080).
@@ -351,12 +350,51 @@ unsafe extern "thiscall" fn blit_sprite(
351
  // Blit dispatch based on high_flags
352
  // ---------------------------------------------------------------
353
 
354
- // Stippled/tiled modes — fall through to original
355
- if (high_flags & 0x0800_0000) != 0
356
- || (high_flags & 0x1000_0000) != 0
357
- || (high_flags & 0x0001_0000) != 0
358
- {
359
- call_original_blit_sprite(this, x, y, sprite_flags, palette);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
  return;
361
  }
362
 
@@ -369,10 +407,6 @@ unsafe extern "thiscall" fn blit_sprite(
369
  core::ptr::null()
370
  };
371
 
372
- if blit_w <= 0 || blit_h <= 0 {
373
- return;
374
- }
375
-
376
  dd_display::acquire_render_lock(gfx);
377
 
378
  // src_x=0, src_y=0 always — vtable[33] already set up the sprite surface
@@ -409,7 +443,6 @@ unsafe fn call_original_blit_sprite(
409
  /// Thiscall wrapper for DDDisplay::DrawScaledSprite (slot 20).
410
  ///
411
  /// Computes coordinates in core, then dispatches the blit via blit_impl.
412
- /// Falls through to the original WA function for unhandled modes (stippled).
413
  unsafe extern "thiscall" fn draw_scaled_sprite(
414
  this: *mut DDDisplay,
415
  x: Fixed,
@@ -449,25 +482,32 @@ unsafe extern "thiscall" fn draw_scaled_sprite(
449
  blit_flags,
450
  );
451
  }
452
- DrawScaledSpriteResult::Handled => {}
453
- DrawScaledSpriteResult::Unhandled => {
454
- // Fall through to original WA function for stippled modes
455
- let orig = originals::DRAW_SCALED_SPRITE.load(Ordering::Relaxed);
456
- if orig != 0 {
457
- let f: unsafe extern "thiscall" fn(
458
- *mut DDDisplay,
459
- Fixed,
460
- Fixed,
461
- *mut DisplayBitGrid,
462
- i32,
463
- i32,
464
- i32,
465
- i32,
466
- u32,
467
- ) = core::mem::transmute(orig as usize);
468
- f(this, x, y, sprite, src_x, src_y, src_w, src_h, flags);
469
- }
 
 
 
 
 
 
470
  }
 
471
  }
472
  }
473
 
@@ -517,7 +557,7 @@ pub fn install() -> Result<(), String> {
517
  set_camera_offset => dd_display::set_camera_offset,
518
  set_clip_rect => dd_display::set_clip_rect,
519
  is_sprite_loaded => dd_display::is_sprite_loaded,
520
- draw_scaled_sprite [originals::DRAW_SCALED_SPRITE] => draw_scaled_sprite,
521
  slot 19 [originals::BLIT_SPRITE] => blit_sprite,
522
  })?;
523
  let _ = log_line("[Display] DDDisplay: patched 15 methods → Rust");
 
55
  mod originals {
56
  use std::sync::atomic::AtomicU32;
57
  pub static BLIT_SPRITE: AtomicU32 = AtomicU32::new(0);
 
58
  }
59
 
60
  /// Rust port of DDDisplay::BlitSprite (slot 19, 0x56B080).
 
350
  // Blit dispatch based on high_flags
351
  // ---------------------------------------------------------------
352
 
353
+ if blit_w <= 0 || blit_h <= 0 {
354
+ return;
355
+ }
356
+
357
+ // Stippled mode (checkerboard per-pixel blit)
358
+ if (high_flags & 0x0800_0000) != 0 || (high_flags & 0x1000_0000) != 0 {
359
+ let stipple_mode: u32 = if (high_flags & 0x1000_0000) != 0 {
360
+ 1
361
+ } else {
362
+ 0
363
+ };
364
+ let parity = *(rb(openwa_core::address::va::G_STIPPLE_PARITY) as *const u32);
365
+
366
+ dd_display::acquire_render_lock(gfx);
367
+
368
+ super::bitgrid::blit_stippled_raw(
369
+ (*gfx).layer_0,
370
+ sprite_surface,
371
+ dst_x,
372
+ dst_y,
373
+ blit_w,
374
+ blit_h,
375
+ 0,
376
+ 0,
377
+ stipple_mode,
378
+ parity,
379
+ );
380
+ return;
381
+ }
382
+
383
+ // Tiled mode (horizontal sprite tiling)
384
+ if (high_flags & 0x0001_0000) != 0 {
385
+ dd_display::acquire_render_lock(gfx);
386
+
387
+ super::bitgrid::blit_tiled_raw(
388
+ (*gfx).layer_0,
389
+ sprite_surface,
390
+ dst_x,
391
+ dst_y,
392
+ blit_w,
393
+ blit_h,
394
+ (*gfx).base.clip_x1,
395
+ (*gfx).base.clip_x2,
396
+ orient_local,
397
+ );
398
  return;
399
  }
400
 
 
407
  core::ptr::null()
408
  };
409
 
 
 
 
 
410
  dd_display::acquire_render_lock(gfx);
411
 
412
  // src_x=0, src_y=0 always — vtable[33] already set up the sprite surface
 
443
  /// Thiscall wrapper for DDDisplay::DrawScaledSprite (slot 20).
444
  ///
445
  /// Computes coordinates in core, then dispatches the blit via blit_impl.
 
446
  unsafe extern "thiscall" fn draw_scaled_sprite(
447
  this: *mut DDDisplay,
448
  x: Fixed,
 
482
  blit_flags,
483
  );
484
  }
485
+ DrawScaledSpriteResult::Stippled {
486
+ layer,
487
+ dst_x,
488
+ dst_y,
489
+ width,
490
+ height,
491
+ sprite,
492
+ src_x,
493
+ src_y,
494
+ stipple_mode,
495
+ } => {
496
+ let parity = *(rb(openwa_core::address::va::G_STIPPLE_PARITY) as *const u32);
497
+ super::bitgrid::blit_stippled_raw(
498
+ layer,
499
+ sprite,
500
+ dst_x,
501
+ dst_y,
502
+ width,
503
+ height,
504
+ src_x,
505
+ src_y,
506
+ stipple_mode,
507
+ parity,
508
+ );
509
  }
510
+ DrawScaledSpriteResult::Handled => {}
511
  }
512
  }
513
 
 
557
  set_camera_offset => dd_display::set_camera_offset,
558
  set_clip_rect => dd_display::set_clip_rect,
559
  is_sprite_loaded => dd_display::is_sprite_loaded,
560
+ draw_scaled_sprite => draw_scaled_sprite,
561
  slot 19 [originals::BLIT_SPRITE] => blit_sprite,
562
  })?;
563
  let _ = log_line("[Display] DDDisplay: patched 15 methods → Rust");
crates/openwa-dll/src/replacements/frame_hook.rs CHANGED
@@ -63,14 +63,11 @@ unsafe extern "stdcall" fn hook_turn_manager(turngame: u32) {
63
 
64
  let frame = FRAMES_PROCESSED.fetch_add(1, Ordering::Relaxed) + 1;
65
 
66
- // Snapshot capture: run once at frame 10 when env vars are set
67
- if frame == 10 {
68
- if std::env::var("OPENWA_CAPTURE_LINE_SNAPSHOTS").is_ok() {
69
- super::bitgrid::capture_line_snapshots();
70
- }
71
- if std::env::var("OPENWA_CAPTURE_BLIT_SNAPSHOTS").is_ok() {
72
- super::bitgrid::capture_blit_snapshots();
73
- }
74
  }
75
 
76
  // Replay test: fast-forward + milestone tracking (no-op if not in replay test mode)
 
63
 
64
  let frame = FRAMES_PROCESSED.fetch_add(1, Ordering::Relaxed) + 1;
65
 
66
+ // Snapshot capture: run all captures once at frame 10 when env var is set
67
+ if frame == 10 && std::env::var("OPENWA_CAPTURE_SNAPSHOTS").is_ok() {
68
+ super::bitgrid::capture_line_snapshots();
69
+ super::bitgrid::capture_blit_snapshots();
70
+ super::bitgrid::capture_stippled_tiled_snapshots();
 
 
 
71
  }
72
 
73
  // Replay test: fast-forward + milestone tracking (no-op if not in replay test mode)
testdata/snapshots/stippled_mode0_par0.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b14bb45346401b853304f3ca75a1b17443e794bc23cca8fb1642d51bc8bc4508
3
+ size 25612
testdata/snapshots/stippled_mode0_par1.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1566e134cc70c463634018056169827d744e9ad2afa8c792e6ba601d2bb27df1
3
+ size 25612
testdata/snapshots/stippled_mode1_par0.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1566e134cc70c463634018056169827d744e9ad2afa8c792e6ba601d2bb27df1
3
+ size 25612
testdata/snapshots/stippled_mode1_par1.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b14bb45346401b853304f3ca75a1b17443e794bc23cca8fb1642d51bc8bc4508
3
+ size 25612
testdata/snapshots/tiled_copy.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:179ab7509ee5d2ce5ec29bd494a45b196cd99f55660a2f13a5b6e576b872497c
3
+ size 87052
testdata/snapshots/tiled_transparent.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d1873c03b636a00b8d579093c4019dbe57d055f5607358868a5b4ae9901551c
3
+ size 87052