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

Author Topic: Closing window returns error.  (Read 3722 times)

0 Members and 1 Guest are viewing this topic.

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Closing window returns error.
« on: July 17, 2013, 01:43:12 am »
When closing the window, I get an error code: -1073741819. What is going on here and how do I fix it? (Win7, CodeBlocks12.11, SFML2.0)

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>

int main()
{
    enum Direction { Down, Left, Right, Up };

    sf::Vector2i screenDimensions(800,600);

    sf::RenderWindow window(sf::VideoMode(screenDimensions.x, screenDimensions.y), "SFML Game");

    float frameCounter = 0, switchFrame = 100, frameSpeed = 500;

    sf::Texture pTexture;
    sf::Sprite playerImage;
    sf::Clock clock;
    sf::Font font;
    sf::Music music;

    sf::Vector2i source(1, Down);

    if(!music.openFromFile("KingOfTheDesert.ogg"))
        std::cout << "Error: Could not locate music file." << std::endl;

    if(!font.loadFromFile("Gabriola.ttf"))
        std::cout << "Error: Could not locate the font file." << std::endl;

    if(!pTexture.loadFromFile("Player.png"))
        std::cout << "Error: Could not load player image." << std::endl;
    else
        playerImage.setTexture(pTexture);

    while (window.isOpen())
    {
        sf::Event Event;
        while (window.pollEvent(Event))
        {
            switch(Event.type)
            {
            case sf::Event::Closed:
                window.close();
            case sf::Event::KeyPressed:
                if(Event.key.code == sf::Keyboard::Up)
                    source.y = Direction::Up;
                else if(Event.key.code == sf::Keyboard::Down)
                    source.y = Direction::Down;
                else if(Event.key.code == sf::Keyboard::Left)
                    source.y = Direction::Left;
                else if(Event.key.code == sf::Keyboard::Right)
                    source.y = Direction::Right;
                if(Event.key.code == sf::Keyboard::P)
                    music.play();
                if(Event.key.code == sf::Keyboard::Escape)
                    window.close();
            }
        }

        source.x++;

        if(source.x * 32 >= pTexture.getSize().x)
            source.x = 0;

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            source.y = Up;
            playerImage.move(0, -10);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            source.y = Down;
            playerImage.move(0, 10);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            source.y = Right;
            playerImage.move(10, 0);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            source.y = Left;
            playerImage.move(-10, 0);
        }

        frameCounter += frameSpeed * clock.restart().asSeconds();
        if(frameCounter >= switchFrame)
        {
            frameCounter = 0;
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
                source.x = 0;
        }

        playerImage.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
        window.draw(playerImage);
        window.display();
        window.clear();
    }
    return 0;
}

 
« Last Edit: July 17, 2013, 02:07:54 am by andrewhopps »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Cannot close window with red X and escape returns error.
« Reply #1 on: July 17, 2013, 01:51:41 am »
switch(Event.type)
{
case sf::Event::KeyPressed:
    if(Event.type == sf::Event::Closed || Event.key.code == sf::Keyboard::Escape)
        window.close();
    // ...
}

This would translate into: When the event type is KeyPressed, then check whether the event type is Closed OR if the key code is Escape.
As you see the event type can never be KeyPressed AND Closed, thus when you click the X, nothing happens. ;)

As for why it crashes when you press Escape, I'm not really sure...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Cannot close window with red X and escape returns error.
« Reply #2 on: July 17, 2013, 02:06:26 am »
switch(Event.type)
{
case sf::Event::KeyPressed:
    if(Event.type == sf::Event::Closed || Event.key.code == sf::Keyboard::Escape)
        window.close();
    // ...
}

This would translate into: When the event type is KeyPressed, then check whether the event type is Closed OR if the key code is Escape.
As you see the event type can never be KeyPressed AND Closed, thus when you click the X, nothing happens. ;)

As for why it crashes when you press Escape, I'm not really sure...

Well I fixed the above and now both the escape key and the red X close the program. The error code still exists of course. *Code updated above*

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Closing window returns error.
« Reply #3 on: July 17, 2013, 09:39:00 pm »
Not sure if the same as your problem but I found with Code::Blocks after a run an app, if I 'X' close the console window thats open it gives me an error code like yours.  If i 'press any key' on the console to have it close that way it closes fine.  Codeblocks just doesn't like the console debug window being 'x' closed.

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Closing window returns error.
« Reply #4 on: July 18, 2013, 12:42:55 am »
While helping with another issue, Foaly provided me with a full main.cpp and it changed the window handling to as follows, and it works with no error!

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::P)
            {