class
Eagle::App
- Eagle::App
- Reference
- Object
Overview
The base class for a game. Subclass it, override the hooks you need, and hand it to Eagle.run.
Every hook is optional. Eagle calls them in this order each frame:
#inputfor each new event (key presses, clicks, window events)#fixed_updatezero or more times, atConfig#fixed_fps#updateonce, with the time since the last frame#draw, after the scene tree has drawn itself
#load runs once, when the window and GPU are ready. Create textures, sounds and
nodes there, or pass the class (not an instance) to Eagle.run so instance-variable
initializers can create them too.
class Game < App
@pos = Vec2.new(100, 100)
@tex : Texture? = nil
def load : Nil
@tex = Texture.new(Image.circle(16, Color::YELLOW))
Input.map "left", Key::A, Key::Left
Input.map "right", Key::D, Key::Right
end
def update(dt : Float32) : Nil
@pos += v2(Input.axis("left", "right") * 200 * dt, 0)
Eagle.quit if Input.pressed?(Key::Escape)
end
def draw(g : Graphics) : Nil
@tex.try { |t| g.draw(t, @pos) }
g.print("fps #{Clock.fps.round}", 10, 10)
end
end
Eagle.run(Game, title: "My Game", width: 960, height: 540)
You can mix this immediate-mode style with the scene tree: add nodes to
SceneTree.root in #load and they update and draw themselves before your #draw.
For tiny programs, skip the subclass and use the block helpers:
Eagle.run(title: "Blocks") do |app|
app.on_draw { |g| g.circle(Window.center, 50) }
end
Defined in:
eagle/core/engine.crInstance Method Summary
-
#configure(config : Config) : Nil
Adjust the configuration before the window opens.
-
#draw(g : Graphics) : Nil
Called once per frame after the scene tree is drawn, so anything you draw here appears on top.
-
#fixed_update(dt : Float32) : Nil
Called at a fixed rate (
Config#fixed_fps), zero or more times per frame. -
#input(event : Event) : Nil
Receives every raw event before the scene tree sees it.
-
#load : Nil
Called once after the window, GPU and audio are ready.
-
#on_draw(&block : Graphics -> ) : self
Adds a block that runs after
#draw. -
#on_fixed_update(&block : Float32 -> ) : self
Adds a block that runs after each
#fixed_update. -
#on_input(&block : Event -> ) : self
Adds a block that runs after
#inputfor each event. -
#on_load(&block : -> ) : self
Adds a block that runs after
#load. -
#on_update(&block : Float32 -> ) : self
Adds a block that runs after
#updateevery frame. -
#quit? : Bool
Called when the player closes the window.
-
#resize(width : Int32, height : Int32) : Nil
Called when the window size changes, with the new size in logical pixels.
-
#unload : Nil
Called once during shutdown.
-
#update(dt : Float32) : Nil
Called once per frame.
Instance Method Detail
Called once per frame after the scene tree is drawn, so anything you draw here appears on top. g is the immediate-mode drawing context.
Called at a fixed rate (Config#fixed_fps), zero or more times per frame. dt is always
the same value. Put physics and anything that must be deterministic here.
Receives every raw event before the scene tree sees it. Set event.handled = true to stop
nodes from receiving it. For "is this key held?" checks, use Input instead.
Called once after the window, GPU and audio are ready. Load assets and build your scene here.
Adds a block that runs after #load. Returns self so calls can be chained.
Called when the player closes the window. Return false to keep running, for example to show an "unsaved changes" prompt.
Called when the window size changes, with the new size in logical pixels.
Called once per frame. dt is the time since the last frame in seconds, already scaled by Clock.scale.
Put movement, game rules and animation here, multiplying speeds by dt.