所以我刚开始学习SFML。所以,我想输入x。当x=1时,我创建的矩形的颜色发生了变化。这是我的代码:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
int main()
{
int x;
sf::RenderWindow MW(sf::VideoMode(1200, 650), "Dominus", sf::Style::Close |
sf::Style::Titlebar);
sf::RectangleShape bg(sf::Vector2f(1200.0f, 650.0f)); bg.setFillColor(sf::Color::Green);
while (MW.isOpen()) {
sf::Event evnt;
while (MW.pollEvent(evnt)) {
switch (evnt.type) {
case sf::Event::Closed:
MW.close(); break;
}
}
cin >> x;
if (x == 1) {
bg.setFillColor(sf::Color::Blue);
}
MW.clear();
MW.draw(bg);
MW.display();
}
return 0;
}
现在,我面临的问题是窗口加载不正确。当我把"cin"移出循环时,我似乎根本无法接受输入。
您可以使用线程:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <mutex>
#include <thread>
int main() {
std::mutex xmutex;
int x = 0;
std::thread thr([&]() {
std::lock_guard<std::mutex> lock(xmutex);
int x;
std::cin >> x;
});
thr.detach();
sf::RenderWindow MW(sf::VideoMode(1200, 650), "Dominus", sf::Style::Close | sf::Style::Titlebar);
sf::RectangleShape bg(sf::Vector2f(1200.0f, 650.0f)); bg.setFillColor(sf::Color::Green);
while (MW.isOpen()) {
sf::Event evnt;
while (MW.pollEvent(evnt)) {
switch (evnt.type) {
case sf::Event::Closed:
MW.close(); break;
}
}
{
std::lock_guard<std::mutex> lock(xmutex);
if (x == 1) {
bg.setFillColor(sf::Color::Blue);
}
}
MW.clear();
MW.draw(bg);
MW.display();
}
}