PCEngineFans.com - The PC Engine and TurboGrafx-16 Community Forum
Tech and Homebrew => Turbo/PCE Game/Tool Development => Topic started by: DarkKobold on December 21, 2016, 08:45:10 AM
-
Difficulty - No "Assembly is better"
In our final level, we are using a lot of animations. Big sprites moving around. Lots of combined animations to step through. However, since there is no player input, we don't need to care too much about CPU usage, more just code size.
In reality, I was just curious what people thought of the two techniques I'm alternating between.
#1
char animstate, animtimer;
animtimer = 30;
for (i=0; i < 120; i++)
{
animtimer--;
if (!animtimer)
{ if (!animstate)
{ do stuffs(0)
animstate = 1;
}
else
{
do stuffs(1)
animstate = 0;
}
animtimer = 30;
}
do other stuffs
vsync();
}
#2
for (i=0; i < 120; i++)
{
if (!(i%60))
{
do stuffs(1)
}
else if (!(i%30))
{
do stuffs(0)
}
do other stuffs
vsync();
}
I really like #2, because its super fast to write, and doesn't require a whole lot of extra variables to keep track of. I have this feeling that (!(i%X)) generates nasty assembly code.
-
For the !(i%60) part, I think you can do:
if(i== 60 || i==0)
And for !(i%30)
if(i==90 || i==30 )
Because of your else if, cases of 60 and 0 won't trigger on the i%30 part. And 120 is never a case because the loop maxes at 119.
-
Approach 1 can be simplified assuming stuffs() is indeed a single function that takes in a variable:
char animstate, animtimer;
for (i=0, animtimer = 30; i < 120; i++,animtimer--)
{
if (!animtimer)
{
dostuffs(animstate);
animstate ^= 1; /* this will toggle animstate every 30 frames, like you want */
animtimer = 30;
}
do other stuffs
vsync();
}
you might not be allowed to do multiple variables in a forloop in HuC. I can't remember if it's possible.
If it doesn't let you, just move animtimer back out of the loop to define it, and decrement it at the start of the loop.
Approach #2 is no longer relevant, because Approach #1 is now easier to write, and I think will also be faster. I am not 100% sure on the speed without seeing what HuC actually does for the two.
Using what Tom said for your second approach is probably the best idea unless you want to setup a jump table instead.