1
Général / 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 :
Merci de votre réponse
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;
}
#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