added bresenham and circle example

This commit is contained in:
Irmen de Jong 2020-03-14 17:11:10 +01:00
parent 4fd14f1366
commit 51bb902162
7 changed files with 118 additions and 12 deletions

View File

@ -16,7 +16,7 @@ sub start() {
void c64.CHRIN()
c64.CLEARSCR()
while(true) {
while true {
uword note
for note in notes {
ubyte note1 = lsb(note)
@ -39,7 +39,7 @@ sub start() {
sub delay() {
ubyte d
for d in 0 to 12 {
while(c64.RASTER!=0) {
while c64.RASTER!=0 {
; tempo delay synced to screen refresh
}
}

View File

@ -19,7 +19,7 @@ main {
sub start() {
float time=0.0
while(true) {
while true {
rotate_vertices(time)
c64scr.clear_screenchars(32)
draw_edges()
@ -27,7 +27,9 @@ main {
c64scr.plot(0,0)
c64scr.print("3d cube! (float) ")
c64scr.print_ub(c64.TIME_LO)
c64scr.print(" jiffies/frame")
c64scr.print(" jiffies/fr = ")
c64scr.print_ub(60/c64.TIME_LO)
c64scr.print(" fps")
c64.TIME_LO=0
}
}

View File

@ -82,7 +82,7 @@ main {
uword angley
uword anglez
word rz=33
while(true) {
while true {
c64.TIME_LO=0
rotate_vertices(msb(anglex), msb(angley), msb(anglez))
position_sprites()
@ -92,7 +92,9 @@ main {
c64scr.plot(0,0)
c64scr.print("3d cube! (sprites) ")
c64scr.print_ub(c64.TIME_LO)
c64scr.print(" jiffies/frame ")
c64scr.print(" jiffies/fr = ")
c64scr.print_ub(60/c64.TIME_LO)
c64scr.print(" fps")
}
}

View File

@ -22,7 +22,7 @@ main {
uword angley
uword anglez
word rz=33
while(true) {
while true {
rotate_vertices(msb(anglex), msb(angley), msb(anglez))
c64scr.clear_screenchars(32)
draw_edges()
@ -30,9 +30,11 @@ main {
angley+=433
anglez+=907
c64scr.plot(0,0)
c64scr.print("3d cube! (integer) ")
c64scr.print("3d cube! ")
c64scr.print_ub(c64.TIME_LO)
c64scr.print(" jiffies/frame")
c64scr.print(" jiffies/fr = ")
c64scr.print_ub(60/c64.TIME_LO)
c64scr.print(" fps")
c64.TIME_LO=0
}
}

100
examples/lines_circles.p8 Normal file
View File

@ -0,0 +1,100 @@
%import c64lib
%import c64utils
%zeropage basicsafe
main {
sub start() {
c64scr.print("mid-point\ncircle\n and\nbresenham\nline\nalgorithms.\n")
const ubyte xcenter = 20
const ubyte ycenter = 12
ubyte r
for r in 3 to 12 step 3 {
circle(r)
}
line(1, 10, 38, 24)
line(1, 20, 38, 2)
line(20, 4, 10, 24)
line(39, 16, 12, 0)
; TODO fix crash arg type incompatible c64scr.setcc(x0, y0 + radius, 81, 1)
sub line(ubyte x1, ubyte y1, ubyte x2, ubyte y2) {
byte d = 0
ubyte dx = abs(x2 - x1)
ubyte dy = abs(y2 - y1)
ubyte dx2 = 2 * dx
ubyte dy2 = 2 * dy
byte ix = sgn(x2-x1)
byte iy = sgn(y2-y1)
ubyte x = x1
ubyte y = y1
; TODO fix sgn
if x1<x2
ix=1
else
ix=-1
; TODO fix sgn
if y1<y2
iy=1
else
iy=-1
if dx >= dy {
; TODO fix assembler problem when defining label here
while true {
c64scr.setcc(x, y, 42, 5)
if x==x2
return
x += ix
d += dy2
if d > dx {
y += iy
d -= dx2
}
}
} else {
; TODO fix assembler problem when defining label here
while true {
c64scr.setcc(x, y, 42, 5)
if y == y2
return
y += iy
d += dx2
if d > dy {
x += ix
d -= dy2
}
}
}
}
sub circle(ubyte radius) {
byte x = radius as byte
byte y = 0
byte decisionOver2 = 1-x
while x>=y {
c64scr.setcc(xcenter + x as ubyte, ycenter + y as ubyte, 81, 1)
c64scr.setcc(xcenter - x as ubyte, ycenter + y as ubyte, 81, 1)
c64scr.setcc(xcenter + x as ubyte, ycenter - y as ubyte, 81, 1)
c64scr.setcc(xcenter - x as ubyte, ycenter - y as ubyte, 81, 1)
c64scr.setcc(xcenter + y as ubyte, ycenter + x as ubyte, 81, 1)
c64scr.setcc(xcenter - y as ubyte, ycenter + x as ubyte, 81, 1)
c64scr.setcc(xcenter + y as ubyte, ycenter - x as ubyte, 81, 1)
c64scr.setcc(xcenter - y as ubyte, ycenter - x as ubyte, 81, 1)
y++
if decisionOver2<=0
decisionOver2 += 2*y+1
else {
x--
decisionOver2 += 2*(y-x)+1
}
}
}
}
}

View File

@ -8,7 +8,7 @@ main {
c64.SCROLY &= %11101111 ; blank the screen
c64utils.set_rasterirq_excl(40) ; register exclusive raster irq handler
while(true) {
while true {
; enjoy the moving bars :)
}

View File

@ -246,7 +246,7 @@ waitkey:
c64scr.print("────────────────────────")
c64.CHROUT('K')
while(c64.GETIN()!=133) {
while c64.GETIN()!=133 {
; endless loop until user presses F1 to restart the game
}
}
@ -568,7 +568,7 @@ blocklogic {
}
sub collapse(ubyte ypos) {
while(ypos>main.startYpos+1) {
while ypos>main.startYpos+1 {
ubyte x
for x in main.boardOffsetX+main.boardWidth-1 downto main.boardOffsetX {
ubyte char = c64scr.getchr(x, ypos-1)