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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - dtCoding

Pages: [1]
1
Window / RenderWindow closing instantly - getEvent not working?
« 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.

2
Window / 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.

Pages: [1]