尝试绘制存储在数组中的矩形,但只出现一个矩形?
Trying to draw rectangles stored in an array, but only one rectangle appears?
我的代码在这里:
如上所述,我试图在屏幕上绘制一系列具有不同 x 位置的条,并将它们存储在数组中。看起来代码只绘制了 1 个矩形,即使我已经检查过并且每个条都有不同的 x 位置,所以我确信它是我如何绘制对象的问题,但感觉是正确的。
我还用矢量制作了一个类似的程序,使用相同的循环进行绘图,但用 .at(i) 代替,它确实有效,但奇怪的是没有。
我已经尝试解决这个问题一段时间了,现在我很累所以请帮忙,指出我的错误...等...
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 640), "Square", sf::Style::Close | sf::Style::Resize);
sf::RectangleShape bar[64] = {sf::RectangleShape(sf::Vector2f( (window.getSize().x)/64.0f ,100.0f))};
// creates 64 bars of equal width
for (int i = 0; i < 64; i++)
{
bar[i].setFillColor(sf::Color(0, 0, 255, 255));
bar[i].setPosition( 10*i , (window.getSize().y)/2);
// sets bars x position to shift over for every bar
}
bar[3].setPosition(600, 300);
// just a test doesn't render even though it should
while (window.isOpen())
{
//////////////////////////////////////////////////
window.clear(sf::Color(130, 130, 150, 255));
for (int i = 0; i < 64; i++)
{
window.draw(bar[i]);
}
window.display();
/////////////////////////////////////////////////
}```
I cut out the rest of the code as the rest works and really has nothing to do with the code for simplicity sake
I want it to render out rectangles across the screen but it only displays one and I can't figure out why?
sf::RectangleShape
具有默认构造函数:
sf::RectangleShape::RectangleShape ( const Vector2f & size = Vector2f(0, 0) )
您只为第一个定义了矩形的大小,其他 63 个具有默认大小 (0,0)
。
您可以 copy/paste 原始数组中的 rect 定义,或使用 std::vector
并调用获取值和元素数量的 ctor:
std::vector<sf::RectangleShape> bars( 64, // num of elems
sf::RectangleShape( sf::Vector2f(window.getSize().x/64.0f ,100.0f) ) );
另一个解决方案是在循环的每次迭代中调用 setSize
(就像 setFillColor
、setPosition
等)。
我的代码在这里:
如上所述,我试图在屏幕上绘制一系列具有不同 x 位置的条,并将它们存储在数组中。看起来代码只绘制了 1 个矩形,即使我已经检查过并且每个条都有不同的 x 位置,所以我确信它是我如何绘制对象的问题,但感觉是正确的。 我还用矢量制作了一个类似的程序,使用相同的循环进行绘图,但用 .at(i) 代替,它确实有效,但奇怪的是没有。
我已经尝试解决这个问题一段时间了,现在我很累所以请帮忙,指出我的错误...等...
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 640), "Square", sf::Style::Close | sf::Style::Resize);
sf::RectangleShape bar[64] = {sf::RectangleShape(sf::Vector2f( (window.getSize().x)/64.0f ,100.0f))};
// creates 64 bars of equal width
for (int i = 0; i < 64; i++)
{
bar[i].setFillColor(sf::Color(0, 0, 255, 255));
bar[i].setPosition( 10*i , (window.getSize().y)/2);
// sets bars x position to shift over for every bar
}
bar[3].setPosition(600, 300);
// just a test doesn't render even though it should
while (window.isOpen())
{
//////////////////////////////////////////////////
window.clear(sf::Color(130, 130, 150, 255));
for (int i = 0; i < 64; i++)
{
window.draw(bar[i]);
}
window.display();
/////////////////////////////////////////////////
}```
I cut out the rest of the code as the rest works and really has nothing to do with the code for simplicity sake
I want it to render out rectangles across the screen but it only displays one and I can't figure out why?
sf::RectangleShape
具有默认构造函数:
sf::RectangleShape::RectangleShape ( const Vector2f & size = Vector2f(0, 0) )
您只为第一个定义了矩形的大小,其他 63 个具有默认大小 (0,0)
。
您可以 copy/paste 原始数组中的 rect 定义,或使用 std::vector
并调用获取值和元素数量的 ctor:
std::vector<sf::RectangleShape> bars( 64, // num of elems
sf::RectangleShape( sf::Vector2f(window.getSize().x/64.0f ,100.0f) ) );
另一个解决方案是在循环的每次迭代中调用 setSize
(就像 setFillColor
、setPosition
等)。