Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - hung

Pages: [1]
1
Graphics / Re: draw text on click
« on: March 04, 2021, 02:59:51 pm »
sorry I don't understand what you said

2
Graphics / draw text on click
« on: March 03, 2021, 01:42:06 pm »
I'm trying to draw some text (an "X") at the position of the mouse after clicked. But the window only draw a dot. Does anybody know how to fix it. Thank you. Sorry if my english is bad
This is my code
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(720, 720), "My window",sf::Style::Titlebar | sf::Style::Close);
    window.setFramerateLimit(50);
    std::vector<sf::Text*> shapes;
    // run the program as long as the window is open
    while (window.isOpen()){
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event)){

            if (event.type == sf::Event::MouseButtonReleased &&
                event.mouseButton.button == sf::Mouse::Left) {
                    sf::Font font;
                    font.loadFromFile("font/arial.ttf");
                    sf::Text *shape = new sf::Text("X",font);
                    shape->setString("X");
                    shape->setCharacterSize(30);
                    shape->setPosition(event.mouseButton.x,event.mouseButton.y);
                    shape->setColor(sf::Color::Green);
                    shapes.push_back(shape);
            }

        // Clear screen
        window.clear();
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
        // window.draw(...);
        for(auto it=shapes.begin();it!=shapes.end();it++)
        {
            window.draw(**it);
        }
        // end the current frame
        window.display();
    }
    return 0;
}
 

Pages: [1]