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

Author Topic: Program not stopping after return EXIT_SUCCESS  (Read 5059 times)

0 Members and 1 Guest are viewing this topic.

wojo1086

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Program not stopping after return EXIT_SUCCESS
« on: January 03, 2014, 06:42:13 pm »
I have a while loop testing to see if a window is open. In this while loop, I have a second while loop for events. Now, in this event loop, if escape is pressed, the window closes. Which means the first while loop is no longer applicable because the window isn't open anymore, which means it should hit the return EXIT_SUCCESS of the main loop. (See code below.)

What keeps happening is the window will close, but I keep having to go into task manager and kill the program because it is still running. I don't know what I'm supposed to do. Any help?

while (window.isOpen())
        {
                // handle events
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.key.code == sf::Keyboard::Escape)
                        {
                                window.close();
                                break;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                                int newX = p.tileX;
                                int newY = p.tileY;
                                colTest = level[newX + (newY * 10)];
                                if (colTest == 0)
                                {
                                        playerSprite.setTextureRect(sf::IntRect(576, 0, 64, 128));
                                }
                                else{
                                        p.walking(p.tileX, p.tileY, 0, window, playerSprite, map);
                                }
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))                               
                        {
                                int newX = p.tileX;
                                int newY = p.tileY + 2;
                                colTest = level[newX + (newY * 10)];
                                if (colTest == 0)
                                {
                                        playerSprite.setTextureRect(sf::IntRect(0, 0, 64, 128));
                                }
                                else{
                                        p.walking(p.tileX, p.tileY, 1, window, playerSprite, map);
                                }
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                                int newX = p.tileX - 1;
                                int newY = p.tileY + 1;
                                int colTest = level[newX + (newY * 10)];
                                if (colTest == 0)
                                {
                                        playerSprite.setTextureRect(sf::IntRect(192, 0, 64, 128));
                                }
                                else{
                                        p.walking(p.tileX, p.tileY, 2, window, playerSprite, map);
                                }
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                int newX = p.tileX + 1;
                                int newY = p.tileY + 1;
                                int colTest = level[newX + (newY * 10)];
                                if (colTest == 0)
                                {
                                        playerSprite.setTextureRect(sf::IntRect(384, 0, 64, 128));
                                }
                                else{
                                        p.walking(p.tileX, p.tileY, 3, window, playerSprite, map);
                                }
                        }
                }

                // draw the map
                window.clear();
                window.draw(map);
                window.draw(playerSprite);
                window.display();
               

        }


        return EXIT_SUCCESS;

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #1 on: January 03, 2014, 06:49:53 pm »
Could you post a complete, yet stripped example? Without all the other events but with all includes, the main function etc?

wojo1086

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #2 on: January 03, 2014, 07:02:06 pm »
Here is the core of the program. I took out a bunch of code that dealt with things like the opening logo sequence, a little character creation, all events dealing with key presses other than escape. Basically, I use the same window for everything, I'm just updating it with different things. For example, the logo sequence displays a picture, fades it to black, and then I keep that window, but display a "New Game/Load Game" option which takes mouse movement.

I'm not sure if this matters, but I changed the program from console to windows in the properties.

#include <SFML/Graphics.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <sstream>
#include <string>
#include <iostream>
#include <SFML/Window/Keyboard.hpp>

//Tilemap class-----------------------------------------------------------------------------------------------------------------------------------------------
class TileMap : public sf::Drawable, public sf::Transformable
{
public:

        bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
        {
                // load the tileset texture
                if (!m_tileset.loadFromFile("images/Tilemap-2.png"))
                        return false;

                // resize the vertex array to fit the level size
                m_vertices.setPrimitiveType(sf::Quads);
                m_vertices.resize(width * height * 4);

                // populate the vertex array, with one quad per tile
                for (unsigned int i = 0; i < width; ++i)
                for (unsigned int j = 0; j < height; ++j)
                {
                        // get the current tile number
                        int tileNumber = tiles[i + j * width];

                        // find its position in the tileset texture
                        int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                        int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                        // get a pointer to the current tile's quad
                        sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

                        // define its 4 corners
                        quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                        quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                        quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                        quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                        // define its 4 texture coordinates
                        quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                        quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                        quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                        quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
                }

                return true;
        }

private:

        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                // apply the transform
                states.transform *= getTransform();

                // apply the tileset texture
                states.texture = &m_tileset;

                // draw the vertex array
                target.draw(m_vertices, states);
        }

        sf::VertexArray m_vertices;
        sf::Texture m_tileset;
};

//Player Class----------------------------------------------------------------P-L-A-Y-E-R---C-L-A-S-S------------------------------------------------------------------------------
class Player
{
public:
        bool gender;
        bool step = false;
        float tileX = 1;
        float tileY = 1;
//Walking function--------------------------------------------------------------W-A-L-K-I-N-G---F-U-N-C-T-I-O-N----------------------------------------------------------
        void walking(float& x, float& y, int dir, sf::RenderWindow& window, sf::Sprite& playerSprite, TileMap lm)
        {
                switch (dir)
                {
                case 0:
                                for (int n = 0; n < 16; ++n)
                                {
                                        y -= .0625;
                                        if (step == false)
                                        {
                                                playerSprite.setTextureRect(sf::IntRect(640, 0, 64, 128));
                                                playerSprite.setPosition(x * 64, y * 64);
                                                window.clear();
                                                window.draw(lm);
                                                window.draw(playerSprite);
                                                window.display();
                                        }
                                        else{
                                                playerSprite.setTextureRect(sf::IntRect(704, 0, 64, 128));
                                                playerSprite.setPosition(x * 64, y * 64);
                                                window.clear();
                                                window.draw(lm);
                                                window.draw(playerSprite);
                                                window.display();
                                        }
                                }
                                playerSprite.setTextureRect(sf::IntRect(576, 0, 64, 128));
                                break;
                case 1:
                        for (int n = 0; n < 16; ++n)
                        {
                                y += .0625;
                                if (step == false)
                                {
                                        playerSprite.setTextureRect(sf::IntRect(64, 0, 64, 128));
                                        playerSprite.setPosition(x * 64, y * 64);
                                        window.clear();
                                        window.draw(lm);
                                        window.draw(playerSprite);
                                        window.display();
                                }
                                else{
                                        playerSprite.setTextureRect(sf::IntRect(128, 0, 64, 128));
                                        playerSprite.setPosition(x * 64, y * 64);
                                        window.clear();
                                        window.draw(lm);
                                        window.draw(playerSprite);
                                        window.display();
                                }
                        }
                        playerSprite.setTextureRect(sf::IntRect(0, 0, 64, 128));
                        break;
                case 2:
                        for (int n = 0; n < 16; ++n)
                        {
                                x -= .0625;
                                if (step == false)
                                {
                                        playerSprite.setTextureRect(sf::IntRect(256, 0, 64, 128));
                                        playerSprite.setPosition(x * 64, y * 64);
                                        window.clear();
                                        window.draw(lm);
                                        window.draw(playerSprite);
                                        window.display();
                                }
                                else{
                                        playerSprite.setTextureRect(sf::IntRect(320, 0, 64, 128));
                                        playerSprite.setPosition(x * 64, y * 64);
                                        window.clear();
                                        window.draw(lm);
                                        window.draw(playerSprite);
                                        window.display();
                                }
                        }
                        playerSprite.setTextureRect(sf::IntRect(192, 0, 64, 128));
                        break;
                case 3:
                        for (int n = 0; n < 16; ++n)
                        {
                                x += .0625;
                                if (step == false)
                                {
                                        playerSprite.setTextureRect(sf::IntRect(448, 0, 64, 128));
                                        playerSprite.setPosition(x * 64, y * 64);
                                        window.clear();
                                        window.draw(lm);
                                        window.draw(playerSprite);
                                        window.display();
                                }
                                else{
                                        playerSprite.setTextureRect(sf::IntRect(512, 0, 64, 128));
                                        playerSprite.setPosition(x * 64, y * 64);
                                        window.clear();
                                        window.draw(lm);
                                        window.draw(playerSprite);
                                        window.display();
                                }
                        }
                        playerSprite.setTextureRect(sf::IntRect(384, 0, 64, 128));
                        break;
                }
                if (step == true)
                {
                        step = false;
                }
                else{
                        step = true;
                }
        }
};

//--------------------------------------------------------------------------M-A-I-N---------------------------------------------------------------------------------------
int main()
{
        // create the window
        sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
        float desktopWidth = desktop.width;
        float desktopHeight = desktop.height;
        sf::RenderWindow window(sf::VideoMode(desktopWidth, desktopHeight, desktop.bitsPerPixel), "Game", sf::Style::Fullscreen);
        window.setFramerateLimit(60);






        //load sprites
        sf::Sprite playerSprite;
        sf::Texture charMapTex;
        if (!charMapTex.loadFromFile("images/character map64.png"))
                return false;
        playerSprite.setTexture(charMapTex);
       
        int colTest;
       
        playerSprite.setTextureRect(sf::IntRect(0, 0, 64, 128));
        playerSprite.setPosition(p.tileX * 64, p.tileY * 64);

        const int level[] =
        {
                2, 0, 0, 0, 0, 0, 0, 0, 0, 3,
                2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                7, 4, 4, 6, 1, 5, 4, 4, 4, 8,
        };

        // create the tilemap from the level definition
        TileMap map;
        if (!map.load("images/Tilemap-2.png", sf::Vector2u(64, 64), level, 10, 10))
                return -1;

        // run the main loop
        while (window.isOpen())
        {
                // handle events
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.key.code == sf::Keyboard::Escape)
                        {
                                window.close();
                                break;
                        }
                }

                // draw the map
                window.clear();
                window.draw(map);
                window.draw(playerSprite);
                window.display();      

        }


        return EXIT_SUCCESS;
       
}
 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Program not stopping after return EXIT_SUCCESS
« Reply #3 on: January 03, 2014, 07:12:08 pm »
Please read this post and edit your previous post correspondingly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

wojo1086

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #4 on: January 03, 2014, 08:06:37 pm »
I sincerely apologize for that long bit of code.  :-[

So I went through and was able to replicate the problem with a minimal amount of code.

#include <SFML/Graphics.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/Window/Keyboard.hpp>


//--------------------------------------------------------------------------M-A-I-N---------------------------------------------------------------------------------------
int main()
{
        sf::RenderWindow window(sf::VideoMode(300, 300, 32), "Test");

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.key.code == sf::Keyboard::Escape)
                        {
                                window.close();
                                break;
                        }
                }
                window.clear();
                window.display();      
        }
        return EXIT_SUCCESS;
}
 

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #5 on: January 03, 2014, 08:19:09 pm »
There seems to be a problem with your environment. The code is pretty simple and straighforward but I compiled it nontheless and it worked as intended :)

How do you compile and start the program?

wojo1086

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #6 on: January 03, 2014, 10:59:40 pm »
I use Visual Studio 2013 and all I do is hit the "Local Windows Debugger" option on the top toolbar.

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #7 on: January 03, 2014, 11:21:48 pm »
What happens if you run the program without the debugger?
The shortcut is CTRL+F5. Just F5 is run + debug.

wojo1086

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #8 on: January 03, 2014, 11:35:44 pm »
It starts right away, but when I hit escape, I still have to force close the program from the task manager. It's like it's still running in the background. I tried changing it back to console mode, but then the console window doesn't close.

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #9 on: January 04, 2014, 05:54:23 am »
change these lines:
Code: [Select]
            if (event.key.code == sf::Keyboard::Escape)
            {
                window.close();
                break;
            }
change them to:
Code: [Select]
            if (event.key.code == sf::Keyboard::Escape)
            {
                window.close();
                break;
return exit_success;
            }
and tell the problem
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10837
    • View Profile
    • development blog
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #10 on: January 04, 2014, 09:48:54 am »
change them to:
Code: [Select]
            if (event.key.code == sf::Keyboard::Escape)
            {
                window.close();
                break;
return exit_success;
            }
and tell the problem
If break gets called the return is never reached.

The only mistake in the code is that he doesn't check for event.type whether there was a keypress at all.

So if you run it in the debugger and press escape, then pause it via the debugger where does it pause?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wojo1086

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #11 on: January 04, 2014, 11:37:56 am »
1) Why is checking for event.type needed if checking for a key.code works?

2) I paused it after I hit escape and it doesn't show me where it paused. It appears all the code has ran, but is still running in the background. I took two screenshots of it. The first one is showing there is no little arrow where it shows where the program paused. The second one is showing a second tab that popped up after I paused it. I'm not sure if this is a problem or not.

I was wondering if the window had to be destroyed. Is that a thing? Destroying windows?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10837
    • View Profile
    • development blog
    • Email
AW: Re: Program not stopping after return EXIT_SUCCESS
« Reply #12 on: January 04, 2014, 11:54:09 am »
1) Why is checking for event.type needed if checking for a key.code works?
Because that's how SFML works - read the tutorial. ;)

2) I paused it after I hit escape and it doesn't show me where it paused. It appears all the code has ran, but is still running in the background. I took two screenshots of it. The first one is showing there is no little arrow where it shows where the program paused. The second one is showing a second tab that popped up after I paused it. I'm not sure if this is a problem or not.
This sounds like a VS issue, nothing to do with the code itself. Btw if you set a break point on the return statement, will it pause at that point?
Maybe try to clear the VS solution, restart VS and restart the PC, seems to help sometimes.

I was wondering if the window had to be destroyed. Is that a thing? Destroying windows?
SFML makes use of RAII, thus the window will automatically get destroyed once it runs out of scope (i.e. leaving main()).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Program not stopping after return EXIT_SUCCESS
« Reply #13 on: January 04, 2014, 12:36:40 pm »
Try the following
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
    window.close();
 

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: Program not stopping after return EXIT_SUCCESS
« Reply #14 on: January 04, 2014, 01:23:40 pm »
I'm sorry,
I don't remember to delete break
Code: [Select]

            if (event.key.code == sf::Keyboard::Escape)
            {
                window.close();
return exit_success;
            }
I'm sorry again
I think it must work know if it doesn't worked, try this:
Code: [Select]

            if (event.key.code == sf::Keyboard::Escape)
            {
                window.close();
exit(0);
            }
this must be worked now
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

 

anything