[英]SFML window immediately closes
我正在關注如何在 sfml 中制作按鈕的教程,然后當我運行它時,window 立即關閉。我有 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())
,所以你的程序立即關閉 window ,把return 0;
在while loop
之外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.