Batched and Map Rendering System

Let's have a look at the batched and map rendering system.

Batched rendering

Using systems automatically turns our game into a multi-threaded program. If we have more than one CPU core (most computers do) systems execute at the same time. This presents a problem with the context-based rendering we used in the previous lessons. Two systems writing to the console at the same time can produce bizarre results, sometimes displaying parts of the output from each system.

Bracket-lib could implement a locking system directly in the context, allowing it to be directly shared as a system resource, but this would complicate our program beyond reason. We’d need to lock (and remember to unlock) the context each time we use it. That degrades performance and makes for overly-complicated code.

Instead, bracket-lib offers a batching service. At any time and from any thread we can request a new draw batch by calling DrawBatch::new(). This creates a buffer of deferred rendering commands.

Our draw commands aren’t executed immediately; instead, they’re stored for presentation all at once. When we’re finished adding to a batch, we call draw_batch.submit(sort_order) to finalize it. The sort_order specifies the order in which the command batches are executed, with zero going first.

When all our batches are ready to render, we instruct bracket-lib to do so with a call to render_draw_buffer(ctx) in our tick() function. Now, extend our tick() function in main.rs to include this:

Get hands-on with 1400+ tech skills courses.