c64-livecoding/wip-hugo/routines/triangle/triangle.s

71 lines
1.9 KiB
ArmAsm

;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
;; Subroutine for drawing a filld triangle.
.proc triangle
;; It takes in values from A_X, A_Y, B_X, B_Y C_X, C_Y, see triangle.inc
.include "triangle.inc"
;; Algorithm
;; (i) For height n it steps through Bresenham's line algorithm to know where the triangle starts and ends at height n.
;; (ii) It draws all pixel in this given range.
;; (iii) It change if the algorithm has hit a corner and need to change line!
;; Implementation
;; In the implementation the triangle split into 2 souch that (iii) can be redused to: check if the triangle has ended.
;; Each side of the sub-triangle can have 4 posiboul states (look at how line is implemented)
;;Lets first sort the edges such as
;; (i) A is at the highest point
;; (ii) B is at the middle point
;; (iii) C is at the lowest point
;; Lets also calculate the height-differense for the new points:
;; * AB for differense betwean A and B
;; * BC for differense betwean B and C
;;AB
SEC
LDA B_Y
SBC A_Y
STA AB
BCC AB_overflow_end
;; A_Y > B_Y
AB_overflow:
EOR #$ff ; Fix byte underflow
;; swop A and B
LDX B_Y
LDY A_Y
STX A_Y
STY B_Y
LDX B_X
LDY A_X
STX A_X
STY B_X
AB_overflow_end:
;;BC
SEC
LDA C_Y
SBC B_Y
STA BC
BCC BC_overflow_end
;; B_Y > C_Y
BC_overflow:
EOR #$ff ; Fix byte underflow
;; swop A and B
LDX C_Y
LDY B_Y
STX B_Y
STY C_Y
LDX C_X
LDY B_X
STX B_X
STY C_X
;; If this is the case A and B may be in the wrong order. Lets redo stuff from the start
JMP triangle
BC_overflow_end:
draw_upper_triangle:
.include "upper_triangle.s"
draw_lower_triangle:
;;This has the same cases as upper triangle
.include "lower_triangle.s"
RTS
.endproc