Potrzebuję zrobić klasę która pomoże mi tworzyć przyciski.
Chcę aby w tym przycisku był tekst który będzie idealnie na środku, a więc na sf::Sprite jest sf::Text i to wszystko rysujemy.
Na mojej drodze napotkałem pewien problem
Poniższy kod pokarze wam o co chodzi:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!");
/// font
sf::Font font;
font.loadFromFile("font.ttf");
/// text
sf::Text text;
text.setFont(font);
text.setCharacterSize(60);
text.setString("start");
text.setFillColor(sf::Color::Black);
/// texture
sf::Texture texture;
texture.loadFromFile("adres.png");
/// sprite
sf::Sprite sprite;
sprite.setTexture(texture);
/// shape
sf::RectangleShape shape;
shape.setSize(sf::Vector2f(text.getGlobalBounds().width,text.getGlobalBounds().height));
shape.setFillColor(sf::Color(0,0,0,0));
shape.setOutlineColor(sf::Color::Red);
shape.setOutlineThickness(3);
text.setPosition((sprite.getGlobalBounds().width-text.getGlobalBounds().width)/2+sprite.getGlobalBounds().left,
(sprite.getGlobalBounds().height-text.getGlobalBounds().height)/2+sprite.getGlobalBounds().top);
shape.setPosition((sprite.getGlobalBounds().width-text.getGlobalBounds().width)/2+sprite.getGlobalBounds().left,
(sprite.getGlobalBounds().height-text.getGlobalBounds().height)/2+sprite.getGlobalBounds().top);
/// dla shape i dla text jest dokładnie takie samo ustawianie tekstu
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(sprite);
window.draw(text);
window.draw(shape);
window.display();
}
return 0;
}
jak napisałem w komentarzu ustawianie pozycji shape i text jest dokładnie takie samo a efekt wygląda tak:
P.S. przy zamianie getGlobalBounds na getLocalBounds nie ma różnicy, tak samo przy ustawieniach okna sf::Style::Fullscreen (w fullscreen ma to działać i do tego dążę, jak ma działać tylko na jednym to na fullscreen)
proszę o pomoc i wytłumaczenie co się dokładnie tu dzieje :)