2024-10-15 15:11:06 -05:00
|
|
|
.{Vec2} := @use("../../../../libraries/stn/src/lib.hb").math;
|
|
|
|
.{random} := @use("../../../../libraries/stn/src/lib.hb")
|
2024-10-14 19:24:29 -05:00
|
|
|
render := @use("../../../../libraries/render/src/lib.hb")
|
|
|
|
|
|
|
|
/* expected result:
|
2024-10-15 15:11:06 -05:00
|
|
|
the square example bounces around the screen */
|
2024-10-14 19:24:29 -05:00
|
|
|
|
|
|
|
example := fn(): void {
|
|
|
|
screen := render.init(true)
|
|
|
|
|
|
|
|
image := render.new_surface(341, 256)
|
2024-10-15 15:11:06 -05:00
|
|
|
vel := Vec2(int).(-1, -1)
|
2024-10-14 19:24:29 -05:00
|
|
|
pos := Vec2(int).(100, 100)
|
2024-10-15 15:11:06 -05:00
|
|
|
vel_inner := Vec2(int).(1, 1)
|
|
|
|
pos_inner := Vec2(int).(10, 10)
|
|
|
|
color := @as(render.Color, @intcast(random.range(int, 0, 0xFFFFFF)))
|
2024-10-14 19:24:29 -05:00
|
|
|
loop {
|
2024-10-15 15:11:06 -05:00
|
|
|
render.clear(image, render.black)
|
|
|
|
render.clear(screen, render.black)
|
|
|
|
|
|
|
|
render.put_filled_rect(image, pos_inner, .(100, 100), color)
|
|
|
|
render.put_rect(image, .(0, 0), .(image.width - 1, image.height - 1), render.white)
|
|
|
|
|
2024-10-14 19:24:29 -05:00
|
|
|
render.put_surface(screen, image, pos)
|
|
|
|
render.sync(screen)
|
2024-10-15 15:11:06 -05:00
|
|
|
|
|
|
|
if pos_inner.x == 0 | pos_inner.x == image.width - 100 {
|
|
|
|
vel_inner.x = -vel_inner.x
|
|
|
|
color = @as(render.Color, @intcast(random.range(int, 0, 0xFFFFFF)))
|
|
|
|
}
|
|
|
|
if pos_inner.y == 0 | pos_inner.y == image.height - 100 {
|
|
|
|
vel_inner.y = -vel_inner.y
|
|
|
|
color = @as(render.Color, @intcast(random.range(int, 0, 0xFFFFFF)))
|
|
|
|
}
|
2024-10-14 19:24:29 -05:00
|
|
|
|
|
|
|
if pos.x == 0 | pos.x == screen.width - image.width {
|
|
|
|
vel.x = -vel.x
|
|
|
|
}
|
|
|
|
if pos.y == 0 | pos.y == screen.height - image.height {
|
|
|
|
vel.y = -vel.y
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += vel
|
2024-10-15 15:11:06 -05:00
|
|
|
pos_inner += vel_inner
|
2024-10-14 19:24:29 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|