pixelfaucet/examples/affine.cr

54 lines
1.2 KiB
Crystal
Raw Normal View History

2022-01-06 06:38:47 +01:00
require "../src/game"
require "../src/sprite"
require "../src/transform2d"
module PF
class Affine < Game
@bricks : Sprite
2022-01-17 20:52:13 +01:00
@top_left : Vector2(Int32) = Vector[0, 0]
2022-01-06 06:38:47 +01:00
@transform : Transform2d = Transform2d.new
@angle = 0.0
2022-01-07 06:06:51 +01:00
@size = 1.0
@zoom = 0.5
2022-01-06 06:38:47 +01:00
def initialize(*args, **kwargs)
super
@bricks = Sprite.new("./assets/bricks.png")
@bricks.convert(@screen)
2022-01-06 06:38:47 +01:00
end
def update(dt, event)
@angle += 1.0 * dt
@zoom, @size = -@zoom, @size.clamp(0.5..2.0) if @size > 2.0 || @size < 0.5
@size = @size + @zoom * dt
2022-01-06 06:38:47 +01:00
end
def draw
clear(50, 127, 200)
@transform
.reset
.translate(-(@bricks.size // 2))
.rotate(@angle)
2022-01-07 06:06:51 +01:00
.scale(@size)
2022-01-06 06:38:47 +01:00
.translate(viewport // 2)
b1, b2 = @transform.bounding_box(@bricks.size.x, @bricks.size.y).map(&.to_i)
@transform.invert
b1.y.upto(b2.y) do |y|
b1.x.upto(b2.x) do |x|
point = @transform.apply(x, y).to_i
2022-01-17 20:52:13 +01:00
if point >= @top_left && point < @bricks.size
2022-01-07 06:06:51 +01:00
draw_point(x.to_i, y.to_i, @bricks.peak(point))
2022-01-06 06:38:47 +01:00
end
end
end
end
end
end
game = PF::Affine.new(300, 200, 2)
game.run!