Reputation: 89
I'm making a game in SFML and I have been making it my PC at home. Lately I had to take my project with me and continue coding it on a different PC. I configured Code::Blocks for SFML on the new PC and I opened the project and i wanted to compile it to see if it worked fine. As soon as the program window opened and all the textures have been drawn, the program crashed. I went through the code and i noticed that the main loop of the program ( which is: while(win.isOpen()) ) happened only once. I tried to fix it by rewriting parts of the code but nothing worked. Since I am a very novice programmer i have no idea how to fix this of what is causing this error. Also if you see some ways i could improve my code i would be greatfull if you could point ot those ways:D Thanks.
So here is the main loop if the game. There are sevral other files included to the project, so i figured to not post all of the files.
while(win.isOpen())
{
win.clear();
win.draw(bg);
Sprite rom[24];
rom[0].setTexture(ro[12]);
r[0].id=1;
if(Keyboard::isKeyPressed(Keyboard::D))
{
if(x<1200)
x=x+10;
}
if(Keyboard::isKeyPressed(Keyboard::A))
{
if(x>1)
x=x-10;
}
if(Keyboard::isKeyPressed(Keyboard::S))
{
if(y<900)
y=y+10;
}
if(Keyboard::isKeyPressed(Keyboard::W))
{
if(y>1)
y=y-10;
}
for(int i=0; i<=23; i++)
{
if(r[i].if_draw==true)
{
if(r[i].type==1 && r[i].siz==1)
{
rom[i].setTexture(ro[0]);
}
if(r[i].type==1 && r[i].siz==2)
{
rom[i].setTexture(ro[1]);
}
if(r[i].type==1 && r[i].siz==3)
{
rom[i].setTexture(ro[2]);
}
if(r[i].type==2 && r[i].siz==1)
{
rom[i].setTexture(ro[3]);
}
if(r[i].type==2 && r[i].siz==2)
{
rom[i].setTexture(ro[4]);
}
if(r[i].type==2 && r[i].siz==3)
{
rom[i].setTexture(ro[5]);
}
if(r[i].type==3 && r[i].siz==1)
{
rom[i].setTexture(ro[6]);
}
if(r[i].type==3 && r[i].siz==2)
{
rom[i].setTexture(ro[7]);
}
if(r[i].type==3 && r[i].siz==3)
{
rom[i].setTexture(ro[8]);
}
if(r[i].type==4 && r[i].siz==1)
{
rom[i].setTexture(ro[9]);
}
if(r[i].type==4 && r[i].siz==2)
{
rom[i].setTexture(ro[10]);
}
if(r[i].type==4 && r[i].siz==3)
{
rom[i].setTexture(ro[11]);
}
get_cords(sx,sy,r[i].id);
rom[i].setPosition(sx,sy);
win.draw(rom[i]);
}
}
while(win.pollEvent(e))
{
if(e.type==e.KeyPressed && e.key.code==Keyboard::Space)
{
if((y<=750 && y>=150) && (x>=150 && x<=1050))
{
sel_space=get_sel_space(x,y);
sel_row=get_row(sel_space);
win.draw(options_b_1);
win.display();
sel_type=get_sel_number(e,win);
if(sel_type==7)
{
sel_type=0;
sel_space=0;
sel_size=0;
sel_row=0;
break;
}
if(sel_type==6 && sel_space!=1)
{
row_t[sel_row-1]=row_t[sel_row-1]-r[sel_space-1].siz;
r[sel_space-1].demolish();
}
else if(sel_type!=7 && sel_space!=1)
{
win.draw(options_b_2);
win.display();
r[sel_space-1].if_draw=true;
for(;;)
{
sel_size=get_sel_number(e,win);
if((sel_size>=1 && sel_size<=3) || (sel_size==7))
{
break;
}
}
if(chk_id_size(sel_space,sel_size)==true)
{
if(sel_size>=1 && sel_size<=3)
{
if_b=chk_if_good_space(r,row_t[sel_row-1],sel_size,sel_space-1,sel_row);
}
else if(sel_size==7)
{
sel_type=0;
sel_space=0;
sel_size=0;
sel_row=0;
break;
}
}
else
{
if_b=false;
}
if(if_b==true)
{
chk_money_pay(money,sel_type,sel_size);
r[sel_space-1].id=sel_space;
r[sel_space-1].type=sel_type;
r[sel_space-1].siz=sel_size;
r[sel_space-1].occ=true;
if(sel_size==2)
{
r[sel_space].occ=true;
}
if(sel_size==3)
{
r[sel_space].occ=true;
r[sel_space+1].occ=true;
}
}
}
}
}
}
for(int i=0; i<=23; i++)
{
rom[i].~Sprite();
}
pointer.setPosition(x,y);
win.draw(pointer);
win.display();
}
}
Upvotes: 2
Views: 117
Reputation: 32732
You're manually calling the destructors for your Sprite
array (rom[i].~Sprite
). This results in Undefined Behavior when the loop iteration ends and the compiler calls the destructors when the lifetime of rom
ends at the end of the loop. Undefined behavior means it can do anything, including working on one system and crashing on another.
Running your code in a debugger, and seeing where it crashes, can clue you in: you want to get rid of the loop that calls ~Sprite
.
Upvotes: 1