Skip to content

Draw once, check twice

  • Sinclair ZX Spectrum
  • Assembly

The boosted game does twice as much simulation work, but the player does not need a complete redraw of every falling object between those two steps. We will save that drawing work while preserving both contact decisions.

This checkpoint extends each object to seven bytes. Offsets 0..4 retain X, Y, kind, speed and drift. Offsets 5 and 6 remember the X/Y of the image currently on screen. Those can differ from the latest simulation position.

Source · view and edit
ZX Spectrum
Companion file: assets.inc

The include line reads this editable data when you assemble. Keep it beside your source for a local build.

Edit the source, then assemble.

Edits are saved in this browser. Keep a downloaded copy of work you want to keep.

Focus the Spectrum screen to use the keyboard, or hold these buttons. Assemble again for a fresh machine.

Run the program to inspect it. Editing source does not change the running machine.

boost_time
—
steps_left
—
frame_delta
—
active_count
—
ticks
—
elapsed
—
Read the active object records

These rows read the twenty-slot pool. Empty slots are omitted. The display samples RAM while the program runs, so values can change between observations.

SlotXYKind / activeSpeedDriftImage XImage Y

Give state and image separate positions

On the first boosted step, copy current X/Y into image X/Y and leave the old image in place. Advance the simulation and check contact. If the object survives, do not draw its intermediate position.

On the second step, erase using the saved image position, advance and check again, then draw the final position. We save one erase and one draw per surviving object per boosted update. At normal speed, a single step still erases and redraws normally.

meteor-storm.asm, excerpt
remember_image:
 ld a,(ix+0)
 ld (ix+5),a
 ld a,(ix+1)
 ld (ix+6),a
 ld a,(steps_left)
 cp 2
 jr z,image_ready
erase_old_image:
 push bc
 ld e,(ix+5)
 ld d,(ix+6)
 call draw_meteor
 pop bc

Open the active-record table and compare X/Y with Image X/Y. The inspector samples between operations, so it may catch a difference. That is useful state, not automatically an erasure fault.

Removal cannot wait for a step that may never happen

A star collected on the first step becomes inactive. The second step will skip it. Therefore its old image must be removed during that first step, before clearing kind. Otherwise an inactive star would remain visible forever.

A newly spawned object also needs its previous-image coordinates initialised: it has already been drawn at its spawn position before the next update begins. Spawn writes both coordinate pairs together.

Why not erase using the object's latest X/Y after both steps?

Show the explanation

Those coordinates describe where the object has moved, not where its old pixels remain. XOR at the new position would change the wrong pixels and leave the old image behind. Save the image position before changing the simulation state.

Compare a measured budget

Play the same course with Space held and compare frame_delta with the previous checkpoint. The native route test records occasional 4s before this change and only 1 or 2 afterwards; a starting difference of 1 is possible because a run can begin between the clock’s alternate-frame opportunities.

A browser inspector samples values and may miss a rare 4. Not seeing one in a short run is weaker evidence than recording every update. The native checks cover a full keyboard-driven passage and retain the measured differences. Both versions still check every course step.

The optimisation depends on the XOR contract: no unrelated drawing may overwrite the saved old image between its draw and erase. This is appropriate for our plain playfield and separate HUD. Adding scrolling scenery would require revisiting that assumption.

Sources

The maintained Meteor Storm source linked through this page supplies the game rules and data layout. Its experiments run original Z80 instructions on an emulated 48K PAL Spectrum; the game rules are design choices, not hardware properties.

Run it here

This unit's program, built for you.

Program
meteor-storm.tap
Machine
ZX Spectrum 48K (PAL)

Download meteor-storm.tap