Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Thor2.0: Linking resource / texture to Animation  (Read 2493 times)

0 Members and 1 Guest are viewing this topic.

shadetree

  • Newbie
  • *
  • Posts: 5
    • View Profile
Thor2.0: Linking resource / texture to Animation
« on: February 26, 2015, 07:11:30 am »
Hello!

I've just got Thor 2.0 installed and found myself puzzled at how to point a sprite to it's texture.

Rough example,  previously...
sf::Image img;
img.loadfromfile("file.png");

//mask it

sf::Texture tex;
tex.loadfromimage(img);

sf::Sprite sprite;
sprite.setTexture(tex);

sf::intRect *rect;
sprite.setTextureRect(rect); //loop or do whatever  rect[i]
 


i'm not sure how to make sure those animations are over the correct texture.   If you had something like...
#define frames 2

sf::IntRect rect[] = {
{0,0,0,0},
{1,1,1,1},
{2,2,2,2}
};
        thor::ResourceKey<sf::Texture> key = thor::Resources::fromFile<sf::Texture>("image.jpg");

        thor::ResourceCache<sf::Texture> cache;  
        cache.acquire(key);

        std::shared_ptr<sf::Texture> texture = cache.acquire(key);  

        thor::FrameAnimation Idle;

        for (int c = 0; c <= frames; c++)
        {
                Idle.addFrame(1.f, rect[c]);
        }

        thor::Animator<sf::Sprite, std::string> animator;
        animator.addAnimation("Idle", Idle, sf::seconds(1.f));

.........

       animator.play(Idle);
 



So even though the documentation is really helpful,  my understanding of linking the resources to the sprites or FrameAnimations is fuzzy.   In theory,  the intRect's and texture are there,  now how do you tell the program which resource to use it's intRect's over?     


VS2013 Express
Windows 7 64bit OS
Latest version of SFML and Thor
« Last Edit: February 26, 2015, 09:37:11 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Thor2.0: Linking resource / texture to Animation
« Reply #1 on: February 26, 2015, 09:41:42 am »
You use a regular sprite and tell the animator to animate() said sprite. You might want to check out the animation example that comes with Thor.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

shadetree

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Thor2.0: Linking resource / texture to Animation
« Reply #2 on: February 26, 2015, 02:32:30 pm »
Thank you.   I've been thumbing through forums and docs while completely missing the examples folder.   Since I have your attention,  is this runtime error familar to you?   Everything should be linked correctly,  using the debug libs in debug mode etc.

Error:



Produced from the following @ animator.playAnimation();
#include <Windows.h>
#include <SFML\Graphics.hpp>
#include <Thor\Animations.hpp>
#include <Thor\Graphics.hpp>

#define frames 6

sf::IntRect rect[] = {
        { 83, 28, 40, 92 },
        { 133, 27, 41, 93 },
        { 183, 27, 41, 93 },
        { 235, 27, 38, 93 },
        { 283, 28, 38, 92 },
        { 332, 28, 39, 92 },
        { 381, 28, 40, 92 },
};


int WINAPI WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR lpCmdLine,
        int nCmdShow)
{



        sf::Vector2f Screen(600, 400);
        sf::RenderWindow window(sf::VideoMode(Screen.x, Screen.y), "Title");


        sf::Image img;
        img.loadFromFile("img.png");

        sf::Color color(159, 82, 159);
        img.createMaskFromColor(color);

        sf::Texture tex;
        tex.loadFromImage(img);

        sf::Sprite sprite;
        sprite.setTexture(tex);


        thor::FrameAnimation Idle;

        for (int c = 0; c <= frames; c++)
        {
                Idle.addFrame(1.f, rect[c]);
        }

        thor::Animator<sf::Sprite, std::string> animator;
        animator.addAnimation("idle", Idle, sf::seconds(1.f));


        sf::Clock frameclock;

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        }


                }

                animator.update(frameclock.restart());
                animator.playAnimation("Idle", true);

                animator.animate(sprite);

                window.draw(sprite);
                window.display();
                window.clear();

        }

}


I've seen a few posts here with other people having similar runtime errors but with different references.   One suggestion was to rebuild with Cmake,  check the examples box and test.   I've tried this as well.   It's some simple user error surely.
« Last Edit: February 26, 2015, 04:03:53 pm by shadetree »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Thor2.0: Linking resource / texture to Animation
« Reply #3 on: February 26, 2015, 02:50:36 pm »
Here on the forum you should use [code=cpp][/code] for posting C++ code.

Run it in debug mode and provide the callstack.

You should use forward slashes everywhere. Windows understands them just fine. (see your includes).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

shadetree

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Thor2.0: Linking resource / texture to Animation
« Reply #4 on: February 26, 2015, 04:03:25 pm »
I appreciate you fixing the code tags,  I thought they looked odd.   I've also updated the code above and will keep that in mind for the future.

I'm not sure what i'm looking for.   animator.playAnimation(...) makes the call, goes through alot of pointer magic,  and ends up at the playAnimation() function definition where it throws.   If I just run it without breakpoints,  the call stack is empty on the assertion.





//////////////////////////////


« Last Edit: February 26, 2015, 04:05:02 pm by shadetree »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Thor2.0: Linking resource / texture to Animation
« Reply #5 on: February 26, 2015, 04:48:56 pm »
I've never used Thor, but it looks like your problem is that you're mixing lowercase and capital letters.

animator.addAnimation("idle", Idle, sf::seconds(1.f));
animator.playAnimation("Idle", true);
 

"idle" vs "Idle"

shadetree

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Thor2.0: Linking resource / texture to Animation
« Reply #6 on: February 26, 2015, 05:01:48 pm »
I've never used Thor, but it looks like your problem is that you're mixing lowercase and capital letters.

animator.addAnimation("idle", Idle, sf::seconds(1.f));
animator.playAnimation("Idle", true);
 

"idle" vs "Idle"


good "eye"!  That was the cause of the assertion.   It surprisingly didn't enumerate through frames,  it was just a still frame.   At least now I can tinker and find out why,  thank you very much to the both of you.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Thor2.0: Linking resource / texture to Animation
« Reply #7 on: February 26, 2015, 05:17:14 pm »
Again, I've never used Thor so I could be off-base here, but my guess as to why it isn't enumerating through the frames is because you call

animator.playAnimation("Idle", true);

every step in the loop. I'm guessing that is resetting the animation back to the first frame. You probably only want to call it once, and then only call
animator.update(frameclock.restart());
animator.animate(sprite)
 

every step. Again, this is just a guess though because I don't know Thor and haven't looked at any of the documentation.
« Last Edit: February 26, 2015, 05:19:02 pm by Arcade »

shadetree

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Thor2.0: Linking resource / texture to Animation
« Reply #8 on: February 26, 2015, 05:32:05 pm »
Yup it sure was,  moved out of the main loop and now it's animated.   Thanks again for your time,  really rescued my afternoon.