【问题标题】:C++ menu sfml center all thingsC++ 菜单 sfml 中心所有东西
【发布时间】:2020-09-16 21:02:51
【问题描述】:
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; 

enter image description here

我不知道如何使这个菜单居中,我想居中(在中间),看起来像我附上的图像。 ][1] Seconde 图像现在是菜单 - 它看起来如何......

【问题讨论】:

    标签: c++ menu window center sfml


    【解决方案1】:

    如果您想将任何图像或文本居中,那么

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

    这适用于原点位于图像左上角的图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-15
      • 2015-12-08
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 2014-08-06
      相关资源
      最近更新 更多