1
0
Fork 0
forked from AbleOS/ableos
ableos-idl/sysdata/programs/render_example/src/examples/tactical_screen.hb

83 lines
2.7 KiB
Plaintext
Raw Normal View History

render := @use("../../../../libraries/render/src/lib.hb");
.{math, random} := @use("../../../../libraries/stn/src/lib.hb")
Vec2 := math.Vec2
/* expected result:
a grid of green lines scrolling from the left top corner to the right bottom one
with a "target" randomly apperaing in one of them and a "seeker" "catching" it */
example := fn(): void {
2024-10-14 19:24:29 -05:00
screen := render.init(true)
2024-10-14 19:24:29 -05:00
width := screen.width
height := screen.height
cell_size := 0
2024-10-25 10:37:38 -05:00
range := Vec2(uint).(0, 0)
if width > height {
cell_size = width / 40
range = .(39, height / cell_size - 1)
} else {
cell_size = height / 40
range = .(width / cell_size - 1, 39)
}
width -= 1
height -= 1
scroll := 0
2024-10-25 10:37:38 -05:00
target := Vec2(uint).(random.range(uint, 0, range.x), random.range(uint, 0, range.y))
halfcell := cell_size / 2
octcell := cell_size / 8
sevenoctcell := cell_size - octcell
2024-10-25 10:37:38 -05:00
seeker := Vec2(uint).(random.range(uint, 0, range.x), random.range(uint, 0, range.y))
loop {
2024-10-14 19:24:29 -05:00
render.clear(screen, render.black)
2024-10-25 10:37:38 -05:00
target_pixel_coord := target * .(@bitcast(cell_size), @bitcast(cell_size)) + .(scroll, scroll)
render.put_trirect(screen, target_pixel_coord, .(@bitcast(cell_size), @bitcast(cell_size)), render.red, render.light_red)
2024-10-14 19:24:29 -05:00
render.put_hline(screen, target_pixel_coord.y + halfcell, target_pixel_coord.x - octcell, target_pixel_coord.x - sevenoctcell, render.light_red)
render.put_hline(screen, target_pixel_coord.y + halfcell, target_pixel_coord.x + cell_size + octcell, target_pixel_coord.x + cell_size + sevenoctcell, render.light_red)
render.put_vline(screen, target_pixel_coord.x + halfcell, target_pixel_coord.y - octcell, target_pixel_coord.y - sevenoctcell, render.light_red)
render.put_vline(screen, target_pixel_coord.x + halfcell, target_pixel_coord.y + cell_size + octcell, target_pixel_coord.y + cell_size + sevenoctcell, render.light_red)
x := scroll
loop if x > width break else {
2024-10-14 19:24:29 -05:00
render.put_vline(screen, x, 0, height, .(0, 127, 0, 127))
x += cell_size
}
y := scroll
loop if y > height break else {
2024-10-14 19:24:29 -05:00
render.put_hline(screen, y, 0, width, .(0, 127, 0, 127))
y += cell_size
}
2024-10-16 07:34:19 -05:00
render.put_hline(screen, seeker.y * cell_size + halfcell + scroll, 0, width, render.blue)
render.put_vline(screen, seeker.x * cell_size + halfcell + scroll, 0, height, render.blue)
2024-10-14 19:24:29 -05:00
render.sync(screen)
if seeker.x < target.x {
seeker.x += 1
} else if seeker.x > target.x {
seeker.x -= 1
} else if seeker.y < target.y {
seeker.y += 1
} else if seeker.y > target.y {
seeker.y -= 1
} else {
2024-10-25 10:37:38 -05:00
target = .(random.range(uint, 0, range.x), random.range(uint, 0, range.y))
}
scroll += 1
if scroll > cell_size {
scroll = 0
target += .(1, 1)
seeker += .(1, 1)
}
}
return
}