SFML窗口立即关闭

擎天柱

我正在关注有关如何在sfml中制作按钮的教程,然后在运行它时,窗口立即关闭。我有2个文件,一个文件名为main.cpp,另一个文件名为button.h。我在互联网上找不到任何可解决此问题的修复程序,那么我该如何解决呢?这是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的代码

#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;
            }

程序中没有错误,所以我该如何解决该问题?

蓝影

您放进return 0;while(window.isOpen()),所以您的程序立即关闭窗口,放到return 0;外面while loop

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SDL OpenGL窗口立即关闭

来自分类Dev

Win32窗口立即关闭

来自分类Dev

SDL2 OpenGL 窗口立即关闭

来自分类Dev

Python(PyQT)中的GUI窗口立即闪烁并关闭?

来自分类Dev

wxPython窗口立即在Mac上使用Komodo崩溃/关闭

来自分类Dev

为什么我的 pygame 窗口打开并立即关闭?

来自分类Dev

立即停止SFML TcpListener

来自分类Dev

立即开启窗口关闭后刷新开启窗口的开启程序

来自分类Dev

SFML闪烁的渲染窗口?

来自分类Dev

SFML-窗口错误

来自分类Dev

SFML 视图与窗口混淆

来自分类Dev

html css javascript我需要我的导航窗口在用户单击链接后立即关闭

来自分类Dev

如何防止Visual Studio 2013控制台窗口在运行程序后立即关闭?

来自分类Dev

如何修复在单击按钮打开后立即关闭的模式弹出窗口

来自分类Dev

html css javascript我需要我的导航窗口在用户单击链接后立即关闭

来自分类Dev

如何立即关闭Microsoft Windows 7中的所有Windows照片查看器窗口?

来自分类Dev

在PyQt 4(anaconda)中运行程序后,窗口将立即关闭[PyCharm 4.5]

来自分类Dev

当我运行程序时,控制台窗口立即关闭

来自分类Dev

MySQL Reader立即关闭

来自分类Dev

那会是一个错误吗?执行时立即关闭当前cmd窗口:move nul 2>&0

来自分类Dev

我想在窗口 2 关闭 JAVAFX 后立即在窗口 1 上创建一个事件(添加更多详细信息)

来自分类Dev

.pyw tk程序立即关闭?

来自分类Dev

简单的桌面程序立即关闭

来自分类Dev

Xubuntu热键可立即关闭

来自分类Dev

QWizard立即打开和关闭

来自分类Dev

Netty-频道立即关闭

来自分类Dev

xterm参数终端关闭立即

来自分类Dev

立即关闭小吃店

来自分类Dev

Dovecot立即连接关闭

Related 相关文章

热门标签

归档