Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Pictures do not display, help please.  (Read 2480 times)

0 Members and 1 Guest are viewing this topic.

BlurrOfDeath

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Pictures do not display, help please.
« on: May 25, 2012, 10:53:43 am »
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;


}







 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Pictures do not display, help please.
« Reply #1 on: May 25, 2012, 11:05:02 am »
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.
« Last Edit: May 25, 2012, 11:08:15 am by Laurent »
Laurent Gomila - SFML developer

BlurrOfDeath

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Pictures do not display, help please.
« Reply #2 on: May 25, 2012, 11:07:59 am »
What do I put in them?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Pictures do not display, help please.
« Reply #3 on: May 25, 2012, 11:08:40 am »
Quote
What do I put in them?
Something that tells you "hey, there was an error".
Laurent Gomila - SFML developer

BlurrOfDeath

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Pictures do not display, help please.
« Reply #4 on: May 25, 2012, 11:14:45 am »
I don't know how to do that.(i'm new to coding)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Pictures do not display, help please.
« Reply #5 on: May 25, 2012, 11:21:59 am »
No offense, but if you don't even know how to handle a simple error, then you should learn more before trying to do complicated things (graphics programming with an external library is a complicated thing for a total beginner).

Otherwise we'll see you everyday on this forum, and we'll give you answers that you'll most likely not understand cpmpletely.
Laurent Gomila - SFML developer

ingwik

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Pictures do not display, help please.
« Reply #6 on: May 25, 2012, 09:15:37 pm »
You've made a bunch of newbie errors, I have put CHANGE at each row that I've changed for you.
*Remember to clear screen right before you redraw
*Keep the draw routine inside the App open routine, else it will just draw once.
*I don't know why you restricted the frame rate to just 10? I commented out anyway.
*You have to draw the background first, and then the other sprites. If you do as you did, you draw the players and then the background on top of them so the background will cover them.
*You never placed the background anywhere, usually you put it on coordinates 0,0.
*Make a habit of putting as much comments as possible in the code, it makes it a lot easier to follow
*I never bothered to do any other changes than to make the images visible, from now, you're on your own but you have the altered code below. I tested the code with random pictures from Internet and they displayed as expected.
------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
using namespace std;

int main()
{
   sf::Sprite Sprite; //Holds person image-person1 to 3
   sf::Image person[3]; //Holds the 3 different person images
   sf::RenderWindow App(sf::VideoMode(800, 600, 32),"Test");
   int whichimage = 0; //start person with image 1?
   sf:: Clock timer;
   sf:: Event Event;

   sf:: Sprite Sprite2; //Holds background
   sf:: Sprite Sprite3; //Holds ground
   sf:: Image background; //Is given to Sprite2
   sf:: Image ground; //Is given to Sprite3


   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);//Here starts the person
   Sprite2.SetImage(background);
   Sprite2.SetPosition(0.0f, 0.0f); //CHANGE added a place to draw background
   Sprite3.SetImage(ground);
   Sprite3.SetPosition(0.f, 548.f); //Here is the ground

   // App.SetFramerateLimit(10); //CHANGE removed, maximum framerate? why 10???

  while (App.IsOpened())
   { //App opened start
     if (App.GetInput().IsKeyDown(sf::Key::Right))
      { //Right key start
         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;}

    } //end of right key press


   if (App.GetInput().IsKeyDown(sf::Key::Left))
    { //Begin left key press
       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;}

    }//End of left key pressed

   if(App.GetEvent(Event))
   {
    if (Event.Type == sf::Event::Closed)
    App.Close();
   }

   App.Clear(sf::Color::White); //CHANGE added to the end of cykle

   App.Draw(Sprite2); //Background
   App.Draw(Sprite3); //Ground
   App.Draw(Sprite); //Person CHANGE is drawn after ground and background

   App.Display();
  } //CHANGE, moved so the draw routine is within app opened
    return 0;

}
« Last Edit: May 25, 2012, 09:24:32 pm by ingwik »

BlurrOfDeath

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Pictures do not display, help please.
« Reply #7 on: May 27, 2012, 09:49:17 am »
Thanks so much!!

 

anything