feat: port DisplayGfx slot 22 (draw_tiled_terrain)
Browse filesPort the terrain tile rendering method (21/38 slots). Renders bitmap
tiles in a row-major grid pattern with camera offset, delegating to
BlitBitmapClipped (0x56A700) via a naked asm bridge.
- Define TileBitmapSet struct (count + bitmap_ptrs array pointer)
- Name DisplayGfx tile grid fields: tile_bitmap_sets, tile_total_width/
height, tile_col_width/row_height (previously _unknown_4ddc gap)
- Use Fixed type for x/y coordinates (was raw i32 with >> 16)
- Rename stream_data → draw_tiled_terrain (matches actual purpose)
- Add offset assertions for all tile config fields
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
crates/openwa-core/src/display/display_vtable.rs
CHANGED
|
@@ -175,9 +175,13 @@ pub struct DisplayVtable {
|
|
| 175 |
#[slot(21)]
|
| 176 |
pub draw_via_callback:
|
| 177 |
fn(this: *mut DisplayGfx, x: Fixed, y: Fixed, obj: *mut u8, p5: u32, p6: u32),
|
| 178 |
-
///
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
#[slot(22)]
|
| 180 |
-
pub
|
| 181 |
/// set layer visibility (0x56A5D0, RET 0x8)
|
| 182 |
#[slot(23)]
|
| 183 |
pub set_layer_visibility: fn(this: *mut DisplayGfx, layer: i32, visible: i32),
|
|
@@ -951,6 +955,152 @@ pub unsafe extern "thiscall" fn draw_via_callback(
|
|
| 951 |
callback(obj, layer_0, pixel_x, pixel_y, p5, p6);
|
| 952 |
}
|
| 953 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 954 |
/// Port of DisplayGfx::DrawPixelStrip (vtable slot 15, 0x56BE10).
|
| 955 |
///
|
| 956 |
/// Draws `count + 1` pixels starting at (x, y), stepping by (dx, dy) each
|
|
|
|
| 175 |
#[slot(21)]
|
| 176 |
pub draw_via_callback:
|
| 177 |
fn(this: *mut DisplayGfx, x: Fixed, y: Fixed, obj: *mut u8, p5: u32, p6: u32),
|
| 178 |
+
/// draw tiled terrain (0x56C5A0, RET 0x10)
|
| 179 |
+
///
|
| 180 |
+
/// Tiles bitmaps from a grid config in a row-major pattern with camera offset.
|
| 181 |
+
/// `count` limits the number of pixel-rows rendered. `flags` low 16 bits
|
| 182 |
+
/// selects mode (only 1 supported), bit 19 controls a blit transparency flag.
|
| 183 |
#[slot(22)]
|
| 184 |
+
pub draw_tiled_terrain: fn(this: *mut DisplayGfx, x: Fixed, y: Fixed, count: i32, flags: u32),
|
| 185 |
/// set layer visibility (0x56A5D0, RET 0x8)
|
| 186 |
#[slot(23)]
|
| 187 |
pub set_layer_visibility: fn(this: *mut DisplayGfx, layer: i32, visible: i32),
|
|
|
|
| 955 |
callback(obj, layer_0, pixel_x, pixel_y, p5, p6);
|
| 956 |
}
|
| 957 |
|
| 958 |
+
/// Port of DisplayGfx::StreamData (vtable slot 22, 0x56C5A0).
|
| 959 |
+
///
|
| 960 |
+
/// Tiles bitmaps from a grid configuration in row-major order with camera offset.
|
| 961 |
+
/// The tile grid is defined by `tile_total_width/height` and `tile_col_width/row_height`
|
| 962 |
+
/// fields on DisplayGfx. Bitmaps come from the object at `tile_bitmap_sets[1]`,
|
| 963 |
+
/// whose field at +0x04 is a pointer array of bitmap pointers.
|
| 964 |
+
///
|
| 965 |
+
/// `x`/`y` are Fixed-point (>> 16 for pixels). `count` limits how many pixel-rows
|
| 966 |
+
/// are rendered. `flags` low 16 bits must be 1 (only supported mode); bit 19
|
| 967 |
+
/// controls a blit transparency flag.
|
| 968 |
+
///
|
| 969 |
+
/// # Safety
|
| 970 |
+
/// `this` must be a valid `*mut DisplayGfx`.
|
| 971 |
+
pub unsafe extern "thiscall" fn draw_tiled_terrain(
|
| 972 |
+
this: *mut DisplayGfx,
|
| 973 |
+
x: Fixed,
|
| 974 |
+
y: Fixed,
|
| 975 |
+
mut count: i32,
|
| 976 |
+
flags: u32,
|
| 977 |
+
) {
|
| 978 |
+
if count <= 0 {
|
| 979 |
+
return;
|
| 980 |
+
}
|
| 981 |
+
|
| 982 |
+
let total_height = (*this).tile_total_height;
|
| 983 |
+
if count > total_height {
|
| 984 |
+
count = total_height;
|
| 985 |
+
}
|
| 986 |
+
|
| 987 |
+
// Only mode 1 is supported
|
| 988 |
+
if (flags & 0xFFFF) != 1 {
|
| 989 |
+
return;
|
| 990 |
+
}
|
| 991 |
+
|
| 992 |
+
// Get the bitmap tile set (mode 1 → tile_bitmap_sets[1] at offset 0x4DD8)
|
| 993 |
+
let tile_set = (*this).tile_bitmap_sets[1];
|
| 994 |
+
if tile_set.is_null() {
|
| 995 |
+
return;
|
| 996 |
+
}
|
| 997 |
+
|
| 998 |
+
let bitmap_array = (*tile_set).bitmap_ptrs;
|
| 999 |
+
|
| 1000 |
+
let pixel_x = (*this).camera_x + x.to_int();
|
| 1001 |
+
let pixel_y = (*this).camera_y + y.to_int();
|
| 1002 |
+
let blit_flags = (!flags >> 19) & 2;
|
| 1003 |
+
|
| 1004 |
+
let total_width = (*this).tile_total_width;
|
| 1005 |
+
let col_width = (*this).tile_col_width;
|
| 1006 |
+
let row_height = (*this).tile_row_height;
|
| 1007 |
+
|
| 1008 |
+
let mut bitmap_idx = 0u32;
|
| 1009 |
+
let mut y_offset = 0i32;
|
| 1010 |
+
|
| 1011 |
+
if total_height <= 0 {
|
| 1012 |
+
return;
|
| 1013 |
+
}
|
| 1014 |
+
|
| 1015 |
+
while y_offset < total_height {
|
| 1016 |
+
if y_offset >= count {
|
| 1017 |
+
return;
|
| 1018 |
+
}
|
| 1019 |
+
|
| 1020 |
+
// Clamp row height to remaining count
|
| 1021 |
+
let mut row_h = row_height;
|
| 1022 |
+
if count - y_offset < row_height {
|
| 1023 |
+
row_h = count - y_offset;
|
| 1024 |
+
}
|
| 1025 |
+
|
| 1026 |
+
let mut x_offset = 0i32;
|
| 1027 |
+
while x_offset < total_width {
|
| 1028 |
+
// Clamp column width to remaining grid width
|
| 1029 |
+
let col_w = col_width.min(total_width - x_offset);
|
| 1030 |
+
|
| 1031 |
+
let bitmap_ptr = *bitmap_array.add(bitmap_idx as usize);
|
| 1032 |
+
|
| 1033 |
+
blit_bitmap_clipped(
|
| 1034 |
+
this,
|
| 1035 |
+
col_w,
|
| 1036 |
+
pixel_x + x_offset,
|
| 1037 |
+
pixel_y + y_offset,
|
| 1038 |
+
row_h,
|
| 1039 |
+
bitmap_ptr,
|
| 1040 |
+
blit_flags,
|
| 1041 |
+
);
|
| 1042 |
+
|
| 1043 |
+
bitmap_idx += 1;
|
| 1044 |
+
x_offset += col_width;
|
| 1045 |
+
}
|
| 1046 |
+
|
| 1047 |
+
y_offset += row_height;
|
| 1048 |
+
}
|
| 1049 |
+
}
|
| 1050 |
+
|
| 1051 |
+
/// Bridge to DisplayGfx__BlitBitmapClipped (0x56A700).
|
| 1052 |
+
///
|
| 1053 |
+
/// Usercall: EAX=this (DisplayGfx*), EDX=col_width, 5 stack params (dst_x, dst_y,
|
| 1054 |
+
/// row_height, bitmap_ptr, flags), RET 0x14.
|
| 1055 |
+
///
|
| 1056 |
+
/// Clips the bitmap rectangle against the display clip rect, calls flush_render_lock,
|
| 1057 |
+
/// then delegates to the low-level blit function (0x403C60).
|
| 1058 |
+
unsafe fn blit_bitmap_clipped(
|
| 1059 |
+
gfx: *mut DisplayGfx,
|
| 1060 |
+
col_width: i32,
|
| 1061 |
+
dst_x: i32,
|
| 1062 |
+
dst_y: i32,
|
| 1063 |
+
row_height: i32,
|
| 1064 |
+
bitmap_ptr: u32,
|
| 1065 |
+
flags: u32,
|
| 1066 |
+
) {
|
| 1067 |
+
blit_bitmap_clipped_bridge(
|
| 1068 |
+
gfx as u32,
|
| 1069 |
+
col_width as u32,
|
| 1070 |
+
dst_x as u32,
|
| 1071 |
+
dst_y as u32,
|
| 1072 |
+
row_height as u32,
|
| 1073 |
+
bitmap_ptr,
|
| 1074 |
+
flags,
|
| 1075 |
+
rb(va::DISPLAY_GFX_BLIT_BITMAP_CLIPPED),
|
| 1076 |
+
);
|
| 1077 |
+
}
|
| 1078 |
+
|
| 1079 |
+
/// Naked bridge: sets EAX=this, EDX=col_width, pushes 5 stack params, calls target.
|
| 1080 |
+
#[unsafe(naked)]
|
| 1081 |
+
unsafe extern "cdecl" fn blit_bitmap_clipped_bridge(
|
| 1082 |
+
_this: u32,
|
| 1083 |
+
_col_width: u32,
|
| 1084 |
+
_dst_x: u32,
|
| 1085 |
+
_dst_y: u32,
|
| 1086 |
+
_row_height: u32,
|
| 1087 |
+
_bitmap_ptr: u32,
|
| 1088 |
+
_flags: u32,
|
| 1089 |
+
_target: u32,
|
| 1090 |
+
) {
|
| 1091 |
+
core::arch::naked_asm!(
|
| 1092 |
+
"mov eax, [esp + 4]", // EAX = this
|
| 1093 |
+
"mov edx, [esp + 8]", // EDX = col_width
|
| 1094 |
+
"push [esp + 28]", // flags
|
| 1095 |
+
"push [esp + 28]", // bitmap_ptr (shifted by our push)
|
| 1096 |
+
"push [esp + 28]", // row_height
|
| 1097 |
+
"push [esp + 28]", // dst_y
|
| 1098 |
+
"push [esp + 28]", // dst_x
|
| 1099 |
+
"call [esp + 52]", // target (offset: 5 pushes × 4 + 32 original)
|
| 1100 |
+
"ret",
|
| 1101 |
+
);
|
| 1102 |
+
}
|
| 1103 |
+
|
| 1104 |
/// Port of DisplayGfx::DrawPixelStrip (vtable slot 15, 0x56BE10).
|
| 1105 |
///
|
| 1106 |
/// Draws `count + 1` pixels starting at (x, y), stepping by (dx, dy) each
|
crates/openwa-core/src/display/gfx.rs
CHANGED
|
@@ -129,12 +129,25 @@ pub struct DisplayGfx {
|
|
| 129 |
/// 0x3DD4: Sprite table — 1024 DWORD entries, zeroed in DisplayGfx__Init.
|
| 130 |
/// Used for tracking loaded sprites/bitmaps by ID.
|
| 131 |
pub sprite_table: [u32; 0x400],
|
| 132 |
-
//
|
| 133 |
-
|
| 134 |
-
//
|
| 135 |
-
|
| 136 |
-
///
|
| 137 |
-
pub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
// =========================================================================
|
| 140 |
// Color mixing lookup tables (0x4DF4 - 0x24DF3)
|
|
@@ -168,6 +181,11 @@ pub struct DisplayGfx {
|
|
| 168 |
}
|
| 169 |
|
| 170 |
const _: () = assert!(core::mem::size_of::<DisplayGfx>() == 0x24E28);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
impl DisplayGfx {
|
| 173 |
/// Allocate and construct a DisplayGfx via WA's native constructor.
|
|
@@ -183,3 +201,15 @@ impl DisplayGfx {
|
|
| 183 |
ctor(WABox::<Self>::alloc(0x24E28, 0x24E08).leak())
|
| 184 |
}
|
| 185 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
/// 0x3DD4: Sprite table — 1024 DWORD entries, zeroed in DisplayGfx__Init.
|
| 130 |
/// Used for tracking loaded sprites/bitmaps by ID.
|
| 131 |
pub sprite_table: [u32; 0x400],
|
| 132 |
+
// =========================================================================
|
| 133 |
+
// Tile stream config (0x4DD4 - 0x4DF3)
|
| 134 |
+
// =========================================================================
|
| 135 |
+
/// 0x4DD4: Bitmap tile set pointers, indexed by mode (0-based).
|
| 136 |
+
/// draw_tiled_terrain (slot 22) uses mode 1 (index 1, at offset 0x4DD8). Init 0.
|
| 137 |
+
pub tile_bitmap_sets: [*const TileBitmapSet; 2],
|
| 138 |
+
/// 0x4DDC: Total tile grid width in pixels. Inner loop iterates
|
| 139 |
+
/// x from 0 to this value in steps of `tile_col_width`.
|
| 140 |
+
pub tile_total_width: i32,
|
| 141 |
+
/// 0x4DE0: Total tile grid height in pixels. Outer loop iterates
|
| 142 |
+
/// y from 0 to this value in steps of `tile_row_height`.
|
| 143 |
+
/// Also used to clamp the `count` parameter in draw_tiled_terrain.
|
| 144 |
+
pub tile_total_height: i32,
|
| 145 |
+
/// 0x4DE4: Width of each tile column in pixels.
|
| 146 |
+
pub tile_col_width: i32,
|
| 147 |
+
/// 0x4DE8: Height of each tile row in pixels.
|
| 148 |
+
pub tile_row_height: i32,
|
| 149 |
+
/// 0x4DEC - 0x4DF3: Unknown (2 u32s)
|
| 150 |
+
pub _unknown_4dec: [u32; 2],
|
| 151 |
|
| 152 |
// =========================================================================
|
| 153 |
// Color mixing lookup tables (0x4DF4 - 0x24DF3)
|
|
|
|
| 181 |
}
|
| 182 |
|
| 183 |
const _: () = assert!(core::mem::size_of::<DisplayGfx>() == 0x24E28);
|
| 184 |
+
const _: () = assert!(core::mem::offset_of!(DisplayGfx, tile_bitmap_sets) == 0x4DD4);
|
| 185 |
+
const _: () = assert!(core::mem::offset_of!(DisplayGfx, tile_total_width) == 0x4DDC);
|
| 186 |
+
const _: () = assert!(core::mem::offset_of!(DisplayGfx, tile_total_height) == 0x4DE0);
|
| 187 |
+
const _: () = assert!(core::mem::offset_of!(DisplayGfx, tile_col_width) == 0x4DE4);
|
| 188 |
+
const _: () = assert!(core::mem::offset_of!(DisplayGfx, tile_row_height) == 0x4DE8);
|
| 189 |
|
| 190 |
impl DisplayGfx {
|
| 191 |
/// Allocate and construct a DisplayGfx via WA's native constructor.
|
|
|
|
| 201 |
ctor(WABox::<Self>::alloc(0x24E28, 0x24E08).leak())
|
| 202 |
}
|
| 203 |
}
|
| 204 |
+
|
| 205 |
+
/// Tile bitmap set — holds an array of bitmap pointers for tiled rendering.
|
| 206 |
+
///
|
| 207 |
+
/// Pointed to by `DisplayGfx::tile_bitmap_sets`. Used by draw_tiled_terrain (slot 22)
|
| 208 |
+
/// to iterate over bitmap tiles in a grid pattern.
|
| 209 |
+
#[repr(C)]
|
| 210 |
+
pub struct TileBitmapSet {
|
| 211 |
+
/// 0x00: Bitmap count or total size (observed values: 0x168 = 360)
|
| 212 |
+
pub count: u32,
|
| 213 |
+
/// 0x04: Pointer to array of bitmap pointers (one per grid tile, row-major order)
|
| 214 |
+
pub bitmap_ptrs: *const u32,
|
| 215 |
+
}
|
crates/openwa-dll/src/replacements/display.rs
CHANGED
|
@@ -682,6 +682,7 @@ pub fn install() -> Result<(), String> {
|
|
| 682 |
draw_outlined_pixel => display_vtable::draw_outlined_pixel,
|
| 683 |
fill_rect => display_vtable::fill_rect,
|
| 684 |
draw_via_callback => display_vtable::draw_via_callback,
|
|
|
|
| 685 |
flush_render => display_vtable::flush_render,
|
| 686 |
set_camera_offset => display_vtable::set_camera_offset,
|
| 687 |
set_clip_rect => display_vtable::set_clip_rect,
|
|
@@ -691,7 +692,7 @@ pub fn install() -> Result<(), String> {
|
|
| 691 |
update_palette => display_vtable::update_palette,
|
| 692 |
slot 19 => blit_sprite,
|
| 693 |
})?;
|
| 694 |
-
let _ = log_line("[Display] DisplayGfx: patched
|
| 695 |
}
|
| 696 |
|
| 697 |
Ok(())
|
|
|
|
| 682 |
draw_outlined_pixel => display_vtable::draw_outlined_pixel,
|
| 683 |
fill_rect => display_vtable::fill_rect,
|
| 684 |
draw_via_callback => display_vtable::draw_via_callback,
|
| 685 |
+
draw_tiled_terrain => display_vtable::draw_tiled_terrain,
|
| 686 |
flush_render => display_vtable::flush_render,
|
| 687 |
set_camera_offset => display_vtable::set_camera_offset,
|
| 688 |
set_clip_rect => display_vtable::set_clip_rect,
|
|
|
|
| 692 |
update_palette => display_vtable::update_palette,
|
| 693 |
slot 19 => blit_sprite,
|
| 694 |
})?;
|
| 695 |
+
let _ = log_line("[Display] DisplayGfx: patched 21 methods → Rust");
|
| 696 |
}
|
| 697 |
|
| 698 |
Ok(())
|