Comfy 0.2 release notes: context objects versus globals in Rust

Comfy 0.2 landed on 15 October 2023, three weeks after the first public release of the open-source 2D Rust engine. It is a small release by line count and a large one by philosophy. The headline change is that the EngineContext parameter, which almost every function in a Comfy game used to receive, is no longer something game code has to pass around. Its contents moved to global functions. There are also better defaults for integrated GPUs and a long list of bug fixes.
I want to use this release as an excuse to look at a question every engine author meets sooner or later: should game code get its engine services through an explicit context object, or through globals? Comfy has picked a side. It is worth understanding why, and what it costs.
What changed
In 0.1, a typical function took c: &mut EngineContext and read everything through it. In 0.2, the fields became free functions. The release notes give the mapping, and the main entries are:
| 0.1 | 0.2 |
|---|---|
c.delta |
delta() |
c.world() |
world() |
c.commands() |
commands() |
c.cooldowns() |
cooldowns() |
c.mouse_world |
mouse_world() |
c.egui |
egui() |
c.config |
game_config() and game_config_mut() |
A few internal handles that were never really public got global accessors too. The maintainers say the engine still uses a context internally for some things, but game code should not need it.
The motivation is stated plainly. The most common question from new users was why there were two context types, a GameContext and an EngineContext, and why either was needed at all. Many of the values in EngineContext were already globals that the context simply re-exported. Moving the rest made the model consistent rather than half and half.
One detail I liked: egui() is cheap. The egui context is already a shared, internally synchronised handle, so the accessor just hands back a static reference. There is no hidden cost to calling it in every draw function.
Two ways to reach the engine
Here is the explicit style, simplified. This is illustrative code, not Comfy's real types:
// Explicit context: every dependency is visible in the signature.
struct Ctx { dt: f32, mouse: (f32, f32) }
fn update_turret(ctx: &Ctx, turret: &mut Turret) {
let dx = ctx.mouse.0 - turret.x;
let dy = ctx.mouse.1 - turret.y;
turret.angle = dy.atan2(dx);
turret.cooldown -= ctx.dt;
}
struct Turret { x: f32, y: f32, angle: f32, cooldown: f32 }
And the global style, again illustrative, with the engine owning the state behind accessor functions:
use std::cell::Cell;
thread_local! {
static DT: Cell<f32> = Cell::new(0.0);
}
fn delta() -> f32 { DT.with(|d| d.get()) }
fn update_turret(turret: &mut Turret) {
turret.cooldown -= delta();
}
The second version uses a thread-local cell because it is the plainest safe way to model engine-owned state in std Rust. A real engine might use a static with a lock or a once-initialised value; the shape for the caller is the same.
The case for a context object
Explicit contexts have real strengths, and it would be unfair to skip them.
- Visible dependencies. A function signature tells you what it touches.
fn update(ctx: &Ctx)can read the clock;fn update()could be reading anything. - Testing. You can build a fake context with a fixed delta and a scripted mouse position and call your function in a unit test. Globals make that harder, because you have to set up and tear down shared state.
- Multiple instances. If you ever want two simulations at once, for a replay viewer or a server running several matches, a context is easy to duplicate. A global is, by definition, one.
- The borrow checker helps you. With
&mut Ctx, the compiler stops two parts of the code mutating the same thing at once.
The case for globals
The arguments on the other side are mostly about friction, and friction matters a lot in game code.
- No threading of parameters. Gameplay code is a deep tree of small helpers. If the leaf function that spawns a hit spark needs the command buffer, every function above it needs the context too. With globals, the leaf just calls
commands(). - Fewer borrow fights. Holding
&mut EngineContextwhile also wanting a field of it in a nested call is a classic way to make the borrow checker unhappy. Separate accessors each borrow only what they return. - There is only one of everything anyway. A small 2D game has one window, one clock, one audio device and one ECS world. The multiple-instance argument is real in theory and rare in practice for this kind of project.
- Less to learn. A newcomer does not need to understand the context hierarchy before drawing a sprite.
Comfy's maintainers say openly that some ideas the wider Rust community would call bad turned out to be big productivity wins in their own use, and that they would rather decide from building games than from debate. I do not read that as dismissing the objections. It is a statement about which costs they are willing to pay.
A rough cost estimate
Numbers make the friction argument concrete. Take a modest game with about 150 gameplay functions. Suppose 60 of them need at least one engine service, and those sit an average of three calls deep from the top-level update. In the context style, that is on the order of 60 times 3, or roughly 180 signatures carrying a ctx parameter that most of them only forward. Every refactor that moves a call deeper adds more.
None of that is hard work. It is just noise, and noise is what makes a codebase feel heavy at month four of a project. The testing argument, on the other hand, is mostly about pure logic (damage formulas, pathfinding, inventory rules), and that code does not usually need the engine at all. Keep it in plain functions that take plain data and you can test it whichever style the engine uses.
The game state is still yours. The release notes suggest that most projects will re-export things like the delta time into their own game state struct anyway, which is a sensible middle path: engine services are global, your own data is explicit.
Bloom, aberration and integrated GPUs
The other notable change is about defaults. In 0.1, bloom was on out of the box, and the built-in bloom performs 20 blur passes. That is fine on a desktop card and painful on older integrated graphics, where several users reported trouble. In 0.2 bloom is off by default. You turn it back on through the config:
// From the release notes.
game_config_mut().bloom_enabled = true;
A quick sanity check on why 20 passes hurts: at 1920 by 1080, a full-screen blur pass touches about 2 million pixels. Even at reduced resolution for most passes, 20 of them adds up to tens of millions of texture reads per frame, which is a large share of what a low-end integrated GPU can do in 16.7 milliseconds. Opt-in is the right default.
Chromatic aberration, which was also on by default and never documented, is switched off as well. A new example shows how to configure bloom properly. A smaller change: GameConfig is no longer Copy, which should not affect anyone since it already lived behind a RefCell.
Asset loading and benchmarks
Comfy already includes a parallel, asynchronous asset loader. The maintainers say its API is going to change, so it remains mostly undocumented. They also started a work-in-progress benchmark suite with an asset loading test and a rough sprite comparison against other engines. By their own measurement, Comfy is behind Bevy's sprite sample at this point, and they state an aim of catching up in a later release. They are equally clear that being the fastest renderer is not the goal; being fast enough that you never worry while writing gameplay code is.
What to watch next
The release notes say the next version will clean up the global namespace, which currently exposes a lot of names. That is the honest downside of the globals approach: everything is reachable from everywhere, so the public surface needs careful curation. If you are trying 0.2, a useful exercise is to port one small 0.1 example by hand using the mapping table above, then look at which helper functions lost their parameter and which ones still deserve one. The features page and the book cover the rest of the API.
Filed in Rust Gamedev · Engines & Architecture

Written by Tendai Mukwena
Tendai writes about architecture: game loops, entity component systems, collision, pathfinding and procedural generation, usually with a profiler open. He works on simulation tools by day and builds roguelikes at night in Harare.


