Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lunasorcery/80c79ad289a93dc3c273492c61872f4e to your computer and use it in GitHub Desktop.
Save lunasorcery/80c79ad289a93dc3c273492c61872f4e to your computer and use it in GitHub Desktop.
; partial disassembly from Drome Racers (GBA, 2003)
; fill a flat-colored 2D polygon that's fully inside screen bounds
_WW080006B4_jump_entry1_colored_inbounds_poly:
str r0, [sp, #4] ; back up our current renderstream pointer
; read three verts from the renderstream into (r1,r2,r3)
ldr r1, [r0, #12]
ldr r2, [r0, #16]
ldr r3, [r0, #20]
; sort (r1,r2,r3) into ascending order
XOR_SORT r1, r2
XOR_SORT r1, r3
XOR_SORT r2, r3
; load the division table pointer
ldr lr, _WW080006B0 ; side-note: constant loads can reference backwards in the rom??
; split out X and Y coordinates from (r1,r2,r3)
; Y coordinates come from the top 16 bits and get put into (r4,r5,r6)
; X coordinates come from the bottom 16 bits and get put into (r1,r2,r3)
mov r4, r1, lsr #16
mov r5, r2, lsr #16
mov r6, r3, lsr #16
eor r1, r1, r4, lsl #16
eor r2, r2, r5, lsl #16
eor r3, r3, r6, lsl #16
sub r7, r6, r5
ldr r7, [lr, r7, lsl #2]
sub r8, r3, r2
mov r8, r8, lsl #18
vasm_smull vasm_r8, vasm_r7, vasm_r8, vasm_r7
str r7, [sp, #8] ; BC per-scanline delta
and r8, r5, #7
rsb r8, r8, #8
mul r8, r7, r8
mov r8, r8, asr #3
add r8, r8, r2, lsl #13
str r8, [sp, #12] ; BC start x-coordinate
sub r7, r5, r4
ldr r7, [lr, r7, lsl #2]
sub r8, r2, r1
mov r8, r8, lsl #18
vasm_smull vasm_r8, vasm_r7, vasm_r8, vasm_r7
str r7, [sp, #16] ; AB per-scanline delta
and r10, r4, #7
rsb r10, r10, #8
mul r8, r7, r10
mov r8, r8, asr #3
add r8, r8, r1, lsl #13
str r8, [sp, #20] ; AB start x-coordinate
sub r9, r6, r4
ldr r9, [lr, r9, lsl #2]
sub r8, r3, r1
mov r8, r8, lsl #18
vasm_smull vasm_r8, vasm_r9, vasm_r8, vasm_r9
str r9, [sp, #24] ; AC per-scanline delta
mul r8, r9, r10
mov r8, r8, asr #3
add r8, r8, r1, lsl #13
str r8, [sp, #28] ; AC start x-coordinate
; compare per-scanline deltas for AB and AC
; to work out whether B is on the left or right
cmp r7, r9
mov r8, r5, asr #3 ; By
mov r9, r4, asr #3 ; Ay
rsb r7, r8, r6, asr #3 ; Cy - By => bottom segment height
str r7, [sp, #56] ; bottom segment height
; get pointer to start of first row
ldr r7, [sp, #60] ; pointer to start of screen
add r11, r7, r9, lsl #8 ; + Ay x 256
sub r11, r11, r9, lsl #4 ; - Ay x 16
ldrb r7, [r0, #11] ; read color index from renderstream
orr r7, r7, r7, lsl #8 ; duplicate color index from 8bits to 16bits
sub r0, r8, r9 ; By - Ay => top segment height
ble _WW08000830 ; branch based on compared deltas from above
; mid-corner is on right, so both halves of triangle share a left edge
; so we only set r1 (left edge start) and r2 (left edge delta) once
; A
; /'.
; / '.
; / '.
; +---------+B
; / .-'
; / .-'
; / .-'
; / .-'
; /-'
;C
ldr r1, [sp, #28] ; AC start x-coordinate
ldr r2, [sp, #24] ; AC per-scanline delta
ldr r3, [sp, #20] ; AB start x-coordinate
ldr r4, [sp, #16] ; AB per-scanline delta
cmp r0, #0 ; check height of the top segment, if it's more than zero pixels..
blgt _WW08000898_fill_colored_trapezoid ; ...draw the top segment
ldr r3, [sp, #12] ; BC start x-coordinate
ldr r4, [sp, #8] ; BC per-scanline delta
ldr r0, [sp, #56] ; bottom segment height
cmp r0, #0 ; check height of the bottom segment, if it's more than zero pixels..
blgt _WW08000898_fill_colored_trapezoid ; ...draw the bottom segment
ldr r0, [sp, #4] ; restore current renderstream pointer
JUMP_TO_NEXT_RENDERSTREAM_ENTRY
_WW08000830:
; mid-corner is on left, so both halves of triangle share a right edge
; so we only set r3 (right edge start) and r4 (right edge delta) once
; A
; .'\
; .' \
; .' \
; B+---------+
; '-. \
; '-. \
; '-. \
; '-. \
; '-\
; C
ldr r1, [sp, #20] ; AB start x-coordinate
ldr r2, [sp, #16] ; AB per-scanline delta
ldr r3, [sp, #28] ; AC start x-coordinate
ldr r4, [sp, #24] ; AC per-scanline delta
cmp r0, #0 ; check height of the top segment, if it's more than zero pixels..
blgt _WW08000898_fill_colored_trapezoid ; ...draw the top segment
ldr r1, [sp, #12] ; BC start x-coordinate
ldr r2, [sp, #8] ; BC per-scanline delta
ldr r0, [sp, #56] ; bottom segment height
cmp r0, #0 ; check height of the bottom segment, if it's more than zero pixels..
blgt _WW08000898_fill_colored_trapezoid ; ...draw the bottom segment
ldr r0, [sp, #4] ; restore current renderstream pointer
JUMP_TO_NEXT_RENDERSTREAM_ENTRY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment