Bienvenue, Invité. Merci de vous connecter ou de vous inscrire.
Avez-vous perdu votre e-mail d'activation ?

Auteur Sujet: SFML rect et position  (Lu 956 fois)

0 Membres et 1 Invité sur ce sujet

pulk66

  • Newbie
  • *
  • Messages: 2
    • Voir le profil
SFML rect et position
« le: Août 31, 2020, 03:29:53 pm »
Salut a tous alors voila, j'essaye de créer un bouton grâce a un rect et aux positions de la souris / rect. le seul problème que j'ai et que je ne comprend pas c'est que cela ne marche que pour un plein écran et non pour une petite fenêtre.

Voici le code :
#include <iostream>
#include <SFML/Graphics.hpp>
#include <string>

sf::Texture get_texture(std::string link) {
    sf::Texture background_t;
    if (!background_t.loadFromFile(link)) {
        std::cout << "loading image problem\n";
    }
    return (background_t);
}

int main(void)
{
    sf::RenderWindow game(sf::VideoMode(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height), "SFML works!");
    int s_width = game.getSize().x;
    int s_height = game.getSize().y;

    /*
    ** ENTERING SCREEN, BACKGROUND PICTURE.
    */

    sf::Texture background_t = get_texture("../sprite/background.png");
    sf::Sprite background;
    background.setTexture(background_t);
    background.setTextureRect(sf::IntRect(0, 0, s_width, s_height));

    /*
    ** ENTERING SCREEN, ENTERING TEXT.
    */

    sf::Font entering_font;
    if (!entering_font.loadFromFile("../font/arial.ttf")) {
        std::cout << "loading font problem\n";
    }
    sf::Text entering_text;
    entering_text.setFont(entering_font);
    entering_text.setString("CLICK HERE TO CONTINUE");
    entering_text.setCharacterSize(72);
    entering_text.setFillColor(sf::Color::Red);
    int text_w = entering_text.getLocalBounds().width;
    int text_h = entering_text.getLocalBounds().height;
    entering_text.setPosition(s_width / 2 - text_w / 2, s_height / 2 - text_h / 2);
    sf::RectangleShape r(sf::Vector2f(entering_text.getLocalBounds().width, entering_text.getLocalBounds().height));
    r.setPosition(entering_text.getPosition().x, entering_text.getPosition().y);

    while (game.isOpen()) {
        sf::Event event;
        while (game.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                game.close();
            }
        }

        /*
        ** VERIFY MOUSE POSITION IN ENTERING TEXT
        */

        sf::Vector2i mouse_p = sf::Mouse::getPosition(game);
        std::cout << "mpx : " << mouse_p.x << " mpy : " << mouse_p.y << " tpx : " << entering_text.getPosition().x << " tpy : " << entering_text.getPosition().y << " tw : " << entering_text.getLocalBounds().width << " th : " << entering_text.getLocalBounds().height << "\n";
        if (mouse_p.x >= entering_text.getPosition().x && mouse_p.x <= entering_text.getPosition().x + entering_text.getLocalBounds().width \
        && mouse_p.y >= entering_text.getPosition().y && mouse_p.y <= entering_text.getPosition().y + entering_text.getLocalBounds().height) {
            entering_text.setFillColor(sf::Color::Green);
        } else {
            entering_text.setFillColor(sf::Color::Red);
        }

        game.clear();
        game.draw(background);
        game.draw(r);
        game.draw(entering_text);
        game.display();
    }
    return 0;
}
 

Merci de votre réponse

Laurent

  • Administrator
  • Hero Member
  • *****
  • Messages: 32504
    • Voir le profil
    • SFML's website
    • E-mail
Re: SFML rect et position
« Réponse #1 le: Septembre 01, 2020, 07:35:11 am »
Les fonctions dont tu as besoin :
- RenderWindow::mapPixelToCoords
- FloatRect::contains
Laurent Gomila - SFML developer

 

anything