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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - fjanisze

Pages: [1]
1
General / Re: How to handle animations properly
« on: August 04, 2014, 05:53:13 pm »
I think i got your point guys, thanks for the feedback and if you have any other resource to share you're welcome :)

2
General / Re: How to handle animations properly
« on: August 04, 2014, 09:28:35 am »
Quote
Multithreading is not needed here -- moreover, it's a bad idea, since it increases the complexity unnecesarily. There is nothing that has to run in parallel in order to animate sprites. You should rather update the animation logic in every frame, dependent on the elapsed time (can be fixed time step).

Quote
Using a separate thread for this is an absolutely bizarre way of doing it.  At the very least you should be updating the sprite's position within your normal game loop (the same one where you draw it), if only because doing this in a separate thread gains you nothing but race conditions.

For this specific case is not rquired, but in other circumstances is needed. I use the same engine also for other projects and having the whole application blocked until an animation (which may take seconds) is ongoing is a bad idea (expecially when i have plenty of object rendered). For this reason, the drawing logic works on a separate thread.

Thanks all for your links, i will read them carefully.

3
General / How to handle animations properly
« on: August 03, 2014, 08:30:18 pm »
Hello folks,

Some time ago in order to learn how SFML works I wrote my personal version of the game 2048. Today i came back to my code with the idea of going some refactor.. Besides many issues, one the most problematic is how the animation are handled, i really think it may be a better way but i have no idea on how to proceed using SFML.

To animate a Sprite, my app every give time just change the position of the sprite object in order to emulate the motion, it happen in two different steps:

  • A thread runs in background and update the sprite positions continuously, the frequency of the update depends on the sprite 'speed', if i want a quick moving object the position update will happen very frequently
  • The main thread draws the sprite on the rendering window 60 times per seconds

The idea works, since the sprites are animated.. But the effect i think is very poor and my question to you is: How to animate efficiently sf::Sprite object in SFML? Is there any 'proper' way?

For reference:
A video of the game (if you dont want to build everything):
The quality of the video is pretty poor.
Repo for the game: https://github.com/fjanisze/2048.git
Repo for the animation engine only (with a banal demo to test the code): https://github.com/fjanisze/simple_animation_engine.git

Please do not comment the texture quality ecc, i know is everthing ugly :)

Thanks



4
Graphics / Re: sf::Font and sf::Text
« on: December 12, 2013, 12:40:53 pm »
I have an update about this issue, seems that the call to glFlush() need to be performed in the thread which change the string not in the loop which perform the draw/display sequence.

If after each setstring, the glFlush function is invoked everything works properly.

To summarize, in case of parallel threads i need to refresh the context with glFlush in the thread's where a modification of the SFML structure is done, in this case in the thread which modify the sf::Text.

 8)

5
Graphics / Re: sf::Font and sf::Text
« on: December 12, 2013, 11:25:31 am »
I will use the cpp tag next time :)

For a reference about lock_guard please have a look at http://en.cppreference.com/w/cpp/thread/lock_guard.

No matter where i put the glFlush the thing is not working and the string appearing.

What make me very confused is why if i remove the 200ms time point the first string is printed, is this related to the underlying opengl library or SFML?

About why the app is multithreaded instead of single threaded, well, for performance reason! The problem appear in a multithreaded economic simulator of about 10k LOC, here i'm trying just to identify the reason of the malfunction to apply the 'fix' in the real application.  :P

6
Graphics / Re: sf::Font and sf::Text
« on: December 12, 2013, 09:28:09 am »
I made a small change in the code, now the output is even more odd.. First of all, a mutex protect the sf::Text variable from data races, and now in the 'main' function i wait for some time before changing the string with setString:

class foo
{
    sf::Font font;
    sf::Text text;
    std::thread th;
    sf::RenderWindow window;
    std::mutex mt;
public:
    foo()
    {
        text.setCharacterSize(12);
        text.setStyle( sf::Text::Regular );
        text.setColor( sf::Color::Green );
        text.setPosition( sf::Vector2f( 0, 0 ) );
        //Run the thread
        std::thread myth( &foo::loop, this );
        th = std::move( myth );
    }
    ~foo()
    {
        th.join();
    }
    void loop()
    {
        window.create(sf::VideoMode(300, 100), "My window");
        if( !font.loadFromFile("consola.ttf") )
        {
            std::cerr<<"FONT LOAD ERROR\n";
            std::terminate();
        }
        text.setFont( font );

        while (window.isOpen())
        {
            window.clear(sf::Color::Black);
            sf::Event event;
            while (window.pollEvent(event))
            {
                // "close requested" event: we close the window
                if (event.type == sf::Event::Closed)
                    window.close();
            }
            glFlush();
            mt.lock();
            window.draw( text );
            window.display();
            mt.unlock();
            std::this_thread::sleep_for( std::chrono::milliseconds{ 50 } );
        }
    }
    void setstring( const std::string& str )
    {
        std::lock_guard< std::mutex > lock( mt );
        text.setString( str.c_str() );
    }
};

int main()
{
    foo myfoo;
    std::this_thread::sleep_for( std::chrono::milliseconds{ 200 } );
    //Change the string.
    myfoo.setstring("THIS IS A TEST");
    std::this_thread::sleep_for( std::chrono::milliseconds{ 2500 } );
    myfoo.setstring("DO NOT WORK ");
    std::this_thread::sleep_for( std::chrono::milliseconds{ 2500 } );
    return 0;
}

At first the instance of foo is created and the thread runs, after 200ms a call to sestring set a new string in the sf::Text variable, then the app wait 2500ms, in this time something should be displayed but only dark is visible, 'THIS IS A  TEST' is not even printed. after 2500ms the second string is printed, but without the 'K'

"DO NOT WOR ".

This is pretty strange to me, if i remove the first 200ms waiting point, the first string is properly printed, but the second one is still without the 'K'.

The glFlush call is right before the 'draw' and 'display' call.

Any idea on what may be wrong here?  ???

7
Graphics / Re: sf::Font and sf::Text
« on: December 12, 2013, 08:20:24 am »
I was looking for a similar post in the forum, but i get this error "Please try again. If you come back to this error screen, report the error to an administrator." when i look for something in the forum  :-[

Anyway, where should i put the call to glFlush()? I tried in several way with no success. Have a look at this crap code below:


class foo
{
    sf::Font font;
    sf::Text text;
    std::thread th;
    sf::RenderWindow window;
public:
    foo()
    {
        text.setString("THIS IS A PRINT");
        text.setCharacterSize(12);
        text.setStyle( sf::Text::Regular );
        text.setColor( sf::Color::Green );
        text.setPosition( sf::Vector2f( 0, 0 ) );
        //Run the thread
        std::thread myth( &foo::loop, this );
        th = std::move( myth );
    }
    ~foo()
    {
        th.join();
    }
    void loop()
    {
        window.create(sf::VideoMode(300, 100), "My window");
        if( !font.loadFromFile("consola.ttf") )
        {
            std::cerr<<"FONT LOAD ERROR\n";
            std::terminate();
        }
        text.setFont( font );

        while (window.isOpen())
        {
            window.clear(sf::Color::Black);
            sf::Event event;
            while (window.pollEvent(event))
            {
                // "close requested" event: we close the window
                if (event.type == sf::Event::Closed)
                    window.close();
            }

            window.draw( text );
            window.display();
            std::this_thread::sleep_for( std::chrono::milliseconds{ 100 } );
        }
    }
    void setstring( const std::string& str )
    {
        text.setString( str.c_str() );
    }
};

int main()
{
    foo myfoo;
    std::this_thread::sleep_for( std::chrono::milliseconds{ 1000 } );
    //Change the string.
    myfoo.setstring("NOW CHANGE THE STRING");
    return 0;
}

This look very bad but is sufficient to reproduce the problem, when i put the call to glFlush before the window.display the output is:

"THIS IS A PRINT"

and

"NOW CHANG  TH  STRING"

The 'E' for some reason is missing..

8
Graphics / sf::Font and sf::Text
« on: December 11, 2013, 02:59:08 pm »
Hello,

There's something i dont understand about how the sf::Font and sf::Text objects works, let's assume that we have two threads, A and B. The thread A load the font in a local variable and then enter a loop in which just draw a text, the text -sf::Text- we can safely assume that is a member variable of a class.

Something like this -pseudocode-:

class foo
{
    sf::Font font;
    sf::Text text;
    sf::RenderWindow window;
public:
    foo();
    void set_text( const string& text );
    void loop();
};

where:

foo::foo()
{
   text.setString("THIS IS A TEXT");
   //Set all the other stuff, like position, color ecc ecc
   std::thread run_me( &f , this ); //Run the the function 'loop' in a separate thread.
}

void foo::loop()
{
    //Load the font
    while( )
    {
        //Bla bla
        window.draw( text );
        //bla bla
        //Wait for a while, this_thread::sleep_for
    }
}

Then we have:

int main()
{
    foo f; //the thread is spawned and 'loop' is running, the window shows 'THIS IS A TEXT'
    //Sleep for 1 sec
    f.set_text("THIS NOT WORK");
   //Continue showing the text
}

Let assume that all the missing code is present, the class 'foo' is thread safe and no dangerous operation are done, the question is how is it possible that the second string printed is not THIS NOT WORK but: "THIS   T".

It looks like the character which are printed in the second case are the one in common with the first string already printed, but no other. Why?

If the first text printed is ABC and i want to print in the second BCD then only BC is printed..

Thanks

Thanks

Pages: [1]
anything