C++ 使用类功能在窗口屏幕上设置卡片位置 我不想把卡的位置硬编码在屏幕中间,我们做了这样一个没有类的项目。因此,虽然很容易将我所做的事情放在中间,但无论我做了什么,卡都会留在左上角
我甚至有时会注意到,如果我使用矩形大小或其他东西,当屏幕最大化时,矩形的比例会改变,看起来像一个正方形 我做错了什么 这是我的背景cpp文件:C++ 使用类功能在窗口屏幕上设置卡片位置 我不想把卡的位置硬编码在屏幕中间,我们做了这样一个没有类的项目。因此,虽然很容易将我所做的事情放在中间,但无论我做了什么,卡都会留在左上角,c++,class,object,c++14,sfml,C++,Class,Object,C++14,Sfml,我甚至有时会注意到,如果我使用矩形大小或其他东西,当屏幕最大化时,矩形的比例会改变,看起来像一个正方形 我做错了什么 这是我的背景cpp文件: #include "background.h" background::background() : background(450, 750) { } background::background(float x, float y) { sf::RenderWindow window; sf::RectangleS
#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);
};
所以,是的,我不确定为什么这个位置没有被设置,或者是从窗口大小中取出来,或者可能是因为我做的矩形大小和被误读。我还认为这与x和y有关,因为我已经用450和750设置了它们。在没有完整代码的情况下,很难帮助您,因为我不知道您到底想如何使用
设置定位。
经过一个小的变通,它终于出现在屏幕中央。
如果我的示例仍然不能满足您的需求,请随时发表评论
在头文件中,我添加了对sf::RenderWindow
的引用,以便在setposting
中使用它
更新背景。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
(因为对它的引用存储在类中)的一些冗余引用。
更新的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
});
}
在main函数中,我在事件循环之前调用了setpositioning
,并添加了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();
}
}
它看起来不像任何调用setpositioning
,这是故意的吗?是的,很抱歉我忘了撤销我删除的内容和调用函数的所有内容是的,因此尝试调用它仍然会给我一个错误或根本没有被使用,并且不确定我可以从这里做什么。是的,我想我根本不知道如何调用我的函数。我想我明白了,但不管怎样,我总是在同一个地方得到矩形
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();
}
}