derekfountain / zxwonkyonekey

A game for the ZX Spectrum, written in C
4 stars 1 forks source link

Border colour changes when music is on and he bounces off a wall #22

Closed derekfountain closed 5 years ago

derekfountain commented 5 years ago

Like everyone else, I'd play this game with the music off. But if I leave it on, the border colour changes when he bounces off a wall.

I quite like the effect, actually, so if I can work out what's causing it I might leave it doing it consistently. :)

derekfountain commented 5 years ago

The teleporter sound effect causes it to happen too.

derekfountain commented 5 years ago

This was because the background music player started by setting A to zero, which it then wrote out of port 0xfe, toggling the speaker bit every so often. The bottom 3 bits of the value are the border colour, so priming it with 0 meant the border went black.

z88dk keeps track of the port 0xfe status in a global called _GLOBAL_ZX_PORT_FE. All the library code which writes to that port updates that value, including the border setting code. My music player was setting the bottom 3 bits to zero without updating z88dk's global, so things got confused.

The fix was for the music player to prime the code which writes to that port from the _GLOBAL_ZX_PORT_FE location, thus picking up the currrent border colour and the speaker bit value. That stops the border changing and wierd things happening.

I also now write back the last value written to the the port into _GLOBAL_ZX_PORT_FE before exiting the music player function. It probably doesn't matter a great deal, but that's the correct thing to do.