[HOWTO] Setting colours on sp1 sprites

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

[HOWTO] Setting colours on sp1 sprites

Post by Timmy »

I've seen a lot of comments lately asking about how to set colours on sprites, so I've write a simple tutorial on it.

1) In your code, add a function sprite_setcolour:

Code: Select all

uchar sprite_attr;
uchar sprite_amask;

void sprite_setcolour(struct sp1_cs *c)
{
   c->attr_mask = sprite_amask;       // set the sprite character's attribute mask
   c->attr = sprite_attr;             // set the sprite character's attribute
}
2) Call it like this:

Code: Select all

	// Setting colour of the player here
	
	sprite_attr  = 7 ;                     // white ink, black paper (paper colour will be masked by amask)
	sprite_amask = 0xF8;                 // mask will keep everything except INK 
	sp1_IterateSprChar(sprtbl[0].s, sprite_setcolour);    // colour the sprite
There are probably better ways but I haven't research on it.
This code is inspired by ex4a in github https://github.com/z88dk/z88dk/blob/bd1 ... les/ex4a.c but may be obsolete.

NOTE: I only tested this on old lib in the old lib on sccz80. It will probably work on the sdcc versions or the new lib but I haven't tested that.

B) There is an alternative way to set colours for multiple sprites at once, as mentioned in https://www.z88dk.org/forum/viewtopic.php?f=2&t=3209.
I have not looked into it, most of my games only need about 5 sprites and they don't always need colours.

C) You can also not set colours at all and use the background colour of the tiles instead.
Much easier and saves extra code.
derekfountain
Member
Posts: 121
Joined: Mon Mar 26, 2018 1:49 pm

Re: [HOWTO] Setting colours on sp1 sprites

Post by derekfountain »

Ah yes, that broken interface where you have to pass the iterator function its parameters via global variables. :D

If I may be so bold, and for those who might end up here via a search, I'll plug my getting started tutorial which covers this point:

https://github.com/z88dk/z88dk/blob/mas ... ing-colour
derekfountain
Member
Posts: 121
Joined: Mon Mar 26, 2018 1:49 pm

Re: [HOWTO] Setting colours on sp1 sprites

Post by derekfountain »

I've not seen that alternative approach you link to, so I spent some time experimenting with it. Example code for anyone interested:

https://github.com/derekfountain/z88dk- ... r_faster.c
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: [HOWTO] Setting colours on sp1 sprites

Post by Timmy »

Yes, it's not new information. I used this technique in Cherry, 10+ years ago.

I'm only reposting it because it seems this information was lost in the time between.
Post Reply