funfriend/src/chatter_context.cr

64 lines
1.5 KiB
Crystal

class Funfriend::ChatterContext < Funfriend::WindowContext
getter renderer : TextRenderer
getter window_size : NamedTuple(width: Int32, height: Int32)
property timer : Float64
WINDOW_SIZE = {width: 256, height: 32}
DEFAULT_DURATION = 10.0
PADDING = 10
def initialize(text : String, position : NamedTuple(x: Int32, y: Int32), duration : Float64 = DEFAULT_DURATION)
sheet = FontMan.parse_bm(File.read "assets/fonts/SpaceMono.fnt")
position_data = FontMan.position_text(text, sheet)
@window_size = {
width: position_data[:width] + PADDING * 2,
height: position_data[:height] + PADDING * 2
}
super(
title: "??__FUNFRIEND__?? > CHATTER",
width: window_size[:width], height: window_size[:height],
transparent: false
)
# just for initialization, let OpenGL know this is the current context
window.make_context_current
@timer = duration
@renderer = TextRenderer.new(text, sheet, window_size[:width], window_size[:height])
window.position = {
x: position[:x] - window_size[:width]//2,
y: position[:y] - window_size[:height]//2,
}
end
def render(dt : Float64)
# let OpenGL draw to it
window.make_context_current
LibGL.clear_color(0.0, 0.0, 0.0, 1.0)
LibGL.clear(Buffer::Bit::Color)
renderer.render(dt)
end
def update(dt : Float64)
@timer = @timer - dt
if @timer <= 0.0
window.should_close
end
render(dt)
window.swap_buffers
end
def clean_up
renderer.clean_up
end
end