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

Author Topic: Trouble switching states...  (Read 3885 times)

0 Members and 1 Guest are viewing this topic.

Kulade

  • Newbie
  • *
  • Posts: 8
    • View Profile
Trouble switching states...
« on: February 02, 2014, 10:01:14 pm »
Ok, so I think my problem here has a little less to do with SFML and a bit more to do with C++, but no matter.

I'm trying to draw a character sprite from a class, but this code seems to freeze the program.

I wrote a game class that essentially is the state pattern, but it also runs a window. And for the actual level [ being run in GameLoop() ], I've created a character that should appear and fall. However, it seems to freeze before it even gets there, not even succeeding at clearing the window before it switches states.

However, I've tested that code and it doesn't seem to be the problem. The problem only occurs in GameLoop, and I presume it's because of how I'm using the draw() function here:

void Game::GameLoop ()
{
// We already set up the window. Everything that occurs will be inside the window.
//I'm gonna write some code for this and link it to the title loop.
        Character Character;

        while (GameState == TestRoom);
        {

                Character.UpdatePosition();
                Window.draw(Character.Shantae);
                Window.display();

        }


}

I knew it was bad coding practice, but I didn't think it would be a big deal to make "Shantae" (the sprite, of course) temporarily public. Anyway, I'm not sure what's the best way to access this sprite so that I can use window.draw() without errors. Here's a bit of the Character class, by the way:
class Character
{
public:
        //UpdatePosition is going to check for all movement starting with falling. We can call it from within the game loop.
        void UpdatePosition();
        Character();
        sf::Sprite Shantae;

};

Thanks for any help, you guys.
« Last Edit: February 02, 2014, 10:04:14 pm by Kulade »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Trouble switching states...
« Reply #1 on: February 02, 2014, 10:07:26 pm »
I don't see where you clear() your window...
You want to
  clear();
  draw();
  display();
Every frame. The tutorials explain this.

Kulade

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trouble switching states...
« Reply #2 on: February 02, 2014, 11:07:43 pm »
I clear it in a different loop. (Thanks for reminding me about that, though..)

void Game::TitleLoop ()
{
        //this is not a while loop. it runs only once. That said, I can create a texture object here as long as I take care to destroy it
        sf::Texture TitleScreen;
        sf::Sprite TScreen;
       
        TitleScreen.loadFromFile("Cacturne.png");

        TScreen.setTexture(TitleScreen);

        //A title screen. Wooohooo!
        while (GameState == Title)
        {
                TScreen.setPosition(0,0);
                Window.draw(TScreen);
                Window.display();

                //Polling afterwards because I want to be able to destroy anything I've loaded at this point
                while(Window.pollEvent(Event))
                {
                        if (Event.type == sf::Event::Closed)
                        {
                                //allows the program to exit this loop, and then, the program
                                GameState = Exiting;
                               
                        }
                        if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Space))
                        {
                                //Whenever I switch states, I must clear the window.
                                Window.clear();
                                GameState = TestRoom;
                        }
                }


               
               
        }

}
« Last Edit: February 02, 2014, 11:18:47 pm by Kulade »

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Trouble switching states...
« Reply #3 on: February 03, 2014, 02:57:12 am »
Never never never poll after window.display(). Have you read the SFML tutorials?
Current Projects:
Technoport

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Trouble switching states...
« Reply #4 on: February 03, 2014, 03:03:46 am »
It doesn't matter if you poll before clear-draw-display or the other way around...
Back to C++ gamedev with SFML in May 2023

Kulade

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trouble switching states...
« Reply #5 on: February 03, 2014, 04:43:20 am »
I've read the tutorials; it's just been a fairly long time since I last used SFML, so I forgot how I was supposed to do a few things. Given the feedback on my code, I changed the order of the polling and added a clear() after the display() method, but it didn't do anything to solve the actual problem. The game still freezes when I attempt to switch states, and it fails to clear the screen as well. I still think the problem lies in GameLoop(), as I've gotten the state switching to work just fine with TitleLoop() (as it was) before.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Re: Re: Trouble switching states...
« Reply #6 on: February 03, 2014, 08:50:00 am »

 

while (GameState == TestRoom);
{


 

Thanks for any help, you guys.
Remove the ;

Kulade

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trouble switching states...
« Reply #7 on: February 03, 2014, 09:13:11 pm »
Wow, that is absolutely embarrassing... Sure enough, the problem is solved. Thanks for pointing that out, panithadrum!