paavohtl Claude Opus 4.6 (1M context) commited on
Commit
a6c70ec
·
1 Parent(s): 5c0eb9d

refactor: document DDDisplay/DisplayGfx identity, add DDDisplay::as_gfx

Browse files

DDDisplay, DisplayBase, and DisplayGfx are the same object in memory —
only the vtable pointer differs. Document this explicitly and add
DDDisplay::as_gfx() helper to replace raw `this as *mut DisplayGfx`
casts throughout dd_display.rs.

Also extend PaletteContext struct with dirty_range_min/max and cache_iter
fields, replacing raw pointer arithmetic in update_palette.

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

crates/openwa-core/src/display/dd_display.rs CHANGED
@@ -32,13 +32,36 @@ use crate::fixed::Fixed;
32
  /// - 0x3D98: render lock flag (cleared by FlushRender)
33
  /// - 0x3D9C: display context pointer (read during FlushRender)
34
  ///
35
- /// OPAQUE: Full struct layout not yet mapped (see DisplayGfx for size).
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  #[repr(C)]
37
  pub struct DDDisplay {
38
  /// 0x000: Vtable pointer (0x66A218)
39
  pub vtable: *const DDDisplayVtable,
40
  }
41
 
 
 
 
 
 
 
 
 
 
 
42
  /// DDDisplay vtable (0x66A218, 38 slots).
43
  ///
44
  /// Slots 2, 3, 25, 29 are stubs (CGameTask no-ops).
@@ -303,7 +326,7 @@ pub unsafe extern "thiscall" fn get_dimensions(
303
  out_w: *mut u32,
304
  out_h: *mut u32,
305
  ) {
306
- let gfx = this as *mut DisplayGfx;
307
  *out_w = (*gfx).base.display_width;
308
  *out_h = (*gfx).base.display_height;
309
  }
@@ -325,7 +348,7 @@ use crate::rebase::rb;
325
  /// `this` must be a valid `*mut DDDisplay` (actually a `*mut DisplayGfx`).
326
  /// `g_DDDisplayWrapper` (0x79D6D4) must be initialized.
327
  pub unsafe extern "thiscall" fn flush_render(this: *mut DDDisplay) {
328
- let gfx = this as *mut DisplayGfx;
329
  let wrapper = *(rb(va::G_DD_DISPLAY_WRAPPER) as *const *mut DDDisplayWrapper);
330
 
331
  if (*gfx).render_lock != 0 {
@@ -347,7 +370,7 @@ pub unsafe extern "thiscall" fn flush_render(this: *mut DDDisplay) {
347
  /// # Safety
348
  /// `this` must be a valid `*mut DDDisplay` (actually a `*mut DisplayGfx`).
349
  pub unsafe extern "thiscall" fn set_camera_offset(this: *mut DDDisplay, x: Fixed, y: Fixed) {
350
- let gfx = this as *mut DisplayGfx;
351
  (*gfx).camera_x = x.to_int();
352
  (*gfx).camera_y = y.to_int();
353
  }
@@ -367,7 +390,7 @@ pub unsafe extern "thiscall" fn set_clip_rect(
367
  x2: Fixed,
368
  y2: Fixed,
369
  ) {
370
- let gfx = this as *mut DisplayGfx;
371
  let base = &mut (*gfx).base;
372
 
373
  // Convert Fixed to pixel integers
@@ -527,7 +550,7 @@ pub unsafe extern "thiscall" fn fill_rect(
527
  y2: i32,
528
  color: u32,
529
  ) {
530
- let gfx = this as *mut DisplayGfx;
531
  let base = &(*gfx).base;
532
 
533
  // Apply camera offset
@@ -589,7 +612,7 @@ pub unsafe extern "thiscall" fn draw_outlined_pixel(
589
  color_fg: u32,
590
  color_bg: i32,
591
  ) {
592
- let gfx = this as *mut DisplayGfx;
593
  let cx = x + (*gfx).camera_x;
594
  let cy = y + (*gfx).camera_y;
595
 
@@ -627,7 +650,7 @@ pub unsafe extern "thiscall" fn draw_crosshair(
627
  color_fg: u32,
628
  color_bg: u32,
629
  ) {
630
- let gfx = this as *mut DisplayGfx;
631
  let cx = x + (*gfx).camera_x;
632
  let cy = y + (*gfx).camera_y;
633
 
@@ -703,7 +726,7 @@ pub unsafe extern "thiscall" fn draw_line(
703
  color1: u32,
704
  color2: u32,
705
  ) {
706
- let gfx = this as *mut DisplayGfx;
707
  let cam_x = Fixed::from_int((*gfx).camera_x);
708
  let cam_y = Fixed::from_int((*gfx).camera_y);
709
 
@@ -736,7 +759,7 @@ pub unsafe extern "thiscall" fn draw_line_clipped(
736
  y2: Fixed,
737
  color: u32,
738
  ) {
739
- let gfx = this as *mut DisplayGfx;
740
  let cam_x = Fixed::from_int((*gfx).camera_x);
741
  let cam_y = Fixed::from_int((*gfx).camera_y);
742
 
@@ -767,7 +790,7 @@ pub unsafe extern "thiscall" fn draw_polyline(
767
  count: i32,
768
  color: u32,
769
  ) {
770
- let gfx = this as *mut DisplayGfx;
771
  let cam_x = Fixed::from_int((*gfx).camera_x);
772
  let cam_y = Fixed::from_int((*gfx).camera_y);
773
 
@@ -804,7 +827,7 @@ pub unsafe extern "thiscall" fn is_sprite_loaded(this: *mut DDDisplay, id: i32)
804
  if id_u.wrapping_sub(1) >= 0x3FF {
805
  return 0;
806
  }
807
- let gfx = this as *mut DisplayGfx;
808
  let base = &(*gfx).base;
809
  if base.sprite_array_1[id as usize] != 0
810
  || base.sprite_array_2[id as usize] != 0
@@ -833,7 +856,7 @@ pub unsafe extern "thiscall" fn draw_via_callback(
833
  p5: u32,
834
  p6: u32,
835
  ) {
836
- let gfx = this as *mut DisplayGfx;
837
  acquire_render_lock(gfx);
838
 
839
  let pixel_x = (Fixed::from_int((*gfx).camera_x) + x).to_int();
@@ -884,7 +907,7 @@ pub unsafe fn draw_scaled_sprite(
884
  src_h: i32,
885
  flags: u32,
886
  ) -> DrawScaledSpriteResult {
887
- let gfx = this as *mut DisplayGfx;
888
 
889
  let width = src_w - src_x;
890
  let height = src_h - src_y;
@@ -1010,7 +1033,7 @@ pub unsafe extern "thiscall" fn draw_pixel_strip(
1010
  count: i32,
1011
  color: u32,
1012
  ) {
1013
- let gfx = this as *mut DisplayGfx;
1014
  let mut cx = Fixed::from_int((*gfx).camera_x) + x;
1015
  let mut cy = Fixed::from_int((*gfx).camera_y) + y;
1016
 
@@ -1035,7 +1058,7 @@ pub unsafe extern "thiscall" fn draw_pixel_strip(
1035
  /// # Safety
1036
  /// `this` must be a valid `*mut DDDisplay` (actually a `*mut DisplayGfx`).
1037
  pub unsafe extern "thiscall" fn set_active_layer(this: *mut DDDisplay, layer: i32) -> *mut u8 {
1038
- let gfx = this as *mut DisplayGfx;
1039
  if (layer as u32).wrapping_sub(1) < 3 {
1040
  (*gfx).base.layer_contexts[layer as usize] as *mut u8
1041
  } else {
@@ -1061,7 +1084,7 @@ pub unsafe extern "thiscall" fn update_palette(
1061
  palette_ctx: *mut PaletteContext,
1062
  commit: i32,
1063
  ) {
1064
- let gfx = this as *mut DisplayGfx;
1065
  let ctx = &mut *palette_ctx;
1066
 
1067
  // Reset iteration counter
@@ -1160,7 +1183,7 @@ pub unsafe extern "thiscall" fn set_layer_visibility(
1160
  layer: i32,
1161
  visible: i32,
1162
  ) {
1163
- let gfx = this as *mut DisplayGfx;
1164
 
1165
  let layer_ctx = set_active_layer(this, layer) as *mut PaletteContext;
1166
  if !layer_ctx.is_null() {
 
32
  /// - 0x3D98: render lock flag (cleared by FlushRender)
33
  /// - 0x3D9C: display context pointer (read during FlushRender)
34
  ///
35
+ /// In memory, `DDDisplay` is the same object as `DisplayGfx` (which contains
36
+ /// `DisplayBase`). They all start at offset 0 with the vtable pointer. The
37
+ /// vtable determines the class identity:
38
+ ///
39
+ /// ```text
40
+ /// +0x0000 vtable ptr ← DDDisplay (0x66A218), DisplayBase (0x6645F8 or 0x66A0F8)
41
+ /// +0x0004 DisplayBase fields (sprite cache, arrays, clip rect, ...)
42
+ /// ...
43
+ /// +0x3560 DisplayGfx fields (camera, palette, layers, ...)
44
+ /// ... (total: 0x24E28 bytes)
45
+ /// ```
46
+ ///
47
+ /// `DDDisplay` is used as the `this` type in vtable method signatures.
48
+ /// To access fields, cast to `*mut DisplayGfx`: both point to offset 0.
49
  #[repr(C)]
50
  pub struct DDDisplay {
51
  /// 0x000: Vtable pointer (0x66A218)
52
  pub vtable: *const DDDisplayVtable,
53
  }
54
 
55
+ impl DDDisplay {
56
+ /// Cast to the full `DisplayGfx` layout for field access.
57
+ ///
58
+ /// Safe because `DDDisplay` and `DisplayGfx` are the same object in memory.
59
+ #[inline(always)]
60
+ pub fn as_gfx(ptr: *mut DDDisplay) -> *mut DisplayGfx {
61
+ ptr as *mut DisplayGfx
62
+ }
63
+ }
64
+
65
  /// DDDisplay vtable (0x66A218, 38 slots).
66
  ///
67
  /// Slots 2, 3, 25, 29 are stubs (CGameTask no-ops).
 
326
  out_w: *mut u32,
327
  out_h: *mut u32,
328
  ) {
329
+ let gfx = DDDisplay::as_gfx(this);
330
  *out_w = (*gfx).base.display_width;
331
  *out_h = (*gfx).base.display_height;
332
  }
 
348
  /// `this` must be a valid `*mut DDDisplay` (actually a `*mut DisplayGfx`).
349
  /// `g_DDDisplayWrapper` (0x79D6D4) must be initialized.
350
  pub unsafe extern "thiscall" fn flush_render(this: *mut DDDisplay) {
351
+ let gfx = DDDisplay::as_gfx(this);
352
  let wrapper = *(rb(va::G_DD_DISPLAY_WRAPPER) as *const *mut DDDisplayWrapper);
353
 
354
  if (*gfx).render_lock != 0 {
 
370
  /// # Safety
371
  /// `this` must be a valid `*mut DDDisplay` (actually a `*mut DisplayGfx`).
372
  pub unsafe extern "thiscall" fn set_camera_offset(this: *mut DDDisplay, x: Fixed, y: Fixed) {
373
+ let gfx = DDDisplay::as_gfx(this);
374
  (*gfx).camera_x = x.to_int();
375
  (*gfx).camera_y = y.to_int();
376
  }
 
390
  x2: Fixed,
391
  y2: Fixed,
392
  ) {
393
+ let gfx = DDDisplay::as_gfx(this);
394
  let base = &mut (*gfx).base;
395
 
396
  // Convert Fixed to pixel integers
 
550
  y2: i32,
551
  color: u32,
552
  ) {
553
+ let gfx = DDDisplay::as_gfx(this);
554
  let base = &(*gfx).base;
555
 
556
  // Apply camera offset
 
612
  color_fg: u32,
613
  color_bg: i32,
614
  ) {
615
+ let gfx = DDDisplay::as_gfx(this);
616
  let cx = x + (*gfx).camera_x;
617
  let cy = y + (*gfx).camera_y;
618
 
 
650
  color_fg: u32,
651
  color_bg: u32,
652
  ) {
653
+ let gfx = DDDisplay::as_gfx(this);
654
  let cx = x + (*gfx).camera_x;
655
  let cy = y + (*gfx).camera_y;
656
 
 
726
  color1: u32,
727
  color2: u32,
728
  ) {
729
+ let gfx = DDDisplay::as_gfx(this);
730
  let cam_x = Fixed::from_int((*gfx).camera_x);
731
  let cam_y = Fixed::from_int((*gfx).camera_y);
732
 
 
759
  y2: Fixed,
760
  color: u32,
761
  ) {
762
+ let gfx = DDDisplay::as_gfx(this);
763
  let cam_x = Fixed::from_int((*gfx).camera_x);
764
  let cam_y = Fixed::from_int((*gfx).camera_y);
765
 
 
790
  count: i32,
791
  color: u32,
792
  ) {
793
+ let gfx = DDDisplay::as_gfx(this);
794
  let cam_x = Fixed::from_int((*gfx).camera_x);
795
  let cam_y = Fixed::from_int((*gfx).camera_y);
796
 
 
827
  if id_u.wrapping_sub(1) >= 0x3FF {
828
  return 0;
829
  }
830
+ let gfx = DDDisplay::as_gfx(this);
831
  let base = &(*gfx).base;
832
  if base.sprite_array_1[id as usize] != 0
833
  || base.sprite_array_2[id as usize] != 0
 
856
  p5: u32,
857
  p6: u32,
858
  ) {
859
+ let gfx = DDDisplay::as_gfx(this);
860
  acquire_render_lock(gfx);
861
 
862
  let pixel_x = (Fixed::from_int((*gfx).camera_x) + x).to_int();
 
907
  src_h: i32,
908
  flags: u32,
909
  ) -> DrawScaledSpriteResult {
910
+ let gfx = DDDisplay::as_gfx(this);
911
 
912
  let width = src_w - src_x;
913
  let height = src_h - src_y;
 
1033
  count: i32,
1034
  color: u32,
1035
  ) {
1036
+ let gfx = DDDisplay::as_gfx(this);
1037
  let mut cx = Fixed::from_int((*gfx).camera_x) + x;
1038
  let mut cy = Fixed::from_int((*gfx).camera_y) + y;
1039
 
 
1058
  /// # Safety
1059
  /// `this` must be a valid `*mut DDDisplay` (actually a `*mut DisplayGfx`).
1060
  pub unsafe extern "thiscall" fn set_active_layer(this: *mut DDDisplay, layer: i32) -> *mut u8 {
1061
+ let gfx = DDDisplay::as_gfx(this);
1062
  if (layer as u32).wrapping_sub(1) < 3 {
1063
  (*gfx).base.layer_contexts[layer as usize] as *mut u8
1064
  } else {
 
1084
  palette_ctx: *mut PaletteContext,
1085
  commit: i32,
1086
  ) {
1087
+ let gfx = DDDisplay::as_gfx(this);
1088
  let ctx = &mut *palette_ctx;
1089
 
1090
  // Reset iteration counter
 
1183
  layer: i32,
1184
  visible: i32,
1185
  ) {
1186
+ let gfx = DDDisplay::as_gfx(this);
1187
 
1188
  let layer_ctx = set_active_layer(this, layer) as *mut PaletteContext;
1189
  if !layer_ctx.is_null() {