paavohtl Claude Opus 4.6 (1M context) commited on
Commit
2d138bf
·
1 Parent(s): 3ce5627

feat: port core sprite blit with snapshot tests and hook

Browse files

Port BitGrid__BlitSpriteRect (0x4F6910) — the core sprite/bitmap
blitting function used for both rendering and collision mask
construction. Hooked via MinHook with fallthrough to original for
unsupported modes.

Sprite blit module (sprite_blit.rs):
- Direct copy (mode 0) and color-table/transparency (mode 1)
- 8 orientation modes (mirror X/Y/XY, 90° rotations)
- Destination clip rect support
- GIF test image loader with transparent index normalization

18 snapshot tests captured from WA's native function verify
pixel-perfect output. All 15 headless replay tests pass (simulation
checksums match), confirming correctness for collision masks.

Also ports DDDisplay::is_sprite_loaded (slot 32) and
DDDisplay::draw_via_callback (slot 21), bringing ported DDDisplay
methods to 13 of 38. Splits DisplayBase _buf_008 into named
sprite_array_0/1/2 fields.

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

Files changed (31) hide show
  1. Cargo.lock +27 -0
  2. crates/openwa-core/Cargo.toml +2 -0
  3. crates/openwa-core/src/address.rs +2 -2
  4. crates/openwa-core/src/display/base.rs +7 -2
  5. crates/openwa-core/src/display/bitgrid.rs +2 -0
  6. crates/openwa-core/src/display/dd_display.rs +57 -0
  7. crates/openwa-core/src/display/mod.rs +1 -0
  8. crates/openwa-core/src/display/sprite_blit.rs +1305 -0
  9. crates/openwa-dll/Cargo.toml +1 -0
  10. crates/openwa-dll/src/replacements/display.rs +507 -1
  11. crates/openwa-dll/src/replacements/frame_hook.rs +4 -1
  12. testdata/assets/sprite_test.gif +3 -0
  13. testdata/assets/sprite_transparent_test.gif +3 -0
  14. testdata/snapshots/blit_opaque_clipped.bin +3 -0
  15. testdata/snapshots/blit_opaque_colortable.bin +3 -0
  16. testdata/snapshots/blit_opaque_colortable_mx.bin +3 -0
  17. testdata/snapshots/blit_opaque_identity.bin +3 -0
  18. testdata/snapshots/blit_opaque_mirror_x.bin +3 -0
  19. testdata/snapshots/blit_opaque_mirror_xy.bin +3 -0
  20. testdata/snapshots/blit_opaque_mirror_y.bin +3 -0
  21. testdata/snapshots/blit_opaque_rotate90.bin +3 -0
  22. testdata/snapshots/blit_opaque_subrect.bin +3 -0
  23. testdata/snapshots/blit_transparent_clipped.bin +3 -0
  24. testdata/snapshots/blit_transparent_colortable.bin +3 -0
  25. testdata/snapshots/blit_transparent_colortable_mx.bin +3 -0
  26. testdata/snapshots/blit_transparent_identity.bin +3 -0
  27. testdata/snapshots/blit_transparent_mirror_x.bin +3 -0
  28. testdata/snapshots/blit_transparent_mirror_xy.bin +3 -0
  29. testdata/snapshots/blit_transparent_mirror_y.bin +3 -0
  30. testdata/snapshots/blit_transparent_rotate90.bin +3 -0
  31. testdata/snapshots/blit_transparent_subrect.bin +3 -0
Cargo.lock CHANGED
@@ -316,6 +316,12 @@ dependencies = [
316
  "error-code",
317
  ]
318
 
 
 
 
 
 
 
319
  [[package]]
320
  name = "combine"
321
  version = "4.6.7"
@@ -701,6 +707,16 @@ dependencies = [
701
  "wasip2",
702
  ]
703
 
 
 
 
 
 
 
 
 
 
 
704
  [[package]]
705
  name = "gl_generator"
706
  version = "0.14.0"
@@ -971,6 +987,8 @@ checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104"
971
  dependencies = [
972
  "bytemuck",
973
  "byteorder-lite",
 
 
974
  "moxcms",
975
  "num-traits",
976
  "png",
@@ -1480,8 +1498,10 @@ dependencies = [
1480
  name = "openwa-core"
1481
  version = "0.1.0"
1482
  dependencies = [
 
1483
  "heapless",
1484
  "hound",
 
1485
  "inventory",
1486
  "openwa-config",
1487
  "openwa-debug-proto",
@@ -1527,6 +1547,7 @@ dependencies = [
1527
  name = "openwa-dll"
1528
  version = "0.1.0"
1529
  dependencies = [
 
1530
  "heapless",
1531
  "hound",
1532
  "minhook",
@@ -2485,6 +2506,12 @@ dependencies = [
2485
  "web-sys",
2486
  ]
2487
 
 
 
 
 
 
 
2488
  [[package]]
2489
  name = "winapi"
2490
  version = "0.3.9"
 
316
  "error-code",
317
  ]
318
 
319
+ [[package]]
320
+ name = "color_quant"
321
+ version = "1.1.0"
322
+ source = "registry+https://github.com/rust-lang/crates.io-index"
323
+ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
324
+
325
  [[package]]
326
  name = "combine"
327
  version = "4.6.7"
 
707
  "wasip2",
708
  ]
709
 
710
+ [[package]]
711
+ name = "gif"
712
+ version = "0.14.1"
713
+ source = "registry+https://github.com/rust-lang/crates.io-index"
714
+ checksum = "f5df2ba84018d80c213569363bdcd0c64e6933c67fe4c1d60ecf822971a3c35e"
715
+ dependencies = [
716
+ "color_quant",
717
+ "weezl",
718
+ ]
719
+
720
  [[package]]
721
  name = "gl_generator"
722
  version = "0.14.0"
 
987
  dependencies = [
988
  "bytemuck",
989
  "byteorder-lite",
990
+ "color_quant",
991
+ "gif",
992
  "moxcms",
993
  "num-traits",
994
  "png",
 
1498
  name = "openwa-core"
1499
  version = "0.1.0"
1500
  dependencies = [
1501
+ "gif",
1502
  "heapless",
1503
  "hound",
1504
+ "image",
1505
  "inventory",
1506
  "openwa-config",
1507
  "openwa-debug-proto",
 
1547
  name = "openwa-dll"
1548
  version = "0.1.0"
1549
  dependencies = [
1550
+ "gif",
1551
  "heapless",
1552
  "hound",
1553
  "minhook",
 
2506
  "web-sys",
2507
  ]
2508
 
2509
+ [[package]]
2510
+ name = "weezl"
2511
+ version = "0.1.12"
2512
+ source = "registry+https://github.com/rust-lang/crates.io-index"
2513
+ checksum = "a28ac98ddc8b9274cb41bb4d9d4d5c425b6020c50c46f25559911905610b4a88"
2514
+
2515
  [[package]]
2516
  name = "winapi"
2517
  version = "0.3.9"
crates/openwa-core/Cargo.toml CHANGED
@@ -12,6 +12,8 @@ openwa-derive = { path = "../openwa-derive" }
12
  openwa-debug-proto = { path = "../openwa-debug-proto" }
13
 
14
  [dev-dependencies]
 
 
15
  openwa-config = { path = "../openwa-config" }
16
 
17
  [target.'cfg(target_os = "windows")'.dependencies]
 
12
  openwa-debug-proto = { path = "../openwa-debug-proto" }
13
 
14
  [dev-dependencies]
15
+ gif = "0.14"
16
+ image = { version = "0.25", default-features = false, features = ["gif", "png"] }
17
  openwa-config = { path = "../openwa-config" }
18
 
19
  [target.'cfg(target_os = "windows")'.dependencies]
crates/openwa-core/src/address.rs CHANGED
@@ -73,8 +73,8 @@ pub mod va {
73
  pub use crate::display::base::DISPLAY_BASE_VTABLE;
74
  pub use crate::display::bitgrid::{
75
  BitGridBaseVtable, BitGridCollisionVtable, BitGridDisplayVtable, BIT_GRID_BASE_VTABLE,
76
- BIT_GRID_COLLISION_VTABLE, BIT_GRID_DISPLAY_VTABLE, BIT_GRID_INIT, DRAW_LINE_CLIPPED,
77
- DRAW_LINE_TWO_COLOR,
78
  };
79
  pub use crate::display::compat_renderer::{
80
  CompatRendererVtable, COMPAT_RENDERER_VTABLE, DDRAW8_RENDERER_VTABLE,
 
73
  pub use crate::display::base::DISPLAY_BASE_VTABLE;
74
  pub use crate::display::bitgrid::{
75
  BitGridBaseVtable, BitGridCollisionVtable, BitGridDisplayVtable, BIT_GRID_BASE_VTABLE,
76
+ BIT_GRID_COLLISION_VTABLE, BIT_GRID_DISPLAY_VTABLE, BIT_GRID_INIT, BLIT_SPRITE_RECT,
77
+ DRAW_LINE_CLIPPED, DRAW_LINE_TWO_COLOR,
78
  };
79
  pub use crate::display::compat_renderer::{
80
  CompatRendererVtable, COMPAT_RENDERER_VTABLE, DDRAW8_RENDERER_VTABLE,
crates/openwa-core/src/display/base.rs CHANGED
@@ -32,8 +32,13 @@ pub struct DisplayBase {
32
  pub vtable: *const DisplayBaseVtable,
33
  // +0x004: sprite cache wrapper (0x28-byte SpriteCacheWrapper)
34
  pub sprite_cache: Ptr32,
35
- // +0x008..0x3008: three contiguous 0x400-entry u32 arrays, all zeroed by ctor
36
- pub _buf_008: [u32; 0xC00],
 
 
 
 
 
37
  // +0x3008..0x3018: gap (0x10 bytes, 4 u32s: indices 0xC02..0xC05)
38
  pub _gap_3008: [u32; 4],
39
  // +0x3018: field at u32 index 0xC06, zeroed by ctor
 
32
  pub vtable: *const DisplayBaseVtable,
33
  // +0x004: sprite cache wrapper (0x28-byte SpriteCacheWrapper)
34
  pub sprite_cache: Ptr32,
35
+ // +0x008..0x1008: sprite pointer array 0 (0x400 entries), zeroed by ctor.
36
+ // IsSpriteLoaded checks this at offset 0x1008 (which is array 1 below).
37
+ pub sprite_array_0: [u32; 0x400],
38
+ // +0x1008..0x2008: sprite pointer array 1 (0x400 entries), zeroed by ctor.
39
+ pub sprite_array_1: [u32; 0x400],
40
+ // +0x2008..0x3008: sprite pointer array 2 (0x400 entries), zeroed by ctor.
41
+ pub sprite_array_2: [u32; 0x400],
42
  // +0x3008..0x3018: gap (0x10 bytes, 4 u32s: indices 0xC02..0xC05)
43
  pub _gap_3008: [u32; 4],
44
  // +0x3018: field at u32 index 0xC06, zeroed by ctor
crates/openwa-core/src/display/bitgrid.rs CHANGED
@@ -44,6 +44,8 @@ crate::define_addresses! {
44
  class "BitGrid" {
45
  /// BitGrid::init (0x4F6370) — allocates buffer, sets base vtable
46
  fn BIT_GRID_INIT = 0x004F_6370;
 
 
47
  /// Clipped line draw on 8bpp BitGrid (0x4F7500)
48
  fn DRAW_LINE_CLIPPED = 0x004F_7500;
49
  /// Two-color line draw on 8bpp BitGrid (0x4F7A60)
 
44
  class "BitGrid" {
45
  /// BitGrid::init (0x4F6370) — allocates buffer, sets base vtable
46
  fn BIT_GRID_INIT = 0x004F_6370;
47
+ /// Core sprite blit (0x4F6910) — ESI=dst BitGrid, 9 stack params
48
+ fn/Usercall BLIT_SPRITE_RECT = 0x004F_6910;
49
  /// Clipped line draw on 8bpp BitGrid (0x4F7500)
50
  fn DRAW_LINE_CLIPPED = 0x004F_7500;
51
  /// Two-color line draw on 8bpp BitGrid (0x4F7A60)
crates/openwa-core/src/display/dd_display.rs CHANGED
@@ -779,6 +779,63 @@ pub unsafe extern "thiscall" fn draw_polyline(
779
  line_draw::draw_polygon_filled(&mut writer, &verts[..n], color as u8);
780
  }
781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
782
  /// Port of DDDisplay::DrawPixelStrip (vtable slot 15, 0x56BE10).
783
  ///
784
  /// Draws `count + 1` pixels starting at (x, y), stepping by (dx, dy) each
 
779
  line_draw::draw_polygon_filled(&mut writer, &verts[..n], color as u8);
780
  }
781
 
782
+ /// Port of DDDisplay::IsSpriteLoaded (vtable slot 32, 0x56A480).
783
+ ///
784
+ /// Returns 1 if the sprite ID is loaded in any of the three sprite arrays
785
+ /// (DisplayBase sprite_array_1/sprite_array_2, DisplayGfx sprite_table).
786
+ /// ID must be in range [1, 0x400).
787
+ ///
788
+ /// # Safety
789
+ /// `this` must be a valid `*mut DDDisplay` (actually a `*mut DisplayGfx`).
790
+ pub unsafe extern "thiscall" fn is_sprite_loaded(this: *mut DDDisplay, id: i32) -> u32 {
791
+ let id_u = id as u32;
792
+ if id_u.wrapping_sub(1) >= 0x3FF {
793
+ return 0;
794
+ }
795
+ let gfx = this as *mut DisplayGfx;
796
+ let base = &(*gfx).base;
797
+ if base.sprite_array_1[id as usize] != 0
798
+ || base.sprite_array_2[id as usize] != 0
799
+ || (*gfx).sprite_table[id as usize] != 0
800
+ {
801
+ 1
802
+ } else {
803
+ 0
804
+ }
805
+ }
806
+
807
+ /// Port of DDDisplay::DrawViaCallback (vtable slot 21, 0x56B7C0).
808
+ ///
809
+ /// Acquires the render lock, applies camera offset to fixed-point coordinates,
810
+ /// then calls `obj->vtable[2](layer_0, pixel_x, pixel_y, p5, p6)`.
811
+ ///
812
+ /// # Safety
813
+ /// `this` must be a valid `*mut DDDisplay` (actually a `*mut DisplayGfx`).
814
+ /// `obj` must point to a valid object with a vtable where slot 2 is a
815
+ /// drawing callback.
816
+ pub unsafe extern "thiscall" fn draw_via_callback(
817
+ this: *mut DDDisplay,
818
+ x: Fixed,
819
+ y: Fixed,
820
+ obj: *mut u8,
821
+ p5: u32,
822
+ p6: u32,
823
+ ) {
824
+ let gfx = this as *mut DisplayGfx;
825
+ acquire_render_lock(gfx);
826
+
827
+ let pixel_x = (Fixed::from_int((*gfx).camera_x) + x).to_int();
828
+ let pixel_y = (Fixed::from_int((*gfx).camera_y) + y).to_int();
829
+ let layer_0 = (*gfx).layer_0;
830
+
831
+ // Call obj->vtable[2](obj, layer_0, pixel_x, pixel_y, p5, p6)
832
+ // vtable[2] is at offset 8 in the vtable
833
+ let vtable = *(obj as *const *const u32);
834
+ let callback: unsafe extern "thiscall" fn(*mut u8, *mut DisplayBitGrid, i32, i32, u32, u32) =
835
+ core::mem::transmute(*vtable.add(2));
836
+ callback(obj, layer_0, pixel_x, pixel_y, p5, p6);
837
+ }
838
+
839
  /// Port of DDDisplay::DrawPixelStrip (vtable slot 15, 0x56BE10).
840
  ///
841
  /// Draws `count + 1` pixels starting at (x, y), stepping by (dx, dy) each
crates/openwa-core/src/display/mod.rs CHANGED
@@ -8,6 +8,7 @@ pub mod gradient;
8
  pub mod line_draw;
9
  pub mod opengl;
10
  pub mod palette;
 
11
 
12
  pub use base::{DisplayBase, DisplayBaseVtable, SpriteBufferCtrl, SpriteCacheWrapper};
13
  pub use bitgrid::{
 
8
  pub mod line_draw;
9
  pub mod opengl;
10
  pub mod palette;
11
+ pub mod sprite_blit;
12
 
13
  pub use base::{DisplayBase, DisplayBaseVtable, SpriteBufferCtrl, SpriteCacheWrapper};
14
  pub use bitgrid::{
crates/openwa-core/src/display/sprite_blit.rs ADDED
@@ -0,0 +1,1305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //! Sprite blitting algorithms for 8bpp BitGrid surfaces.
2
+ //!
3
+ //! Pure Rust port of WA's core blit pipeline (`FUN_004f6910`).
4
+ //!
5
+ //! The blit function copies a rectangular region from a source surface to a
6
+ //! destination surface with:
7
+ //!
8
+ //! - Clipping to the destination's clip rectangle
9
+ //! - 16 orientation modes (identity, mirror, 90°/180°/270° rotation, and combos)
10
+ //! - Blend modes: direct copy, color-table remap, additive, subtractive
11
+ //!
12
+ //! ## Architecture
13
+ //!
14
+ //! ```text
15
+ //! blit_sprite_rect()
16
+ //! ├── Clip dest rect to dst clip bounds
17
+ //! ├── Adjust src offsets based on orientation
18
+ //! └── Copy pixels (row-major scan)
19
+ //! ├── Mode 0: direct copy (memcpy-like)
20
+ //! ├── Mode 1: color-table blend
21
+ //! ├── Mode 2: additive color mix
22
+ //! └── Mode 3: subtractive color mix
23
+ //! ```
24
+
25
+ use super::line_draw::PixelGrid;
26
+
27
+ /// Source surface for blit operations.
28
+ ///
29
+ /// Wraps an 8bpp indexed pixel buffer with dimensions and stride.
30
+ /// This is the pure-Rust equivalent of a BitGrid used as a blit source.
31
+ pub struct BlitSource<'a> {
32
+ pub data: &'a [u8],
33
+ pub width: u32,
34
+ pub height: u32,
35
+ pub row_stride: u32,
36
+ }
37
+
38
+ impl<'a> From<&'a PixelGrid> for BlitSource<'a> {
39
+ fn from(grid: &'a PixelGrid) -> Self {
40
+ BlitSource {
41
+ data: &grid.data,
42
+ width: grid.width,
43
+ height: grid.height,
44
+ row_stride: grid.row_stride,
45
+ }
46
+ }
47
+ }
48
+
49
+ /// Orientation for sprite blitting (high 16 bits of flags).
50
+ ///
51
+ /// WA supports 16 orientations (0-15) which combine rotation and mirroring.
52
+ /// The orientation determines how source coordinates map to destination coordinates.
53
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
54
+ #[repr(u16)]
55
+ pub enum BlitOrientation {
56
+ /// No transform — source maps directly to destination.
57
+ Normal = 0,
58
+ /// Horizontal mirror (flip X).
59
+ MirrorX = 1,
60
+ /// Vertical mirror (flip Y).
61
+ MirrorY = 2,
62
+ /// Mirror X + Y (180° rotation).
63
+ MirrorXY = 3,
64
+ /// 90° clockwise rotation.
65
+ Rotate90 = 4,
66
+ /// 90° CW + mirror X.
67
+ Rotate90MirrorX = 5,
68
+ /// 90° CW + mirror Y.
69
+ Rotate90MirrorY = 6,
70
+ /// 90° CW + mirror XY (= 270° CW).
71
+ Rotate90MirrorXY = 7,
72
+ }
73
+
74
+ impl BlitOrientation {
75
+ /// Parse orientation from the high 16 bits of the flags word.
76
+ /// Values 8-15 mirror values 0-7 (they wrap with paired symmetry in WA).
77
+ pub fn from_flags(flags: u32) -> Self {
78
+ match (flags >> 16) & 0xFFFF {
79
+ 0 | 11 => Self::Normal,
80
+ 1 | 10 => Self::MirrorX,
81
+ 2 | 9 => Self::MirrorY,
82
+ 3 | 8 => Self::MirrorXY,
83
+ 4 | 15 => Self::Rotate90,
84
+ 5 | 14 => Self::Rotate90MirrorX,
85
+ 6 | 13 => Self::Rotate90MirrorY,
86
+ 7 | 12 => Self::Rotate90MirrorXY,
87
+ _ => Self::Normal,
88
+ }
89
+ }
90
+ }
91
+
92
+ /// Blend mode for sprite blitting (low 16 bits of flags).
93
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
94
+ pub enum BlitBlend {
95
+ /// Direct copy — source pixels replace destination.
96
+ /// Transparent pixels (index 0) are skipped.
97
+ Copy,
98
+ /// Color-table blend — each source pixel is remapped through a 256-byte LUT.
99
+ /// If `color_table` is None, uses direct copy with transparency.
100
+ ColorTable,
101
+ /// Additive color mix — source pixel index used as a fixed-point X scale.
102
+ Additive,
103
+ /// Subtractive color mix.
104
+ Subtractive,
105
+ }
106
+
107
+ impl BlitBlend {
108
+ pub fn from_flags(flags: u32) -> Self {
109
+ match flags & 0xFFFF {
110
+ 0 => Self::Copy,
111
+ 1 => Self::ColorTable,
112
+ 2 => Self::Additive,
113
+ 3 => Self::Subtractive,
114
+ _ => Self::Copy,
115
+ }
116
+ }
117
+ }
118
+
119
+ /// Blit a rectangular region from `src` onto `dst`.
120
+ ///
121
+ /// This is the pure Rust port of WA's core blit function (0x4F6910).
122
+ ///
123
+ /// # Parameters
124
+ ///
125
+ /// - `dst`: Destination pixel grid (written to).
126
+ /// - `src`: Source surface (read from).
127
+ /// - `dst_x`, `dst_y`: Top-left destination position.
128
+ /// - `width`, `height`: Size of the region to blit.
129
+ /// - `src_x`, `src_y`: Offset into the source surface.
130
+ /// - `color_table`: Optional 256-byte LUT for color remapping (blend mode 1).
131
+ /// - `orientation`: Transform to apply.
132
+ /// - `blend`: Pixel compositing mode.
133
+ ///
134
+ /// Returns `true` if any pixels were drawn.
135
+ pub fn blit_sprite_rect(
136
+ dst: &mut PixelGrid,
137
+ src: &BlitSource,
138
+ dst_x: i32,
139
+ dst_y: i32,
140
+ width: i32,
141
+ height: i32,
142
+ src_x: i32,
143
+ src_y: i32,
144
+ color_table: Option<&[u8; 256]>,
145
+ orientation: BlitOrientation,
146
+ blend: BlitBlend,
147
+ ) -> bool {
148
+ if width == 0 || height == 0 {
149
+ return false;
150
+ }
151
+
152
+ // Destination rect before clipping
153
+ let dst_right = dst_x + width;
154
+ let dst_bottom = dst_y + height;
155
+
156
+ // Clip bounds
157
+ let clip_left = dst.clip_left as i32;
158
+ let clip_top = dst.clip_top as i32;
159
+ let clip_right = dst.clip_right as i32;
160
+ let clip_bottom = dst.clip_bottom as i32;
161
+
162
+ // Early-out: completely outside clip rect
163
+ if dst_x >= clip_right
164
+ || dst_right <= clip_left
165
+ || dst_y >= clip_bottom
166
+ || dst_bottom <= clip_top
167
+ {
168
+ return false;
169
+ }
170
+
171
+ // Clamp to clip rect
172
+ let vis_left = dst_x.max(clip_left);
173
+ let vis_right = dst_right.min(clip_right);
174
+ let vis_top = dst_y.max(clip_top);
175
+ let vis_bottom = dst_bottom.min(clip_bottom);
176
+
177
+ if vis_left >= vis_right || vis_top >= vis_bottom {
178
+ return false;
179
+ }
180
+
181
+ // Calculate source offsets after clipping, adjusted for orientation.
182
+ // The orientation determines how clipped edges map to source coordinates.
183
+ let vis_w = vis_right - vis_left;
184
+ let vis_h = vis_bottom - vis_top;
185
+
186
+ match blend {
187
+ BlitBlend::Copy => {
188
+ // WA's mode 0 (direct copy) is always a forward memcpy.
189
+ // Orientation is ignored — the DDDisplay layer handles visual
190
+ // mirroring by adjusting destination coordinates before calling
191
+ // the core blit. Source offset uses Normal orientation.
192
+ let (sx_start, sy_start) = adjust_source_for_clip(
193
+ BlitOrientation::Normal,
194
+ src_x,
195
+ src_y,
196
+ dst_x,
197
+ dst_y,
198
+ width,
199
+ height,
200
+ vis_left,
201
+ vis_top,
202
+ vis_right,
203
+ vis_bottom,
204
+ );
205
+ blit_copy(
206
+ dst, src, vis_left, vis_top, vis_w, vis_h, sx_start, sy_start, 1, 1,
207
+ );
208
+ }
209
+ BlitBlend::ColorTable => {
210
+ // WA's mode 1 uses orientation-specific inner loops:
211
+ // orientation 0 = forward copy, orientation 1 = reverse (mirrored).
212
+ let (sx_start, sy_start) = adjust_source_for_clip(
213
+ orientation,
214
+ src_x,
215
+ src_y,
216
+ dst_x,
217
+ dst_y,
218
+ width,
219
+ height,
220
+ vis_left,
221
+ vis_top,
222
+ vis_right,
223
+ vis_bottom,
224
+ );
225
+ let (sx_step, sy_step, swap_axes) = orientation_steps(orientation);
226
+ let table = color_table.unwrap_or(&IDENTITY_TABLE);
227
+ if swap_axes {
228
+ blit_color_table_swapped(
229
+ dst, src, vis_left, vis_top, vis_w, vis_h, sx_start, sy_start, sx_step,
230
+ sy_step, table,
231
+ );
232
+ } else {
233
+ blit_color_table(
234
+ dst, src, vis_left, vis_top, vis_w, vis_h, sx_start, sy_start, sx_step,
235
+ sy_step, table,
236
+ );
237
+ }
238
+ }
239
+ BlitBlend::Additive => {
240
+ // TODO: port additive blend
241
+ }
242
+ BlitBlend::Subtractive => {
243
+ // TODO: port subtractive blend
244
+ }
245
+ }
246
+
247
+ true
248
+ }
249
+
250
+ /// Identity color table (no remapping).
251
+ const IDENTITY_TABLE: [u8; 256] = {
252
+ let mut t = [0u8; 256];
253
+ let mut i = 0;
254
+ while i < 256 {
255
+ t[i] = i as u8;
256
+ i += 1;
257
+ }
258
+ t
259
+ };
260
+
261
+ /// Calculate the starting source coordinates after clipping, accounting for orientation.
262
+ ///
263
+ /// When stepping forward (+1), we skip clipped pixels from the start:
264
+ /// start = src_origin + clipped_count
265
+ /// When stepping backward (-1), we start from the far end minus clipped pixels:
266
+ /// start = src_origin + extent - 1 - clipped_count
267
+ fn adjust_source_for_clip(
268
+ orientation: BlitOrientation,
269
+ src_x: i32,
270
+ src_y: i32,
271
+ dst_x: i32,
272
+ dst_y: i32,
273
+ width: i32,
274
+ height: i32,
275
+ vis_left: i32,
276
+ vis_top: i32,
277
+ _vis_right: i32,
278
+ _vis_bottom: i32,
279
+ ) -> (i32, i32) {
280
+ use BlitOrientation::*;
281
+
282
+ let clip_left = vis_left - dst_x; // pixels clipped from left
283
+ let clip_top = vis_top - dst_y; // pixels clipped from top
284
+
285
+ // For non-rotated modes: dst_x maps to src_x, dst_y maps to src_y.
286
+ // For rotated modes (swap_axes): dst_y maps to src_x, dst_x maps to src_y.
287
+ match orientation {
288
+ Normal => (src_x + clip_left, src_y + clip_top),
289
+ MirrorX => (src_x + width - 1 - clip_left, src_y + clip_top),
290
+ MirrorY => (src_x + clip_left, src_y + height - 1 - clip_top),
291
+ MirrorXY => (src_x + width - 1 - clip_left, src_y + height - 1 - clip_top),
292
+ // Rotated: src_x tracks dst_y, src_y tracks dst_x
293
+ Rotate90 => (src_x + clip_top, src_y + clip_left),
294
+ Rotate90MirrorX => (src_x + clip_top, src_y + width - 1 - clip_left),
295
+ Rotate90MirrorY => (src_x + height - 1 - clip_top, src_y + clip_left),
296
+ Rotate90MirrorXY => (src_x + height - 1 - clip_top, src_y + width - 1 - clip_left),
297
+ }
298
+ }
299
+
300
+ /// Returns (x_step, y_step, swap_axes) for the given orientation.
301
+ ///
302
+ /// For non-swapped modes: outer loop is dst Y, inner is dst X.
303
+ /// source advances by (sx_step, 0) per dst X pixel, (0, sy_step) per dst Y row.
304
+ ///
305
+ /// For swapped modes (90° rotations): source X advances with dst Y, source Y with dst X.
306
+ fn orientation_steps(orientation: BlitOrientation) -> (i32, i32, bool) {
307
+ use BlitOrientation::*;
308
+ match orientation {
309
+ Normal => (1, 1, false),
310
+ MirrorX => (-1, 1, false),
311
+ MirrorY => (1, -1, false),
312
+ MirrorXY => (-1, -1, false),
313
+ Rotate90 => (1, 1, true),
314
+ Rotate90MirrorX => (1, -1, true),
315
+ Rotate90MirrorY => (-1, 1, true),
316
+ Rotate90MirrorXY => (-1, -1, true),
317
+ }
318
+ }
319
+
320
+ // ---------------------------------------------------------------------------
321
+ // Inner blit loops
322
+ // ---------------------------------------------------------------------------
323
+
324
+ /// Direct copy blit — non-rotated orientations.
325
+ ///
326
+ /// Source is scanned with sx advancing per dst X, sy advancing per dst Y.
327
+ fn blit_copy(
328
+ dst: &mut PixelGrid,
329
+ src: &BlitSource,
330
+ vis_left: i32,
331
+ vis_top: i32,
332
+ vis_w: i32,
333
+ vis_h: i32,
334
+ sx_start: i32,
335
+ sy_start: i32,
336
+ sx_step: i32,
337
+ sy_step: i32,
338
+ ) {
339
+ let dst_stride = dst.row_stride as usize;
340
+ let src_stride = src.row_stride as usize;
341
+
342
+ let mut sy = sy_start;
343
+ for dy in 0..vis_h {
344
+ let dst_row = (vis_top + dy) as usize * dst_stride + vis_left as usize;
345
+ let src_row = sy as usize * src_stride;
346
+
347
+ if sx_step == 1 {
348
+ // Forward copy — can use slice copy
349
+ let sx = sx_start as usize;
350
+ let src_slice = &src.data[src_row + sx..src_row + sx + vis_w as usize];
351
+ dst.data[dst_row..dst_row + vis_w as usize].copy_from_slice(src_slice);
352
+ } else {
353
+ // Reverse copy (mirrored X)
354
+ let mut sx = sx_start;
355
+ for dx in 0..vis_w {
356
+ dst.data[dst_row + dx as usize] = src.data[src_row + sx as usize];
357
+ sx += sx_step;
358
+ }
359
+ }
360
+
361
+ sy += sy_step;
362
+ }
363
+ }
364
+
365
+ /// Color-table blend blit — non-rotated orientations.
366
+ ///
367
+ /// Each non-zero source pixel is looked up in the color table before writing.
368
+ /// Zero (transparent) pixels are skipped.
369
+ fn blit_color_table(
370
+ dst: &mut PixelGrid,
371
+ src: &BlitSource,
372
+ vis_left: i32,
373
+ vis_top: i32,
374
+ vis_w: i32,
375
+ vis_h: i32,
376
+ sx_start: i32,
377
+ sy_start: i32,
378
+ sx_step: i32,
379
+ sy_step: i32,
380
+ table: &[u8; 256],
381
+ ) {
382
+ let dst_stride = dst.row_stride as usize;
383
+ let src_stride = src.row_stride as usize;
384
+
385
+ let mut sy = sy_start;
386
+ for dy in 0..vis_h {
387
+ let dst_row = (vis_top + dy) as usize * dst_stride + vis_left as usize;
388
+ let src_row = sy as usize * src_stride;
389
+
390
+ let mut sx = sx_start;
391
+ for dx in 0..vis_w {
392
+ let pixel = src.data[src_row + sx as usize];
393
+ if pixel != 0 {
394
+ dst.data[dst_row + dx as usize] = table[pixel as usize];
395
+ }
396
+ sx += sx_step;
397
+ }
398
+
399
+ sy += sy_step;
400
+ }
401
+ }
402
+
403
+ /// Direct copy blit — 90° rotated orientations (axes swapped).
404
+ ///
405
+ /// Source X advances with dst Y, source Y advances with dst X.
406
+ fn blit_copy_swapped(
407
+ dst: &mut PixelGrid,
408
+ src: &BlitSource,
409
+ vis_left: i32,
410
+ vis_top: i32,
411
+ vis_w: i32,
412
+ vis_h: i32,
413
+ sx_start: i32,
414
+ sy_start: i32,
415
+ sx_step: i32,
416
+ sy_step: i32,
417
+ ) {
418
+ let dst_stride = dst.row_stride as usize;
419
+ let src_stride = src.row_stride as usize;
420
+
421
+ let mut src_x = sx_start;
422
+ for dy in 0..vis_h {
423
+ let dst_row = (vis_top + dy) as usize * dst_stride + vis_left as usize;
424
+
425
+ let mut src_y = sy_start;
426
+ for dx in 0..vis_w {
427
+ dst.data[dst_row + dx as usize] =
428
+ src.data[src_x as usize * src_stride + src_y as usize];
429
+ src_y += sy_step;
430
+ }
431
+
432
+ src_x += sx_step;
433
+ }
434
+ }
435
+
436
+ /// Color-table blend blit — 90° rotated orientations (axes swapped).
437
+ fn blit_color_table_swapped(
438
+ dst: &mut PixelGrid,
439
+ src: &BlitSource,
440
+ vis_left: i32,
441
+ vis_top: i32,
442
+ vis_w: i32,
443
+ vis_h: i32,
444
+ sx_start: i32,
445
+ sy_start: i32,
446
+ sx_step: i32,
447
+ sy_step: i32,
448
+ table: &[u8; 256],
449
+ ) {
450
+ let dst_stride = dst.row_stride as usize;
451
+ let src_stride = src.row_stride as usize;
452
+
453
+ let mut src_x = sx_start;
454
+ for dy in 0..vis_h {
455
+ let dst_row = (vis_top + dy) as usize * dst_stride + vis_left as usize;
456
+
457
+ let mut src_y = sy_start;
458
+ for dx in 0..vis_w {
459
+ let pixel = src.data[src_x as usize * src_stride + src_y as usize];
460
+ if pixel != 0 {
461
+ dst.data[dst_row + dx as usize] = table[pixel as usize];
462
+ }
463
+ src_y += sy_step;
464
+ }
465
+
466
+ src_x += sx_step;
467
+ }
468
+ }
469
+
470
+ // ---------------------------------------------------------------------------
471
+ // Test sprite creation from images
472
+ // ---------------------------------------------------------------------------
473
+
474
+ /// Create a PixelGrid from raw 8bpp indexed pixel data.
475
+ ///
476
+ /// This is the simplest way to create test sprites — provide raw palette indices.
477
+ pub fn pixel_grid_from_indexed(width: u32, height: u32, pixels: &[u8]) -> PixelGrid {
478
+ assert_eq!(
479
+ pixels.len(),
480
+ (width * height) as usize,
481
+ "pixel data size must match width * height"
482
+ );
483
+
484
+ let mut grid = PixelGrid::new(width, height);
485
+ for y in 0..height {
486
+ let src_row = (y * width) as usize;
487
+ let dst_row = (y * grid.row_stride) as usize;
488
+ grid.data[dst_row..dst_row + width as usize]
489
+ .copy_from_slice(&pixels[src_row..src_row + width as usize]);
490
+ }
491
+ grid
492
+ }
493
+
494
+ #[cfg(test)]
495
+ mod tests {
496
+ use super::*;
497
+
498
+ /// Create a simple 8x8 test pattern with distinct values in each quadrant.
499
+ fn make_test_sprite() -> PixelGrid {
500
+ let mut pixels = vec![0u8; 8 * 8];
501
+ for y in 0..8u32 {
502
+ for x in 0..8u32 {
503
+ // Quadrant coloring: TL=1, TR=2, BL=3, BR=4
504
+ let color = match (x >= 4, y >= 4) {
505
+ (false, false) => 1,
506
+ (true, false) => 2,
507
+ (false, true) => 3,
508
+ (true, true) => 4,
509
+ };
510
+ pixels[(y * 8 + x) as usize] = color;
511
+ }
512
+ }
513
+ pixel_grid_from_indexed(8, 8, &pixels)
514
+ }
515
+
516
+ #[test]
517
+ fn blit_copy_identity() {
518
+ let sprite = make_test_sprite();
519
+ let src = BlitSource::from(&sprite);
520
+ let mut dst = PixelGrid::new(16, 16);
521
+
522
+ blit_sprite_rect(
523
+ &mut dst,
524
+ &src,
525
+ 4,
526
+ 4, // dst position
527
+ 8,
528
+ 8, // size
529
+ 0,
530
+ 0, // src offset
531
+ None,
532
+ BlitOrientation::Normal,
533
+ BlitBlend::Copy,
534
+ );
535
+
536
+ // Check quadrant corners
537
+ let p = |x: i32, y: i32| dst.data[(y as u32 * dst.row_stride + x as u32) as usize];
538
+ assert_eq!(p(4, 4), 1, "top-left of sprite");
539
+ assert_eq!(p(8, 4), 2, "top-right of sprite");
540
+ assert_eq!(p(4, 8), 3, "bottom-left of sprite");
541
+ assert_eq!(p(8, 8), 4, "bottom-right of sprite");
542
+ // Outside sprite region should be 0
543
+ assert_eq!(p(3, 4), 0, "left of sprite");
544
+ assert_eq!(p(12, 4), 0, "right of sprite");
545
+ }
546
+
547
+ #[test]
548
+ fn blit_copy_ignores_orientation() {
549
+ // WA's mode 0 (Copy) is always forward memcpy — orientation has no effect.
550
+ let sprite = make_test_sprite();
551
+ let src = BlitSource::from(&sprite);
552
+ let mut dst_normal = PixelGrid::new(16, 16);
553
+ let mut dst_mirror = PixelGrid::new(16, 16);
554
+
555
+ blit_sprite_rect(
556
+ &mut dst_normal,
557
+ &src,
558
+ 4,
559
+ 4,
560
+ 8,
561
+ 8,
562
+ 0,
563
+ 0,
564
+ None,
565
+ BlitOrientation::Normal,
566
+ BlitBlend::Copy,
567
+ );
568
+ blit_sprite_rect(
569
+ &mut dst_mirror,
570
+ &src,
571
+ 4,
572
+ 4,
573
+ 8,
574
+ 8,
575
+ 0,
576
+ 0,
577
+ None,
578
+ BlitOrientation::MirrorX,
579
+ BlitBlend::Copy,
580
+ );
581
+
582
+ assert_eq!(
583
+ dst_normal.data, dst_mirror.data,
584
+ "Copy mode ignores orientation"
585
+ );
586
+ }
587
+
588
+ #[test]
589
+ fn blit_colortable_mirror_x() {
590
+ // Mirroring works in ColorTable mode (mode 1).
591
+ let sprite = make_test_sprite();
592
+ let src = BlitSource::from(&sprite);
593
+ let mut dst = PixelGrid::new(16, 16);
594
+
595
+ blit_sprite_rect(
596
+ &mut dst,
597
+ &src,
598
+ 4,
599
+ 4,
600
+ 8,
601
+ 8,
602
+ 0,
603
+ 0,
604
+ None,
605
+ BlitOrientation::MirrorX,
606
+ BlitBlend::ColorTable,
607
+ );
608
+
609
+ let p = |x: i32, y: i32| dst.data[(y as u32 * dst.row_stride + x as u32) as usize];
610
+ assert_eq!(p(4, 4), 2, "mirror: was TR, now TL");
611
+ assert_eq!(p(8, 4), 1, "mirror: was TL, now TR");
612
+ assert_eq!(p(4, 8), 4, "mirror: was BR, now BL");
613
+ assert_eq!(p(8, 8), 3, "mirror: was BL, now BR");
614
+ }
615
+
616
+ #[test]
617
+ fn blit_colortable_mirror_y() {
618
+ let sprite = make_test_sprite();
619
+ let src = BlitSource::from(&sprite);
620
+ let mut dst = PixelGrid::new(16, 16);
621
+
622
+ blit_sprite_rect(
623
+ &mut dst,
624
+ &src,
625
+ 4,
626
+ 4,
627
+ 8,
628
+ 8,
629
+ 0,
630
+ 0,
631
+ None,
632
+ BlitOrientation::MirrorY,
633
+ BlitBlend::ColorTable,
634
+ );
635
+
636
+ let p = |x: i32, y: i32| dst.data[(y as u32 * dst.row_stride + x as u32) as usize];
637
+ assert_eq!(p(4, 4), 3, "mirror-y: was BL, now TL");
638
+ assert_eq!(p(8, 4), 4, "mirror-y: was BR, now TR");
639
+ assert_eq!(p(4, 8), 1, "mirror-y: was TL, now BL");
640
+ assert_eq!(p(8, 8), 2, "mirror-y: was TR, now BR");
641
+ }
642
+
643
+ #[test]
644
+ fn blit_copy_clipped() {
645
+ let sprite = make_test_sprite();
646
+ let src = BlitSource::from(&sprite);
647
+ let mut dst = PixelGrid::new(16, 16);
648
+ // Clip to only show bottom-right quadrant of the sprite
649
+ dst.clip_left = 8;
650
+ dst.clip_top = 8;
651
+
652
+ blit_sprite_rect(
653
+ &mut dst,
654
+ &src,
655
+ 4,
656
+ 4,
657
+ 8,
658
+ 8,
659
+ 0,
660
+ 0,
661
+ None,
662
+ BlitOrientation::Normal,
663
+ BlitBlend::Copy,
664
+ );
665
+
666
+ let p = |x: i32, y: i32| dst.data[(y as u32 * dst.row_stride + x as u32) as usize];
667
+ // Only the bottom-right quadrant should be visible
668
+ assert_eq!(p(7, 7), 0, "clipped away");
669
+ assert_eq!(p(8, 8), 4, "visible bottom-right");
670
+ assert_eq!(p(11, 11), 4, "still bottom-right");
671
+ }
672
+
673
+ #[test]
674
+ fn blit_color_table_transparency() {
675
+ let sprite = make_test_sprite();
676
+ let src = BlitSource::from(&sprite);
677
+ let mut dst = PixelGrid::new(16, 16);
678
+ // Fill dst with color 10
679
+ dst.data.fill(10);
680
+
681
+ // Remap: 1->11, 2->12, 3->13, 4->14
682
+ let mut table = [0u8; 256];
683
+ for i in 0..256 {
684
+ table[i] = (i as u8).wrapping_add(10);
685
+ }
686
+ table[0] = 0; // transparent stays 0
687
+
688
+ blit_sprite_rect(
689
+ &mut dst,
690
+ &src,
691
+ 4,
692
+ 4,
693
+ 8,
694
+ 8,
695
+ 0,
696
+ 0,
697
+ Some(&table),
698
+ BlitOrientation::Normal,
699
+ BlitBlend::ColorTable,
700
+ );
701
+
702
+ let p = |x: i32, y: i32| dst.data[(y as u32 * dst.row_stride + x as u32) as usize];
703
+ assert_eq!(p(4, 4), 11, "remapped color 1 -> 11");
704
+ assert_eq!(p(8, 4), 12, "remapped color 2 -> 12");
705
+ // Outside sprite: background preserved
706
+ assert_eq!(p(3, 4), 10, "background unchanged");
707
+ }
708
+
709
+ #[test]
710
+ fn blit_color_table_preserves_transparent() {
711
+ // Create a sprite with some transparent (0) pixels
712
+ let mut pixels = vec![0u8; 8 * 8];
713
+ // Only set pixels on the diagonal
714
+ for i in 0..8 {
715
+ pixels[i * 8 + i] = 5;
716
+ }
717
+ let sprite = pixel_grid_from_indexed(8, 8, &pixels);
718
+ let src = BlitSource::from(&sprite);
719
+
720
+ let mut dst = PixelGrid::new(16, 16);
721
+ dst.data.fill(99); // background
722
+
723
+ blit_sprite_rect(
724
+ &mut dst,
725
+ &src,
726
+ 0,
727
+ 0,
728
+ 8,
729
+ 8,
730
+ 0,
731
+ 0,
732
+ None,
733
+ BlitOrientation::Normal,
734
+ BlitBlend::ColorTable,
735
+ );
736
+
737
+ let p = |x: i32, y: i32| dst.data[(y as u32 * dst.row_stride + x as u32) as usize];
738
+ assert_eq!(p(0, 0), 5, "diagonal pixel drawn");
739
+ assert_eq!(p(1, 0), 99, "transparent pixel preserved background");
740
+ assert_eq!(p(3, 3), 5, "diagonal pixel drawn");
741
+ assert_eq!(p(4, 3), 99, "transparent pixel preserved background");
742
+ }
743
+
744
+ // =======================================================================
745
+ // Image loading helpers (for snapshot tests with real images)
746
+ // =======================================================================
747
+
748
+ /// Load a GIF file as a PixelGrid, preserving raw palette indices.
749
+ ///
750
+ /// GIF is naturally a paletted format (up to 256 colors), so the pixel
751
+ /// values in the returned grid are the original palette indices — exactly
752
+ /// what the blit functions operate on.
753
+ ///
754
+ /// Returns `(grid, palette)` where palette is the RGB color table.
755
+ fn load_gif_indexed(path: &str) -> (PixelGrid, Vec<[u8; 3]>) {
756
+ use gif::DecodeOptions;
757
+ use std::fs::File;
758
+
759
+ let file = File::open(path).unwrap_or_else(|e| panic!("Failed to open {path}: {e}"));
760
+ let mut opts = DecodeOptions::new();
761
+ opts.set_color_output(gif::ColorOutput::Indexed);
762
+ let mut decoder = opts
763
+ .read_info(file)
764
+ .unwrap_or_else(|e| panic!("Failed to decode GIF {path}: {e}"));
765
+
766
+ // Grab global palette before borrowing frame
767
+ let global_pal = decoder.global_palette().map(|p| p.to_vec());
768
+
769
+ let frame = decoder
770
+ .read_next_frame()
771
+ .unwrap_or_else(|e| panic!("Failed to read GIF frame: {e}"))
772
+ .expect("GIF has no frames");
773
+
774
+ let w = frame.width as u32;
775
+ let h = frame.height as u32;
776
+
777
+ // Extract palette (frame-local or global)
778
+ let palette_bytes = frame
779
+ .palette
780
+ .as_deref()
781
+ .or(global_pal.as_deref())
782
+ .expect("GIF has no palette");
783
+ let palette: Vec<[u8; 3]> = palette_bytes
784
+ .chunks_exact(3)
785
+ .map(|c| [c[0], c[1], c[2]])
786
+ .collect();
787
+
788
+ // Remap transparent index to 0 (WA convention).
789
+ // GIF's transparent index can be any palette entry; WA always uses 0.
790
+ let transparent_idx = frame.transparent;
791
+ let mut pixels: Vec<u8> = frame.buffer.to_vec();
792
+ let mut palette = palette;
793
+
794
+ if let Some(ti) = transparent_idx {
795
+ if ti != 0 {
796
+ // Swap palette entries 0 and ti, remap all pixels
797
+ palette.swap(0, ti as usize);
798
+ for p in &mut pixels {
799
+ if *p == 0 {
800
+ *p = ti;
801
+ } else if *p == ti {
802
+ *p = 0;
803
+ }
804
+ }
805
+ }
806
+ }
807
+
808
+ let grid = pixel_grid_from_indexed(w, h, &pixels);
809
+ (grid, palette)
810
+ }
811
+
812
+ fn test_asset_path(name: &str) -> String {
813
+ format!(
814
+ "{}/../../testdata/assets/{name}",
815
+ env!("CARGO_MANIFEST_DIR"),
816
+ )
817
+ }
818
+
819
+ #[test]
820
+ fn load_sprite_test_gif() {
821
+ let path = test_asset_path("sprite_test.gif");
822
+ let (grid, palette) = load_gif_indexed(&path);
823
+ assert!(grid.width > 0 && grid.height > 0, "image has dimensions");
824
+ assert!(palette.len() <= 256, "palette fits in 8bpp");
825
+ // Verify some pixels are non-zero (not all transparent)
826
+ let nonzero = grid.data.iter().filter(|&&p| p != 0).count();
827
+ assert!(nonzero > 0, "image has non-transparent pixels");
828
+ }
829
+
830
+ #[test]
831
+ fn load_transparent_gif() {
832
+ let path = test_asset_path("sprite_transparent_test.gif");
833
+ let (grid, _palette) = load_gif_indexed(&path);
834
+ assert!(grid.width > 0 && grid.height > 0);
835
+ // Should have both transparent (0) and opaque pixels
836
+ let zeros = grid.data.iter().filter(|&&p| p == 0).count();
837
+ let nonzeros = grid.data.iter().filter(|&&p| p != 0).count();
838
+ assert!(zeros > 0, "should have transparent pixels");
839
+ assert!(nonzeros > 0, "should have opaque pixels");
840
+ }
841
+
842
+ // =======================================================================
843
+ // Real-image blit tests
844
+ // =======================================================================
845
+
846
+ /// Blit the opaque sprite_test.gif at various positions and orientations.
847
+ #[test]
848
+ fn blit_opaque_sprite_identity() {
849
+ let path = test_asset_path("sprite_test.gif");
850
+ let (sprite, _) = load_gif_indexed(&path);
851
+ let src = BlitSource::from(&sprite);
852
+ let sw = sprite.width as i32;
853
+ let sh = sprite.height as i32;
854
+
855
+ // Blit centered onto a larger canvas
856
+ let canvas_w = (sw + 32) as u32;
857
+ let canvas_h = (sh + 32) as u32;
858
+ let mut dst = PixelGrid::new(canvas_w, canvas_h);
859
+
860
+ blit_sprite_rect(
861
+ &mut dst,
862
+ &src,
863
+ 16,
864
+ 16,
865
+ sw,
866
+ sh,
867
+ 0,
868
+ 0,
869
+ None,
870
+ BlitOrientation::Normal,
871
+ BlitBlend::Copy,
872
+ );
873
+
874
+ // Border should be untouched (0)
875
+ let p = |x: i32, y: i32| dst.data[(y as u32 * dst.row_stride + x as u32) as usize];
876
+ assert_eq!(p(0, 0), 0, "top-left border");
877
+ assert_eq!(p(15, 16), 0, "left border");
878
+
879
+ // Interior should match source
880
+ for y in 0..sh.min(4) {
881
+ for x in 0..sw.min(4) {
882
+ let expected = sprite.data[(y as u32 * sprite.row_stride + x as u32) as usize];
883
+ assert_eq!(p(x + 16, y + 16), expected, "pixel ({x},{y})");
884
+ }
885
+ }
886
+ }
887
+
888
+ #[test]
889
+ fn blit_opaque_sprite_mirror_x() {
890
+ // Mirror works in ColorTable mode, not Copy mode.
891
+ let path = test_asset_path("sprite_test.gif");
892
+ let (sprite, _) = load_gif_indexed(&path);
893
+ let src = BlitSource::from(&sprite);
894
+ let sw = sprite.width as i32;
895
+ let sh = sprite.height as i32;
896
+
897
+ let mut dst = PixelGrid::new(sprite.width, sprite.height);
898
+ blit_sprite_rect(
899
+ &mut dst,
900
+ &src,
901
+ 0,
902
+ 0,
903
+ sw,
904
+ sh,
905
+ 0,
906
+ 0,
907
+ None,
908
+ BlitOrientation::MirrorX,
909
+ BlitBlend::ColorTable,
910
+ );
911
+
912
+ // First row: dst[0] should be src[sw-1], dst[1] should be src[sw-2], etc.
913
+ for x in 0..sw {
914
+ let dst_val = dst.data[(0 * dst.row_stride as i32 + x) as usize];
915
+ let src_val = sprite.data[(0 * sprite.row_stride as i32 + (sw - 1 - x)) as usize];
916
+ assert_eq!(dst_val, src_val, "mirror-x row 0, col {x}");
917
+ }
918
+ }
919
+
920
+ #[test]
921
+ fn blit_opaque_sprite_clipped() {
922
+ let path = test_asset_path("sprite_test.gif");
923
+ let (sprite, _) = load_gif_indexed(&path);
924
+ let src = BlitSource::from(&sprite);
925
+ let sw = sprite.width as i32;
926
+ let sh = sprite.height as i32;
927
+
928
+ // Blit at (-16, -16) so only bottom-right portion is visible
929
+ let mut dst = PixelGrid::new(sprite.width, sprite.height);
930
+ blit_sprite_rect(
931
+ &mut dst,
932
+ &src,
933
+ -16,
934
+ -16,
935
+ sw,
936
+ sh,
937
+ 0,
938
+ 0,
939
+ None,
940
+ BlitOrientation::Normal,
941
+ BlitBlend::Copy,
942
+ );
943
+
944
+ // dst[0,0] should correspond to src[16,16]
945
+ let dst_val = dst.data[0];
946
+ let src_val = sprite.data[(16 * sprite.row_stride + 16) as usize];
947
+ assert_eq!(dst_val, src_val, "clipped blit: dst(0,0) == src(16,16)");
948
+ }
949
+
950
+ /// Blit transparent sprite over a filled background — transparent pixels
951
+ /// should preserve the background.
952
+ #[test]
953
+ fn blit_transparent_sprite_preserves_background() {
954
+ let path = test_asset_path("sprite_transparent_test.gif");
955
+ let (sprite, _) = load_gif_indexed(&path);
956
+ let src = BlitSource::from(&sprite);
957
+ let sw = sprite.width as i32;
958
+ let sh = sprite.height as i32;
959
+
960
+ let mut dst = PixelGrid::new(sprite.width, sprite.height);
961
+ dst.data.fill(42); // background color
962
+
963
+ blit_sprite_rect(
964
+ &mut dst,
965
+ &src,
966
+ 0,
967
+ 0,
968
+ sw,
969
+ sh,
970
+ 0,
971
+ 0,
972
+ None,
973
+ BlitOrientation::Normal,
974
+ BlitBlend::ColorTable,
975
+ );
976
+
977
+ // Check that transparent source pixels left the background intact,
978
+ // and opaque pixels overwrote it.
979
+ let mut bg_preserved = 0u32;
980
+ let mut overwritten = 0u32;
981
+ for y in 0..sh {
982
+ for x in 0..sw {
983
+ let src_idx = (y as u32 * sprite.row_stride + x as u32) as usize;
984
+ let dst_idx = (y as u32 * dst.row_stride + x as u32) as usize;
985
+ if sprite.data[src_idx] == 0 {
986
+ assert_eq!(
987
+ dst.data[dst_idx], 42,
988
+ "transparent pixel at ({x},{y}) should preserve background"
989
+ );
990
+ bg_preserved += 1;
991
+ } else {
992
+ assert_ne!(
993
+ dst.data[dst_idx], 42,
994
+ "opaque pixel at ({x},{y}) should overwrite background"
995
+ );
996
+ overwritten += 1;
997
+ }
998
+ }
999
+ }
1000
+ assert!(bg_preserved > 0, "some pixels should be transparent");
1001
+ assert!(overwritten > 0, "some pixels should be opaque");
1002
+ }
1003
+
1004
+ /// Blit transparent sprite with mirror — verify transparency still works.
1005
+ #[test]
1006
+ fn blit_transparent_sprite_mirrored() {
1007
+ let path = test_asset_path("sprite_transparent_test.gif");
1008
+ let (sprite, _) = load_gif_indexed(&path);
1009
+ let src = BlitSource::from(&sprite);
1010
+ let sw = sprite.width as i32;
1011
+ let sh = sprite.height as i32;
1012
+
1013
+ let mut dst = PixelGrid::new(sprite.width, sprite.height);
1014
+ dst.data.fill(42);
1015
+
1016
+ blit_sprite_rect(
1017
+ &mut dst,
1018
+ &src,
1019
+ 0,
1020
+ 0,
1021
+ sw,
1022
+ sh,
1023
+ 0,
1024
+ 0,
1025
+ None,
1026
+ BlitOrientation::MirrorX,
1027
+ BlitBlend::ColorTable,
1028
+ );
1029
+
1030
+ // Mirrored: dst column x reads from src column (sw-1-x)
1031
+ for y in 0..sh.min(8) {
1032
+ for x in 0..sw {
1033
+ let src_x = sw - 1 - x;
1034
+ let src_idx = (y as u32 * sprite.row_stride + src_x as u32) as usize;
1035
+ let dst_idx = (y as u32 * dst.row_stride + x as u32) as usize;
1036
+ if sprite.data[src_idx] == 0 {
1037
+ assert_eq!(
1038
+ dst.data[dst_idx], 42,
1039
+ "mirrored transparent at dst({x},{y}) from src({src_x},{y})"
1040
+ );
1041
+ } else {
1042
+ assert_eq!(
1043
+ dst.data[dst_idx], sprite.data[src_idx],
1044
+ "mirrored opaque at dst({x},{y}) from src({src_x},{y})"
1045
+ );
1046
+ }
1047
+ }
1048
+ }
1049
+ }
1050
+
1051
+ // =======================================================================
1052
+ // Snapshot tests — compare Rust blit output against WA's native output
1053
+ // =======================================================================
1054
+
1055
+ fn load_snapshot(name: &str) -> PixelGrid {
1056
+ let path = format!(
1057
+ "{}/../../testdata/snapshots/{}.bin",
1058
+ env!("CARGO_MANIFEST_DIR"),
1059
+ name
1060
+ );
1061
+ let bytes = std::fs::read(&path)
1062
+ .unwrap_or_else(|e| panic!("Failed to load snapshot {name}: {e} (path: {path})"));
1063
+ PixelGrid::from_snapshot(&bytes)
1064
+ }
1065
+
1066
+ fn assert_matches_snapshot(actual: &PixelGrid, name: &str) {
1067
+ let expected = load_snapshot(name);
1068
+ assert_eq!(actual.width, expected.width, "{name}: width mismatch");
1069
+ assert_eq!(actual.height, expected.height, "{name}: height mismatch");
1070
+ assert_eq!(
1071
+ actual.row_stride, expected.row_stride,
1072
+ "{name}: stride mismatch"
1073
+ );
1074
+ if actual.data != expected.data {
1075
+ let mut diff_count = 0;
1076
+ let mut first_diff = None;
1077
+ for y in 0..actual.height {
1078
+ for x in 0..actual.width {
1079
+ let idx = (y * actual.row_stride + x) as usize;
1080
+ if actual.data[idx] != expected.data[idx] {
1081
+ diff_count += 1;
1082
+ if first_diff.is_none() {
1083
+ first_diff = Some((x, y, actual.data[idx], expected.data[idx]));
1084
+ }
1085
+ }
1086
+ }
1087
+ }
1088
+ let (fx, fy, got, want) = first_diff.unwrap();
1089
+ panic!(
1090
+ "{name}: {diff_count} pixel(s) differ. First at ({fx},{fy}): got {got}, want {want}"
1091
+ );
1092
+ }
1093
+ }
1094
+
1095
+ /// Run a blit with the same parameters as the DLL capture and compare output.
1096
+ ///
1097
+ /// `blit_size`: (w, h) override, or None to use sprite dimensions.
1098
+ /// `src_offset`: (x, y) source offset.
1099
+ fn run_snapshot_test(
1100
+ gif_name: &str,
1101
+ snap_name: &str,
1102
+ dst_x: i32,
1103
+ dst_y: i32,
1104
+ blit_size: Option<(i32, i32)>,
1105
+ src_offset: (i32, i32),
1106
+ orientation: BlitOrientation,
1107
+ blend: BlitBlend,
1108
+ bg_fill: u8,
1109
+ ) {
1110
+ let path = test_asset_path(gif_name);
1111
+ let (sprite, _) = load_gif_indexed(&path);
1112
+ let src = BlitSource::from(&sprite);
1113
+
1114
+ let sw = sprite.width as i32;
1115
+ let sh = sprite.height as i32;
1116
+ let (blit_w, blit_h) = blit_size.unwrap_or((sw, sh));
1117
+
1118
+ let canvas_w = (sw + 32) as u32;
1119
+ let canvas_h = (sh + 32) as u32;
1120
+ let mut dst = PixelGrid::new(canvas_w, canvas_h);
1121
+ dst.data.fill(bg_fill);
1122
+
1123
+ blit_sprite_rect(
1124
+ &mut dst,
1125
+ &src,
1126
+ dst_x,
1127
+ dst_y,
1128
+ blit_w,
1129
+ blit_h,
1130
+ src_offset.0,
1131
+ src_offset.1,
1132
+ None,
1133
+ orientation,
1134
+ blend,
1135
+ );
1136
+
1137
+ assert_matches_snapshot(&dst, snap_name);
1138
+ }
1139
+
1140
+ // Snapshot tests — use sprite's own dimensions (matching DLL capture).
1141
+
1142
+ #[test]
1143
+ fn snap_blit_opaque_identity() {
1144
+ run_snapshot_test(
1145
+ "sprite_test.gif",
1146
+ "blit_opaque_identity",
1147
+ 16,
1148
+ 16,
1149
+ None,
1150
+ (0, 0),
1151
+ BlitOrientation::Normal,
1152
+ BlitBlend::Copy,
1153
+ 0,
1154
+ );
1155
+ }
1156
+ #[test]
1157
+ fn snap_blit_opaque_mirror_x() {
1158
+ run_snapshot_test(
1159
+ "sprite_test.gif",
1160
+ "blit_opaque_mirror_x",
1161
+ 16,
1162
+ 16,
1163
+ None,
1164
+ (0, 0),
1165
+ BlitOrientation::MirrorX,
1166
+ BlitBlend::Copy,
1167
+ 0,
1168
+ );
1169
+ }
1170
+ #[test]
1171
+ fn snap_blit_opaque_mirror_y() {
1172
+ run_snapshot_test(
1173
+ "sprite_test.gif",
1174
+ "blit_opaque_mirror_y",
1175
+ 16,
1176
+ 16,
1177
+ None,
1178
+ (0, 0),
1179
+ BlitOrientation::MirrorY,
1180
+ BlitBlend::Copy,
1181
+ 0,
1182
+ );
1183
+ }
1184
+ #[test]
1185
+ fn snap_blit_opaque_mirror_xy() {
1186
+ run_snapshot_test(
1187
+ "sprite_test.gif",
1188
+ "blit_opaque_mirror_xy",
1189
+ 16,
1190
+ 16,
1191
+ None,
1192
+ (0, 0),
1193
+ BlitOrientation::MirrorXY,
1194
+ BlitBlend::Copy,
1195
+ 0,
1196
+ );
1197
+ }
1198
+ #[test]
1199
+ fn snap_blit_opaque_rotate90() {
1200
+ // Capture used (sh, sw) for rotate90 blit dimensions
1201
+ let path = test_asset_path("sprite_test.gif");
1202
+ let (sprite, _) = load_gif_indexed(&path);
1203
+ let sh = sprite.height as i32;
1204
+ let sw = sprite.width as i32;
1205
+ run_snapshot_test(
1206
+ "sprite_test.gif",
1207
+ "blit_opaque_rotate90",
1208
+ 16,
1209
+ 16,
1210
+ Some((sh, sw)),
1211
+ (0, 0),
1212
+ BlitOrientation::Rotate90,
1213
+ BlitBlend::Copy,
1214
+ 0,
1215
+ );
1216
+ }
1217
+ #[test]
1218
+ fn snap_blit_opaque_clipped() {
1219
+ run_snapshot_test(
1220
+ "sprite_test.gif",
1221
+ "blit_opaque_clipped",
1222
+ -16,
1223
+ -16,
1224
+ None,
1225
+ (0, 0),
1226
+ BlitOrientation::Normal,
1227
+ BlitBlend::Copy,
1228
+ 0,
1229
+ );
1230
+ }
1231
+ #[test]
1232
+ fn snap_blit_opaque_colortable() {
1233
+ run_snapshot_test(
1234
+ "sprite_test.gif",
1235
+ "blit_opaque_colortable",
1236
+ 16,
1237
+ 16,
1238
+ None,
1239
+ (0, 0),
1240
+ BlitOrientation::Normal,
1241
+ BlitBlend::ColorTable,
1242
+ 77,
1243
+ );
1244
+ }
1245
+ #[test]
1246
+ fn snap_blit_opaque_colortable_mx() {
1247
+ run_snapshot_test(
1248
+ "sprite_test.gif",
1249
+ "blit_opaque_colortable_mx",
1250
+ 16,
1251
+ 16,
1252
+ None,
1253
+ (0, 0),
1254
+ BlitOrientation::MirrorX,
1255
+ BlitBlend::ColorTable,
1256
+ 77,
1257
+ );
1258
+ }
1259
+ #[test]
1260
+ fn snap_blit_opaque_subrect() {
1261
+ let path = test_asset_path("sprite_test.gif");
1262
+ let (sprite, _) = load_gif_indexed(&path);
1263
+ let sw = sprite.width as i32;
1264
+ let sh = sprite.height as i32;
1265
+ run_snapshot_test(
1266
+ "sprite_test.gif",
1267
+ "blit_opaque_subrect",
1268
+ 16,
1269
+ 16,
1270
+ Some((sw / 2, sh / 2)),
1271
+ (sw / 4, sh / 4),
1272
+ BlitOrientation::Normal,
1273
+ BlitBlend::Copy,
1274
+ 0,
1275
+ );
1276
+ }
1277
+ #[test]
1278
+ fn snap_blit_transparent_identity() {
1279
+ run_snapshot_test(
1280
+ "sprite_transparent_test.gif",
1281
+ "blit_transparent_identity",
1282
+ 16,
1283
+ 16,
1284
+ None,
1285
+ (0, 0),
1286
+ BlitOrientation::Normal,
1287
+ BlitBlend::Copy,
1288
+ 0,
1289
+ );
1290
+ }
1291
+ #[test]
1292
+ fn snap_blit_transparent_colortable() {
1293
+ run_snapshot_test(
1294
+ "sprite_transparent_test.gif",
1295
+ "blit_transparent_colortable",
1296
+ 16,
1297
+ 16,
1298
+ None,
1299
+ (0, 0),
1300
+ BlitOrientation::Normal,
1301
+ BlitBlend::ColorTable,
1302
+ 77,
1303
+ );
1304
+ }
1305
+ }
crates/openwa-dll/Cargo.toml CHANGED
@@ -15,6 +15,7 @@ debug-ui = ["dep:openwa-debugui"]
15
  openwa-core = { path = "../openwa-core" }
16
  openwa-debug-proto = { path = "../openwa-debug-proto" }
17
  openwa-debugui = { path = "../openwa-debugui", optional = true }
 
18
  heapless = "0.9"
19
  hound = "3"
20
  minhook = "0.9"
 
15
  openwa-core = { path = "../openwa-core" }
16
  openwa-debug-proto = { path = "../openwa-debug-proto" }
17
  openwa-debugui = { path = "../openwa-debugui", optional = true }
18
+ gif = "0.14"
19
  heapless = "0.9"
20
  hound = "3"
21
  minhook = "0.9"
crates/openwa-dll/src/replacements/display.rs CHANGED
@@ -266,6 +266,502 @@ pub unsafe fn capture_line_snapshots() {
266
  destructor(grid, 1);
267
  }
268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
  pub fn install() -> Result<(), String> {
270
  let _ = log_line("[Display] Patching DisplayBase vtables");
271
 
@@ -307,11 +803,21 @@ pub fn install() -> Result<(), String> {
307
  draw_crosshair => dd_display::draw_crosshair,
308
  draw_outlined_pixel => dd_display::draw_outlined_pixel,
309
  fill_rect => dd_display::fill_rect,
 
310
  flush_render => dd_display::flush_render,
311
  set_camera_offset => dd_display::set_camera_offset,
312
  set_clip_rect => dd_display::set_clip_rect,
 
313
  })?;
314
- let _ = log_line("[Display] DDDisplay: patched 10 methods → Rust");
 
 
 
 
 
 
 
 
315
  }
316
 
317
  Ok(())
 
266
  destructor(grid, 1);
267
  }
268
 
269
+ /// Capture sprite blit snapshots from WA's native FUN_004f6910.
270
+ ///
271
+ /// Creates source and destination BitGrids, loads test GIF images,
272
+ /// calls WA's blit with various orientations and blend modes,
273
+ /// saves pixel output to `testdata/snapshots/`. Activated by
274
+ /// `OPENWA_CAPTURE_BLIT_SNAPSHOTS=1`.
275
+ pub unsafe fn capture_blit_snapshots() {
276
+ use openwa_core::display::bitgrid::DisplayBitGrid;
277
+ use std::fs;
278
+ use std::io::Write;
279
+
280
+ let _ = log_line("[Display] BLIT SNAPSHOT: Starting capture");
281
+
282
+ // Project root at compile time — WA.exe runs from its install dir,
283
+ // so we need absolute paths to reach testdata/.
284
+ const PROJECT_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../..");
285
+
286
+ // Load test images from GIF files
287
+ let opaque_data = match fs::read(format!("{PROJECT_ROOT}/testdata/assets/sprite_test.gif")) {
288
+ Ok(d) => d,
289
+ Err(e) => {
290
+ let _ = log_line(&format!(
291
+ "[Display] BLIT SNAPSHOT: Failed to read sprite_test.gif: {e}"
292
+ ));
293
+ return;
294
+ }
295
+ };
296
+ let transparent_data = match fs::read(format!(
297
+ "{PROJECT_ROOT}/testdata/assets/sprite_transparent_test.gif"
298
+ )) {
299
+ Ok(d) => d,
300
+ Err(e) => {
301
+ let _ = log_line(&format!(
302
+ "[Display] BLIT SNAPSHOT: Failed to read sprite_transparent_test.gif: {e}"
303
+ ));
304
+ return;
305
+ }
306
+ };
307
+
308
+ let opaque_img = match decode_gif_indexed(&opaque_data) {
309
+ Some(img) => img,
310
+ None => {
311
+ let _ = log_line("[Display] BLIT SNAPSHOT: Failed to decode sprite_test.gif");
312
+ return;
313
+ }
314
+ };
315
+ let transparent_img = match decode_gif_indexed(&transparent_data) {
316
+ Some(img) => img,
317
+ None => {
318
+ let _ =
319
+ log_line("[Display] BLIT SNAPSHOT: Failed to decode sprite_transparent_test.gif");
320
+ return;
321
+ }
322
+ };
323
+
324
+ let dir = format!("{PROJECT_ROOT}/testdata/snapshots");
325
+ let _ = fs::create_dir_all(&dir);
326
+ let mut count = 0u32;
327
+
328
+ // Helper: save a BitGrid to a snapshot file
329
+ let save_grid = |grid: *mut DisplayBitGrid, name: &str| {
330
+ let w = (*grid).width;
331
+ let h = (*grid).height;
332
+ let stride = (*grid).row_stride;
333
+ let data = (*grid).data;
334
+ let data_size = (stride * h) as usize;
335
+ let path = format!("{dir}/{name}.bin");
336
+ if let Ok(mut file) = fs::File::create(&path) {
337
+ let _ = file.write_all(&w.to_le_bytes());
338
+ let _ = file.write_all(&h.to_le_bytes());
339
+ let _ = file.write_all(&stride.to_le_bytes());
340
+ let _ = file.write_all(core::slice::from_raw_parts(data, data_size));
341
+ }
342
+ };
343
+
344
+ // For each test image, run blit with various parameters
345
+ for (img, prefix) in [
346
+ (&opaque_img, "blit_opaque"),
347
+ (&transparent_img, "blit_transparent"),
348
+ ] {
349
+ // Create source BitGrid from image data
350
+ let src = DisplayBitGrid::alloc(8, img.width, img.height);
351
+ if src.is_null() {
352
+ let _ = log_line("[Display] BLIT SNAPSHOT: Failed to allocate source BitGrid");
353
+ continue;
354
+ }
355
+ // Copy image pixels into source BitGrid
356
+ for y in 0..img.height {
357
+ let src_row = (y * img.width) as usize;
358
+ let dst_row = (*src).data.add((y * (*src).row_stride) as usize);
359
+ core::ptr::copy_nonoverlapping(
360
+ img.pixels.as_ptr().add(src_row),
361
+ dst_row,
362
+ img.width as usize,
363
+ );
364
+ }
365
+ (*src).clip_left = 0;
366
+ (*src).clip_top = 0;
367
+ (*src).clip_right = img.width;
368
+ (*src).clip_bottom = img.height;
369
+
370
+ let sw = img.width as i32;
371
+ let sh = img.height as i32;
372
+
373
+ // Destination is larger than source to test positioning
374
+ let dw = (sw + 32) as u32;
375
+ let dh = (sh + 32) as u32;
376
+ let dst = DisplayBitGrid::alloc(8, dw, dh);
377
+ if dst.is_null() {
378
+ let destructor = (*(*src).vtable).destructor;
379
+ destructor(src, 1);
380
+ let _ = log_line("[Display] BLIT SNAPSHOT: Failed to allocate dest BitGrid");
381
+ continue;
382
+ }
383
+ let dst_data = (*dst).data;
384
+ let dst_size = ((*dst).row_stride * dh) as usize;
385
+
386
+ // Test cases: (name_suffix, dst_x, dst_y, width, height, src_x, src_y, flags, bg_fill)
387
+ let test_cases: &[(&str, i32, i32, i32, i32, i32, i32, u32, u8)] = &[
388
+ // Basic orientations (blend mode 0 = direct copy)
389
+ ("identity", 16, 16, sw, sh, 0, 0, 0x0000_0000, 0),
390
+ ("mirror_x", 16, 16, sw, sh, 0, 0, 0x0001_0000, 0),
391
+ ("mirror_y", 16, 16, sw, sh, 0, 0, 0x0002_0000, 0),
392
+ ("mirror_xy", 16, 16, sw, sh, 0, 0, 0x0003_0000, 0),
393
+ ("rotate90", 16, 16, sh, sw, 0, 0, 0x0004_0000, 0),
394
+ // Clipped (negative offset, only bottom-right visible)
395
+ ("clipped", -16, -16, sw, sh, 0, 0, 0x0000_0000, 0),
396
+ // Color-table blend (mode 1) with identity table — tests transparency
397
+ ("colortable", 16, 16, sw, sh, 0, 0, 0x0000_0001, 77),
398
+ // Color-table + mirror_x
399
+ ("colortable_mx", 16, 16, sw, sh, 0, 0, 0x0001_0001, 77),
400
+ // Source sub-rect
401
+ (
402
+ "subrect",
403
+ 16,
404
+ 16,
405
+ sw / 2,
406
+ sh / 2,
407
+ sw / 4,
408
+ sh / 4,
409
+ 0x0000_0000,
410
+ 0,
411
+ ),
412
+ ];
413
+
414
+ let blit_target = rb(va::BLIT_SPRITE_RECT);
415
+
416
+ for &(suffix, dx, dy, w, h, sx, sy, flags, bg) in test_cases {
417
+ // Clear destination
418
+ core::ptr::write_bytes(dst_data, bg, dst_size);
419
+
420
+ // Call WA's native blit: ESI=dst, 9 stack params
421
+ wa_blit_sprite_rect(
422
+ dst,
423
+ dx,
424
+ dy,
425
+ w,
426
+ h,
427
+ src,
428
+ sx,
429
+ sy,
430
+ 0, // color_table = 0 (none) for mode 0; ignored for mode 1 without table
431
+ flags,
432
+ blit_target,
433
+ );
434
+
435
+ let name = format!("{prefix}_{suffix}");
436
+ save_grid(dst, &name);
437
+ count += 1;
438
+ }
439
+
440
+ // Free grids
441
+ let destructor = (*(*src).vtable).destructor;
442
+ destructor(src, 1);
443
+ let destructor = (*(*dst).vtable).destructor;
444
+ destructor(dst, 1);
445
+ }
446
+
447
+ let _ = log_line(&format!(
448
+ "[Display] BLIT SNAPSHOT: Saved {count} snapshots to {dir}/"
449
+ ));
450
+ }
451
+
452
+ /// Decoded indexed image (no WA dependencies).
453
+ struct IndexedImage {
454
+ width: u32,
455
+ height: u32,
456
+ pixels: Vec<u8>,
457
+ }
458
+
459
+ /// Decode a GIF to raw indexed pixels, remapping transparent index to 0.
460
+ fn decode_gif_indexed(data: &[u8]) -> Option<IndexedImage> {
461
+ use gif::DecodeOptions;
462
+ use std::io::Cursor;
463
+
464
+ let mut opts = DecodeOptions::new();
465
+ opts.set_color_output(gif::ColorOutput::Indexed);
466
+ let mut decoder = opts.read_info(Cursor::new(data)).ok()?;
467
+ let _global_pal = decoder.global_palette().map(|p| p.to_vec());
468
+ let frame = decoder.read_next_frame().ok()?.cloned()?;
469
+
470
+ let w = frame.width as u32;
471
+ let h = frame.height as u32;
472
+ let mut pixels = frame.buffer.to_vec();
473
+
474
+ // Remap transparent index to 0 (WA convention)
475
+ if let Some(ti) = frame.transparent {
476
+ if ti != 0 {
477
+ for p in &mut pixels {
478
+ if *p == 0 {
479
+ *p = ti;
480
+ } else if *p == ti {
481
+ *p = 0;
482
+ }
483
+ }
484
+ }
485
+ }
486
+
487
+ Some(IndexedImage {
488
+ width: w,
489
+ height: h,
490
+ pixels,
491
+ })
492
+ }
493
+
494
+ /// Call WA's native blit function (FUN_004f6910).
495
+ ///
496
+ /// The function is a usercall: ESI = destination BitGrid, 9 stack params.
497
+ /// `target` is the rebased runtime address of FUN_004f6910.
498
+ #[unsafe(naked)]
499
+ unsafe extern "cdecl" fn wa_blit_sprite_rect(
500
+ dst: *mut openwa_core::display::DisplayBitGrid,
501
+ dst_x: i32,
502
+ dst_y: i32,
503
+ width: i32,
504
+ height: i32,
505
+ src: *mut openwa_core::display::DisplayBitGrid,
506
+ src_x: i32,
507
+ src_y: i32,
508
+ color_table: u32,
509
+ flags: u32,
510
+ target: u32,
511
+ ) {
512
+ // cdecl params on stack: [ESP+4]=dst, [ESP+8]=dst_x, ...
513
+ // [ESP+40]=flags, [ESP+44]=target
514
+ // Need to: set ESI=dst, push 9 params, call target, restore ESI
515
+ //
516
+ // After push esi + push edi, the params start at ESP+12:
517
+ // ESP+12=dst, ESP+16=dst_x, ESP+20=dst_y, ESP+24=width,
518
+ // ESP+28=height, ESP+32=src, ESP+36=src_x, ESP+40=src_y,
519
+ // ESP+44=color_table, ESP+48=flags, ESP+52=target
520
+ //
521
+ // Each `push [esp+48]` pushes the topmost remaining param (flags first),
522
+ // then the sliding window moves: after each push, the NEXT param occupies
523
+ // the same [ESP+48] slot.
524
+ core::arch::naked_asm!(
525
+ "push esi",
526
+ "push edi",
527
+ "mov esi, [esp + 12]", // ESI = dst BitGrid
528
+ "mov edi, [esp + 52]", // EDI = target function address
529
+ "push dword ptr [esp + 48]", // flags
530
+ "push dword ptr [esp + 48]", // color_table
531
+ "push dword ptr [esp + 48]", // src_y
532
+ "push dword ptr [esp + 48]", // src_x
533
+ "push dword ptr [esp + 48]", // src
534
+ "push dword ptr [esp + 48]", // height
535
+ "push dword ptr [esp + 48]", // width
536
+ "push dword ptr [esp + 48]", // dst_y
537
+ "push dword ptr [esp + 48]", // dst_x
538
+ "call edi", // RET 0x24 cleans 9 params
539
+ "pop edi",
540
+ "pop esi",
541
+ "ret",
542
+ );
543
+ }
544
+
545
+ // =========================================================================
546
+ // Core sprite blit hook (BitGrid__BlitSpriteRect, 0x4F6910)
547
+ // =========================================================================
548
+
549
+ use core::sync::atomic::{AtomicU32, Ordering};
550
+
551
+ /// Trampoline to the original WA blit function (for unsupported modes).
552
+ static ORIG_BLIT: AtomicU32 = AtomicU32::new(0);
553
+
554
+ /// Naked trampoline: captures ESI (dst BitGrid) and forwards to cdecl impl.
555
+ ///
556
+ /// Original calling convention: ESI=dst, 9 stdcall params, RET 0x24.
557
+ #[unsafe(naked)]
558
+ unsafe extern "C" fn blit_hook_trampoline() {
559
+ core::arch::naked_asm!(
560
+ "push ebp",
561
+ "mov ebp, esp",
562
+ // Push 9 stack params + ESI as 10 cdecl params
563
+ "push dword ptr [ebp+0x28]", // flags
564
+ "push dword ptr [ebp+0x24]", // color_table
565
+ "push dword ptr [ebp+0x20]", // src_y
566
+ "push dword ptr [ebp+0x1C]", // src_x
567
+ "push dword ptr [ebp+0x18]", // src BitGrid
568
+ "push dword ptr [ebp+0x14]", // height
569
+ "push dword ptr [ebp+0x10]", // width
570
+ "push dword ptr [ebp+0x0C]", // dst_y
571
+ "push dword ptr [ebp+0x08]", // dst_x
572
+ "push esi", // dst BitGrid
573
+ "call {impl_fn}",
574
+ "add esp, 40",
575
+ "mov esp, ebp",
576
+ "pop ebp",
577
+ "ret 0x24",
578
+ impl_fn = sym blit_impl,
579
+ );
580
+ }
581
+
582
+ /// Rust implementation of the core sprite blit.
583
+ ///
584
+ /// Handles 8bpp blend modes 0 (copy) and 1 (color table / transparency).
585
+ /// Falls through to the original WA function for unsupported modes.
586
+ unsafe extern "cdecl" fn blit_impl(
587
+ dst: *mut openwa_core::display::DisplayBitGrid,
588
+ dst_x: i32,
589
+ dst_y: i32,
590
+ width: i32,
591
+ height: i32,
592
+ src: *mut openwa_core::display::DisplayBitGrid,
593
+ src_x: i32,
594
+ src_y: i32,
595
+ color_table: u32,
596
+ flags: u32,
597
+ ) -> u32 {
598
+ use openwa_core::display::sprite_blit::{
599
+ blit_sprite_rect, BlitBlend, BlitOrientation, BlitSource,
600
+ };
601
+
602
+ if width == 0 || height == 0 {
603
+ return 0;
604
+ }
605
+
606
+ let blend_mode = flags & 0xFFFF;
607
+ let src_cpp = (*src).cells_per_unit;
608
+ let dst_cpp = (*dst).cells_per_unit;
609
+
610
+ // Only handle 8bpp surfaces with blend modes 0 and 1
611
+ if dst_cpp != 8 || src_cpp != 8 || blend_mode > 1 {
612
+ return call_original_blit(
613
+ dst,
614
+ dst_x,
615
+ dst_y,
616
+ width,
617
+ height,
618
+ src,
619
+ src_x,
620
+ src_y,
621
+ color_table,
622
+ flags,
623
+ );
624
+ }
625
+
626
+ // For mode 1 with a color table pointer, fall through for now
627
+ // (we'd need to read 256 bytes from the pointer, which could be a mixing LUT)
628
+ if blend_mode == 1 && color_table != 0 {
629
+ return call_original_blit(
630
+ dst,
631
+ dst_x,
632
+ dst_y,
633
+ width,
634
+ height,
635
+ src,
636
+ src_x,
637
+ src_y,
638
+ color_table,
639
+ flags,
640
+ );
641
+ }
642
+
643
+ let orientation = BlitOrientation::from_flags(flags);
644
+ let blend = BlitBlend::from_flags(flags);
645
+
646
+ // Build source view
647
+ let src_data =
648
+ core::slice::from_raw_parts((*src).data, ((*src).row_stride * (*src).height) as usize);
649
+ let blit_src = BlitSource {
650
+ data: src_data,
651
+ width: (*src).width,
652
+ height: (*src).height,
653
+ row_stride: (*src).row_stride,
654
+ };
655
+
656
+ // Build mutable destination view
657
+ let dst_stride = (*dst).row_stride;
658
+ let dst_h = (*dst).height;
659
+ let dst_w = (*dst).width;
660
+ let dst_data_len = (dst_stride * dst_h) as usize;
661
+
662
+ // Create a temporary PixelGrid wrapping the destination BitGrid's memory
663
+ let mut dst_grid = openwa_core::display::line_draw::PixelGrid {
664
+ data: Vec::new(), // placeholder — we'll swap in the real data
665
+ width: dst_w,
666
+ height: dst_h,
667
+ row_stride: dst_stride,
668
+ clip_left: (*dst).clip_left,
669
+ clip_top: (*dst).clip_top,
670
+ clip_right: (*dst).clip_right,
671
+ clip_bottom: (*dst).clip_bottom,
672
+ };
673
+ // Swap the real data in (avoids allocation)
674
+ let mut real_data = Vec::from_raw_parts((*dst).data, dst_data_len, dst_data_len);
675
+ core::mem::swap(&mut dst_grid.data, &mut real_data);
676
+
677
+ let result = blit_sprite_rect(
678
+ &mut dst_grid,
679
+ &blit_src,
680
+ dst_x,
681
+ dst_y,
682
+ width,
683
+ height,
684
+ src_x,
685
+ src_y,
686
+ None, // no color table for modes we handle
687
+ orientation,
688
+ blend,
689
+ );
690
+
691
+ // Swap data back — we must NOT let Vec drop the BitGrid's data
692
+ core::mem::swap(&mut dst_grid.data, &mut real_data);
693
+ core::mem::forget(real_data); // BitGrid owns this memory
694
+
695
+ result as u32
696
+ }
697
+
698
+ /// Call the original WA blit function via trampoline.
699
+ ///
700
+ /// The trampoline expects: ESI=dst, 9 stdcall params.
701
+ unsafe fn call_original_blit(
702
+ dst: *mut openwa_core::display::DisplayBitGrid,
703
+ dst_x: i32,
704
+ dst_y: i32,
705
+ width: i32,
706
+ height: i32,
707
+ src: *mut openwa_core::display::DisplayBitGrid,
708
+ src_x: i32,
709
+ src_y: i32,
710
+ color_table: u32,
711
+ flags: u32,
712
+ ) -> u32 {
713
+ let orig = ORIG_BLIT.load(Ordering::Relaxed);
714
+ call_original_blit_asm(
715
+ dst,
716
+ dst_x,
717
+ dst_y,
718
+ width,
719
+ height,
720
+ src,
721
+ src_x,
722
+ src_y,
723
+ color_table,
724
+ flags,
725
+ orig,
726
+ )
727
+ }
728
+
729
+ /// Naked asm to call the original blit with ESI set correctly.
730
+ #[unsafe(naked)]
731
+ unsafe extern "cdecl" fn call_original_blit_asm(
732
+ _dst: *mut openwa_core::display::DisplayBitGrid,
733
+ _dst_x: i32,
734
+ _dst_y: i32,
735
+ _width: i32,
736
+ _height: i32,
737
+ _src: *mut openwa_core::display::DisplayBitGrid,
738
+ _src_x: i32,
739
+ _src_y: i32,
740
+ _color_table: u32,
741
+ _flags: u32,
742
+ _target: u32,
743
+ ) -> u32 {
744
+ core::arch::naked_asm!(
745
+ "push esi",
746
+ "push edi",
747
+ "mov esi, [esp + 12]", // dst
748
+ "mov edi, [esp + 52]", // target
749
+ "push dword ptr [esp + 48]", // flags
750
+ "push dword ptr [esp + 48]", // color_table
751
+ "push dword ptr [esp + 48]", // src_y
752
+ "push dword ptr [esp + 48]", // src_x
753
+ "push dword ptr [esp + 48]", // src
754
+ "push dword ptr [esp + 48]", // height
755
+ "push dword ptr [esp + 48]", // width
756
+ "push dword ptr [esp + 48]", // dst_y
757
+ "push dword ptr [esp + 48]", // dst_x
758
+ "call edi",
759
+ "pop edi",
760
+ "pop esi",
761
+ "ret",
762
+ );
763
+ }
764
+
765
  pub fn install() -> Result<(), String> {
766
  let _ = log_line("[Display] Patching DisplayBase vtables");
767
 
 
803
  draw_crosshair => dd_display::draw_crosshair,
804
  draw_outlined_pixel => dd_display::draw_outlined_pixel,
805
  fill_rect => dd_display::fill_rect,
806
+ draw_via_callback => dd_display::draw_via_callback,
807
  flush_render => dd_display::flush_render,
808
  set_camera_offset => dd_display::set_camera_offset,
809
  set_clip_rect => dd_display::set_clip_rect,
810
+ is_sprite_loaded => dd_display::is_sprite_loaded,
811
  })?;
812
+ let _ = log_line("[Display] DDDisplay: patched 12 methods → Rust");
813
+
814
+ // Hook core sprite blit (0x4F6910) via MinHook
815
+ let orig = crate::hook::install(
816
+ "BitGrid__BlitSpriteRect",
817
+ va::BLIT_SPRITE_RECT,
818
+ blit_hook_trampoline as *const (),
819
+ )?;
820
+ ORIG_BLIT.store(orig as u32, Ordering::Relaxed);
821
  }
822
 
823
  Ok(())
crates/openwa-dll/src/replacements/frame_hook.rs CHANGED
@@ -63,11 +63,14 @@ unsafe extern "stdcall" fn hook_turn_manager(turngame: u32) {
63
 
64
  let frame = FRAMES_PROCESSED.fetch_add(1, Ordering::Relaxed) + 1;
65
 
66
- // Line snapshot capture: run once at frame 10 when env var is set
67
  if frame == 10 {
68
  if std::env::var("OPENWA_CAPTURE_LINE_SNAPSHOTS").is_ok() {
69
  super::display::capture_line_snapshots();
70
  }
 
 
 
71
  }
72
 
73
  // 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 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::display::capture_line_snapshots();
70
  }
71
+ if std::env::var("OPENWA_CAPTURE_BLIT_SNAPSHOTS").is_ok() {
72
+ super::display::capture_blit_snapshots();
73
+ }
74
  }
75
 
76
  // Replay test: fast-forward + milestone tracking (no-op if not in replay test mode)
testdata/assets/sprite_test.gif ADDED

Git LFS Details

  • SHA256: 74507e52e32ed61b0027eb2a752b0a8a80ae25e567e374dc599908fbe42bb081
  • Pointer size: 129 Bytes
  • Size of remote file: 8.64 kB
testdata/assets/sprite_transparent_test.gif ADDED

Git LFS Details

  • SHA256: 123874fa48068c0c628d407b255c49d930d976e0c666de08efa8d800996f2e92
  • Pointer size: 128 Bytes
  • Size of remote file: 482 Bytes
testdata/snapshots/blit_opaque_clipped.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d70b5a0d9eb622a9fba4949487013fa83b3ccb54033ac7c1e4465292663086ae
3
+ size 25612
testdata/snapshots/blit_opaque_colortable.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:674b3978de2a4221bc70426270b04d787919b514a9d4d936862eac33c5708088
3
+ size 25612
testdata/snapshots/blit_opaque_colortable_mx.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ca2b5e03d46a5165878e6f2d45e318b0d38255ee17c271560fae67c604e5076b
3
+ size 25612
testdata/snapshots/blit_opaque_identity.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:373212b13209d949dc01ec5da36d7ce2cd20f62420f35fded1c5fb351e50898c
3
+ size 25612
testdata/snapshots/blit_opaque_mirror_x.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:373212b13209d949dc01ec5da36d7ce2cd20f62420f35fded1c5fb351e50898c
3
+ size 25612
testdata/snapshots/blit_opaque_mirror_xy.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:373212b13209d949dc01ec5da36d7ce2cd20f62420f35fded1c5fb351e50898c
3
+ size 25612
testdata/snapshots/blit_opaque_mirror_y.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:373212b13209d949dc01ec5da36d7ce2cd20f62420f35fded1c5fb351e50898c
3
+ size 25612
testdata/snapshots/blit_opaque_rotate90.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:373212b13209d949dc01ec5da36d7ce2cd20f62420f35fded1c5fb351e50898c
3
+ size 25612
testdata/snapshots/blit_opaque_subrect.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:39abc5c41763fcfb2708c905ccc80b752a61a4c984ceb7354a2ceadd50e08436
3
+ size 25612
testdata/snapshots/blit_transparent_clipped.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:298c2591ee90cc2fba916fe0fb95f8199b8847465efd0037629466cd50704d5a
3
+ size 25612
testdata/snapshots/blit_transparent_colortable.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:943144ddded70c2c54ece1b7b5713247d4c36d042f9e8cd301e4355b2646b519
3
+ size 25612
testdata/snapshots/blit_transparent_colortable_mx.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1f962c6dc14dcedbe7507a3c28dd5839b9033839dd178cf2af7e73d7879f256f
3
+ size 25612
testdata/snapshots/blit_transparent_identity.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f892414f33d882e46788ba903eb5ebb9b29f3548d7a946b140e230ba1db37ce3
3
+ size 25612
testdata/snapshots/blit_transparent_mirror_x.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f892414f33d882e46788ba903eb5ebb9b29f3548d7a946b140e230ba1db37ce3
3
+ size 25612
testdata/snapshots/blit_transparent_mirror_xy.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f892414f33d882e46788ba903eb5ebb9b29f3548d7a946b140e230ba1db37ce3
3
+ size 25612
testdata/snapshots/blit_transparent_mirror_y.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f892414f33d882e46788ba903eb5ebb9b29f3548d7a946b140e230ba1db37ce3
3
+ size 25612
testdata/snapshots/blit_transparent_rotate90.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f892414f33d882e46788ba903eb5ebb9b29f3548d7a946b140e230ba1db37ce3
3
+ size 25612
testdata/snapshots/blit_transparent_subrect.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c2f109d83eb085d2f4e3d2810e4850aee7348be635cfcb30e65d450586bd4a27
3
+ size 25612