简体   繁体   中英

SFML Window closes by itself when mouse goes outside of window

My SFML application builds and runs as expected, but when let the mouse exit/enter the window on the left window-border, then it closes unexpectedly.

How can I make this error stop happening? I don't want the window to close unless I call it in the code.

Visual Studio 2019

SFML-2.5.1 (I think I downloaded the version: Visual C++ 15 (2017) - 32-bit)

Here's a bare minimum example of the code I'm running, that produces said error:

#include "SFML-2.5.1/include/SFML/Graphics.hpp"
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::endl;
using std::string;

sf::Vector2f window_size(800, 600);
sf::VideoMode video_mode(window_size.x, window_size.y);
sf::RenderWindow window(video_mode, "Hello");

int main() {
    while (window.isOpen()) {
        //Deal with input and update program
        sf::Event event;

        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed
            || event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
        }

        //Draw things on screen
        window.clear(sf::Color::White);
        //Draw things here, not nothing right now    
        window.display();
    }
}

sf::Event is organised as a tagged union, where the type member is the tag, and the active member depends on the tag.

The key member is only active when type is one of the keyboard event types ( sf::Event::KeyPressed or sf::Event::KeyReleased ).

Examining an inactive union member is undefined behaviour. In practice, what's happening is probably something like the following. Some other event occurs, with a member which is numerically equal to sf::Keyboard::Escape and which physically shares the space with key.code . The program is accessing that member and erroneously interpreting it as key.code .


Relevant documentation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM