diff --git a/compiler/res/prog8lib/c64/graphics.p8 b/compiler/res/prog8lib/c64/graphics.p8 index f3dab57df..1c112ab23 100644 --- a/compiler/res/prog8lib/c64/graphics.p8 +++ b/compiler/res/prog8lib/c64/graphics.p8 @@ -2,7 +2,7 @@ %import textio ; bitmap pixel graphics module for the C64 -; only black/white monchrome for now +; only black/white monchrome 320x200 for now ; assumes bitmap screen memory is $2000-$3fff graphics { @@ -135,33 +135,33 @@ graphics { } } - sub disc(uword cx, ubyte cy, ubyte radius) { + sub disc(uword xcenter, ubyte ycenter, ubyte radius) { ; Midpoint algorithm, filled ubyte xx = radius ubyte yy = 0 byte decisionOver2 = 1-xx as byte while xx>=yy { - ubyte cy_plus_yy = cy + yy - ubyte cy_min_yy = cy - yy - ubyte cy_plus_xx = cy + xx - ubyte cy_min_xx = cy - xx + ubyte ycenter_plus_yy = ycenter + yy + ubyte ycenter_min_yy = ycenter - yy + ubyte ycenter_plus_xx = ycenter + xx + ubyte ycenter_min_xx = ycenter - xx - for internal_plotx in cx to cx+xx { - internal_plot(cy_plus_yy) - internal_plot(cy_min_yy) + for internal_plotx in xcenter to xcenter+xx { + internal_plot(ycenter_plus_yy) + internal_plot(ycenter_min_yy) } - for internal_plotx in cx-xx to cx-1 { - internal_plot(cy_plus_yy) - internal_plot(cy_min_yy) + for internal_plotx in xcenter-xx to xcenter-1 { + internal_plot(ycenter_plus_yy) + internal_plot(ycenter_min_yy) } - for internal_plotx in cx to cx+yy { - internal_plot(cy_plus_xx) - internal_plot(cy_min_xx) + for internal_plotx in xcenter to xcenter+yy { + internal_plot(ycenter_plus_xx) + internal_plot(ycenter_min_xx) } - for internal_plotx in cx-yy to cx { - internal_plot(cy_plus_xx) - internal_plot(cy_min_xx) + for internal_plotx in xcenter-yy to xcenter { + internal_plot(ycenter_plus_xx) + internal_plot(ycenter_min_xx) } yy++ if decisionOver2<=0 diff --git a/compiler/res/prog8lib/cx16/graphics.p8 b/compiler/res/prog8lib/cx16/graphics.p8 new file mode 100644 index 000000000..d6e308b94 --- /dev/null +++ b/compiler/res/prog8lib/cx16/graphics.p8 @@ -0,0 +1,56 @@ +%target cx16 +%import syslib + +; bitmap pixel graphics module for the CommanderX16 +; wraps the graphics functions that are in ROM. +; only black/white monchrome 320x200 for now. + +graphics { + + sub enable_bitmap_mode() { + ; enable bitmap screen, erase it and set colors to black/white. + void cx16.screen_set_mode($80) + cx16.r0 = 0 + cx16.GRAPH_init() + clear_screen(1, 0) + } + + sub clear_screen(ubyte pixelcolor, ubyte bgcolor) { + cx16.GRAPH_set_colors(pixelcolor, pixelcolor, bgcolor) + cx16.GRAPH_clear() + } + + sub line(uword @zp x1, ubyte @zp y1, uword @zp x2, ubyte @zp y2) { + cx16.r0 = x1 + cx16.r1 = y1 + cx16.r2 = x2 + cx16.r3 = y2 + cx16.GRAPH_draw_line() + } + + sub circle(uword xcenter, ubyte ycenter, ubyte radius) { + cx16.r0 = xcenter - radius/2 + cx16.r1 = ycenter - radius/2 + cx16.r2 = radius + cx16.r3 = radius + cx16.GRAPH_draw_oval(false) ; TODO currently seems to crash + } + + sub disc(uword xcenter, ubyte ycenter, ubyte radius) { + cx16.r0 = xcenter - radius/2 + cx16.r1 = ycenter - radius/2 + cx16.r2 = radius + cx16.r3 = radius + cx16.GRAPH_draw_oval(true) ; TODO currently seems to crash + } + + sub plot(uword plotx, ubyte ploty) { + cx16.r0 = plotx + cx16.r1 = ploty + cx16.FB_cursor_position() + cx16.FB_cursor_position() + cx16.FB_set_pixel(1) + } +} + + diff --git a/examples/cube3d-gfx.p8 b/examples/cube3d-gfx.p8 index d3b5c9c3e..193fe80bf 100644 --- a/examples/cube3d-gfx.p8 +++ b/examples/cube3d-gfx.p8 @@ -1,9 +1,7 @@ -%target c64 -%import graphics %import syslib +%import graphics -; TODO make the graphics library not C64 specific - +; Note: this program is compatible with C64 and CX16. main { @@ -38,13 +36,23 @@ main { angley+=217 anglez+=452 - while c64.RASTER!=255 { - } - while c64.RASTER!=254 { - } + wait_a_little_bit() } } + sub wait_a_little_bit() { + %asm {{ + stx P8ZP_SCRATCH_REG + lda #0 + jsr c64.SETTIM +- jsr c64.RDTIM + cmp #1 + bne - + ldx P8ZP_SCRATCH_REG + rts + }} + } + sub rotate_vertices(ubyte ax, ubyte ay, ubyte az) { ; rotate around origin (0,0,0) diff --git a/examples/cx16/mandelbrot-gfx.p8 b/examples/cx16/mandelbrot-gfx-colors.p8 similarity index 100% rename from examples/cx16/mandelbrot-gfx.p8 rename to examples/cx16/mandelbrot-gfx-colors.p8 diff --git a/examples/line-circle-gfx.p8 b/examples/line-circle-gfx.p8 index aa77bcc1e..060800b51 100644 --- a/examples/line-circle-gfx.p8 +++ b/examples/line-circle-gfx.p8 @@ -1,6 +1,6 @@ %import graphics -; TODO make graphics lib cross-system +; Note: this program is compatible with C64 and CX16. main { diff --git a/examples/mandelbrot-gfx.p8 b/examples/mandelbrot-gfx.p8 index 0a2870cbf..b83f07f7b 100644 --- a/examples/mandelbrot-gfx.p8 +++ b/examples/mandelbrot-gfx.p8 @@ -3,10 +3,10 @@ %zeropage floatsafe ; Draw a mandelbrot in graphics mode (the image will be 256 x 200 pixels). -; NOTE: this will take an eternity to draw on a real c64. +; NOTE: this will take an eternity to draw on a real c64. A CommanderX16 is a bit faster. ; even in Vice in warp mode (700% speed on my machine) it's slow, but you can see progress -; TODO make graphics lib cross-system +; Note: this program is compatible with C64 and CX16. main { const ubyte width = 255