Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing a vector of rectangle shapes  (Read 7475 times)

0 Members and 1 Guest are viewing this topic.

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Drawing a vector of rectangle shapes
« on: January 30, 2017, 06:57:05 pm »
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:
(click to show/hide)
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.cpp
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(Block block, sf::RenderWindow& window)
{
        window.draw(block);
}

blockContainer.cpp
ContainerOfBlocks::ContainerOfBlocks(int useless)
{
        blockContainer.push_back(Block(100, 1));
        blockContainer.push_back(Block(202, 1));
}

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

blockContainer.h
#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 :)

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: Drawing a vector of rectangle shapes
« Reply #1 on: January 30, 2017, 07:28:02 pm »
couldn't this
  blockContainer.draw(blockContainer, window);

be like this
  blockContainer.draw(window);

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Drawing a vector of rectangle shapes
« Reply #2 on: January 30, 2017, 08:42:52 pm »
Quote
couldn't this
  blockContainer.draw(blockContainer, window);

be like this
  blockContainer.draw(window);

Yes, I just noticed that before I checked this forum.

Although i don't get an error, the shapes do not appear on screen at all now. I was thinking maybe it is to do with the way I have coded it. Does the constructor for blockContainer actually push back these objects or are they destroyed as the constructor finishes.
I think I will need to make them on the heap and then point to them? I'm not sure though I could be completely wrong, just a thought.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing a vector of rectangle shapes
« Reply #3 on: January 30, 2017, 09:11:39 pm »
The container does push back two blocks, yes.

However, your loop that draws the container has an error in its logic. The condition will never be true.
(click to show/hide)

Although there is no use in the code you used of getContainer, note that it will return a copy of the vector, not the actual vector itself so, not only can it slow down your program if this vector has a lot of elements but, if you modify anything in the vector returned by getContainer, it won't modify the actual vector.
If you want to access the vector, return a reference or a pointer:
std::vector<Block>& getContainer();

Hope this helps  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Drawing a vector of rectangle shapes
« Reply #4 on: January 30, 2017, 09:33:41 pm »
Quote
However, your loop that draws the container has an error in its logic. The condition will never be true.

Thank you for pointing that out. I did not notice i did that. No matter how hard I try I can't seem to get it to return a pointer to the vector.

This is the code I am using.
Block* ContainerOfBlocks::getContainer()
{
        return *blockContainer;
}

and this is my error:
(click to show/hide)

I know this may sound stupid, but I thought an array would be better as I could access each individual block and delete them as they get hit. Which would be easier than the vector. Yet I can't get this working with an array, it breaks when I run the program.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing a vector of rectangle shapes
« Reply #5 on: January 30, 2017, 09:45:43 pm »
It's still a vector of blocks so return a reference to the vector of blocks or a pointer to the vector of blocks.
i.e.
std::vector<Block>& getContainer();
or
std::vector<Block>* getContainer();

If you really want to, you can return a pointer to the actual block data but it's not as nice to use:
return blockContainer.data();
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Drawing a vector of rectangle shapes
« Reply #6 on: January 30, 2017, 10:01:26 pm »
std::vector<Block>* getContainer()
{
        return *blockContainer;
}

This still doesn't work. It is saying it's unidentified now?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing a vector of rectangle shapes
« Reply #7 on: January 30, 2017, 10:07:05 pm »
blockContainer is not a pointer; it is a vector.
To return a pointer to it, return its address:
std::vector<Block>* getContainer()
{
    return &blockContainer;
}

I still think reference might be easier :p
std::vector<Block>& getContainer()
{
    return blockContainer;
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Drawing a vector of rectangle shapes
« Reply #8 on: January 30, 2017, 10:54:26 pm »
std::vector<Block>& getContainer()
{
        return blockContainer;
}

that still says:

Error: identifier "blockContainer" is unidentified

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Drawing a vector of rectangle shapes
« Reply #9 on: January 30, 2017, 11:11:22 pm »
Nevermind, error on my part. Did not put ContainerOfBlocks:: before the function name. Silly mistake :) thank you for all the help!

I have another problem now, funnily enough.

The constructor inside my containerOfBlocks.cpp file has an error on the '{' saying
(click to show/hide)
ContainerOfBlocks::ContainerOfBlocks()
{
        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));
}

Not sure what this means to be completely honest
« Last Edit: January 30, 2017, 11:15:32 pm by DylanMorgan »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing a vector of rectangle shapes
« Reply #10 on: January 30, 2017, 11:18:58 pm »
You're welcome. Glad you got it working!
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing a vector of rectangle shapes
« Reply #11 on: January 31, 2017, 12:28:53 am »
You didn't change:
std::vector<Block> blockContainer;
to a reference, did you?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Drawing a vector of rectangle shapes
« Reply #12 on: January 31, 2017, 04:25:37 pm »
Wow, good spot. I did, don't remember why I did that mind. All fixed now, cheers for your help  :)

 

anything