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

Author Topic: Problems with sf::Thread  (Read 4761 times)

0 Members and 1 Guest are viewing this topic.

marcsven

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problems with sf::Thread
« on: January 18, 2010, 06:18:13 pm »
Hi! :)
My name is Marcus and I've just started learning SFML. I've recently run in to a small problem that needs to be solved. I'm using AniSprite (http://www.sfml-dev.org/wiki/en/sources/anisprite) to do a little program with an animated character that can move around a bit. However it's not the graphics part that I have a problem with. When a specified key is pressed I want him to move in a direction and do the animation at the same time. Therefore I thought I could use the sf::Thread-function, but (sadly) it's not working like I want it to work. :(
Here's the code for main.cpp:
Code: [Select]
#include <SFML/Graphics.hpp>
#include "AniSprite.h"

using namespace std;

void Left(AniSprite Sprite)
{
    Sprite.Play(4, 8);
    Sprite.Update();
}

void Right(AniSprite Sprite)
{
    Sprite.Play(12, 0);
    Sprite.Update();
}

int main()
{
    sf::RenderWindow App(sf::VideoMode(1440, 900), "Test");

    sf::Image Image;
    if(!Image.LoadFromFile("character.png"))
    {
        return EXIT_FAILURE;
    }

    AniSprite Sprite(Image, 47, 64);
    Sprite.SetLoopSpeed(10);
    Sprite.SetPosition(0, 400);

    [b]sf::Thread Thread1(Left(Sprite));[/b]
    [b]sf::Thread Thread2(Right(Sprite));[/b]

    int Direction = 1;

    while(App.IsOpened())
    {
        sf::Event Event;
        while(App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
            {
                App.Close();
            }
        }

        float ElapsedTime = App.GetFrameTime();

        if (App.GetInput().IsKeyDown(sf::Key::Left))
        {
            Thread1.Launch();
            Sprite.Move(-100 * ElapsedTime, 0);
            Direction = 1;
        }
        else if(App.GetInput().IsKeyDown(sf::Key::Right))
        {
            Thread2.Launch();
            Sprite.Move(100 * ElapsedTime, 0);
            Direction = 0;
        }
        else if(Direction == 1)
        {
            Sprite.Play(4, 5);
        }
        else
        {
            Sprite.Play(12, 13);
        }

        App.Clear();

        Sprite.Update();

        App.Draw(Sprite);

        App.Display();
    }
    return EXIT_SUCCESS;
}

Error code (line 32 and 33):
Code: [Select]
error: invalid use of void expression
I've tried different parameters in the Left- and Right-functions like:
Code: [Select]
void Left(AniSprite Sprite(const sf::Image& Img, int frameW, int frameH))
But that'll just end me up with more error codes. :(
I'll appreciate all help I can get. :)

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Problems with sf::Thread
« Reply #1 on: January 18, 2010, 06:32:16 pm »
First of all, you could read the tutorials and see that what you are trying to do is impossible.
You can still do it, just not like that. You have to convert the parameter to void*, and pass the parameter differently.
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problems with sf::Thread
« Reply #2 on: January 18, 2010, 06:51:45 pm »
And threads are not the solution, you don't need them. Just call Update() in your main loop.
Laurent Gomila - SFML developer

marcsven

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problems with sf::Thread
« Reply #3 on: January 18, 2010, 08:32:08 pm »
I've tried using Play() and Update() in the main loop, but when I press the key the animation stops. I thought it was because it can't move and update the animation at the same time. :?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problems with sf::Thread
« Reply #4 on: January 18, 2010, 09:41:49 pm »
Look at the small example on the wiki page, it's not complicated to use (and without threads) ;)
Laurent Gomila - SFML developer

marcsven

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problems with sf::Thread
« Reply #5 on: January 19, 2010, 05:06:11 pm »
Look at this code and you'll maybe get my problem:
Code: [Select]
#include <SFML/Graphics.hpp>
#include "AniSprite.h"

using namespace std;

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "Test");

    sf::Image Image;
    if(!Image.LoadFromFile("character.png"))
    {
        return EXIT_FAILURE;
    }

    AniSprite Sprite(Image, 47, 64);
    Sprite.SetLoopSpeed(10);
    Sprite.SetPosition(0, 400);

    int Direction = 1;

    while(App.IsOpened())
    {
        sf::Event Event;
        while(App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
            {
                App.Close();
            }
        }

        float ElapsedTime = App.GetFrameTime();

        if (App.GetInput().IsKeyDown(sf::Key::Left))
        {
            Sprite.Play(12, 0);
            Sprite.Move(-100 * ElapsedTime, 0);
            Direction = 0;
        }
        else if(App.GetInput().IsKeyDown(sf::Key::Right))
        {
            Sprite.Play(4, 8);
            Sprite.Move(100 * ElapsedTime, 0);
            Direction = 1;
        }
        else if(Direction == 1)
        {
            Sprite.Play(4, 5);
        }
        else
        {
            Sprite.Play(12, 13);
        }

        App.Clear();

        Sprite.Update();

        App.Draw(Sprite);

        App.Display();
    }
    return EXIT_SUCCESS;
}

The animation won't update when the user is moving it. :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problems with sf::Thread
« Reply #6 on: January 19, 2010, 05:12:14 pm »
You call Play as long as the key is pressed, thus the animation is continuously restarted and you always see the same frame (maybe Play should rather be named Start).

You should only call Play once when you want to start a new animation. You can achieve this by checking if the new direction is different from the previous one, of by using the KeyPressed event.
Laurent Gomila - SFML developer

marcsven

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problems with sf::Thread
« Reply #7 on: January 19, 2010, 08:12:28 pm »
Thanks a lot, guys! The animation works great now! :D