SFML, drawing a vector of shapes

Hey all, I just got back into coding. Trying to make a simple breakout game, I did start of just making a simple 'pong' game but found it quite easy so I am trying to expand it to a breakout game (image attached for those who do not know what it is).

To handle the blocks at the top of the screen I have used a vector of blocks, from which right now I am trying to draw them onto the screen. I am unable to do this as I am getting an error:
error C2664: 'void sf::RenderTarget::draw(const sf::Vertex *,size_t,sf::PrimitiveType,const sf::RenderStates &)' : cannot convert argument 1 from 'Block' to 'const sf::Drawable &'
which is inside the block.cpp file

Here is the relevant code, there are more functions but they do not apply to this. Sorry for any bad code in there :)

block.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once

class Block
{
public:
	Block(float startX, float startY);
	~Block();

	sf::RectangleShape getBlock();
	sf::FloatRect getPosition();

	void draw(sf::RenderWindow& window);

private:
	sf::RectangleShape block;

	sf::Color colour;
	sf::Vector2f position;

	int width = 100;
	int height = 25;
};


block.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Block::Block(float startX, float startY)
{
    position.x = startX;
    position.y = startY;

    colour = sf::Color::White;

    block.setSize(sf::Vector2f(width, height));
    block.setFillColor(colour);
    block.setPosition(position);
}

void Block::draw(sf::RenderWindow& window)
{
    window.draw(block);
}


blockContainer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ContainerOfBlocks::ContainerOfBlocks(int useless)
{
	blockContainer.push_back(Block(0, 1));
	blockContainer.push_back(Block(102, 1));
	blockContainer.push_back(Block(204, 1));
	blockContainer.push_back(Block(306, 1));
	blockContainer.push_back(Block(408, 1));
	blockContainer.push_back(Block(510, 1));
	blockContainer.push_back(Block(612, 1));
	blockContainer.push_back(Block(714, 1));
	blockContainer.push_back(Block(816, 1));
	blockContainer.push_back(Block(918, 1));
}

void ContainerOfBlocks::drawContainer(sf::RenderWindow& window)
{
    for (unsigned int i = 1; i < blockContainer.size() ; i++)
    {
        blockContainer[i].draw(window);
    }
}


blockContainer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

class ContainerOfBlocks
{
public:
    ContainerOfBlocks(int useless);
    ~ContainerOfBlocks();

    std::vector<Block> getContainer();
   
    void drawContainer(sf::RenderWindow& window);

private:
    std::vector<Block> blockContainer;
};


Thank you for any help :)
Last edited on
Is Block a sf::Drawable?
I have eddited it to include the block.h as I am not sure what you mean by 'sf::Drawable' and the change I made to the loop in 'drawContainer'

I no longer get the error, however nothing comes up on the screen still.
The sf::RenderWindow::draw function expects a sf::Drawable object as argument, but in Block::draw you're passing a Block object as argument and that's why I asked if Block was a sf::Drawable (meaning that Block inherits from sf::Drawable). I see now that it's not the case.

What you intended to do was probably to pass the sf::RectangleShape (a subclass of sf::Drawable) member variable named block as argument. I guess you have already figured this out because you are no longer passing the block object as an argument to Block::draw but it seems like you've forgot to update the code in Block::draw because it still have two parameters.

I think the problem you're facing now is caused by the loop. i is never greater than 10 so the loop never runs. Shouldn't you instead check if i is less than blockContainer.size()? Also don't forget that indices start counting from 0.
Last edited on
I have made all the edits you suggested, I can see the 10 blocks on the screen now. Thank you for your help mate :)
Topic archived. No new replies allowed.