SFML库的简单使用

显示圆和矩形

#include 

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);
    /*shape.setTexture()*/

    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);//绘制rect
        }
        window.draw(shape);
        window.display();
    }

    return 0;
}

打印实时坐标

#include 
#include 

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Window");

    sf::Vector2i mousePosition;  // 鼠标位置
    sf::Font font;  // 字体
    //font.loadFromFile("arial.ttf"); // 替换为您自己的字体文件路径
    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;
}

你可能感兴趣的:(SFML,c++)