显示圆和矩形
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(10.f);
shape.setFillColor(sf::Color::Green);
shape.setPosition(0, 0);
sf::RectangleShape rect(sf::Vector2f(5, 10));
rect.setPosition(sf::Vector2f(20, 20));
rect.setFillColor(sf::Color(255, 255, 255, 128));
rect.setRotation(45);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
for (int i = 0; i < 50; i++) {
rect.setPosition(sf::Vector2f(20 + i * 5, 20 + i * 5));
rect.setFillColor(sf::Color(255-i*5, 255 - i * 5, 255, 128));
rect.setRotation(45 - i * 2);
window.draw(rect);
}
window.draw(shape);
window.display();
}
return 0;
}
打印实时坐标
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Window");
sf::Vector2i mousePosition;
sf::Font font;
sf::Text text;
text.setFont(font);
text.setCharacterSize(90);
text.setFillColor(sf::Color::Black);
text.setPosition(20, 20);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
mousePosition = sf::Mouse::getPosition(window);
std::cout << "Mouse Position: (" << mousePosition.x << ", " << mousePosition.y << ")" << std::endl;
text.setString("Mouse Position: (" + std::to_string(mousePosition.x) + ", " + std::to_string(mousePosition.y) + ")");
window.clear(sf::Color::Yellow);
window.draw(text);
window.display();
}
return 0;
}