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

Author Topic: draw text on click  (Read 3578 times)

0 Members and 1 Guest are viewing this topic.

hung

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
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;
}
 
« Last Edit: March 03, 2021, 01:44:52 pm by hung »

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: draw text on click
« Reply #1 on: March 03, 2021, 11:01:52 pm »
Why use pointers at all?

#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);
    sf::Font font;
    font.loadFromFile("font/arial.ttf");
    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::Text shape("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();
    }
}

hung

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: draw text on click
« Reply #2 on: March 04, 2021, 02:59:51 pm »
sorry I don't understand what you said

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: draw text on click
« Reply #3 on: March 04, 2021, 03:09:31 pm »
Your mistake is incorrect usage of pointers, I fixed it and provide code. Also you can load font only once and reuse it whenever you want.