首页 > 解决方案 > 使用类函数在窗口屏幕上设置卡片位置

问题描述

我不想将卡片的位置硬编码在屏幕中间,我们做了一个没有类的项目。因此,尽管将我所做的让卡片放在中间很容易,但无论我做什么,卡片都会留在左上角。

我什至有时会注意到,如果我将 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();
    }

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

标签: c++classobjectc++14sfml

解决方案


在没有完整代码的情况下帮助您是很棘手的,因为我不知道您到底想如何使用setPostioning. 经过一个小的解决方法,它终于出现在屏幕中央。如果我的示例仍然不能满足您的需求,请随时发表评论。

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

更新背景.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 文件中,我删除了一些多余的 refs sf::RectangleShape(因为你已经从它继承了)和 to sf::RenderWindod(因为它的引用存储在类中)。
更新了 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();
    }
}

推荐阅读