diff --git a/wip-hugo/routines/line/line.inc b/wip-hugo/routines/line/line.inc index 4060b9b..fc2d0ca 100644 --- a/wip-hugo/routines/line/line.inc +++ b/wip-hugo/routines/line/line.inc @@ -1,6 +1,7 @@ ;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*- ;;Not values but register position in memory + ;; F4 - EB X_end = $EF Y_end = $F0 dx = $F3 diff --git a/wip-hugo/routines/triangle/triangle.inc b/wip-hugo/routines/triangle/triangle.inc new file mode 100644 index 0000000..b53011f --- /dev/null +++ b/wip-hugo/routines/triangle/triangle.inc @@ -0,0 +1,17 @@ +;;The triangles 3 corner-positions, they may be in any given order. +A_X = $EA +A_Y = $E9 +B_X = $E8 +B_Y = $E7 +C_X = $E6 +C_Y = $E5 +;; Differense in height (after these points have been sorted look in triangle.s for more detail) +AB = $E4 +BC = $E3 + +;; These come from mem.inc +;; Takes up FF - F5 +X_pos = $FC +Y_pos = $FB +byte_to_paint = $FE ; Byte with one 1 that corisponds to a pixel. +btp_mem_pos = $F9 ; 16-bit value (uses FA), byte to paint memory position diff --git a/wip-hugo/routines/triangle/triangle.s b/wip-hugo/routines/triangle/triangle.s new file mode 100644 index 0000000..0a28e50 --- /dev/null +++ b/wip-hugo/routines/triangle/triangle.s @@ -0,0 +1,80 @@ +.proc triangle +;; Subroutine for drawing a filld 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: +# This triangle has 10 posibol cases, see table bellow +# ---------------------------------------------------- +# | name left_side right_side | +# ---------------------------------------------------- +# | RR right-sloaping-steep right-sloaping-steep| +# | rR right-sloaping right-sloaping-steep| +# | lR left-sloaping right-sloaping-steep| +# | LR left-sloaping-steep right-sloaping-steep| +# | rr right-sloaping right-sloaping | +# | lr left-sloaping right-sloaping | +# | Lr left-sloaping-steep right-sloaping | +# | ll left-sloaping left-sloaping | +# | Ll left-sloaping-sleep left-sloaping | +# | LL left-sloaping-sleep left-sloaping-sleep | +# ---------------------------------------------------- +RTS +.endproc diff --git a/wip-hugo/routines/triangle/triangle_test.s b/wip-hugo/routines/triangle/triangle_test.s new file mode 100644 index 0000000..f46bd62 --- /dev/null +++ b/wip-hugo/routines/triangle/triangle_test.s @@ -0,0 +1,38 @@ +.scope triangle_test +;; Test program that tries to draw a triangle + .include "triangle.inc" + LDA #$33 + STA A_X + STA A_Y + LDA #$55 + STA C_X + STA C_Y + LDA #$77 + STA B_X + LDA #$20 + STA B_Y + +; jmp skipp_edges ; Skip drawing the edges +;;Draw the edges (only for reference) +edges: + X_end = $EF + Y_end = $F0 + ;X_pos = $FC + ;Y_pos = $FB +;;; A -- B + Mov_16 X_pos, Y_pos, A_X, A_Y + Mov_16 X_end, Y_end, B_X, B_Y + jsr line +;;; A -- C + Mov_16 X_pos, Y_pos, A_X, A_Y + Mov_16 X_end, Y_end, C_X, C_Y + jsr line +;;; B -- C + Mov_16 X_pos, Y_pos, C_X, C_Y + Mov_16 X_end, Y_end, B_X, B_Y + jsr line +skip_edges: +;; lets draw the example triangle +jsr triangle +jmp exit +.endscope diff --git a/wip-hugo/source.s b/wip-hugo/source.s index 17e6df9..44893be 100755 --- a/wip-hugo/source.s +++ b/wip-hugo/source.s @@ -6,11 +6,13 @@ .include "routines/memory/mem.inc" ;;Code to run .include "STARTUP.s" -.include "routines/line/line_test.s" +;.include "routines/line/line_test.s" ;.include "routines/memory/memcpy_test.s" +.include "routines/triangle/triangle_test.s" exit: jmp exit .include "routines/line/line.s" +.include "routines/triangle/triangle.s" .include "routines/memory/pixel_draw.s" .include "routines/memory/memset.s" .include "routines/memory/memcpy.s"