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

Author Topic: RenderWindow closing instantly - getEvent not working?  (Read 5882 times)

0 Members and 1 Guest are viewing this topic.

dtCoding

  • Newbie
  • *
  • Posts: 2
    • View Profile
RenderWindow closing instantly - getEvent not working?
« on: June 17, 2011, 06:03:23 am »
Hi, I'm a beginner and I'm essentially hacking up the display tutorial into a separate class and I think I broke something.  For the record the tutorial worked fine, I'm using visual studio 2010 and I recompiled SFML for it.

I attempted to split some of the code into a class called "Game", and when I did that the window no longer constantly displayed.  The window flashes for a second, and closes.  From what I can tell it seems like while(App.GetEvent()) isn't doing anything after I moved it into the Game class.  Here's the code.

main.cpp
Code: [Select]

/* main.cpp */
#include "Game.h"

int main()
{
Game game;
game.init();

while (game.run())
{
game.handleEvents();
}

std::cout << "DONE\n"; //test
system("pause");
return EXIT_SUCCESS;
}



game.cpp
Code: [Select]

/* Game.cpp */
#include "Game.h"

void Game::init()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
App.SetFramerateLimit(60);
std::cout << "INIT\n";
}

void Game::handleEvents()
{

while(App.GetEvent(Event)) //WTF?
{
if (Event.Type == sf::Event::Closed)
running = false;

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
running = false;

std::cout << " EVENT-loop\n"; //test
}
App.Clear();
App.Display();

std::cout << "loop-check\n"; //test
running = false; //test: no infinite loop
}

bool Game::run()
{
return running;
}



and the console output after running it is this:
Code: [Select]
INIT
loop-check
DONE
Press any key to continue . . .



I added cout statements and the while loop is being ignored.  I'm assume this has something to do with my lack of knowledge on c++ classes.  This issue is driving me crazy, any help would be good.  Thanks.

edit: forgot to mention that the sf::Event variable I'm using for the broken while loop is defined in Game.h as a private member variable for the Game class.  I know the issue is in that while loop but I can't quite figure it out.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
RenderWindow closing instantly - getEvent not working?
« Reply #1 on: June 17, 2011, 07:40:46 am »
Quote
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

You're creating a new, temporary RenderWindow instance instead of initializing your class member.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
RenderWindow closing instantly - getEvent not working?
« Reply #2 on: June 17, 2011, 12:31:40 pm »
The right way is to use the constructor instead of init() methods. Then, you can directly use the initializer list to initialize your members:
Code: [Select]
Game::Game()
: app(sf::VideoMode(800, 600, 32), "SFML Graphics")
, running(true)
{
    app.SetFramerateLimit(60);
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

dtCoding

  • Newbie
  • *
  • Posts: 2
    • View Profile
RenderWindow closing instantly - getEvent not working?
« Reply #3 on: June 17, 2011, 06:58:58 pm »
that worked perfectly, thanks!  I guess I saw so many people use an init() function that I completely ignored the constructor.

Also I had no idea you can initialize a constructor that way

Code: [Select]
Game::Game()
: app(sf::VideoMode(800, 600, 32), "SFML Graphics")
, running(true)


I think I'm going to spend the day reading about classes.

 

anything