Hey all, I feel like such an idiot having to post this problem again. I have looked it up but can't seem to find the exact answer for the error I am having. The program runs completely fine, I then make the ball hit any block and the program crashes. It throws an exception saying:
Microsoft Visual Studio C Runtime Library has detected a fatal error in Pong.exe.
Press Break to debug the program or Continue to terminate the program.
I ran it in debug and the screen attatched popped up on my screen
I am not sure what this even means, below is the code for anything I think could be relevant, if you want to see anything else just ask and I'll edit this to include it. Thank you for helping anyone!
containerOfBlocks.h
#pragma once
class ContainerOfBlocks
{
public:
ContainerOfBlocks(int yPosition, sf::Color colour);
~ContainerOfBlocks();
std::vector<Block>& getContainer();
void drawContainer(sf::RenderWindow& window);
void deleteObjectFromVector(int objectNumber);
private:
std::vector<Block> blockContainer;
};
containerOfBlocks.cpp
ContainerOfBlocks::ContainerOfBlocks(int yPosition, sf::Color colour)
{
blockContainer.push_back(Block(2, 2));
blockContainer.push_back(Block(104, 2));
blockContainer.push_back(Block(206, 2));
blockContainer.push_back(Block(308, 2));
blockContainer.push_back(Block(410, 2));
blockContainer.push_back(Block(512, 2));
blockContainer.push_back(Block(614, 2));
blockContainer.push_back(Block(716, 2));
blockContainer.push_back(Block(818, 2));
blockContainer.push_back(Block(920, 2));
for (unsigned int i = 0; i < 10; ++i)
{
blockContainer[i].setPosition(yPosition);
}
for (unsigned int i = 0; i < 10; ++i)
{
blockContainer[i].setColour(colour);
}
}
std::vector<Block>& ContainerOfBlocks::getContainer()
{
return blockContainer;
}
void ContainerOfBlocks::drawContainer(sf::RenderWindow& window)
{
for (unsigned int i = 0; i < 10; ++i)
{
blockContainer[i].draw(window);
}
}
void ContainerOfBlocks::deleteObjectFromVector(int objectNumber)
{
blockContainer.erase(blockContainer.begin() + 2);
}
main.cpp
int main()
{
float windowWidth = 1022;
float windowHeight = 648;
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "pong");
window.setPosition(sf::Vector2i(125, 0));
ContainerOfBlocks blockContainer(2, sf::Color::Red);
ContainerOfBlocks blockContainer2(29, sf::Color::Yellow);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
for (unsigned int i = 0; i < 10; i++)
{
if (ball.getPosition().intersects(blockContainer.getContainer()[i].getPosition()))
{
// testing
std::cout << "Hit 1st row, block: " << i;
// Reverse ball
ball.reboundSurface();
// Delete block
blockContainer.deleteObjectFromVector(i);
break;
}
else if (ball.getPosition().intersects(blockContainer2.getContainer()[i].getPosition()))
{
// testing
std::cout << "Hit 2nd row, block: " << i;
// Reverse ball
ball.reboundSurface();
// Delete Block
blockContainer2.deleteObjectFromVector(i);
break;
}
}