Wiki

Clone wiki

agtools / Tutorial 3d - Background Restore

Outline

ts3d.png

In the previous steps we covered the 3 main sprite formats and drawing functions for those. In all cases however the background got destroyed by the sprites. This step will show how to auto-repair the background.

All 3 sprite formats provide background-restore functionality by default. SLABS however do need some extra care - it will be covered later.

Changes:

The first thing we should do is inform the playfield definition that we intend to use background-restore functions. The playfield doesn't perform the restore operations by default, so we need to configure it to provide the correct internal layout.

typedef playfield_template
<
    /*tilesize=*/4,
    /*max_scroll=*/8192,
    /*vscroll=*/VScrollMode_LOOPBACK,
    /*hscroll=*/HScrollMode_NONE,
    /*restore=*/RestoreMode_PAGERESTORE
> playfield_t;

The key line is RestoreMode_PAGERESTORE (which normally defaults to RestoreMode_NONE)

Next we replace myworld.bgrestore_flush() with myworld.bgrestore() in the mainloop. Now instead of discarding the restore events collected from previous sprite drawing, it executes them and 'repaints the holes'.

Note: trying to execute the restore without the configured RestoreMode_PAGERESTORE or failing to flush or execute each frame will lead to corrupt display and/or crash.

The restore process is local to each playfield (i.e. buffer), and it must clear sprites after they have been drawn AND displayed (which means: just before the playfield is redrawn, for a double-buffered display).

The only other change we have made is to swap the asset for a larger sprite (15x15 increased to 25x23) and reduce the sprite count from 42 to 20. The restore operations do cost something, but less than the sprites themselves.

That's it. No other changes required. The sprites should now move across the background without causing damage. The process doesn't interfere with scrolling function.

Updated