SFML Vector of sprites and Sprite Collision Error - c++

I am making a game with C++ and SFML, and I have a serious problem with it.
What I want to make is, when my player(a.k.a. human character) collides with one item, then only that item should be erased. For example, when player collides with 'chicken' item, then only 'chicken' item should be erased, not other items.
However, when I run my program, and when player collides with 'chicken' item, then other items are all erased. And I don't know why. I really need your help. In spite of my poor English, thanks for reading!
And here's my code :
#include <SFML/Graphics.hpp>
...
using namespace std;
using namespace sf;
...
int main () {
...
//item Sprites
Texture bombTex;
bombTex.loadFromFile("images/bomb.png");
Sprite bomb;
...
Texture bomb2Tex;
bomb2Tex.loadFromFile("images/bomb_2.png");
Sprite bomb_2;
...
Texture cakeTex;
cakeTex.loadFromFile("images/cake.png");
Sprite cake;
...
Texture coffeeTex;
coffeeTex.loadFromFile("images/coffee.png");
Sprite coffee;
...
Texture chickenTex;
chickenTex.loadFromFile("images/chicken.png");
Sprite chicken;
...
Texture pizzaTex;
pizzaTex.loadFromFile("images/pizza.png");
Sprite pizza;
//item array (I made an item array to display & render various items in the game screen.)
Sprite item[10];
item[0] = bomb;
item[1] = coffee;
item[2] = bomb_2;
item[3] = chicken;
item[4] = pizza;
std::vector<Sprite> items;
items.push_back(Sprite(item[4]));
...
while (window.isOpen())
{ ...
...
for (size_t i = 0; i < items.size(); i++)
{
if (humanArr[index].getGlobalBounds().intersects(item[i].getGlobalBounds()))
//humanArr[index] is a player Sprite.
{
...
items.erase(items.begin());
}
}
...
window.clear();
...
for (size_t i = 0; i < items.size(); i++)
{
window.draw(item[i]);
}
...
window.display();
}
return 0;
}

for (size_t i = 0; i < items.size(); i++)
{
// first problem: you are accessing item[i] instead of items[i]
if (humanArr[index].getGlobalBounds().intersects(item[i].getGlobalBounds()))
{
...
items.erase(items.begin()); // <---------- second problem is here
}
}
You are not erasing the item you are iterating over, you are instead erasing the first item. You are also accessing the wrong array. I recommend changing the name of item to avoid this in the future. Here is how you solve this:
// Pull human bounds out of the loop so that we dont' access them each
// iteration.
const humanBounds = humanArr[index];
for (auto iter = items.begin(); iter != items.end();)
{
if (humanBounds.intersects(iter->getGlobalBounds()))
{
iter = items.erase(iter);
}
else
{
++iter;
}
}
See std::vector::erase for more information.

Related

How to detect / avoid overlapping of text in Qt?

I am having QGraphicsView, which has multiple QGraphicsItem's. On QGraphicsView I am performing multiple transformation like zoom-in, zoom-out, Fit-in etc.
In QGraphicsItem, I am having few rectangles and some polylines. Polylines are attached to rectangles. The positions of polylines are very near to each other. Every polyline has its own name written above it. So when the design loads, names of the polylines overlap with each other. If I zoomed-in till particular level the distance between polylines makes their names non overlapping.
So I want to detect overlapping of text. If text overlap, I should not
show them on polyline. Once I zoomed in, and when text become non overlapping, names of polylines should be visible.
I thought like this.
After every zoom-in and zoom-out I am calculating for intersection.
void myClass :: addItems()
{
QGraphicsTextItem *text1 = new QGraphicsTextItem("line1");
text1->setPos(20,40);
text1->setDefaultTextColor(Qt::black);
scene->addItem(text1);
}
void myClass::ZoomIn()
{
double scaleFactor = 1.1;
view->scale(scaleFactor,scaleFactor);
Intersection();
}
void myClass:: Intersection()
{
QList<QGraphicsTextItem*> textList;
_isIntersect = false;
foreach(QGraphicsItem* currentItem, _scene->items())
{
QGraphicsTextItem* tItem = qgraphicsitem_cast<QGraphicsTextItem*>(currentItem);
if(tItem)
{
textList.push_back(tItem);
}
}
for(int i = 0 ; i < textList.size(); i++)
{
QGraphicsTextItem* iItem = textList[i];
for( int j = i+1; j < textList.size(); j++)
{
QGraphicsTextItem* jItem = textList[j];
if(iItem->boundingRect().intersects(jItem->boundingRect()))
{
_isIntersect = true;
break;
}
}
if(_isIntersect)
break;
}
if(!_isIntersect)
qDebug()<<"Not intersected ";
else
qDebug()<<"Intersected ";
}
But for every view, above logic is showing "Intersected". Where am I wrong ?

C++ SFML Array Error: Access violation reading location 0xC0000005

I am making a game with C++ and SFML, and I am having a problem during rendering various items with array. When I try to draw an array of sprites, the debugging is alright, and there are no warning and errors, but my game program runs only 20 seconds, then it stops running, and says, 'Unhandled exception at 0x7C50CF2E(sfml-graphics-d-2.dll), ECOVID-19 SFML.exe): 0xC0000005: Access violation reading location 0xCCCCCCD0.' I did everything I can, but I don't know why this exception occurs.
Here are my codes that I suspect the causation of the error.
Thank you for reading in spite of my poor English.
#include <SFML/Graphics.hpp>
...
using namespace std;
using namespace sf;
...
int main () {
...
//item Sprites
Texture bombTex;
bombTex.loadFromFile("images/bomb.png");
Sprite bomb;
...
Texture bomb2Tex;
bomb2Tex.loadFromFile("images/bomb_2.png");
Sprite bomb_2;
...
Texture cakeTex;
cakeTex.loadFromFile("images/cake.png");
Sprite cake;
...
Texture coffeeTex;
coffeeTex.loadFromFile("images/coffee.png");
Sprite coffee;
...
Texture chickenTex;
chickenTex.loadFromFile("images/chicken.png");
Sprite chicken;
...
Texture pizzaTex;
pizzaTex.loadFromFile("images/pizza.png");
Sprite pizza;
//item array (I made an item array to display & render various items in the game screen.)
Sprite item[10]; //Although I change the array size to 4 or 5, the same exception occurs.
item[0] = bomb;
item[1] = coffee;
item[2] = bomb_2;
item[3] = chicken;
item[4] = pizza;
std::vector<Sprite> items;
items.push_back(Sprite(item[4]));
...
while (window.isOpen())
{ ...
...
for (size_t i = 0; i < items.size(); i++)
{
if (humanArr[index].getGlobalBounds().intersects(item[i].getGlobalBounds()))
//humanArr[index] is a player Sprite.
{
...
items.erase(items.begin() + i);
}
}
...
window.clear();
...
for (size_t i = 0; i < items.size(); i++)
{
window.draw(item[i]); // <- the exception error occurs here.
}
...
window.display();
}
return 0;
}
What may be happening is that when you copy the Sprite item[10]; to the std::vector<Sprite> items; the Sprite class is making a shallow copy. It means that if the Sprite class is allocating any memory with new operator and storing it in a member pointer, then the shallow copy will only copy the address to which the pointer is pointing. When you do call items.erase(items.begin() + i); the destructor of the Sprite will be called, and in the destructor it may be calling delete in that pointer to some resourse.
When you call window.draw(item[i]); the library will try to use that resource and will find an invalid address.
What I suggest is that you don't use the Sprite item[10]; and only the std::vector<Sprite> items; , like this:
std::vector<Sprite> items;
items.push_back(bomb);
items.push_back(coffee);
...
window.draw(items[i]);
You don't need to use an intermediate array, just the std::vector<Sprite>;

SFML How to draw heap of multiple textures at specific time

Hello I'm making a game using SFML, but I ran into some problems.
I want to complete this function:
if a human character collide with items, one garbage draw on screen. (5 textures)
So multiple textures of garbages stack up from the bottom of the window.
I tried to use vector and Sprite array, but my codes can't add garbages. It just draw one garbage, and then change another textures.
What should I do to keep garbages drawn until window closes?
I should add many Sprites to solve this problem, but I have no ideas... I've done everything I can.
My code is here:
// out of main loop
Texture GarbagesTexture[5];
GarbagesTexture[0] = garbageTex1;
GarbagesTexture[1] = garbageTex2;
GarbagesTexture[2] = garbageTex3;
GarbagesTexture[3] = garbageTex4;
GarbagesTexture[4] = garbageTex5;
int SelectedGarbage = 0;
vector<Sprite> Garbages;
Position GarbagesPosition;
GarbagesPosition.x = 890.f;
GarbagesPosition.y = 590.f;
// main loop
//collision
for (size_t i = 0; i < items.size(); i++)
{
if (humanArr[index].getGlobalBounds().intersects(item[i].getGlobalBounds()))
{
...
...
CollisionTime++;
SelectedGarbage++;
if (SelectedGarbage > 5) SelectedGarbage = 0;
GarbagesPosition.x = GarbagesPosition.x - 10.f;
GarbagesPosition.y = GarbagesPosition.y - 5.f;
Sprite GarbageInputSprite;
Garbages.push_back(GarbageInputSprite);
Garbages[i].setTexture(GarbagesTexture[SelectedGarbage];
Garbages[i].setPosition(GarbagesPosition.x, GarbagesPosition.y);
}
}
//draw
window.clear();
...
...
for (int i = 0; i < CollisionTime; i++)
window.draw(Garbages[i]);
window.display();
I am a poor beginner in SFML, but I'll try to do my best.
I would really appreciate it if you could help me solve this problem.

How to draw SFML shapes stored in a vector

I'm making a copy of the game "Asteroids" using SFML.
To store all my asteroids I use a std::vector<sf::ConvexShape> vector, which stores all of the asteroid shapes. The problem is drawing the shapes from the vector. I looked at this post and saw that I could use an iterator to draw my shapes (I know he used sprites in that post but I presume it makes no difference if I were to use shapes.)
So I tried that:
for(std::vector<sf::ConvexShape>::iterator it=allShapes.begin();it!= allShapes.end(); ++it){
window.draw(*it);
}
And that throws an exception, terminating my program.
FYI: above, allShapes contains sf::ConvexShape shapes.
So the question is: How do I draw shapes that I store in a vector?
Full sauce:
using namespace std;
class asteroid{
public:
sf::Vector2f pos;
double angle;
void update(sf::Vector2f);
void create(std::vector<sf::ConvexShape>);
};
/* Will be implemented later
void asteroid::update(sf::Vector2f a){
pos += a;
};
*/
void asteroid::create(std::vector<sf::ConvexShape> a){
cout << "Creating..." << endl;
a.push_back(sf::ConvexShape()); //New asteroid SHAPE
std::vector<sf::ConvexShape>::iterator tempIt = a.end();
tempIt->setPointCount(4);
for(int i = 0; i < tempIt->getPointCount()+1; i++){ //Drawing asteroid
tempIt->setPoint(i, sf::Vector2f(i*100, i*100));
}
tempIt->setFillColor(sf::Color::White);
cout << "Done!" << endl;
};
int main()
{
// Init //
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
std::vector<sf::ConvexShape> allShapes; //List of all asteroid SHAPES
std::vector<asteroid> allAsteroids; //List of asteroid CLASS OBJECTS
allAsteroids.push_back(asteroid()); //New asteroid CLASS OBJECT
for(std::vector<asteroid>::iterator it = allAsteroids.begin(); it != allAsteroids.end(); ++it){ //Creating asteroids
it->create(allShapes);
}
// Loop //
while (window.isOpen())
{
// Event //
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// Display //
window.clear();
for(std::vector<sf::ConvexShape>::iterator it = allShapes.begin(); it != allShapes.end(); ++it){
window.draw(*it);
}
window.display();
}
return 0;
}
You have a few problems here...
First:
std::vector<sf::ConvexShape>::iterator tempIt = a.end();
tempIt->setPointCount(4);
end() does NOT give you an iterator to the last element in the vector - it gives you an iterator to the "element" just past it, and is used for bounds checking (e.g. in a for loop). In other words it does not point to valid data, so the second line is invalid. There are many approaches you could use here; the following two are "most correct" though:
//with iterators using a.end() - 1
std::vector<sf::ConvexShape>::iterator tempIt = a.end() - 1;
tempIt->setPointCount(4);
//with back, accessing the element directly
a.back().setPointCount(4);
Secondly:
for(int i = 0; i < tempIt->getPointCount()+1; i++)
The asteroid has four points, so getPointCount()+1 = 5. This means that the loop executes for i = 0, 1, 2, 3, and 4 - one too many times.
Thirdly:
tempIt->setPoint(i, sf::Vector2f(i*100, i*100));
This line runs once for each i. Assuming you make the change above, this results in the points (0,0), (100, 100), (200, 200), and (300, 300). This is a line, not a convex shape. You will need to revise your formula. Setting the points directly is also a fine option, as there are only 4 to set.
You also may want to pass the vector by reference to avoid making any unnecessary copies:
void create(std::vector<sf::ConvexShape>& a){
//...
}

SFML 2.0 Looping a sprite to display more than once

I have asked a similar question in the past but I still can't get my head around this. I am doing an invaders game based on SFML 2.0. So far I have one sprite sheet which runs through using my clock. This part works just fine:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <string>
int spriteWalkSpeed = 10;
int DownspriteWalkSpeed = 5;
int up=-spriteWalkSpeed, down=DownspriteWalkSpeed, left=-spriteWalkSpeed, right=spriteWalkSpeed;
int xVelocity =0, yVelocity=0;
const int SC_WIDTH=800;
const int SC_HEIGHT= 600;
const float REFRESH_RATE =0.01f; //how often we draw the frame in seconds
const double delay=0.1;
const int SPRITEROWS=1; //number of ROWS OF SPRITES
const int SPRITECOLS=2;//number of COLS OF SPRITES
std::string gameOver = "Game Over";
int main()
{
// Create the main window
sf::RenderWindow App (sf::VideoMode(SC_WIDTH, SC_HEIGHT, 32), "Space Invaders!",sf::Style::Close );
// Create a clock for measuring time elapsed
sf::Clock Clock;
//background texture
sf::Texture backGround;
backGround.loadFromFile("images/background.jpg");
sf::Sprite back;
back.setTexture(backGround);
//load the invaders images
sf::Texture invaderTexture;
invaderTexture.loadFromFile("images/invaders.png");
sf::Sprite invadersSprite(invaderTexture);
std::vector<sf::Sprite> invaderSprites(10, sf::Sprite(invaderTexture));
int invadersWidth=invaderTexture.getSize().x;
int invadersHeight=invaderTexture.getSize().y;
int spaceWidth=invadersWidth/SPRITECOLS;
int spaceheight=invadersHeight/SPRITEROWS;
//Sprites
sf::IntRect area(0,0,spaceWidth,spaceheight);
invadersSprite.setTextureRect(area);
invadersSprite.setPosition(30, NULL);
App.setKeyRepeatEnabled(false);
//Collision detection
// Start game loop
while (App.isOpen())
{
// Process events
sf::Event Event;
while (App.pollEvent(Event))
{
// Close window : exit
if (Event.type == sf::Event::Closed)
App.close();
}
// Create an array of 10 sprites (cannot initialise them with textures here)
for (int i = 0; i < 10; i++)
{
invaderSprites[i].setPosition(30,0);
if(Clock.getElapsedTime().asSeconds()>REFRESH_RATE)
{
//carry out updating tasks
static float spriteTimer=0.0; //keep track of sprite time
spriteTimer+=Clock.getElapsedTime().asSeconds();
static int count=0; //keep track of where the sub rect is
if(spriteTimer>delay)
{
invaderSprites[i].setTextureRect(area);
++count;
invaderSprites[i].move(xVelocity, yVelocity);
if(count==SPRITECOLS) //WE HAVE MOVED OFF THE RIGHT OF THE IMAGE
{
area.left=0; //reset texture rect at left
count=0; //reset count
}
else
{
area.left+=spaceWidth; //move texture rect right
}
spriteTimer=0; //we have made one move in the sprite tile - start timing for the next move
}
Clock.restart();
}
App.draw(back);
App.draw(invaderSprites[i]);
// Finally, display the rendered frame on screen
App.display();
}
}
return EXIT_SUCCESS;
}
The issue I am having is that the sprite only shows once, not 10 times (as the for loop states)
std::vector<sf::Sprite> invaderSprites(10, sf::Sprite(invaderTexture));
// Loop over the elements of the vector of sprites
for (int i = 0; i < invaderSprites.size(); i++)
{
invaderSprites[i].setPosition(30, NULL);
}
// Create an array of 10 sprites (cannot initialise them with textures here)
sf::Sprite invaderSprites[10]; // Loop over each sprite, setting their textures
for (int i = 0; i < 10; i++)
{
invaderSprites[i].setTexture(invaderTexture);
}
I am pretty sure it has something to do with the app drawing invadersSprite whereas the loop is setup for invaderSprites. Even just a little insight into what is going wrong would be such a big help.
I am pretty sure it has something to do with the app drawing
invadersSprite whereas the loop is setup for invaderSprites.
Yes, that certainly has something to do with it. You need to call App.draw(...) for each sprite that you want to draw. You're not calling it for any of the sprites in your vector. For that, you would want a loop:
for (int i=0; i<invaderSprites.size(); ++i)
App.draw(invaderSprites[i]);
There are other problems though. For example, why are you declaring an array of sprites called invaderSprites, when you already have a vector of sprites with that same name? The latter hides the former once it is declared.
Another thing is that you are setting all the sprites to the same position, so even if if you do manage to draw them all, they will all be in the same spot, and as such they will not appear as separate objects.