C++菜单sfml中心所有的东西

问题描述 投票:0回答:1
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);

主菜单 。

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; 

在此输入图片说明

我不知道如何居中这个菜单,我想博中心(在中间),看起来像我所附的图像。][1] 第二张图是现在的菜单--它看起来像...

c++ menu window center sfml
1个回答
1
投票

如果你想将任何图片或文字居中,那么就可以用

sf::Vector2f position;
position.x = ((windowSize.x - imageSize.x) / 2.0f);
position.y = ((windowSize.y - imageSize.y) / 2.0f);

这适用于原点在图像左上方的图像。

© www.soinside.com 2019 - 2024. All rights reserved.