SFML community forums

Help => General => Topic started by: arthur08 on May 25, 2014, 03:08:11 pm

Title: Problem in release mode
Post by: arthur08 on May 25, 2014, 03:08:11 pm
Hello everyone,

I am having a weird problem: i made a game where you play as a moving turret and you need to shoot randomly spawning enemies and everything works in debug mode but in release mode, there is one problem, i cannot shoot. It should shoot when there is a Mouse::Released event.
Can someone help me with this?

Using MinGW and netbeans

sorry for my bad english
Title: Re: Problem in release mode
Post by: Nexus on May 25, 2014, 03:43:46 pm
Usually, differences between release/debug mode may result of wrong configurations (linked libraries) or initializations that are not performed.

To help you further: read before posting (http://en.sfml-dev.org/forums/index.php?topic=5559.0)
Title: Re: Problem in release mode
Post by: arthur08 on May 25, 2014, 08:06:33 pm
Ok, checked my debug/release confguration, read the post, tried some things but it's still not working
Here is some info :
OS: Windows 8.1
Graphics card: NVIDIA GeForce GTX 670
SFML version: latest snapshot(i have compiled it)
Compiler: GCC(MinGW) 4.8.1

Note: the application doesn't crash, i just cannot shoot bullets in release mode, while i can in debug mode

The basic bullet code
#include <SFML/Graphics.hpp>
#include <list>

std::list<Bullet> bullets;
sf::Sprite sprite; //player-sprite

sf::RenderWindow window(sf::VideoMode(1200, 800), "Test", sf::Style::Titlebar | sf::Style::Close);

int main()
{


    while (window.isOpen())
    {

        sf::Event event;
        while (window.pollEvent(event))
        {


            switch (event.type)
            {

                case sf::Event::Closed:
                    window.close();
                    break;
                case sf::Event::MouseButtonReleased:
                    fire();
                    break;
            }


        }

        draw();
    }

}

void fire()
{

    sf::Vector2i mouse_pos = sf::Mouse::getPosition(window);
    sf::Vector2f curPos = sprite.getPosition();
    float angle = atan2(mouse_pos.y - curPos.y, mouse_pos.x - curPos.x) * (180 / PI);
    Bullet bullet(curPos, angle, 800.0f, 300.0f); //start-position, angle, start-speed, acceleration
    bullet.sprite.setFillColor(sf::Color::Red);

    bullets.push_back(bullet);

}

void draw()
{


    for (Bullet bullet : bullets)
    {

        bullet.Update(time);
        window->draw(bullet.sprite);


    }


}
 
Title: AW: Problem in release mode
Post by: eXpl0it3r on May 25, 2014, 08:15:20 pm
It might just be too fast thus it flies out of the screen view before it gets drawn.
Make your movement depending on the refresh rate or implement fixed time steps (google for more info).

In any case you could output positiin values to see where the billet is.
Title: Re: Problem in release mode
Post by: arthur08 on May 25, 2014, 08:18:10 pm
Ok thanks, but in my bullet class i multiply the speed with the elapsed time from the last frame in seconds, will try outputting position
Title: Re: Problem in release mode
Post by: Jesper Juhl on May 25, 2014, 08:46:52 pm
...
    for (Bullet bullet : bullets)
...
Don't you want a reference to the original Bullet here to update rather than a copy?
That is "for (Bullet& bullet : bullets)" or "for (auto& bullet : bullets)".
Title: Re: Problem in release mode
Post by: arthur08 on May 25, 2014, 08:52:29 pm
Ok i tried that but still not working, when i output the bullets position to the console, it seems that it only updates once in release mode while it stays updating in debug mode
Title: Re: Problem in release mode
Post by: arthur08 on May 25, 2014, 09:20:29 pm
Now i found this: my bullet moves way faster in release mode then in debug mode, so that it directly leaves the window and gets deleted out of the list. How is this possible???
Title: Re: Problem in release mode
Post by: Nexus on May 25, 2014, 09:23:47 pm
How is this possible???
What do you think does Debug mode do? ;)

The number of assertions and checks for debugging purposes don't come for free, they slow down your program. You should find an approach that doesn't rely on the real time, but rather a fixed time step (http://gafferongames.com/game-physics/fix-your-timestep/) that leads to deterministic and thus reproducible behavior.
Title: Re: Problem in release mode
Post by: Jesper Juhl on May 25, 2014, 09:26:21 pm
Of course things run faster in release mode - that's the whole point. Just like it'll run faster on some computers than others you must deal with this.
Sounds like you are not properly taking the time per frame into account.
I think this thread, and the links mentioned in it, will be helpful to you: http://en.sfml-dev.org/forums/index.php?topic=15175.msg107603#msg107603

Edit: as Nexus said, in debug mode you have assertions and all kinds of other checks enabled that slow you down. But you also don't have the optimizer of the compiler kick in (at least not in any meaningful way). Once all the debug checks go away and the optimizer gets to do its job you'll see orders of magnitude increases in speed.
Title: Re: Problem in release mode
Post by: arthur08 on May 27, 2014, 05:33:34 pm
And the problem disappeared...
Anyways, thanks for your help
And when moving i do: speed * time.asSeconds () so shouldn't the movement in release mode be as fast as in debug mode?