From 40ce7725a1e545f1de31c45c952942d020b8f439 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 26 Oct 2024 21:35:59 +0200 Subject: [PATCH] cleanup c64 sprite examples --- docs/source/todo.rst | 2 - examples/c64/balloonflight.p8 | 102 +++++++++++++++-------------- examples/c64/cube3d-sprites.p8 | 114 ++++++++++++++++----------------- examples/c64/sprites.p8 | 60 ++++++++--------- examples/c64/wizzine.p8 | 61 +++++++++--------- 5 files changed, 165 insertions(+), 174 deletions(-) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 7ce923a78..06e30770c 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,6 @@ TODO ==== -- rewrite c64 sprite examples to use the new @align64 - - add docs for @alignxxx. Note: uninitialized (bss) variables are also correctly aligned (%option align docs say they're not, but that is fixed) - add docs for %align. Note: the directive doesn't modify variable declarations that may follow it! diff --git a/examples/c64/balloonflight.p8 b/examples/c64/balloonflight.p8 index 3c299752d..be6f94316 100644 --- a/examples/c64/balloonflight.p8 +++ b/examples/c64/balloonflight.p8 @@ -11,8 +11,8 @@ main { sub start() { uword moon_x = 310 - c64.set_sprite_ptr(0, $0f00) ; alternatively, set directly: c64.SPRPTR[0] = $0f00 / 64 - c64.set_sprite_ptr(1, $0f00+64) ; alternatively, set directly: c64.SPRPTR[0] = $0f00 / 64 + c64.set_sprite_ptr(0, &spritedata.balloonsprite) ; alternatively, set directly: c64.SPRPTR[0] = $0f00 / 64 + c64.set_sprite_ptr(1, &spritedata.moonsprite) ; alternatively, set directly: c64.SPRPTR[0] = $0f00 / 64 c64.SPENA = %00000011 c64.SP0COL = 14 c64.SP1COL = 7 @@ -156,55 +156,53 @@ irq { } -spritedata $0f00 { - ; this memory block contains the sprite data. it must start on an address aligned to 64 bytes. - ; for simplicity, it's currently statically located at $0f00 (not far in memory after the program code), - ; but there are ways to do this more dynamically. - %option force_output ; make sure the data in this block appears in the resulting program +spritedata { + ; this block contains the sprite data. Sprites must start on an address aligned to 64 bytes. + ubyte[] @align64 balloonsprite = [ + %00000000,%01111111,%00000000, + %00000001,%11111111,%11000000, + %00000011,%11111111,%11100000, + %00000011,%11100011,%11100000, + %00000111,%11011100,%11110000, + %00000111,%11011101,%11110000, + %00000111,%11011100,%11110000, + %00000011,%11100011,%11100000, + %00000011,%11111111,%11100000, + %00000011,%11111111,%11100000, + %00000010,%11111111,%10100000, + %00000001,%01111111,%01000000, + %00000001,%00111110,%01000000, + %00000000,%10011100,%10000000, + %00000000,%10011100,%10000000, + %00000000,%01001001,%00000000, + %00000000,%01001001,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00011100,%00000000 + ] - ubyte[] balloonsprite = [ %00000000,%01111111,%00000000, - %00000001,%11111111,%11000000, - %00000011,%11111111,%11100000, - %00000011,%11100011,%11100000, - %00000111,%11011100,%11110000, - %00000111,%11011101,%11110000, - %00000111,%11011100,%11110000, - %00000011,%11100011,%11100000, - %00000011,%11111111,%11100000, - %00000011,%11111111,%11100000, - %00000010,%11111111,%10100000, - %00000001,%01111111,%01000000, - %00000001,%00111110,%01000000, - %00000000,%10011100,%10000000, - %00000000,%10011100,%10000000, - %00000000,%01001001,%00000000, - %00000000,%01001001,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00011100,%00000000, - 0] - - ubyte[] moonsprite = [ %00000000,%00000110,%00000000, - %00000000,%00011100,%00000000, - %00000000,%01111000,%00000000, - %00000000,%11111000,%00000000, - %00000001,%11110000,%00000000, - %00000011,%11110000,%00000000, - %00000011,%11110000,%00000000, - %00000111,%11100000,%00000000, - %00000111,%11100000,%00000000, - %00000111,%11100000,%00000000, - %00000111,%11100000,%00000000, - %00000111,%11100000,%00000000, - %00000111,%11100000,%00000000, - %00000111,%11100000,%00000000, - %00000011,%11110000,%00000000, - %00000011,%11110000,%00000000, - %00000001,%11110000,%00000000, - %00000000,%11111000,%00000000, - %00000000,%01111000,%00000000, - %00000000,%00011100,%00000000, - %00000000,%00000110,%00000000, - 0] + ubyte[] @align64 moonsprite = [ + %00000000,%00000110,%00000000, + %00000000,%00011100,%00000000, + %00000000,%01111000,%00000000, + %00000000,%11111000,%00000000, + %00000001,%11110000,%00000000, + %00000011,%11110000,%00000000, + %00000011,%11110000,%00000000, + %00000111,%11100000,%00000000, + %00000111,%11100000,%00000000, + %00000111,%11100000,%00000000, + %00000111,%11100000,%00000000, + %00000111,%11100000,%00000000, + %00000111,%11100000,%00000000, + %00000111,%11100000,%00000000, + %00000011,%11110000,%00000000, + %00000011,%11110000,%00000000, + %00000001,%11110000,%00000000, + %00000000,%11111000,%00000000, + %00000000,%01111000,%00000000, + %00000000,%00011100,%00000000, + %00000000,%00000110,%00000000 + ] } diff --git a/examples/c64/cube3d-sprites.p8 b/examples/c64/cube3d-sprites.p8 index df8dd00a1..3c07ca064 100644 --- a/examples/c64/cube3d-sprites.p8 +++ b/examples/c64/cube3d-sprites.p8 @@ -2,64 +2,6 @@ %import textio %import math - -spritedata $2000 { - ; this memory block contains the sprite data - ; it must start on an address aligned to 64 bytes. - %option force_output ; make sure the data in this block appears in the resulting program - - ubyte[] sprites = [ - %00000000,%00000000,%00000000, - %00000000,%00111100,%00000000, - %00000000,%11111111,%00000000, - %00000001,%11111101,%10000000, - %00000001,%11111111,%10000000, - %00000011,%11111111,%11000000, - %00000011,%11111111,%11000000, - %00000011,%11111111,%11000000, - %00000001,%11111111,%10000000, - %00000001,%11111111,%10000000, - %00000000,%11111111,%00000000, - %00000000,%00111100,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - - 0, - - %00000000,%00111110,%00000000, - %00000000,%11111111,%10000000, - %00000001,%11111110,%11000000, - %00000011,%11111111,%01100000, - %00000011,%11111111,%11100000, - %00000111,%11111111,%11110000, - %00000111,%11111111,%11110000, - %00000111,%11111111,%11110000, - %00000111,%11111111,%11110000, - %00000011,%11111111,%11100000, - %00000011,%11111111,%11100000, - %00000001,%11111111,%11000000, - %00000000,%11111111,%10000000, - %00000000,%00111110,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - %00000000,%00000000,%00000000, - - 0 - ] -} - - main { const uword width = 255 @@ -170,11 +112,63 @@ main { ; because we want speed, we don't use the dynamic c64.set_sprite_ptr() here if(zc < 30*128) - c64.SPRPTR[i] = $2000/64 +1 ; large ball + c64.SPRPTR[i] = lsb(&spritedata.sprite_large/64) else - c64.SPRPTR[i] = $2000/64 ; small ball + c64.SPRPTR[i] = lsb(&spritedata.sprite_small/64) c64.SPCOL[i] = spritecolors[(zc>>13) as ubyte + 4] ; further away=darker color } } } + +spritedata { + ; this memory block contains the sprite data + ; it must start on an address aligned to 64 bytes. + ubyte[] @align64 sprite_small = [ + %00000000,%00000000,%00000000, + %00000000,%00111100,%00000000, + %00000000,%11111111,%00000000, + %00000001,%11111101,%10000000, + %00000001,%11111111,%10000000, + %00000011,%11111111,%11000000, + %00000011,%11111111,%11000000, + %00000011,%11111111,%11000000, + %00000001,%11111111,%10000000, + %00000001,%11111111,%10000000, + %00000000,%11111111,%00000000, + %00000000,%00111100,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000 + ] + + ubyte[] @align64 sprite_large = [ + %00000000,%00111110,%00000000, + %00000000,%11111111,%10000000, + %00000001,%11111110,%11000000, + %00000011,%11111111,%01100000, + %00000011,%11111111,%11100000, + %00000111,%11111111,%11110000, + %00000111,%11111111,%11110000, + %00000111,%11111111,%11110000, + %00000111,%11111111,%11110000, + %00000011,%11111111,%11100000, + %00000011,%11111111,%11100000, + %00000001,%11111111,%11000000, + %00000000,%11111111,%10000000, + %00000000,%00111110,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000, + %00000000,%00000000,%00000000 + ] +} diff --git a/examples/c64/sprites.p8 b/examples/c64/sprites.p8 index 48a9a00ad..98a83f6be 100644 --- a/examples/c64/sprites.p8 +++ b/examples/c64/sprites.p8 @@ -3,35 +3,6 @@ %import math %zeropage basicsafe - -spritedata $0a00 { - ; this memory block contains the sprite data - ; it must start on an address aligned to 64 bytes. - %option force_output ; make sure the data in this block appears in the resulting program - - ubyte[] balloonsprite = [ %00000000,%01111111,%00000000, - %00000001,%11111111,%11000000, - %00000011,%11111111,%11100000, - %00000011,%11100011,%11100000, - %00000111,%11011100,%11110000, - %00000111,%11011101,%11110000, - %00000111,%11011100,%11110000, - %00000011,%11100011,%11100000, - %00000011,%11111111,%11100000, - %00000011,%11111111,%11100000, - %00000010,%11111111,%10100000, - %00000001,%01111111,%01000000, - %00000001,%00111110,%01000000, - %00000000,%10011100,%10000000, - %00000000,%10011100,%10000000, - %00000000,%01001001,%00000000, - %00000000,%01001001,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00011100,%00000000 ] -} - main { sub start() { @@ -40,7 +11,7 @@ main { ubyte @zp i for i in 0 to 7 { - c64.set_sprite_ptr(i, $0a00) ; alternatively, set directly: c64.SPRPTR[i] = $0a00 / 64 + c64.set_sprite_ptr(i, &spritedata.balloonsprite) ; alternatively, set directly: c64.SPRPTR[i] = $0a00 / 64 c64.SPXY[i*2] = 50+25*i c64.SPXY[i*2+1] = math.rnd() } @@ -72,3 +43,32 @@ irq { } } + +spritedata { + ; this memory block contains the sprite data + ; it must start on an address aligned to 64 bytes. + + ubyte[] @align64 balloonsprite = [ + %00000000,%01111111,%00000000, + %00000001,%11111111,%11000000, + %00000011,%11111111,%11100000, + %00000011,%11100011,%11100000, + %00000111,%11011100,%11110000, + %00000111,%11011101,%11110000, + %00000111,%11011100,%11110000, + %00000011,%11100011,%11100000, + %00000011,%11111111,%11100000, + %00000011,%11111111,%11100000, + %00000010,%11111111,%10100000, + %00000001,%01111111,%01000000, + %00000001,%00111110,%01000000, + %00000000,%10011100,%10000000, + %00000000,%10011100,%10000000, + %00000000,%01001001,%00000000, + %00000000,%01001001,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00011100,%00000000 + ] +} diff --git a/examples/c64/wizzine.p8 b/examples/c64/wizzine.p8 index 8aa4a2b26..cfd58b874 100644 --- a/examples/c64/wizzine.p8 +++ b/examples/c64/wizzine.p8 @@ -2,40 +2,11 @@ %import math %zeropage basicsafe - -spritedata $0a00 { - ; this memory block contains the sprite data - ; it must start on an address aligned to 64 bytes. - %option force_output ; make sure the data in this block appears in the resulting program - - ubyte[] balloonsprite = [ %00000000,%01111111,%00000000, - %00000001,%11111111,%11000000, - %00000011,%11111111,%11100000, - %00000011,%11100011,%11100000, - %00000111,%11011100,%11110000, - %00000111,%11011101,%11110000, - %00000111,%11011100,%11110000, - %00000011,%11100011,%11100000, - %00000011,%11111111,%11100000, - %00000011,%11111111,%11100000, - %00000010,%11111111,%10100000, - %00000001,%01111111,%01000000, - %00000001,%00111110,%01000000, - %00000000,%10011100,%10000000, - %00000000,%10011100,%10000000, - %00000000,%01001001,%00000000, - %00000000,%01001001,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00111110,%00000000, - %00000000,%00011100,%00000000 ] -} - main { sub start() { ubyte i for i in 0 to 7 { - c64.set_sprite_ptr(i, $0a00) ; alternatively, set directly: c64.SPRPTR[i] = $0a00 / 64 + c64.set_sprite_ptr(i, &spritedata.balloonsprite) ; alternatively, set directly: c64.SPRPTR[i] = $0a00 / 64 } c64.SPENA = 255 ; enable all sprites sys.set_rasterirq(&irq.irqhandler, 230) ; enable animation @@ -65,3 +36,33 @@ irq { return true } } + + +spritedata { + ; this memory block contains the sprite data + ; it must start on an address aligned to 64 bytes. + + ubyte[] @align64 balloonsprite = [ + %00000000,%01111111,%00000000, + %00000001,%11111111,%11000000, + %00000011,%11111111,%11100000, + %00000011,%11100011,%11100000, + %00000111,%11011100,%11110000, + %00000111,%11011101,%11110000, + %00000111,%11011100,%11110000, + %00000011,%11100011,%11100000, + %00000011,%11111111,%11100000, + %00000011,%11111111,%11100000, + %00000010,%11111111,%10100000, + %00000001,%01111111,%01000000, + %00000001,%00111110,%01000000, + %00000000,%10011100,%10000000, + %00000000,%10011100,%10000000, + %00000000,%01001001,%00000000, + %00000000,%01001001,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00111110,%00000000, + %00000000,%00011100,%00000000 + ] +}