enter code sf::FloatRect textRect = text.getLocalBounds();
//Font
if(!font.loadFromFile("arial.ttf")){}
//Tło - trzeba załadować
if(!texture.loadFromFile("bg_menu.png")){}
//play
mainMenu[0].setFont(font);
mainMenu[0].setFillColor(sf::Color::Blue);
mainMenu[0].setString("Play");
mainMenu[0].setCharacterSize(40);
mainMenu[0].setPosition(sf::Vector2f(width / 2, hight / (Max_main_menu + 1)));
//center text (?)
mainMenu[0].setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
mainMenu[1].setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
mainMenu[2].setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
Main menu :
enter main(){
RenderWindow window(VideoMode(SCREEN_HIGH, SCREEN_WIDTH), "Age of Ants!", Style::Close);
Main_menu menu(SCREEN_WIDTH, SCREEN_HIGH);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
//Movin in Menu - Up&Down&Selecting start
if (event.type == Event::KeyReleased) {
if (event.key.code == Keyboard::Up) {
menu.moveUP();
break;
}
if (event.key.code == Keyboard::Down) {
menu.moveDOWN();
break;
}
if (event.key.code == Keyboard::Return) {
switch (menu.mainMenuPressed()) {
case 0:
cout << "Play button has been pressed" << endl;
break;
case 1:
cout << "Options button has been pressed" << endl;
break;
case 2:
window.close();
cout << "Exit button has been pressed" << endl;
break;
I do not know how to center this menu, I would like to bo center (on the middle) and looks like on the image I have attached. ][1] Seconde image is the menu right now - how it looks like...
If you want to center any image or text then
sf::Vector2f position;
position.x = ((windowSize.x - imageSize.x) / 2.0f);
position.y = ((windowSize.y - imageSize.y) / 2.0f);
This works for images whose origin to the topleft of the image