С++ sfml如何用鼠标帮助绘制图形。

问题描述 投票:0回答:1

我想在C++上创建模拟。我想:当我按下鼠标键时,坐标中应该出现一个圆。我想使用类、void或结构体。当我点击鼠标时,调用它是类(我已经做了这个条件)。

c++ sfml
1个回答
1
投票

下面是一个在鼠标位置点击按钮画圆的例子。

#include <SFML/Graphics.hpp>
#include <vector>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "WINDOW_TITLE");
    window.setFramerateLimit(50);
    std::vector<sf::Shape*> shapes;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                    return 0;
                }
                case sf::Event::MouseButtonPressed:
                {
                    sf::CircleShape *shape = new sf::CircleShape(50);
                    shape->setPosition(event.mouseButton.x,event.mouseButton.y);
                    shape->setFillColor(sf::Color(100, 250, 50));
                    shapes.push_back(shape);
                } 
            }
        }

        window.clear();

        for(auto it=shapes.begin();it!=shapes.end();it++)
        {
            window.draw(**it);
        }
        window.display();
    }

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.