Wipe Screen Follow-up

As with everything, the code can and should be improved for speed and size. As challenging it is sometimes for me to write the code, there s always room for improvement.

I was prompted to make the code more efficient by a reader to the blog to do this.

This is in reference to the clear screen effect I had posted in my last blog. Now, this effect is not a single clear screen effect that clears all pixels at once, this is an effect that goes from tile to tile, and blanks out the tile followed by clearing the sprites.

The original code looks like this.

void clearScreen()
{
screenScanX = 0;
screenScanY = 0;
//clear the screen tile by tile
for (screenCounter = 0; screenCounter < 150; ++ screenCounter)
{
if (screenScanX == 15)
{
screenScanX = 0;
++ screenScanY;
}
if (screenScanY == 10)
{
screenScanY = 0;
screenScanX = 0;
}
tiles[screenScanY * scrw + screenScanX]= 0;
tilepaint(screenScanX, screenScanY, screenScanX, screenScanY);
FRAME;
++ screenScanX;//move over a column
}

//clear the sprites
for (screenCounter = 0; screenCounter < 5; ++ screenCounter )
{
sprites[screenCounter].n = 129;
FRAME;
}
screenScanX = 0;
screenScanY = 0;
}

I rather like the silly effect, it clears the screen tile by tile and then wipes the sprites. However, there are several things that can be done to improve this code.

The first thing is that at the end of the code, I am clearing the screenScanX and screenScanY back to 0. This does not need to be done and is superfluous. The second item is that I am using a single loop as opposed to a nested loop. I carried this loop from another that I had done and didn’t even think of the code inefficiency, as this is aimed more toward speed and not as much toward size.

Now, if I wanted to use another method to clear the screen, I have to see what options are open to me to make the code smaller. The code without calling the clearScreen function compiles with 12230 bytes free.

The most efficient code is calling a straight CLS function, the code for that looks like.

void clearScreen()
{
TK_CLS;
}

The results look like this.

https://youtu.be/HluyXSiW_to

Now, this code is fast and small, however, I am not a fan of this. As all it does is blank out the screen. It weighs in around 3 bytes. I’m pretty sure it uses the Spectrum ROM to accomplish this task as it’s extremely small, meaning the only space it requires is calling the ROM and return address.

The compiled size weighs in at 12227 bytes free.

Now, my original screen wipe function looks like this when played.

https://youtu.be/_qI0yAc0_nE

It weighs in at 11974, bytes free or 256 Bytes used.

I prefer my original screen wipe, but there is a way to make this code smaller.

void clearScreen()
{
for (screenScanY = 0; screenScanY < 10; ++ screenScanY)
for (screenScanX = 0; screenScanX < 15; ++ screenScanX)
{
tiles[screenScanY * scrw + screenScanX]= 0;
tilepaint(screenScanX, screenScanY, screenScanX, screenScanY);
FRAME;
}

//clear the sprites
for (screenCounter = 0; screenCounter < 5; ++ screenCounter )
{
sprites[screenCounter].n = 129;
FRAME;
}
}

The effect looks the exact same and saves on memory, weighing in at 12015 bytes free, or 215 bytes used. This improvement gives us an entire 41 bytes extra that we can waste elsewhere.

With our nested loop, we are still calling the same functions.

tiles[screenScanY * scrw + screenScanX]= 0;

Sets the tiles in the tile array to 0. This does not affect the screen immediately. The tiles do need to get cleared out of the array.

tilepaint(screenScanX, screenScanY, screenScanX, screenScanY);

Here, we are painting the tiles block-by-block, to what we had set in the prior line, which is a 0 or blank tile.

FRAME;

Refreshes the drawing performed by the FASE engine.

These 3 lines are required to wipe the screen clear. However, the sprites remain on the screen, so we need to wipe those.

That is a separate loop.

for (screenCounter = 0; screenCounter < 5; ++ screenCounter )
{
sprites[screenCounter].n = 129;
FRAME;
}

We iterate through all the sprites, there are 5 of them (0 to 4).

sprites[screenCounter].n = 129;

Sets the sprite to 129, erasing them from the screen.

Any more efficiencies, please post your Code as a comment, and I will always give it a try.

Advertisements

Author: andydansby

I'm a hobbyist coder working with the ZX Spectrum. Living in New York state near the Syracuse area. I grew up in Virgina. The first computer my parents bought for me was a Timex Sinclair 2068.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s