使用 class 函数在 window 屏幕上设置卡片位置

Setting up the card position on the window screen with class function

我不想将卡片的位置硬编码到屏幕中间,我们在没有 class 的情况下完成了这样的项目。因此,尽管将我所做的使卡片位于中心的操作很容易,但无论我做了什么,卡片都会留在左上角。

有时我什至注意到,如果我将 rectSize 或其他东西放在最大化屏幕时,矩形比例会发生变化,看起来像一个正方形。

我做错了什么?

这是我的背景cpp文件:

#include "background.h"

background::background() : background(450, 750)
{

}
background::background(float x, float y)
{
    sf::RenderWindow window;
    sf::RectangleShape rectangle;

    sf::RectangleShape::setSize({x, y});

//    sf::Vector2f center;
//
//    sf::RectangleShape::setPosition({});
}

void setPostioning (sf::RenderWindow &window, sf::RectangleShape &rectangle, float x, float y)
{
    sf::Vector2f rectSize ={x,y};
    rectangle.setSize(rectSize);
    sf::Vector2f center;

    rectangle.setPosition({
        center.x = window.getSize().x/2 - rectSize.x/2,
        center.y = window.getSize().y/2 - rectSize.y/2
    });
}

这是我所做的头文件:

#include <SFML/Graphics.hpp>

class background : public sf::RectangleShape
{
public:
    background();
    background(float x, float y);
    void setPostioning(sf::RenderWindow &window, sf::RectangleShape &rectangle, float x, float y);
};

现在这是我的主要文件

int main()
{
    //set up of the window
    sf::VideoMode videoMode(1280,1024,32);
    sf::RenderWindow window(videoMode, "SFML Tutorial");//window will display name
    window.setFramerateLimit(15);//frame rate

    background b;
    rank r;
    Card Joker;
    while(window.isOpen())
    {
        sf::Event event;

        while (window.pollEvent(event))
        {
            //when window is running they can close it with the close button
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

            //this if statement will make our card stay in the same ratio no matter what
            if (event.type == sf::Event::Resized)
            {
                // update the view to the new size of the window and keep the center
                window.setView(sf::View(window.getView().getCenter(),
                                        sf::Vector2f((float) event.size.width, (float) event.size.height)));
            }
        }
        //invoking and set up to be drawn and display on the window when running
        window.clear(sf::Color::Black);
        window.draw(Joker);
        window.draw(r);
        window.display();
    }

所以是的,不确定为什么没有设置位置或从 window 大小中获取位置,或者可能与我所做的 rectSize 有关并被误读。我也认为它与 x 和 y 有关,因为我已经将它们设置为 450 和 750。

在没有完整代码的情况下很难为您提供帮助,因为我不知道您究竟想如何使用 setPostioning。 经过一个小小的变通,它终于出现在了屏幕中央。 如果我的示例仍然不能满足您的需求,请随时发表评论。

在头文件中我添加了对 sf::RenderWindow 的引用,以便在 setPostioning.

中使用它

已更新background.h:

class background : public sf::RectangleShape
{
public:
    background(sf::RenderWindow &window);
    background(sf::RenderWindow &window, float x, float y);
    void setPostioning(float x, float y);

private:
    //  Added a refence to original window to have possibility use it in setPositioning
    sf::RenderWindow& m_window;
};

在 .cpp 文件中,我删除了一些对 sf::RectangleShape 的冗余引用(因为你已经从它那里继承了)和 sf::RenderWindod (因为对它的引用存储在 class 中).
已更新 background.cpp:

background::background(sf::RenderWindow &window) : background(window, 450, 750)
{

}
background::background(sf::RenderWindow &window, float x, float y) : m_window(window)
{
    //  sf::RectangleShape rectangle;

    sf::RectangleShape::setSize({ x, y });
}

void background::setPostioning(float x, float y)
{
    sf::Vector2f rectSize = { x,y };

    // don't use rectangle from param, because you already inherited from sf::RectangleShape
    //rectangle.setSize(rectSize);
    setSize(rectSize);
    sf::Vector2f center;

    // again, don't use rectangle from param, because you already inherited from sf::RectangleShape
    //rectangle.setPosition({
    setPosition({
        center.x = m_window.getSize().x / 2 - rectSize.x / 2,
        center.y = m_window.getSize().y / 2 - rectSize.y / 2
    });
}

在主函数中,我在事件循环之前调用了 setPostioning 并添加了 window.draw(b); 来渲染您的背景。 更新主函数:

int main()
{
    //set up of the window
    sf::VideoMode videoMode(1280, 1024, 32);
    sf::RenderWindow window(videoMode, "SFML Tutorial");//window will display name
    window.setFramerateLimit(15);//frame rate

    background b(window);
    //  use setPostioning
    b.setPostioning(200.f, 200.f);

    while (window.isOpen())
    {
        sf::Event event;

        while (window.pollEvent(event))
        {
            //when window is running they can close it with the close button
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

            //this if statement will make our card stay in the same ratio no matter what
            if (event.type == sf::Event::Resized)
            {
                // update the view to the new size of the window and keep the center
                window.setView(sf::View(window.getView().getCenter(),
                    sf::Vector2f((float)event.size.width, (float)event.size.height)));
            }
        }
        //invoking and set up to be drawn and display on the window when running
        window.clear(sf::Color::Black);
        window.draw(b);
        window.display();
    }
}