Tilengine - The 2D retro graphics engine forum

Full Version: C# Animation Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm testing out Tilengine with the CS binding and when making a simple character with an animation I keep getting an Invalid SequencePack error. I will admit that I'm using a 3 year old video as a tutorial of sorts so it's possible something changed since then and I just don't know it. I'll post my code below:

Code:
static int Main(string[] args) {
engine = Engine.Init(512, 288, 11, 30, 30);
engine.SetBackgroundColor(new Color(0x00, 0xCC, 0xCC));
engine.LoadPath = "assets/Sprites/Terri";

Spriteset ss = Spriteset.FromFile("TerriIdle.png");
SequencePack sp = SequencePack.FromFile("TerriIdle.sqx");
Sequence idle = sp.Find("idle");

Sprite sprite = new Sprite();
sprite.Setup(ss, TileFlags.None);

sprite.SetPosition(160, 120);

Animation anim = new Animation();
anim.SetSpriteAnimation(0, idle, 0);

int frame = 0;
Window window = Window.Create(null, WindowFlags.Vsync);
while (window.Process()) {
window.DrawFrame(frame);
frame += 1;
}
return 0;
}
i,

Of course things change over time and something can break. Th C# binding has not been updated in a while but the main engine has continued evolving. One thing that has changed but that hasn't been translated to the binding is animation.

However I can see concept mistake in your code. You're creating Sprite and Animation objects, and then assign assets. You must not do. The engine itselfs creates indexed arrays of Sprite and Animation objects, you must request what one you want (but not create new ones).

For example you init the engine at 400x240 resolution, with 4 layers, 80 sprites and 16 animators:

Code:
Engine engine = Engine.Init(400, 240, 4,80, 16);

If you want to use sprite index 0 (they go from 0 to 79 in this example):

Code:
Sprite sprite = engine.Sprites[0];
sprite.Setup(ss, TileFlags.None);

Same happens with Animation, you don't create them, but use ones available in engine.Animations[] array.



Let me know,
(03-24-2021, 12:28 AM)megamarc Wrote: [ -> ]Same happens with Animation, you don't create them, but use ones available in engine.Animations[] array.

Same error it seems

I'm assuming this is what you mean?
Code:
Animation anim = engine.Animations[0];
anim.SetSpriteAnimation(0, idle, 0);
(03-24-2021, 03:54 AM)Midnight Syntax Wrote: [ -> ]
(03-24-2021, 12:28 AM)megamarc Wrote: [ -> ]Same happens with Animation, you don't create them, but use ones available in engine.Animations[] array.

Same error it seems

I'm assuming this is what you mean?
Code:
Animation anim = engine.Animations[0];
anim.SetSpriteAnimation(0, idle, 0);

Also the error is specifically happening on setSpriteAnimation, I realized I forgot to mention that.
Code-wise seems correct, but without the actual assets I can't gess what's wrong.

Can you please upload here the whole project (sources and assets) so I can have a look?
Here you go, I have a feeling it's my sqx or the other file that I'm defining the animation in
Hi again,

It may be some incompatibility between the current version of the engine and the binding, because there are some breaking changes to the animation API since the C# binding was last updated. As I don't have .netcore environment installed I couldn't run the program.

However I did a quick translate to C, and it works. There's a slight mistake in the sqx file, as animation frame starts at 0, sequence should be 0,1,2,3,4 instead of 1,2,3,4,5, but it only causes a minor glitch on each loop, otherwise it loads and displays correctly. Relative paths seem also correct.

Here is the translated C test, running at .\Testing\Testing\bin\Debug\netcoreapp3.1 to keep the same folder structure:

Code:
#include "Tilengine.h"

int main(int argc, char* argv)
{
    TLN_Init(427, 240, 11, 30, 30);
    
    TLN_SetLoadPath("assets/Sprites/Terri");
    TLN_Spriteset ss = TLN_LoadSpriteset("TerriIdle.png");
    TLN_SequencePack sp = TLN_LoadSequencePack("TerriIdle.sqx");
    TLN_Sequence idle = TLN_FindSequence(sp, "idle");

    int sprite = TLN_GetAvailableSprite();
    TLN_ConfigSprite(sprite, ss, 0);
    TLN_SetSpriteAnimation(sprite, idle, 0);

    int frame = 0;
    TLN_CreateWindow(NULL, 0);
    while (TLN_ProcessWindow()) {
        TLN_DrawFrame(frame);
        frame += 1;
    }
    return 0;
}

And the top left corner captured with GifCam, after correcting the sqx file
Okay I least feel better that I wasn't just dumb haha.

Do you know what the problem could be?
You're not dumb at all Smile 

I think the problem is: prior to release 2.8.0, function for sprite animation took 4 arguments:

Code:
bool TLN_SetSpriteAnimation (int anim_index, int nsprite, TLN_Sequence sequence, int loop);

Whereas for release 2.8.0, the first "anim_index" parameter has been removed, so the function is:

Code:
bool TLN_SetSpriteAnimation (int nsprite, TLN_Sequence sequence, int loop);

The C# binding still targets the old format, but you're using latest 2.9.0 release, so parameter order gets mismatched. It should be updated to the new API. The SetSpriteAnimation function should not belong to Animation class, but to Sprite class. That's the idea:

This method
Code:
Animation::SetSpriteAnimation(int spriteIndex, Sequence sequence, int loop)
{
    TLN_SetSpriteAnimation(index, spriteIndex, sequence.ptr, loop);
}

Should be transformed to:

Code:
Sprite::SetAnimation(Sequence sequence, int loop)
{
    TLN_SetSpriteAnimation(index, sequence.ptr, loop);
}
Success! Moving everything over works now! Thanks so much for the explanations Biggrin
Pages: 1 2