When I run this program, the render window opens, but no images display and it lags.
It's basically an animation which moves on keypress, any help is appreciated.
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
int main()
{
sf::Sprite Sprite;
sf::Image person[3];
sf::RenderWindow App(sf::VideoMode(800, 600, 32),"Test");
int whichimage = 0;
sf:: Clock timer;
sf:: Event Event;
sf:: Sprite Sprite2;
sf:: Sprite Sprite3;
sf:: Image background;
sf:: Image ground;
if(!person[0].LoadFromFile("person1.jpg")) {};
if(!person[1].LoadFromFile("person2.jpg")) {};
if(!person[2].LoadFromFile("person3.jpg")) {};
if(!background.LoadFromFile("background.jpg")) {};
if(!ground.LoadFromFile("ground.jpg")) {};
person[0].CreateMaskFromColor((sf::Color::White),0);
person[1].CreateMaskFromColor((sf::Color::White),0);
person[2].CreateMaskFromColor((sf::Color::White),0);
Sprite.SetImage(person[0]);
Sprite.SetPosition(50.f, 255.f);
Sprite2.SetImage(background);
Sprite3.SetImage(ground);
Sprite3.SetPosition(0.f, 548.f);
App.SetFramerateLimit(10);
while (App.IsOpened())
{
App.Clear(sf::Color::White);
if (App.GetInput().IsKeyDown(sf::Key::Right))
{
Sprite.SetX(Sprite.GetPosition().x+10);
if(Sprite.GetPosition().x > 700)
{
Sprite.SetX(Sprite.GetPosition().x - 10);
}
if(Sprite.GetPosition().y > 400)
{
Sprite.SetX(Sprite.GetPosition().y - 10);//ground
}
if (timer.GetElapsedTime() >= 1.f)
{
go:
Sprite.SetImage(person[+1]);
if (++whichimage > 2)
{
whichimage = 0;
}
Sprite.SetImage(person[whichimage]);
timer.Reset();
}else{goto go;}
}
if (App.GetInput().IsKeyDown(sf::Key::Left))
{
Sprite.SetX(Sprite.GetPosition().x-10);
if(Sprite.GetPosition().x < -10)
{
Sprite.SetX(Sprite.GetPosition().x+10);
}
if(Sprite.GetPosition().y > 400)
{
Sprite.SetX(Sprite.GetPosition().y - 10);
}
if (timer.GetElapsedTime() >= 1.f)
{
go2:
Sprite.SetImage(person[+1]);
if (++whichimage > 2)
{
whichimage = 0;
}
Sprite.SetImage(person[whichimage]);
timer.Reset();
}else{goto go2;}
}
}
if(App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
App.Draw(Sprite);
App.Draw(Sprite2);
App.Draw(Sprite3);
App.Display();
return 0;
}
Can you please:
- indent your code properly and delete these big blocks of empty lines (it's hardly readable)
- remove all the unneeded lines of code (if you have a problem to display an image: keep only the code that loads and draws this image)
- check that the image was correctly loaded (ie. put some code in your {} to handle loading errors)
PS: unless I misread, this code:
if (...)
{
go:
...
}
else
{
goto go;
}
is totally useless, because you'll end up executing what's after "go:" whether the condition is true or false.