File size: 43,731 Bytes
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6be3052
c8f62aa
07389f9
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6be3052
 
 
 
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
59bfee7
 
 
 
 
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
59bfee7
c8f62aa
 
07389f9
c8f62aa
07389f9
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
07389f9
c8f62aa
07389f9
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07389f9
c8f62aa
07389f9
c8f62aa
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfee7
c8f62aa
 
6be3052
 
 
 
 
 
 
 
 
 
 
07389f9
6be3052
 
 
 
 
 
07389f9
6be3052
07389f9
6be3052
 
12a0785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6be3052
 
 
c8f62aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
//! `RenderDrawingQueue` (0x542350) — the per-frame render-queue dispatcher.
//!
//! This is the bridge between the producer-side `RenderQueue` (a buffer of
//! enqueued draw commands) and the consumer-side `DisplayGfx` vtable. Once
//! per frame, `GameRender_Maybe` (0x533DC0) sets up a [`ClipContext`] and
//! invokes this function with `(EAX = render_queue, stack[0] = display_gfx,
//! stack[1] = clip_ctx)`. The dispatcher then:
//!
//! 1. Sorts the entry-pointer array by each command's `layer` field
//!    (ascending; using a non-stable sort to mirror MSVC `qsort`).
//! 2. Walks the sorted list from highest index downward (so high-layer
//!    commands draw first, low-layer commands draw on top).
//! 3. Switches on `cmd[0]` (command type, 0..0xE) and dispatches to the
//!    appropriate `DisplayGfx` vtable slot, passing through one of the
//!    `RQ_*` clip / translate helpers as needed.
//!
//! The 14 command types map to slots 11..22 of [`DisplayGfxVtable`]:
//!
//! | type | producer       | slot | DisplayGfx method      |
//! |------|----------------|------|------------------------|
//! | 0    | DRAW_RECT      | 18   | `fill_rect`            |
//! | 1    | DRAW_BITMAP_GLOBAL | 20 | `draw_scaled_sprite` |
//! | 2    | DRAW_TEXTBOX_LOCAL | 20 | `draw_scaled_sprite` |
//! | 3    | (no Rust producer) | 21 | `draw_via_callback` |
//! | 4    | DRAW_SPRITE_GLOBAL | 19 | `blit_sprite`        |
//! | 5    | DRAW_SPRITE_LOCAL  | 19 | `blit_sprite`        |
//! | 6    | DRAW_SPRITE_OFFSET | 19 | `blit_sprite`        |
//! | 7    | (no Rust producer) | 12 | `draw_polyline`      |
//! | 8    | DRAW_LINE_STRIP    | 14 | `draw_line_clipped`  |
//! | 9    | DRAW_POLYGON       | 13 | `draw_line`          |
//! | 0xA  | (no Rust producer) | 15 | `draw_pixel_strip`   |
//! | 0xB  | DRAW_CROSSHAIR     | 16 | `draw_crosshair`     |
//! | 0xC  | DRAW_OUTLINED_PIXEL | 17 | `draw_outlined_pixel` |
//! | 0xD  | DRAW_TILED_BITMAP  | 11 | `draw_tiled_bitmap`  |
//! | 0xE  | (no Rust producer) | 22 | `draw_tiled_terrain` |
//!
//! ## Calling convention
//!
//! `RenderDrawingQueue` is `__usercall(EAX = *mut RenderQueue,
//! stack[0] = *mut DisplayGfx, stack[1] = *mut ClipContext)`, returning
//! with `RET 0x8`. Verified from caller `GameRender_Maybe` at 0x5340af.
//!
//! ## Helper calling conventions (verified from disassembly)
//!
//! - `RQ_ClipCoordinates` (0x542BA0) is `__thiscall(ClipContext*)` taking
//!   `(x_in, y_in, ref_z, *out_x, *out_y, *out_scale)`.
//! - `RQ_ClipCoordinatesWithRef` (0x542C70) has the same shape but uses
//!   the pivot fields `clip[2]/clip[3]`.
//! - `RQ_TranslateCoordinates` (0x542B10) is `__usercall(ECX = x_in,
//!   EAX = y_in, EDI = ClipContext*, stack[0] = *out_x, stack[1] = *out_y)`.
//!   The Ghidra decompile lost the ECX input — verified from disasm at
//!   0x542B1A (`AND ECX, 0xFFFF0000`). Same shape as `RQ_ClipCoordinates`
//!   but without the perspective scale.

use crate::fixed::Fixed;
use crate::render::display::gfx::DisplayGfx;
use crate::render::message::{RenderMessage, TypedRenderCmd, COMMAND_TYPE_TYPED};
use crate::render::queue::RenderQueue;
use crate::render::sprite::sprite_op::SpriteOp;

// =============================================================================
// ClipContext — the 4-i32 anchor/pivot block GameRender_Maybe builds on stack
// =============================================================================

/// Per-frame clip / camera context, built on the stack by
/// `GameRender_Maybe` (0x533DC0) and passed as the second stack arg to
/// [`render_drawing_queue`].
///
/// The first two fields are the camera anchor in fixed-16.16 coordinates;
/// the second pair are pivot points for `RQ_ClipCoordinatesWithRef` (case 6
/// with `flags & 4`). All four are sourced from `DisplayGfx + 0x8CEC..0x8CFC`
/// at the start of `GameRender_Maybe` and adjusted in place by the
/// perspective-zoom math before being handed to the dispatcher.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct ClipContext {
    /// Camera anchor X in Fixed16. Subtracted from each command's source
    /// X by the clip helpers; only the upper 16 bits matter.
    pub cam_x: Fixed,
    /// Camera anchor Y in Fixed16.
    pub cam_y: Fixed,
    /// Pivot X for the perspective-with-pivot path
    /// (`RQ_ClipCoordinatesWithRef`, case 6 with `cmd.flags & 4`).
    /// Stored as Fixed16 with the fractional bits guaranteed zero by
    /// `GameRender_Maybe`'s setup math.
    pub pivot_x: Fixed,
    /// Pivot Y — same as `pivot_x`.
    pub pivot_y: Fixed,
}

const _: () = assert!(core::mem::size_of::<ClipContext>() == 0x10);

// =============================================================================
// Pixel-aligned subtract with overflow saturation
// =============================================================================

/// Pixel-aligned subtract with overflow saturation (the inner loop body
/// shared by `RQ_ClipCoordinates` and `RQ_TranslateCoordinates`).
///
/// Both inputs are masked to their upper 16 bits via [`Fixed::floor`]
/// (truncated to pixel resolution while staying in fixed-16.16
/// representation). Returns `in_val - cam` clamped to `Fixed(i32::MIN)`
/// / `Fixed(i32::MAX)` on either-direction overflow.
///
/// `FixedDiv16_16` (0x5B3501), incidentally also used by these helpers,
/// is the same operation as [`Fixed::div`] (the `Div` impl on `Fixed`):
/// `(num << 16) / denom` with a 64-bit signed intermediate. We don't
/// need a standalone `fixed_div_16_16` helper.
#[inline]
fn clip_sub_saturate(in_val: Fixed, cam: Fixed) -> Fixed {
    let in_val = in_val.floor();
    let cam = cam.floor();
    let delta = Fixed(in_val.0.wrapping_sub(cam.0));
    if in_val.0 < 0 {
        // in_val is negative; if cam is positive and delta wrapped positive,
        // it's an underflow → saturate to MIN.
        if cam.0 > 0 && delta.0 > 0 {
            return Fixed(i32::MIN);
        }
    } else {
        // in_val is non-negative; if cam is negative and delta wrapped
        // negative, it's an overflow → saturate to MAX.
        if cam.0 < 0 && delta.0 < 0 {
            return Fixed(i32::MAX);
        }
    }
    delta
}

/// Perspective scale `0x100_0000 / projected_z` as a Fixed16 ratio.
/// Pulled out so the same expression doesn't have to be re-derived in
/// each clip helper.
#[inline]
fn perspective_scale(projected_z: i32) -> Fixed {
    // Equivalent to the WA `FixedDiv16_16` call: `(0x100_0000 << 16) / projected_z`.
    Fixed::from_raw(0x0100_0000) / Fixed::from_raw(projected_z)
}

// =============================================================================
// RQ_ClipCoordinates (0x542BA0) — perspective-scaled clip
// =============================================================================

/// `RQ_ClipCoordinates` (0x542BA0) — perspective clip with no pivot.
///
/// Returns `false` if `ref_z + 0x100_0000 < 1` (projection plane behind
/// the eye); otherwise writes `(out_x, out_y, out_scale)` and returns true.
///
/// Calling convention in WA: `__thiscall(ClipContext*)`. The Rust port
/// takes the context by reference instead.
pub fn rq_clip_coordinates(
    clip: &ClipContext,
    x_in: Fixed,
    y_in: Fixed,
    ref_z: i32,
    out_x: &mut Fixed,
    out_y: &mut Fixed,
    out_scale: &mut Fixed,
) -> bool {
    let delta_x = clip_sub_saturate(x_in, clip.cam_x);
    let delta_y = clip_sub_saturate(y_in, clip.cam_y);

    let projected_z = ref_z.wrapping_add(0x0100_0000);
    if projected_z < 1 {
        return false;
    }
    let scale = perspective_scale(projected_z);
    *out_scale = scale;
    *out_x = delta_x.mul_raw(scale);
    *out_y = delta_y.mul_raw(scale);
    true
}

// =============================================================================
// RQ_ClipCoordinatesWithRef (0x542C70) — perspective with pivot
// =============================================================================

/// `RQ_ClipCoordinatesWithRef` (0x542C70) — perspective clip relative to
/// a pivot point (read from `clip.pivot_x/pivot_y`).
///
/// The `(cam − pivot)` delta is scaled by the perspective factor and added
/// back to `pivot`, producing the projected camera position. The input
/// `(x_in, y_in)` is then subtracted from that projected position with
/// saturation.
///
/// Used by case 6 (`DRAW_SPRITE_OFFSET`) when `flags & 4` is set.
pub fn rq_clip_coordinates_with_ref(
    clip: &ClipContext,
    x_in: Fixed,
    y_in: Fixed,
    ref_z: i32,
    out_x: &mut Fixed,
    out_y: &mut Fixed,
    out_scale: &mut Fixed,
) -> bool {
    let projected_z = ref_z.wrapping_add(0x0100_0000);
    if projected_z < 1 {
        return false;
    }
    let scale = perspective_scale(projected_z);
    *out_scale = scale;

    // Project camera position relative to pivot, then subtract from input.
    let cam_x_pixel = clip.cam_x.floor();
    let cam_y_pixel = clip.cam_y.floor();

    let projected_cam_x = (cam_x_pixel - clip.pivot_x).mul_raw(scale) + clip.pivot_x;
    let projected_cam_y = (cam_y_pixel - clip.pivot_y).mul_raw(scale) + clip.pivot_y;

    *out_x = sub_saturate(x_in, projected_cam_x);
    *out_y = sub_saturate(y_in, projected_cam_y);
    true
}

/// Saturating signed subtract on `Fixed` values. Used by
/// `rq_clip_coordinates_with_ref`'s outer subtract, which operates on
/// already-projected positions where the inputs are no longer guaranteed
/// pixel-aligned, so it does NOT pre-floor like [`clip_sub_saturate`].
#[inline]
fn sub_saturate(in_val: Fixed, cam: Fixed) -> Fixed {
    let delta = Fixed(in_val.0.wrapping_sub(cam.0));
    if in_val.0 < 0 {
        if cam.0 > 0 && delta.0 > 0 {
            return Fixed(i32::MIN);
        }
    } else {
        if cam.0 < 0 && delta.0 < 0 {
            return Fixed(i32::MAX);
        }
    }
    delta
}

// =============================================================================
// RQ_TranslateCoordinates (0x542B10) — clip with no perspective
// =============================================================================

/// `RQ_TranslateCoordinates` (0x542B10) — same shape as
/// `rq_clip_coordinates` but without the perspective scaling. Always
/// succeeds.
///
/// In WA this is `__usercall(ECX = x_in, EAX = y_in, EDI = ClipContext*,
/// stack[0] = *out_x, stack[1] = *out_y)`. The Ghidra decompile lost the
/// ECX input (annotated `unaff_EDI` for the context and `in_EAX` for `y`,
/// silently dropping `x`); verified from the prologue disassembly at
/// 0x542B1A which masks ECX with `0xFFFF_0000`.
pub fn rq_translate_coordinates(
    clip: &ClipContext,
    x_in: Fixed,
    y_in: Fixed,
    out_x: &mut Fixed,
    out_y: &mut Fixed,
) {
    *out_x = clip_sub_saturate(x_in, clip.cam_x);
    *out_y = clip_sub_saturate(y_in, clip.cam_y);
}

// =============================================================================
// RenderDrawingQueue (0x542350) — the dispatcher
// =============================================================================

/// `RenderDrawingQueue` (0x542350) — the per-frame render-queue dispatcher.
///
/// See module-level docs for the per-case slot mapping and calling
/// convention. The Rust signature here flattens the WA usercall into a
/// plain function: a thin trampoline in `openwa-dll` captures the EAX
/// register input and forwards the two stack args.
///
/// # Safety
///
/// All three pointers must be valid for the duration of the call. The
/// render queue's `entry_count` must accurately reflect the number of
/// `entry_ptrs` slots in use, and each entry pointer must point at a
/// valid command structure whose first `u32` is a command type in `0..=0xE`
/// (commands with type `> 0xE` are silently ignored, matching WA — the
/// `JA 0x542a9e` at 0x54239f branches into the loop tail).
pub unsafe fn render_drawing_queue(
    rq: *mut RenderQueue,
    display: *mut DisplayGfx,
    clip: *mut ClipContext,
) {
    let count = (*rq).entry_count as usize;
    if count == 0 {
        return;
    }
    let entry_ptrs_base = core::ptr::addr_of_mut!((*rq).entry_ptrs) as *mut *mut u8;
    let entries: &mut [*mut u8] = core::slice::from_raw_parts_mut(entry_ptrs_base, count);

    // Sort by cmd[1] = layer field. The original uses MSVC qsort (unstable);
    // we mirror that with sort_unstable_by_key.
    entries.sort_unstable_by_key(|&p| {
        // SAFETY: entries are valid command pointers; cmd[1] is in range.
        unsafe { *(p as *const i32).add(1) }
    });

    let clip_ref: &ClipContext = &*clip;

    // Walk from the highest index downward.
    for i in (0..count).rev() {
        let cmd = entries[i] as *const u32;
        let cmd_type = *cmd;
        match cmd_type {
            0 => dispatch_case_0_fill_rect(display, clip_ref, cmd),
            1 => dispatch_case_1_bitmap_global(display, cmd),
            2 => dispatch_case_2_textbox_local(display, clip_ref, cmd),
            3 => dispatch_case_3_via_callback(display, clip_ref, cmd),
            4 => dispatch_case_4_sprite_global(display, cmd),
            5 => dispatch_case_5_sprite_local(display, clip_ref, cmd),
            6 => dispatch_case_6_sprite_offset(display, clip_ref, cmd),
            7 => dispatch_case_7_polyline(display, cmd),
            8 => dispatch_case_8_line_strip(display, clip_ref, cmd),
            9 => dispatch_case_9_polygon(display, clip_ref, cmd),
            0xA => dispatch_case_a_pixel_strip(display, clip_ref, cmd),
            0xB => dispatch_case_b_crosshair(display, clip_ref, cmd),
            0xC => dispatch_case_c_outlined_pixel(display, clip_ref, cmd),
            0xD => dispatch_case_d_tiled_bitmap(display, clip_ref, cmd),
            0xE => dispatch_case_e_tiled_terrain(display, clip_ref, cmd),
            COMMAND_TYPE_TYPED => {
                let typed = &(*(cmd as *const TypedRenderCmd)).message;
                dispatch_typed(display, clip_ref, typed);
            }
            _ => { /* unknown command type — silently skipped, matching WA */ }
        }
    }
}

// =============================================================================
// Per-case dispatchers
// =============================================================================
//
// Each case body mirrors the corresponding `case N:` arm of WA's
// `RenderDrawingQueue` (0x542350). Field offsets are the `puVar1[N]`
// indices from the decompile (each step = 4 bytes).
//
// Vtable dispatch goes through the auto-generated `DisplayGfx::*_raw`
// methods (from `bind_DisplayGfxVtable!(DisplayGfx, base.vtable)`),
// which take a raw `*mut DisplayGfx` to avoid creating `&mut self` —
// see the noalias rule in CLAUDE.md.

#[inline]
unsafe fn read_field(cmd: *const u32, idx: usize) -> i32 {
    *(cmd.add(idx) as *const i32)
}

#[inline]
unsafe fn read_fixed(cmd: *const u32, idx: usize) -> Fixed {
    Fixed::from_raw(*(cmd.add(idx) as *const i32))
}

// ---- case 0: DRAW_RECT → slot 18 (fill_rect) -------------------------------
//
//     iVar4 = RQ_ClipCoordinates(this, cmd[3], cmd[4], cmd[7], &local_18, &param_1, ..);
//     if !iVar4 break;
//     iVar4 = RQ_ClipCoordinates(this, cmd[5], cmd[6], cmd[7], &local_10, &local_c, ..);
//     if !iVar4 break;
//     // x/y MIN-corner clamps to keep pixel-perfect rect edges
//     vtable[18](this, x1>>16, y1>>16, x2>>16, y2>>16, cmd[2]);
unsafe fn dispatch_case_0_fill_rect(display: *mut DisplayGfx, clip: &ClipContext, cmd: *const u32) {
    let mut x1 = Fixed::ZERO;
    let mut y1 = Fixed::ZERO;
    let mut x2 = Fixed::ZERO;
    let mut y2 = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if !rq_clip_coordinates(
        clip,
        read_fixed(cmd, 3),
        read_fixed(cmd, 4),
        read_field(cmd, 7),
        &mut x1,
        &mut y1,
        &mut scale,
    ) {
        return;
    }
    if !rq_clip_coordinates(
        clip,
        read_fixed(cmd, 5),
        read_fixed(cmd, 6),
        read_field(cmd, 7),
        &mut x2,
        &mut y2,
        &mut scale,
    ) {
        return;
    }

    // Pixel-perfect MIN/MAX rect-edge clamping when the source is at the
    // sentinel anchor values: cmd[3]/cmd[4] == -0x80000000 → clamp to MIN,
    // cmd[5]/cmd[6] == 0x7FFF0000 → clamp to MAX. WA uses these to draw
    // "infinite" rectangles in screen space.
    if read_field(cmd, 3) == i32::MIN {
        x1 = Fixed(i32::MIN);
    }
    if read_field(cmd, 4) == i32::MIN {
        y1 = Fixed(i32::MIN);
    }
    if read_field(cmd, 5) == 0x7FFF_0000 {
        x2 = Fixed(i32::MAX);
    }
    if read_field(cmd, 6) == 0x7FFF_0000 {
        y2 = Fixed(i32::MAX);
    }

    let color = *(cmd.add(2));
    DisplayGfx::fill_rect_raw(
        display,
        x1.to_int(),
        y1.to_int(),
        x2.to_int(),
        y2.to_int(),
        color,
    );
}

// ---- case 1: DRAW_BITMAP_GLOBAL → slot 20 (draw_scaled_sprite) -------------
//
//     vtable[20](this, cmd[2], cmd[3], cmd[4], cmd[5], cmd[6], cmd[7], cmd[8], cmd[9]);
//
// Pure passthrough — global bitmaps draw at the supplied world coords.
unsafe fn dispatch_case_1_bitmap_global(display: *mut DisplayGfx, cmd: *const u32) {
    use crate::bitgrid::DisplayBitGrid;
    let x = read_fixed(cmd, 2);
    let y = read_fixed(cmd, 3);
    let sprite = read_field(cmd, 4) as *mut DisplayBitGrid;
    let src_x = read_field(cmd, 5);
    let src_y = read_field(cmd, 6);
    let src_w = read_field(cmd, 7);
    let src_h = read_field(cmd, 8);
    let flags = *(cmd.add(9));
    DisplayGfx::draw_scaled_sprite_raw(display, x, y, sprite, src_x, src_y, src_w, src_h, flags);
}

// ---- case 2: DRAW_TEXTBOX_LOCAL → slot 20 (draw_scaled_sprite) -------------
//
// `cmd[2] == 0` selects the simple variant (one clip call); otherwise the
// box has separate top-edge / bottom-edge clip targets and a flag byte at
// `cmd+8` controlling whether the result clamps to the second clip pair.
unsafe fn dispatch_case_2_textbox_local(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    use crate::bitgrid::DisplayBitGrid;
    let mode = *(cmd.add(2));
    let mut x1 = Fixed::ZERO;
    let mut y1 = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if mode == 0 {
        if !rq_clip_coordinates(
            clip,
            read_fixed(cmd, 3),
            read_fixed(cmd, 4),
            read_field(cmd, 5),
            &mut x1,
            &mut y1,
            &mut scale,
        ) {
            return;
        }
    } else {
        if !rq_clip_coordinates(
            clip,
            read_fixed(cmd, 3),
            read_fixed(cmd, 4),
            read_field(cmd, 5),
            &mut x1,
            &mut y1,
            &mut scale,
        ) {
            return;
        }
        let mut x2 = Fixed::ZERO;
        let mut y2 = Fixed::ZERO;
        if !rq_clip_coordinates(
            clip,
            read_fixed(cmd, 3),
            read_fixed(cmd, 4),
            read_field(cmd, 6),
            &mut x2,
            &mut y2,
            &mut scale,
        ) {
            return;
        }
        let flag_byte = *(cmd.add(2)) as u8;
        if flag_byte & 1 != 0 {
            x1 = x2;
        }
        if flag_byte & 2 != 0 {
            y1 = y2;
        }
    }
    let sprite = read_field(cmd, 7) as *mut DisplayBitGrid;
    let src_x = read_field(cmd, 8);
    let src_y = read_field(cmd, 9);
    let src_w = read_field(cmd, 10);
    let src_h = read_field(cmd, 11);
    let flags = *(cmd.add(12));
    DisplayGfx::draw_scaled_sprite_raw(display, x1, y1, sprite, src_x, src_y, src_w, src_h, flags);
}

// ---- case 3 → slot 21 (draw_via_callback) ----------------------------------
//
//     iVar4 = RQ_ClipCoordinates(this, cmd[2], cmd[3], cmd[4], &x, &y, ..);
//     if !iVar4 break;
//     vtable[21](this, x, y, cmd[6], cmd[7], cmd[8]);
unsafe fn dispatch_case_3_via_callback(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    let mut x = Fixed::ZERO;
    let mut y = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if !rq_clip_coordinates(
        clip,
        read_fixed(cmd, 2),
        read_fixed(cmd, 3),
        read_field(cmd, 4),
        &mut x,
        &mut y,
        &mut scale,
    ) {
        return;
    }
    let obj = read_field(cmd, 6) as *mut u8;
    let p5 = *(cmd.add(7));
    let p6 = *(cmd.add(8));
    DisplayGfx::draw_via_callback_raw(display, x, y, obj, p5, p6);
}

// ---- case 4: DRAW_SPRITE_GLOBAL → slot 19 (blit_sprite) --------------------
//
//     vtable[19](this, cmd[2], cmd[3], cmd[4], cmd[5]);
unsafe fn dispatch_case_4_sprite_global(display: *mut DisplayGfx, cmd: *const u32) {
    let x = read_fixed(cmd, 2);
    let y = read_fixed(cmd, 3);
    let sprite = SpriteOp(*(cmd.add(4)));
    let palette = *(cmd.add(5));
    DisplayGfx::blit_sprite_raw(display, x, y, sprite, palette);
}

// ---- case 5: DRAW_SPRITE_LOCAL → slot 19 (blit_sprite) ---------------------
//
//     RQ_TranslateCoordinates(clip, cmd[2], cmd[3], &x, &y);
//     vtable[19](this, x, y, cmd[4], cmd[5]);
unsafe fn dispatch_case_5_sprite_local(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    let mut x = Fixed::ZERO;
    let mut y = Fixed::ZERO;
    rq_translate_coordinates(clip, read_fixed(cmd, 2), read_fixed(cmd, 3), &mut x, &mut y);
    let sprite = SpriteOp(*(cmd.add(4)));
    let palette = *(cmd.add(5));
    DisplayGfx::blit_sprite_raw(display, x, y, sprite, palette);
}

// ---- case 6: DRAW_SPRITE_OFFSET → slot 19 (blit_sprite) --------------------
//
// `cmd[2]` is a flag byte: bit 0/1 are top/bottom-edge clamps, bit 2 selects
// the perspective-with-pivot helper instead of the plain perspective clip.
// `cmd[2] == 0` selects the simple translate-only path used for purely
// screen-space offset sprites.
unsafe fn dispatch_case_6_sprite_offset(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    let flags = *(cmd.add(2));

    let mut x = Fixed::ZERO;
    let mut y = Fixed::ZERO;
    let mut scale = Fixed::ZERO;

    if flags == 0 {
        // Simple translate path (no perspective).
        rq_translate_coordinates(clip, read_fixed(cmd, 3), read_fixed(cmd, 4), &mut x, &mut y);
    } else {
        // Two clip calls (top + bottom Y) with optional MIN/MAX clamps.
        let use_pivot = (flags & 4) != 0;
        let clip_fn: fn(
            &ClipContext,
            Fixed,
            Fixed,
            i32,
            &mut Fixed,
            &mut Fixed,
            &mut Fixed,
        ) -> bool = if use_pivot {
            rq_clip_coordinates_with_ref
        } else {
            rq_clip_coordinates
        };
        if !clip_fn(
            clip,
            read_fixed(cmd, 3),
            read_fixed(cmd, 4),
            read_field(cmd, 5),
            &mut x,
            &mut y,
            &mut scale,
        ) {
            return;
        }
        let mut x2 = Fixed::ZERO;
        let mut y2 = Fixed::ZERO;
        if !clip_fn(
            clip,
            read_fixed(cmd, 3),
            read_fixed(cmd, 4),
            read_field(cmd, 6),
            &mut x2,
            &mut y2,
            &mut scale,
        ) {
            return;
        }
        let flag_byte = *(cmd.add(2)) as u8;
        if flag_byte & 1 != 0 {
            x = x2;
        }
        if flag_byte & 2 != 0 {
            y = y2;
        }
    }

    let sprite = SpriteOp(*(cmd.add(7)));
    let palette = *(cmd.add(8));
    DisplayGfx::blit_sprite_raw(display, x, y, sprite, palette);
}

// ---- case 7 → slot 12 (draw_polyline) --------------------------------------
//
//     vtable[12](this, &cmd[4], cmd[2], cmd[3]);
//
// `cmd[2]` is the vertex count, `cmd[3]` is the color, vertex data
// follows starting at `cmd[4]`.
unsafe fn dispatch_case_7_polyline(display: *mut DisplayGfx, cmd: *const u32) {
    let points = cmd.add(4) as *mut i32;
    let count = read_field(cmd, 2);
    let color = *(cmd.add(3));
    DisplayGfx::draw_polyline_raw(display, points, count, color);
}

// ---- case 8: DRAW_LINE_STRIP → slot 14 (draw_line_clipped) -----------------
//
// First vertex at `cmd[4..6]` plus `cmd[6]` (z), then iterate
// `(count - 1)` more vertices and call slot 14 for each segment.
// `cmd[3]` is the color shared by all segments.
unsafe fn dispatch_case_8_line_strip(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    let mut x1 = Fixed::ZERO;
    let mut y1 = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if !rq_clip_coordinates(
        clip,
        read_fixed(cmd, 4),
        read_fixed(cmd, 5),
        read_field(cmd, 6),
        &mut x1,
        &mut y1,
        &mut scale,
    ) {
        return;
    }
    let count = read_field(cmd, 2);
    let color = *(cmd.add(3));
    if count <= 1 {
        return;
    }
    // Vertex N is at cmd[8 + (N-1)*3 .. 8 + (N-1)*3 + 3], a 3-tuple of (x, y, z).
    // Walk N = 1..count.
    let mut vert = cmd.add(8) as *const u32;
    for _ in 1..count {
        let mut x2 = Fixed::ZERO;
        let mut y2 = Fixed::ZERO;
        if !rq_clip_coordinates(
            clip,
            Fixed::from_raw(*(vert.sub(1)) as i32),
            Fixed::from_raw(*vert as i32),
            *(vert.add(1)) as i32,
            &mut x2,
            &mut y2,
            &mut scale,
        ) {
            break;
        }
        DisplayGfx::draw_line_clipped_raw(display, x1, y1, x2, y2, color);
        x1 = x2;
        y1 = y2;
        vert = vert.add(3);
    }
}

// ---- case 9: DRAW_POLYGON → slot 13 (draw_line) ----------------------------
//
// Same shape as case 8 but with two color params (cmd[3], cmd[4]) and the
// first vertex starts at `cmd[5..8]`. Per-segment vertex stride is 3 u32s.
unsafe fn dispatch_case_9_polygon(display: *mut DisplayGfx, clip: &ClipContext, cmd: *const u32) {
    let mut x1 = Fixed::ZERO;
    let mut y1 = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if !rq_clip_coordinates(
        clip,
        read_fixed(cmd, 5),
        read_fixed(cmd, 6),
        read_field(cmd, 7),
        &mut x1,
        &mut y1,
        &mut scale,
    ) {
        return;
    }
    let count = read_field(cmd, 2);
    let color1 = *(cmd.add(3));
    let color2 = *(cmd.add(4));
    if count <= 1 {
        return;
    }
    let mut vert = cmd.add(9) as *const u32;
    for _ in 1..count {
        let mut x2 = Fixed::ZERO;
        let mut y2 = Fixed::ZERO;
        if !rq_clip_coordinates(
            clip,
            Fixed::from_raw(*(vert.sub(1)) as i32),
            Fixed::from_raw(*vert as i32),
            *(vert.add(1)) as i32,
            &mut x2,
            &mut y2,
            &mut scale,
        ) {
            break;
        }
        DisplayGfx::draw_line_raw(display, x1, y1, x2, y2, color1, color2);
        x1 = x2;
        y1 = y2;
        vert = vert.add(3);
    }
}

// ---- case 0xA → slot 15 (draw_pixel_strip) ---------------------------------
//
//     RQ_TranslateCoordinates(clip, cmd[2], cmd[3], &x, &y);
//     // The translate's `clip_sub_saturate` floor()s its inputs, so its
//     // output drops the fractional 16 bits of cmd[2]/cmd[3]. The dispatcher
//     // re-adds those fractional bits back to recover pixel-fractional precision:
//     x += cmd[2] & 0xFFFF;
//     y += cmd[3] & 0xFFFF;
//     vtable[15](this, x, y, cmd[4], cmd[5], cmd[6], cmd[7]);
unsafe fn dispatch_case_a_pixel_strip(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    let mut x = Fixed::ZERO;
    let mut y = Fixed::ZERO;
    rq_translate_coordinates(clip, read_fixed(cmd, 2), read_fixed(cmd, 3), &mut x, &mut y);
    let cmd_x = *(cmd.add(2));
    let cmd_y = *(cmd.add(3));
    x = x + Fixed::from_raw((cmd_x & 0xFFFF) as i32);
    y = y + Fixed::from_raw((cmd_y & 0xFFFF) as i32);
    let dx = read_fixed(cmd, 4);
    let dy = read_fixed(cmd, 5);
    let count = read_field(cmd, 6);
    let color = *(cmd.add(7));
    DisplayGfx::draw_pixel_strip_raw(display, x, y, dx, dy, count, color);
}

// ---- case 0xB: DRAW_CROSSHAIR → slot 16 (draw_crosshair) -------------------
//
//     iVar4 = RQ_ClipCoordinates(this, cmd[4], cmd[5], cmd[6], &x, &y, ..);
//     if !iVar4 break;
//     vtable[16](this, x>>16, y>>16, cmd[2], cmd[3]);
unsafe fn dispatch_case_b_crosshair(display: *mut DisplayGfx, clip: &ClipContext, cmd: *const u32) {
    let mut x = Fixed::ZERO;
    let mut y = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if !rq_clip_coordinates(
        clip,
        read_fixed(cmd, 4),
        read_fixed(cmd, 5),
        read_field(cmd, 6),
        &mut x,
        &mut y,
        &mut scale,
    ) {
        return;
    }
    let color_fg = *(cmd.add(2));
    let color_bg = *(cmd.add(3));
    DisplayGfx::draw_crosshair_raw(display, x.to_int(), y.to_int(), color_fg, color_bg);
}

// ---- case 0xC → slot 17 (draw_outlined_pixel) ------------------------------
//
//     iVar4 = RQ_ClipCoordinates(this, cmd[4], cmd[5], cmd[6], &x, &y, ..);
//     if !iVar4 break;
//     vtable[17](this, x>>16, y>>16, cmd[2], cmd[3]);
unsafe fn dispatch_case_c_outlined_pixel(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    let mut x = Fixed::ZERO;
    let mut y = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if !rq_clip_coordinates(
        clip,
        read_fixed(cmd, 4),
        read_fixed(cmd, 5),
        read_field(cmd, 6),
        &mut x,
        &mut y,
        &mut scale,
    ) {
        return;
    }
    let color_fg = *(cmd.add(2));
    let color_bg = read_field(cmd, 3);
    DisplayGfx::draw_outlined_pixel_raw(display, x.to_int(), y.to_int(), color_fg, color_bg);
}

// ---- case 0xD: DRAW_TILED_BITMAP → slot 11 (draw_tiled_bitmap) -------------
//
//     iVar4 = RQ_ClipCoordinates(this, 0, cmd[2], cmd[3], &x, &y, ..);
//     if !iVar4 break;
//     // Bit 0 of the flag byte at cmd[5] forces dest_x to 0 instead of x>>16.
//     vtable[11](this, (flag ? 0 : x>>16), y>>16, cmd[4]);
unsafe fn dispatch_case_d_tiled_bitmap(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    use crate::render::display::vtable::TiledBitmapSource;
    let mut x = Fixed::ZERO;
    let mut y = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if !rq_clip_coordinates(
        clip,
        Fixed::ZERO,
        read_fixed(cmd, 2),
        read_field(cmd, 3),
        &mut x,
        &mut y,
        &mut scale,
    ) {
        return;
    }
    let flag_byte = *(cmd.add(5)) as u8;
    let dest_x = if flag_byte != 0 { 0 } else { x.to_int() };
    let source = read_field(cmd, 4) as *const TiledBitmapSource;
    DisplayGfx::draw_tiled_bitmap_raw(display, dest_x, y.to_int(), source);
}

// ---- case 0xE → slot 22 (draw_tiled_terrain) -------------------------------
//
// Same shape as case 2: `cmd[2] == 0` selects the one-clip variant; non-zero
// runs the two-clip path with the same flag-byte clamps.
unsafe fn dispatch_case_e_tiled_terrain(
    display: *mut DisplayGfx,
    clip: &ClipContext,
    cmd: *const u32,
) {
    let mode = *(cmd.add(2));
    let mut x = Fixed::ZERO;
    let mut y = Fixed::ZERO;
    let mut scale = Fixed::ZERO;
    if mode == 0 {
        if !rq_clip_coordinates(
            clip,
            read_fixed(cmd, 3),
            read_fixed(cmd, 4),
            read_field(cmd, 5),
            &mut x,
            &mut y,
            &mut scale,
        ) {
            return;
        }
    } else {
        if !rq_clip_coordinates(
            clip,
            read_fixed(cmd, 3),
            read_fixed(cmd, 4),
            read_field(cmd, 5),
            &mut x,
            &mut y,
            &mut scale,
        ) {
            return;
        }
        let mut x2 = Fixed::ZERO;
        let mut y2 = Fixed::ZERO;
        if !rq_clip_coordinates(
            clip,
            read_fixed(cmd, 3),
            read_fixed(cmd, 4),
            read_field(cmd, 6),
            &mut x2,
            &mut y2,
            &mut scale,
        ) {
            return;
        }
        let flag_byte = *(cmd.add(2)) as u8;
        if flag_byte & 1 != 0 {
            x = x2;
        }
        if flag_byte & 2 != 0 {
            y = y2;
        }
    }
    let count = read_field(cmd, 7);
    let flags = *(cmd.add(8));
    DisplayGfx::draw_tiled_terrain_raw(display, x, y, count, flags);
}

// =============================================================================
// Typed message dispatcher
// =============================================================================

/// Dispatch a [`RenderMessage`] to the appropriate `DisplayGfx` vtable method.
unsafe fn dispatch_typed(display: *mut DisplayGfx, clip: &ClipContext, msg: &RenderMessage) {
    match *msg {
        RenderMessage::Sprite {
            local,
            x,
            y,
            sprite,
            palette,
        } => {
            if local {
                let mut out_x = Fixed::ZERO;
                let mut out_y = Fixed::ZERO;
                rq_translate_coordinates(clip, x, y, &mut out_x, &mut out_y);
                DisplayGfx::blit_sprite_raw(display, out_x, out_y, sprite, palette);
            } else {
                DisplayGfx::blit_sprite_raw(display, x, y, sprite, palette);
            }
        }

        RenderMessage::FillRect {
            color,
            x1,
            y1,
            x2,
            y2,
            ref_z,
        } => {
            let mut ox1 = Fixed::ZERO;
            let mut oy1 = Fixed::ZERO;
            let mut ox2 = Fixed::ZERO;
            let mut oy2 = Fixed::ZERO;
            let mut scale = Fixed::ZERO;
            if !rq_clip_coordinates(clip, x1, y1, ref_z, &mut ox1, &mut oy1, &mut scale) {
                return;
            }
            if !rq_clip_coordinates(clip, x2, y2, ref_z, &mut ox2, &mut oy2, &mut scale) {
                return;
            }
            // Sentinel clamps for "infinite" screen-space rectangles.
            if x1.0 == i32::MIN {
                ox1 = Fixed(i32::MIN);
            }
            if y1.0 == i32::MIN {
                oy1 = Fixed(i32::MIN);
            }
            if x2.0 == 0x7FFF_0000 {
                ox2 = Fixed(i32::MAX);
            }
            if y2.0 == 0x7FFF_0000 {
                oy2 = Fixed(i32::MAX);
            }
            DisplayGfx::fill_rect_raw(
                display,
                ox1.to_int(),
                oy1.to_int(),
                ox2.to_int(),
                oy2.to_int(),
                color,
            );
        }

        RenderMessage::Crosshair {
            color_fg,
            color_bg,
            x,
            y,
        } => {
            let mut ox = Fixed::ZERO;
            let mut oy = Fixed::ZERO;
            let mut scale = Fixed::ZERO;
            if !rq_clip_coordinates(clip, x, y, 0, &mut ox, &mut oy, &mut scale) {
                return;
            }
            DisplayGfx::draw_crosshair_raw(display, ox.to_int(), oy.to_int(), color_fg, color_bg);
        }

        RenderMessage::TiledBitmap {
            x,
            y,
            source,
            flags,
        } => {
            let mut ox = Fixed::ZERO;
            let mut oy = Fixed::ZERO;
            let mut scale = Fixed::ZERO;
            if !rq_clip_coordinates(clip, Fixed::ZERO, x, y.0, &mut ox, &mut oy, &mut scale) {
                return;
            }
            let dest_x = if flags & 1 != 0 { 0 } else { ox.to_int() };
            DisplayGfx::draw_tiled_bitmap_raw(display, dest_x, oy.to_int(), source);
        }

        RenderMessage::SpriteOffset {
            flags,
            x,
            y,
            ref_z_2,
            sprite,
            palette,
        } => {
            let mut ox = Fixed::ZERO;
            let mut oy = Fixed::ZERO;
            let mut scale = Fixed::ZERO;

            if flags == 0 {
                rq_translate_coordinates(clip, x, y, &mut ox, &mut oy);
            } else {
                let use_pivot = (flags & 4) != 0;
                let clip_fn: fn(
                    &ClipContext,
                    Fixed,
                    Fixed,
                    i32,
                    &mut Fixed,
                    &mut Fixed,
                    &mut Fixed,
                ) -> bool = if use_pivot {
                    rq_clip_coordinates_with_ref
                } else {
                    rq_clip_coordinates
                };
                // ref_z (first Z reference) is always 0.
                if !clip_fn(clip, x, y, 0, &mut ox, &mut oy, &mut scale) {
                    return;
                }
                let mut ox2 = Fixed::ZERO;
                let mut oy2 = Fixed::ZERO;
                if !clip_fn(clip, x, y, ref_z_2, &mut ox2, &mut oy2, &mut scale) {
                    return;
                }
                if flags & 1 != 0 {
                    ox = ox2;
                }
                if flags & 2 != 0 {
                    oy = oy2;
                }
            }

            DisplayGfx::blit_sprite_raw(display, ox, oy, sprite, palette);
        }

        RenderMessage::BitmapGlobal {
            x,
            y,
            bitmap,
            src_y,
            src_w,
            src_h,
            flags,
        } => {
            // src_x is always 0 in all known producers.
            DisplayGfx::draw_scaled_sprite_raw(
                display, x, y, bitmap, 0, src_y, src_w, src_h, flags,
            );
        }

        RenderMessage::TextboxLocal {
            x,
            y,
            bitmap,
            src_w,
            src_h,
            flags,
        } => {
            // mode is always 0, so simple one-clip path.
            // ref_z is always 0.
            let mut ox = Fixed::ZERO;
            let mut oy = Fixed::ZERO;
            let mut scale = Fixed::ZERO;
            if !rq_clip_coordinates(clip, x, y, 0, &mut ox, &mut oy, &mut scale) {
                return;
            }
            // src_x and src_y are always 0.
            DisplayGfx::draw_scaled_sprite_raw(display, ox, oy, bitmap, 0, 0, src_w, src_h, flags);
        }

        RenderMessage::LineStrip {
            count,
            color,
            vertices,
        } => {
            if count <= 1 {
                return;
            }
            let verts = core::slice::from_raw_parts(vertices, count as usize);
            let mut x1 = Fixed::ZERO;
            let mut y1 = Fixed::ZERO;
            let mut scale = Fixed::ZERO;
            if !rq_clip_coordinates(
                clip,
                Fixed::from_raw(verts[0][0]),
                Fixed::from_raw(verts[0][1]),
                verts[0][2],
                &mut x1,
                &mut y1,
                &mut scale,
            ) {
                return;
            }
            for v in &verts[1..] {
                let mut x2 = Fixed::ZERO;
                let mut y2 = Fixed::ZERO;
                if !rq_clip_coordinates(
                    clip,
                    Fixed::from_raw(v[0]),
                    Fixed::from_raw(v[1]),
                    v[2],
                    &mut x2,
                    &mut y2,
                    &mut scale,
                ) {
                    break;
                }
                DisplayGfx::draw_line_clipped_raw(display, x1, y1, x2, y2, color);
                x1 = x2;
                y1 = y2;
            }
        }

        RenderMessage::Polygon {
            count,
            color1,
            color2,
            vertices,
        } => {
            if count <= 1 {
                return;
            }
            let verts = core::slice::from_raw_parts(vertices, count as usize);
            let mut x1 = Fixed::ZERO;
            let mut y1 = Fixed::ZERO;
            let mut scale = Fixed::ZERO;
            if !rq_clip_coordinates(
                clip,
                Fixed::from_raw(verts[0][0]),
                Fixed::from_raw(verts[0][1]),
                verts[0][2],
                &mut x1,
                &mut y1,
                &mut scale,
            ) {
                return;
            }
            for v in &verts[1..] {
                let mut x2 = Fixed::ZERO;
                let mut y2 = Fixed::ZERO;
                if !rq_clip_coordinates(
                    clip,
                    Fixed::from_raw(v[0]),
                    Fixed::from_raw(v[1]),
                    v[2],
                    &mut x2,
                    &mut y2,
                    &mut scale,
                ) {
                    break;
                }
                DisplayGfx::draw_line_raw(display, x1, y1, x2, y2, color1, color2);
                x1 = x2;
                y1 = y2;
            }
        }
    }
}

// =============================================================================
// Unit tests for the helpers
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn zero_clip() -> ClipContext {
        ClipContext {
            cam_x: Fixed::ZERO,
            cam_y: Fixed::ZERO,
            pivot_x: Fixed::ZERO,
            pivot_y: Fixed::ZERO,
        }
    }

    #[test]
    fn perspective_scale_at_zero_z() {
        // 0x100_0000 / 0x100_0000 = 0x10000 (Fixed16 1.0)
        assert_eq!(perspective_scale(0x0100_0000), Fixed::ONE);
    }

    #[test]
    fn clip_sub_saturate_no_overflow() {
        // 5.0 - 2.0 = 3.0 in fixed16
        assert_eq!(
            clip_sub_saturate(Fixed::from_int(5), Fixed::from_int(2)),
            Fixed::from_int(3)
        );
    }

    #[test]
    fn clip_sub_saturate_underflow_clamps_to_min() {
        // x_in negative, cam positive, delta wraps positive → MIN
        assert_eq!(
            clip_sub_saturate(Fixed(i32::MIN), Fixed::from_raw(0x4000_0000)),
            Fixed(i32::MIN)
        );
    }

    #[test]
    fn clip_sub_saturate_overflow_clamps_to_max() {
        // x_in positive, cam negative, delta wraps negative → MAX
        assert_eq!(
            clip_sub_saturate(
                Fixed::from_raw(0x4000_0000),
                Fixed::from_raw(i32::MIN + 0x10000)
            ),
            Fixed(i32::MAX)
        );
    }

    #[test]
    fn clip_coordinates_at_z_zero_is_identity() {
        let clip = zero_clip();
        let mut ox = Fixed::ZERO;
        let mut oy = Fixed::ZERO;
        let mut scale = Fixed::ZERO;
        let ok = rq_clip_coordinates(
            &clip,
            Fixed::from_int(5),
            Fixed::from_int(7),
            0,
            &mut ox,
            &mut oy,
            &mut scale,
        );
        assert!(ok);
        // scale = 0x100_0000 / 0x100_0000 = 0x10000 (1.0 in fixed16)
        assert_eq!(scale, Fixed::ONE);
        assert_eq!(ox, Fixed::from_int(5));
        assert_eq!(oy, Fixed::from_int(7));
    }

    #[test]
    fn clip_coordinates_negative_z_returns_false() {
        let clip = zero_clip();
        let mut ox = Fixed::ZERO;
        let mut oy = Fixed::ZERO;
        let mut scale = Fixed::ZERO;
        let ok = rq_clip_coordinates(
            &clip,
            Fixed::from_int(5),
            Fixed::from_int(7),
            -0x100_0001,
            &mut ox,
            &mut oy,
            &mut scale,
        );
        assert!(!ok);
    }

    #[test]
    fn translate_coordinates_camera_offset() {
        let clip = ClipContext {
            cam_x: Fixed::from_int(10),
            cam_y: Fixed::from_int(20),
            pivot_x: Fixed::ZERO,
            pivot_y: Fixed::ZERO,
        };
        let mut ox = Fixed::ZERO;
        let mut oy = Fixed::ZERO;
        rq_translate_coordinates(
            &clip,
            Fixed::from_int(30),
            Fixed::from_int(50),
            &mut ox,
            &mut oy,
        );
        assert_eq!(ox, Fixed::from_int(20));
        assert_eq!(oy, Fixed::from_int(30));
    }
}