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-19 13:45:41 -05:00
|
|
|
bouncing gradient square inside coloured bouncing box inside black screen */
|
2024-10-14 19:24:29 -05:00
|
|
|
|
|
|
|
example := fn(): void {
|
|
|
|
screen := render.init(true)
|
|
|
|
|
2024-10-15 15:43:23 -05:00
|
|
|
image := render.new_surface(screen.width / 3, screen.height / 3)
|
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:43:23 -05:00
|
|
|
side := image.width / 8
|
2024-10-15 15:11:06 -05:00
|
|
|
vel_inner := Vec2(int).(1, 1)
|
2024-10-15 15:43:23 -05:00
|
|
|
pos_inner := Vec2(int).((image.width - side) / 2, (image.height - side) / 2)
|
2024-10-19 09:54:19 -05:00
|
|
|
// workaround for compiler bug
|
|
|
|
color := render.Color.(0, 0, 0, 0)
|
|
|
|
color = random.range(render.Color, render.black, render.white)
|
|
|
|
target_color := color
|
2024-10-14 19:24:29 -05:00
|
|
|
loop {
|
2024-10-15 15:11:06 -05:00
|
|
|
render.clear(screen, render.black)
|
2024-10-15 15:43:23 -05:00
|
|
|
render.put_filled_rect(image, pos_inner, .(side, side), color)
|
2024-10-17 09:31:42 -05:00
|
|
|
render.put_rect(image, pos_inner, .(side, side), render.black)
|
|
|
|
render.put_rect(image, .(0, 0), .(image.width - 1, image.height - 1), color)
|
2024-10-15 15:11:06 -05:00
|
|
|
|
2024-10-19 09:54:19 -05:00
|
|
|
render.put_surface(screen, image, pos, false)
|
2024-10-17 09:31:42 -05:00
|
|
|
render.put_rect(image, pos_inner, .(side, side), color)
|
2024-10-14 19:24:29 -05:00
|
|
|
render.sync(screen)
|
2024-10-15 15:11:06 -05:00
|
|
|
|
2024-10-15 15:43:23 -05:00
|
|
|
if pos_inner.x == 0 | pos_inner.x == image.width - side {
|
2024-10-15 15:11:06 -05:00
|
|
|
vel_inner.x = -vel_inner.x
|
2024-10-19 09:54:19 -05:00
|
|
|
target_color = random.range(render.Color, render.black, render.white)
|
2024-10-15 15:11:06 -05:00
|
|
|
}
|
2024-10-15 15:43:23 -05:00
|
|
|
if pos_inner.y == 0 | pos_inner.y == image.height - side {
|
2024-10-15 15:11:06 -05:00
|
|
|
vel_inner.y = -vel_inner.y
|
2024-10-19 09:54:19 -05:00
|
|
|
target_color = random.range(render.Color, render.black, render.white)
|
2024-10-15 15:11:06 -05:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-19 09:54:19 -05:00
|
|
|
color += .(
|
|
|
|
@intcast(color.b < target_color.b) - @intcast(color.b > target_color.b),
|
|
|
|
@intcast(color.g < target_color.g) - @intcast(color.g > target_color.g),
|
|
|
|
@intcast(color.r < target_color.r) - @intcast(color.r > target_color.r),
|
|
|
|
0,
|
|
|
|
)
|
2024-10-14 19:24:29 -05:00
|
|
|
pos += vel
|
2024-10-15 15:11:06 -05:00
|
|
|
pos_inner += vel_inner
|
2024-10-14 19:24:29 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|