C++ SFML窗口立即关闭

C++ SFML窗口立即关闭,c++,sfml,C++,Sfml,我正在学习如何在sfml中创建按钮的教程,当我运行它时,窗口立即关闭。我有两个文件,一个名为main.cpp,另一个名为button.h。我在internet上找不到任何修复程序来修复此问题,因此如何修复它? 下面是main.cpp: #include <SFML/Graphics.hpp> #include <iostream> #include <windows.h> #include "button.h" using namespac

我正在学习如何在sfml中创建按钮的教程,当我运行它时,窗口立即关闭。我有两个文件,一个名为main.cpp,另一个名为button.h。我在internet上找不到任何修复程序来修复此问题,因此如何修复它? 下面是main.cpp:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <windows.h>
#include "button.h"
using namespace std;
using namespace sf;
int main(){
    bool isStartOpen = false;
    RenderWindow window(VideoMode(500, 500), "Operating System", Style::Close | Style::Resize);
    RectangleShape player(Vector2f(500.0f, 30.0f));
    RectangleShape startMenu(Vector2f(300.0f, 400.0f));
    Texture startTexture;
    if(!startTexture.loadFromFile("startButton.png")){
        MessageBox(NULL, "Error loading image file: startButton.png in the system", "Image File Error", MB_OK | MB_ICONERROR);
    }
    startMenu.setFillColor(Color(0, 0, 0, 200));
    startMenu.setPosition(0.0f, 500.0f);
    player.setFillColor(Color::Black);
    player.setPosition(0.0f, 473.0f);
    Font font;
    if(!font.loadFromFile("buttonFont.ttf")){
       MessageBox(NULL, "Error loading font file: buttonFont.ttf in the system", "Font File Error", MB_OK | MB_ICONERROR);
    }
    Button openNotepad("Open Notepad", {200, 50}, 20, Color::Green, Color::Black);
    openNotepad.setPosition({100, 300});
    openNotepad.setFont(font);
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            switch(event.type){
                case Event::Closed:
                    window.close();
                    cout << "Window has been removed" << endl;
                    break;
                case Event::MouseMoved:
                    if(openNotepad.isMouseOver(window)){
                      openNotepad.setBackColor(Color::White);
                    }else{
                        openNotepad.setBackColor(Color::Green);
                    }
                    break;
                case Event::MouseButtonPressed:
                    if(openNotepad.isMouseOver(window)){
                        cout << "You clicked the button" << endl;
                    }
                    break;
            }
        if(Keyboard::isKeyPressed(Keyboard::Key::S) && !isStartOpen){
            startMenu.setPosition(0.0f, 75.0f);
            isStartOpen = true;
        }else{
            startMenu.setPosition(0.0f, 500.0f);
            isStartOpen = false;
        }
        window.draw(startMenu);
        window.draw(player);
        window.display();
        window.clear(Color(1, 159, 255));
    }
    return 0;
}
}
#包括
#包括
#包括
#包括“button.h”
使用名称空间std;
使用名称空间sf;
int main(){
bool-isStartOpen=false;
RenderWindow窗口(视频模式(500500),“操作系统”,样式::关闭|样式::调整大小);
长方形播放器(矢量2F(500.0f,30.0f));
矩形起始菜单(矢量2F(300.0f,400.0f));
纹理开始纹理;
如果(!startTexture.loadFromFile(“startButton.png”)){
MessageBox(NULL,“在系统中加载图像文件:startButton.png时出错”,“图像文件错误”,MB|U OK | MB|U ICONERROR);
}
startMenu.setFillColor(颜色(0,0,0,200));
开始菜单设置位置(0.0f、500.0f);
player.setFillColor(颜色:黑色);
播放器设置位置(0.0f,473.0f);
字体;
如果(!font.loadFromFile(“buttonFont.ttf”)){
MessageBox(NULL,“在系统中加载字体文件buttonFont.ttf时出错”,“字体文件错误”,MB|U OK | MB|U ICONERROR);
}
按钮openNotepad(“openNotepad”,{200,50},20,颜色::绿色,颜色::黑色);
setPosition({100300});
openNotepad.setFont(字体);
while(window.isOpen())
{
事件;
while(window.pollEvent(事件))
{
开关(事件类型){
案例事件::已结束:
window.close();

您可以将
返回0;
放入
while(window.isOpen())
,因此您的程序会立即关闭窗口,将
返回0;
放在
while循环之外

也许您遇到了崩溃?您是否尝试在调试器中单步调试您的程序以查看真正发生的情况?今天的教训:注意缩进,并保持其一致性。并且始终查看大括号。一个好的编辑器可以帮助您重新编写代码,使您的问题更易于查看。感谢您的帮助,现在我可以继续编写:)
#pragma once
#include <iostream>
#include <SFML/graphics.hpp>
using namespace sf;
using namespace std;
class Button{
    public:
        Button(){

        }
        Button(string t, Vector2f size, int charSize, Color bgColor, Color textColor){
            text.setString(t);
            text.setColor(textColor);
            text.setCharacterSize(charSize);
            button.setSize(size);
            button.setFillColor(bgColor);
        }
        void setFont(Font &font){
            text.setFont(font);
        }
        void setBackColor(Color color){
            button.setFillColor(color);
        }
        void setTextColor(Color color){
            text.setColor(color);
        }
        void setPosition(Vector2f pos){
            button.setPosition(pos);
            float xPos = (pos.x + button.getLocalBounds().width / 3) - (text.getLocalBounds().width / 2);
            float yPos = (pos.y + button.getLocalBounds().height / 4) - (text.getLocalBounds().height / 2);
            text.setPosition({xPos, yPos});
        }
        void drawTo(RenderWindow &window){
            window.draw(button);
            window.draw(text);
        }
        bool isMouseOver(RenderWindow &window){
            float mouseX = Mouse::getPosition(window).x;
            float mouseY = Mouse::getPosition(window).y;
            float btnPosX = button.getPosition().x;
            float btnPosY = button.getPosition().y;
            float btnxPosWidth = button.getPosition().x + button.getLocalBounds().width;
            float btnyPosWidth = button.getPosition().y + button.getLocalBounds().height;
            if(mouseX < btnxPosWidth && mouseX > btnPosX && mouseY < btnyPosWidth && mouseY > btnPosY){
                return true;
            }