SP1: hang in sp1_IterateSprChar()

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
derekfountain
Member
Posts: 50
Joined: Mon Mar 26, 2018 1:49 pm

SP1: hang in sp1_IterateSprChar()

Post by derekfountain »

I've been playing with sp1_IterateSprChar() in the context of updating colour in my sprite. I think I'm on top of it, but was puzzled by a hang I came across.

My iterator function, called via sp1_IterateSprChar() each time round the game loop, contains this:

Code: Select all

    if( ++c->attr == INK_WHITE )
      c->attr = INK_BLUE;
which cycles the colours each refresh. It works fine except when the sprite is stationary because the character cells aren't invalidated. OK, so I added:

Code: Select all

     sp1_InvUpdateStruct(c->update);
but that causes the program to hang.

I found the answer: use sp1_IterateUpdateSpr(), where the iterator function takes a struct sp1_update *u and the character cells can be invalidated with

Code: Select all

sp1_InvUpdateStruct(u);
So all is well, but is there a reason it's not allowed to invalidate the update struct from inside the sp1_IterateSprChar() function?
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

If you look at the sp1 header for struct sp1_cs:

Code: Select all

struct sp1_cs {                       // "char structs" - 24 bytes - Every sprite is broken into pieces fitting into a tile, each of which is described by one of these

   struct sp1_cs     *next_in_spr;    // +0  BIG ENDIAN ; next sprite char within same sprite in row major order (MSB = 0 if none)

   struct sp1_update *update;         // +2  BIG ENDIAN ; tile this sprite char currently occupies (MSB = 0 if none)
"update" is big endian and a leading 0 indicates the sprite character is not currently displayed.

So correct code might be something like this:

Code: Select all

#include <intrinsic.h>

if (*(unsigned char *p = (unsigned char *)c->update))
{
   // valid update struct
   sp1_InvUpdateStruct((void*)intrinsic_swap_endian_16((unsigned int)p));
}
derekfountain
Member
Posts: 50
Joined: Mon Mar 26, 2018 1:49 pm

Post by derekfountain »

That works, thanks.

I'd spotted the MSB of 0 meaning "not displayed". I took that check out of the example in my post for clarity. But I'd completely forgotten the Z80 is little endian. :o}
Post Reply