The entrypoint of the engine. It describes your game logic.
Implementors of this trait should hold the game state.
Coffee forces you to decouple your game state from your view and input state. While this might seem limiting at first, it helps you to keep mutability at bay and forces you to think about the architecture of your game.
Ideally, your game state should be an opaque type with a meaningful API with clear boundaries. External code (like draw code or input code) should rely on this API to do its job.
The view data of your game.
This type should hold all the assets and state necessary to render your game and UI.
The input data of your game.
For instance, you could start by simply using a
HashSet here to track
which keys are pressed at any given time.
const TICKS_PER_SECOND: u16
Defines how many times the
update function should be called per
A common value is
Defines the key that will be used to toggle the
debug view. Set it to
None if you want to disable it.
By default, it is set to
Create your game here.
You need to return your initial game state, view state, and input state.
It is recommended to load your game assets right here. You can use
load module to declaratively describe how to load your
assets and get a consistent loading screen for free!
Update your game state here.
TICKS_PER_SECOND constant defines how many times this function
will be called per second. This function may be called multiple times
per frame if it is necessary.
Notice that you are also allowed to access view and window data. This can be useful if your game state needs to know how much of the world is visible.
Draw your game here.
Check out the
graphics module to learn more about rendering in
This function will be called once per frame.
Process an input event and keep track of it in your
This function may be called multiple times during event processing,
By default, it does nothing.
fn on_close_request(&self, _input: &mut Self::Input) -> bool
Handle a close request from the operating system to the game window.
This function should return true to allow the game loop to end, otherwise false.
By default, it does nothing and returns true.
Input to let users interact with your game.
Right before an
update, input events will be processed and this
function will be called. This reduces latency when multiple updates need
to happen during a single frame.
update is needed during a frame, it will still be called once,
right after processing input events and before drawing. This allows you
to keep your view updated every frame in order to offer a smooth user
experience independently of the
You can access the GPU if, as a consequence of the interaction, you need to prepare some assets before rendering.
By default, it does nothing.
Implement this function to display debug information.
It is called after
draw once per frame when debug has been toggled
DEBUG_KEY. Anything you draw here will be on top. Debug
code is only called when compiling with
debug_assertions or the
debug feature enabled.
By default, it shows
Debug, which displays a brief summary about
game performance in the top left corner.
fn run(window_settings: WindowSettings) -> Result<()> where