pointers no longer implicitly converted to boolean in expressions, to be consistent with how integers are handled in conditionals

adding particles fountain examples
This commit is contained in:
Irmen de Jong
2025-07-18 00:05:38 +02:00
parent dc434d034a
commit b0981a5fae
13 changed files with 316 additions and 49 deletions

View File

@@ -69,7 +69,6 @@ main {
sub learn_new_animal() {
str new_animal = "?" * 30
str answer = "?" * 10
; note that we make copies of the animal name and question strings to store them later
txt.print("\nI give up. What is the animal? ")

View File

@@ -68,14 +68,14 @@ btree {
^^Node parent = root
repeat {
if parent.value >= value {
if parent.left
if parent.left!=0
parent = parent.left
else {
parent.left = node
return
}
} else {
if parent.right
if parent.right!=0
parent = parent.right
else {
parent.right = node
@@ -88,7 +88,7 @@ btree {
sub contains(uword value) -> bool {
^^Node r = root
while r {
while r!=0 {
if r.value==value
return true
if r.value>value
@@ -102,19 +102,19 @@ btree {
sub size() -> ubyte {
ubyte count
if root
if root!=0
count_node(root)
return count
sub count_node(^^Node r) {
count++
if r.left {
if r.left!=0 {
sys.pushw(r)
count_node(r.left)
r = sys.popw()
}
if r.right {
if r.right!=0 {
sys.pushw(r)
count_node(r.right)
r = sys.popw()
@@ -126,7 +126,7 @@ btree {
; note: we don't deallocate the memory from the node, for simplicity sake
^^Node n = root
^^Node parent = 0
while n {
while n!=0 {
if n.value==value {
if n.left==0
replacechild(parent, n, n.right)
@@ -154,7 +154,7 @@ btree {
sub find_successor(^^Node p) -> ^^Node {
^^Node succ = p
p = p.right
while p {
while p!=0 {
succ = p
p = p.left
}
@@ -171,19 +171,19 @@ btree {
sub print_tree_inorder() {
if root
if root!=0
print_tree(root)
txt.nl()
sub print_tree(^^Node r) {
if r.left {
if r.left!=0 {
sys.pushw(r)
print_tree(r.left)
r = sys.popw()
}
txt.print_uw(r.value)
txt.print(", ")
if r.right {
if r.right!=0 {
sys.pushw(r)
print_tree(r.right)
r = sys.popw()
@@ -193,7 +193,7 @@ btree {
sub print_tree_preorder() {
if root
if root!=0
print_tree(root,0)
txt.nl()
@@ -201,14 +201,14 @@ btree {
repeat depth txt.print(" ")
txt.print_uw(r.value)
txt.nl()
if r.left {
if r.left!=0 {
sys.pushw(r)
sys.push(depth)
print_tree(r.left, depth+1)
depth = sys.pop()
r = sys.popw()
}
if r.right {
if r.right!=0 {
sys.pushw(r)
sys.push(depth)
print_tree(r.right, depth+1)

View File

@@ -0,0 +1,104 @@
; Particle fountain.
; This is NOT necessarily the most efficient or idiomatic Prog8 way to do this!
; But it is just an example for how you could allocate and use structs dynamically.
; It uses a linked list to store all active particles.
%import math
main {
struct Particle {
word x,y
byte speedx, speedy
ubyte brightness
^^Particle next
}
const uword MAX_PARTICLES = 400
const ubyte GRAVITY = 1
^^Particle particles
uword active_particles = 0
sub start() {
repeat 4
spawnrandom()
sys.gfx_enable(0) ; enable lo res screen
repeat {
sys.gfx_clear(0)
update_particles()
sys.wait(2)
sys.waitvsync()
}
}
sub spawnrandom() {
if active_particles < MAX_PARTICLES {
^^Particle pp = arena.alloc(sizeof(Particle))
if particles!=0 ; TODO use if expression once fixed
pp.next = particles
else
pp.next = 0
particles = pp
initparticle(pp)
active_particles++
}
}
sub initparticle(^^Particle pp) {
pp.x = 160
pp.y = 238
pp.speedx = math.rnd() % 10 as byte -5
if pp.speedx==0
pp.speedx=1
pp.speedy = -10 - math.rnd() % 12
pp.brightness = 255
}
sub update_particles() {
^^Particle pp = particles
while pp!=0 {
pp.speedy += GRAVITY
pp.x += pp.speedx
pp.y += pp.speedy
if pp.y >= 239 {
initparticle(pp)
spawnrandom()
} else {
; TODO use clamp once fixed?
if pp.x < 0 {
pp.x = 0
pp.speedx = 0
}
else if pp.x > 319 {
pp.x = 319
pp.speedx = 0
}
}
sys.gfx_plot(pp.x as uword, pp.y as uword, pp.brightness)
if pp.brightness>=7
pp.brightness -= 7
pp = pp.next
}
}
}
arena {
; extremely trivial arena allocator (that never frees)
uword buffer = memory("arena", 4000, 0)
uword next = buffer
sub alloc(ubyte size) -> uword {
defer next += size
return next ; TODO why is this a defer warning? "using defer with nontrivial return value(s) incurs stack overhead"
}
}

View File

@@ -28,7 +28,7 @@ main {
sub printlist() {
ubyte count = 0
^^slist.Node n = slist.head
while n {
while n!=0 {
txt.print_uw(n.size)
txt.chrout(':')
txt.chrout(n.letter)
@@ -60,10 +60,10 @@ slist {
uword size = node.size
^^Node predecessor = 0
^^Node current = head
while current {
while current!=0 {
if current.size >= size {
node.next = current
if predecessor
if predecessor!=0
break
else {
head = node