class Eagle::App

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:

  1. #input for each new event (key presses, clicks, window events)
  2. #fixed_update zero or more times, at Config#fixed_fps
  3. #update once, with the time since the last frame
  4. #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.cr

Instance Method Summary

Instance Method Detail

def configure(config : Config) : Nil #

Adjust the configuration before the window opens. Runs before #load.


def draw(g : Graphics) : Nil #

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.


def fixed_update(dt : Float32) : Nil #

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.


def input(event : Event) : Nil #

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.


def load : Nil #

Called once after the window, GPU and audio are ready. Load assets and build your scene here.


def on_draw(&block : Graphics -> ) : self #

Adds a block that runs after #draw.


def on_fixed_update(&block : Float32 -> ) : self #

Adds a block that runs after each #fixed_update.


def on_input(&block : Event -> ) : self #

Adds a block that runs after #input for each event.


def on_load(&block : -> ) : self #

Adds a block that runs after #load. Returns self so calls can be chained.


def on_update(&block : Float32 -> ) : self #

Adds a block that runs after #update every frame.


def quit? : Bool #

Called when the player closes the window. Return false to keep running, for example to show an "unsaved changes" prompt.


def resize(width : Int32, height : Int32) : Nil #

Called when the window size changes, with the new size in logical pixels.


def unload : Nil #

Called once during shutdown. Save settings or high scores here.


def update(dt : Float32) : Nil #

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.