c++ - 使用类函数在窗口屏幕上设置卡片位置
问题描述
我不想将卡片的位置硬编码在屏幕中间,我们做了一个没有类的项目。因此,尽管将我所做的让卡片放在中间很容易,但无论我做什么,卡片都会留在左上角。
我什至有时会注意到,如果我将 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 设置它们。
解决方案
在没有完整代码的情况下帮助您是很棘手的,因为我不知道您到底想如何使用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();
}
}
推荐阅读
- java - 在spring中初始化一个没有任何字段的实例
- flutter - Flutter 错误:包 io.flutter.plugins.webviewflutter 不存在
- pandas - 你如何为 tensorflow 编译混合数据类型?
- flutter - 如何使用颤振返回json列表
- git - 无法在 Windows 10 中使用来自 Powershell 7.1 的 git
- django - 如何像在 celery 任务中一样在 HUEY 动态周期性任务中添加到期日期?
- html - 我可以根据视口宽度重新分配 SASS 变量吗(媒体查询)
- sql - 将 FTS 传递给物化视图
- django - Django:不会删除帖子
- mysql - mysql根据select从一个表插入到另一个表