SP1 - Animation of background tiles: intriguing sentence...

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

SP1 - Animation of background tiles: intriguing sentence...

Post by jorgegv »

Morning...

reading this old post (seems to be from the days when SP1 was first released):

https://worldofspectrum.org/forums/disc ... edirect/p1

In the "What's new from SPLIB2" section, point 11 says:
11. Features have been added to quickly animate background tiles.
Since I'm developing features now for animated tiles (_not_ sprites) in RAGE1, I'd like to ask the original "Alcoholics Anonymous" poster what these features were, just in case there is some already existing infrastructure in place that I can use instead of developing my own animation algorithm.

My currently thoughts about animating btiles include just painting _what_ changes, only painting it _when_ it must change, and make a distnction to animate only pixels, only attrs, and both (for a given tile).

Thanks
J.
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by jorgegv »

OK, shame on me...

On page 2 of that discussion, I think I found the answer:

https://worldofspectrum.org/forums/disc ... ck-v3-0/p2
What is brand new in this program is the use of two new SP1 functions to quickly print background tiles to the screen:

Code: Select all

extern void  __LIB__   sp1_PutTiles(struct sp1_Rect *r, struct sp1_tp *src);
extern void  __LIB__   sp1_PutTilesInv(struct sp1_Rect *r, struct sp1_tp *src);
I guess I'll revisit my tile drawing code to see if I can easily use this functions instead of my own printing loops.
derekfountain
Member
Posts: 121
Joined: Mon Mar 26, 2018 1:49 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by derekfountain »

AA moved on to the Next and doesn't post here any more, which is a shame given how much of it he wrote and how much he knows. We have to manage without him. :)

There's a rather complicated looking example in the standard distribution:

z88dk/libsrc/sprites/software/sp1/spectrum/examples/ex6b.c

which I think uses this technique to animate some fans as background tiles.

I've never looked at this technique, so if you come up with a simpler example I'd be interested to hear about it.
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by jorgegv »

Mmmm doesn't look very complicated to me. He is using sp1_PrintString, which he says is slow, and at the beginning of the C file there is a comment which says:

Code: Select all

// ...We will
// be taking a look at background animations later on where
// we will discuss several better approaches using functions like
// sp1_PutTiles(), sp1_IterateUpdateRect() and sp1_IterateUpdateArr()
// and we will look at controlling animation rates using the
// 50/60Hz frame interrupt.
...which points to the functions I'm just interested in, but refers for later examples... and the 6b is the last example.

So I guess I'll do my own tests and report back :-)

Thanks for the pointer.
J.
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by jorgegv »

BTW, I also ordered a Next, will arrive this summer (hopefully). My intention is to port my engine to the Next, and I suppose I'll need to somehow bridge (or replace) SP1 with the Next's HW sprite engine...
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by Timmy »

I normally use two functions in sp1 when printing tiles:

1) sp1_PutTiles(), in case I need to update a lot of tiles, usually when a screen changes. For a screen of 32x24 tiles, this needs an array of 32x24x3 bytes. But this can be done very quickly. Example in z88dk github: Link

An example code of using a smaller rect {2, 2, 28, 20} and a smaller [28*20*3] array can be found in the map editor thread. (second post)

Each entry of this [w*h*3] array is populated with (tile number 0-255, 0, colour). But you can also populate it with (low byte of address, high byte of address, colour) if you don't want to use an UDG defined somewhere in memory (Note that I never have tested the latter. The byte order might be wrong.)

2) For small amount of tiles I use sp1_PrintAt(). This function needs a lot of parameters just to put one tile, so maybe this is a bit slow if you need to animate many tiles.

3) I never used the sp1_PrintString() from the ex6b.c. I think I'm just too lazy to learn the control codes. :p But this might be the fastest way to update several tiles at once.
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by Timmy »

Timmy wrote: Fri Apr 30, 2021 2:43 pm Each entry of this [w*h*3] array is populated with (tile number 0-255, 0, colour). But you can also populate it with (low byte of address, high byte of address, colour) if you don't want to use an UDG defined somewhere in memory (Note that I never have tested the latter. The byte order might be wrong.)
This is not true, I think I wrote down the wrong order. The order is (colour, tile number 0-255, 0). I hope this is correct now, but you might check the example instead in case I get this wrong again. :lol:
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by jorgegv »

Well, I'm using sp1_PrintAt() for the moment, and I can draw the screen quickly with lots of tiles, I don't notice anything uncomfortably slow when switching screens...

With my current routines, this means one sp1_PrintAt() call for each 8x8 pixel tile, but if I used sp1_PutTiles(), this would mean instead one call per _group_ of 8x8 tiles (I call them BTiles -for big tiles- in my engine). On an average screen, this would cut the number of calls by about 75-80%, since BTiles are typically 2x2, 2x3 or 3x3 chars.

I think I can change my internal BTile format to the one required by sp1_PutTiles without much effort, so i'll probably do it for better speed. My game loop is starting to run lots of things in each frame, so I'd better be efficient.

As I said, I'll report back.
J.
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by Timmy »

If you are using sp1_PrintAt() and it is fast enough for you, then maybe you don't need sp1_PutTiles(). I don't like using sp1_PutTiles() because it needs a very big array that I keep it in memory during the whole game. Setting up the big array costs time too, so if you are happy with sp1_PrintAt() then maybe you don't need to change.

Don't forget to invalidate those squares if you use these functions!
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by jorgegv »

I intend to use sp1_PutTiles with small rects, not big ones, and I have already this memory (with a different layout) already reserved for each of the tiles, so I think I can do it without too much trouble.

Regarding invalidating, yes, of course I'm using sp1_PrintAtInv :)
amateus
Member
Posts: 46
Joined: Fri Nov 15, 2019 9:13 am

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by amateus »

You may have missed this one: viewtopic.php?f=2&t=11459
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: SP1 - Animation of background tiles: intriguing sentence...

Post by jorgegv »

Mmmm... yes I remember having read it some weeks ago but I didn't remember.

I think it's a very clever optimization: instead of thinking of optimizing the destinations (i.e. reduce the number of calls to sp1_Print*, etc.), you are optimizing the source of the tiles. I think this could be _very_ much quicker if you have a number of identical animations running on.

It has one drawback, though: sp1_TileENtry only works for the single byte Tiles, but my engine uses the double byte only, as it is more generic (no limitation on the number of tiles).

Also, I guess in the end SP1 ends up having to copy the bytes to the screen for all animations, no matter they are the same or a different source... so may be you are not gaining so much in the end...mmm
Post Reply