making snow example more interesting

This commit is contained in:
Irmen de Jong 2022-12-22 12:07:29 +01:00
parent 475e927178
commit 4e103a1963
3 changed files with 104 additions and 34 deletions

View File

@ -23,7 +23,7 @@ diskio {
void c64.CHRIN() ; skip the 4 prologue bytes void c64.CHRIN() ; skip the 4 prologue bytes
} }
; while not key pressed / EOF encountered, read data. ; while not stop key pressed / EOF encountered, read data.
status = c64.READST() status = c64.READST()
if status!=0 { if status!=0 {
status = 1 status = 1
@ -84,7 +84,6 @@ io_error:
if c64.READST()!=0 if c64.READST()!=0
goto io_error goto io_error
; while not key pressed / EOF encountered, read data.
cx16.r0 = &list_filename cx16.r0 = &list_filename
repeat { repeat {
@(cx16.r0) = c64.CHRIN() @(cx16.r0) = c64.CHRIN()

View File

@ -3,6 +3,13 @@ TODO
For next release For next release
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- fix compiler crash on : bool bb = gfx2.pget(pilex1, flakes1_yy[idx]+1) in PILED_SNOW_COLORS
- bool bb = not bb -> larger code than bool bb ^= 1
- bool xx = ~xx -> larger code than xx ^= 1
- ubyte xx = not xx -> much larger code than xx ^= 1
- try to support "not (xx in array)" or maybe even "xx not in array"
- astchecker: "containment check is currently not supported inside complex expressions" fix this?
or at least disable this check for virtual target?
... ...

View File

@ -5,37 +5,44 @@ main {
sub start() { sub start() {
gfx2.screen_mode(4) gfx2.screen_mode(4)
uword[128] xx uword[128] flakes1_xx
uword[128] yy uword[128] flakes1_yy
uword[128] flakes2_xx
uword[128] flakes2_yy
ubyte @zp idx ubyte @zp idx
for idx in 0 to 127 { for idx in 0 to 127 {
xx[idx] = math.rndw() % 320 flakes1_xx[idx] = math.rndw() % 320
yy[idx] = math.rndw() % 240 flakes1_yy[idx] = math.rndw() % 240
flakes2_xx[idx] = math.rndw() % 320
flakes2_yy[idx] = math.rndw() % 240
} }
gfx2.text(96, 90, 2, sc:"**************") draw_screen()
gfx2.text(104, 100, 5, sc:"let it snow!")
gfx2.text(96, 110, 2, sc:"**************")
const ubyte FALLING_SNOW_COLOR = 1 const ubyte FALLING_SNOW_COLOR = 1
const ubyte FALLING_SNOW_COLOR2 = 12
const ubyte PILED_SNOW_COLOR = 15 const ubyte PILED_SNOW_COLOR = 15
ubyte[] PILED_SNOW_COLORS = [PILED_SNOW_COLOR, 5, 2] ; falling snow colors should NOT be in here!
bool fall_layer_2 = false
repeat { repeat {
sys.waitvsync() for idx in 0 to len(flakes1_xx)-1 {
for idx in 0 to 127 { gfx2.plot(flakes1_xx[idx], flakes1_yy[idx], FALLING_SNOW_COLOR)
gfx2.plot(xx[idx], yy[idx], FALLING_SNOW_COLOR) gfx2.plot(flakes2_xx[idx], flakes2_yy[idx], FALLING_SNOW_COLOR2)
} }
sys.waitvsync() sys.waitvsync()
for idx in 0 to 127 {
if yy[idx]==239 { for idx in 0 to len(flakes1_xx)-1 {
if flakes1_yy[idx]==239 {
; reached the floor ; reached the floor
gfx2.plot(xx[idx], yy[idx], PILED_SNOW_COLOR) gfx2.plot(flakes1_xx[idx], flakes1_yy[idx], PILED_SNOW_COLOR)
yy[idx] = 0 flakes1_yy[idx] = 0
xx[idx] = math.rndw() % 320 flakes1_xx[idx] = math.rndw() % 320
} else if gfx2.pget(xx[idx], yy[idx]+1)==PILED_SNOW_COLOR { } else if gfx2.pget(flakes1_xx[idx], flakes1_yy[idx]+1) in PILED_SNOW_COLORS {
; pile up ; pile up
uword @zp snowx = xx[idx] ; TODO somehow prevent growing horizontally/diagonally like a crystal
uword @zp snowx = flakes1_xx[idx]
if snowx!=0 and snowx!=319 { ; check to avoid x coordinate under/overflow if snowx!=0 and snowx!=319 { ; check to avoid x coordinate under/overflow
uword pilex1 uword pilex1
uword pilex2 uword pilex2
@ -46,30 +53,63 @@ main {
pilex1 = snowx+1 pilex1 = snowx+1
pilex2 = snowx-1 pilex2 = snowx-1
} }
if gfx2.pget(pilex1, yy[idx]+1)==0 { ubyte pixel_side1 = gfx2.pget(pilex1, flakes1_yy[idx]+1)
gfx2.plot(snowx, yy[idx], 0) ubyte pixel_side2 = gfx2.pget(pilex2, flakes1_yy[idx]+1)
gfx2.plot(pilex1, yy[idx]+1, PILED_SNOW_COLOR) if pixel_side1 == 0 or pixel_side1 == FALLING_SNOW_COLOR2 {
} else if gfx2.pget(pilex2, yy[idx]+1)==0 { gfx2.plot(snowx, flakes1_yy[idx], 0)
gfx2.plot(snowx, yy[idx], 0) gfx2.plot(pilex1, flakes1_yy[idx]+1, PILED_SNOW_COLOR)
gfx2.plot(pilex2, yy[idx]+1, PILED_SNOW_COLOR) } else if pixel_side2 == 0 or pixel_side2 == FALLING_SNOW_COLOR2 {
gfx2.plot(snowx, flakes1_yy[idx], 0)
gfx2.plot(pilex2, flakes1_yy[idx]+1, PILED_SNOW_COLOR)
} else { } else {
gfx2.plot(snowx, yy[idx], PILED_SNOW_COLOR) gfx2.plot(snowx, flakes1_yy[idx], PILED_SNOW_COLOR)
} }
} }
yy[idx] = 0 flakes1_yy[idx] = 0
xx[idx] = math.rndw() % 320 flakes1_xx[idx] = math.rndw() % 320
} else { } else {
; fall ; fall
gfx2.plot(xx[idx], yy[idx], 0) gfx2.plot(flakes1_xx[idx], flakes1_yy[idx], 0)
yy[idx]++ flakes1_yy[idx]++
when math.rnd() & 3 { when math.rnd() & 3 {
1 -> { 1 -> {
if xx[idx] if flakes1_xx[idx]
xx[idx]-- flakes1_xx[idx]--
} }
2 -> { 2 -> {
if xx[idx] < 319 if flakes1_xx[idx] < 319
xx[idx]++ flakes1_xx[idx]++
}
}
}
}
fall_layer_2 = not fall_layer_2
if fall_layer_2 {
for idx in 0 to len(flakes2_xx)-1 {
; the second 'layer' of snowflakes
if flakes2_yy[idx]==239 {
; reached the floor, don't pile up just fall again
flakes2_yy[idx] = 0
flakes2_xx[idx] = math.rndw() % 320
} else if gfx2.pget(flakes2_xx[idx], flakes2_yy[idx]+1) in PILED_SNOW_COLORS {
; reached an obstruction, don't pile up just fall again
flakes2_yy[idx] = 0
flakes2_xx[idx] = math.rndw() % 320
} else {
; fall normally
gfx2.plot(flakes2_xx[idx], flakes2_yy[idx], 0)
flakes2_yy[idx]+=1
when math.rnd() & 3 {
1 -> {
if flakes2_xx[idx]
flakes2_xx[idx]--
}
2 -> {
if flakes2_xx[idx] < 319
flakes2_xx[idx]++
}
} }
} }
} }
@ -79,4 +119,28 @@ main {
repeat { repeat {
} }
} }
sub draw_screen() {
gfx2.text(32, 130, 2, sc: "******************" )
gfx2.text(40, 140, 5, sc: "happy holidays !" )
gfx2.text(32, 150, 5, sc: "from commander x16" )
gfx2.text(32, 160, 2, sc: "******************" )
; draw a tree
const uword TREEX = 240
ubyte maxwidth = 20
ubyte branchesy = 100
gfx2.fillrect(TREEX-5, 210, 10, 30, 9)
repeat 5 {
ubyte width
for width in 1 to maxwidth {
gfx2.horizontal_line(TREEX-width/2, branchesy, width, 5)
branchesy++
}
branchesy -= maxwidth/2
maxwidth += 8
}
}
} }