Today I was writing some SDL C++ program, with squares called particles. My problem is that, for some reason, variable y in instances of class Particle is always resetting to the value passed into the constructor after incrementing it by 1. I'm storing objects in a list. That's a method called every frame:
void everyFrame(){
this->y+=1;
std::cout<<"update y: "<<this->y<<std::endl;
}
And this method is also called every frame, after the everyFrame() method:
void blit(){
this->rect.x=this->x*10;
this->rect.y=this->y*10;
std::cout<<"blitting y: "<<this->y<<std::endl;
SDL_BlitSurface(this->image,NULL,screen,&this->rect);
}
This is part of the code, where I'm adding an object/objects to the list:
std::list<Particle> particles;
particles.push_back(Particle(2,10,5));
And there I'm executing these 2 methods in the main loop:
for(Particle x:particles){
x.everyFrame();
}
for(Particle x:particles){
x.blit();
}
The console output of the program when y 5 is passed into the constructor is just:
update y: 6
blitting y: 5
looped around. I also found out that when I'm storing an object in a normal variable, not in the list, then it works. Is there any reason/fix for it not working in the list?