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

Author Topic: SFML Window Quiting when I start program  (Read 1331 times)

0 Members and 1 Guest are viewing this topic.

noaboa

  • Newbie
  • *
  • Posts: 14
    • View Profile
SFML Window Quiting when I start program
« on: July 28, 2015, 03:12:54 pm »
Hi, I just started at a pang tutorial. It's made from SFML 1.6 but I have found and corrected what's wrong and updated it to the newest version. But I seem to have a error, whenever I load the window it quits. I can't seem to find out whats wrong. Heres my code

Game.h:

I have added the headers and everything else

Code: [Select]
public:
static void Start();

private:
static bool IsExisting();
static void GameLoop();

enum GameState { Unititialized, ShowingSplash, Paused,
ShowingMenu, Playing, Exiting };

static GameState _gameState;
static sf::RenderWindow _mainWindow;

Game.cpp
Code: [Select]
#include "stdafx.h"
#include "Game.h"

void Game::Start(void)
{
if (_gameState != Unititialized)
return;

_mainWindow.create(sf::VideoMode(1024, 768, 32), "Pang!");
_gameState = Game::Playing;

while (!IsExisting())
{
GameLoop();
}

_mainWindow.close();
}

bool Game::IsExisting()
{
if (_gameState == Game::Exiting)
return false;
else
return true;
}

void Game::GameLoop()
{
sf::Event currentEvent;
while (_mainWindow.pollEvent(currentEvent))
{

switch (_gameState)
{
case Game::Playing:
{
_mainWindow.clear(sf::Color(255, 0, 0));
_mainWindow.display();

if (currentEvent.type == sf::Event::Closed)
{
_gameState = Game::Exiting;
}
break;
}
}
}
}


Game::GameState Game::_gameState = Unititialized;
sf::RenderWindow Game::_mainWindow;

stadfax.h:
Code: [Select]
#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>

#include <map>
#include <iostream>
#include <cassert>

Thanks in advance

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: SFML Window Quiting when I start program
« Reply #1 on: July 28, 2015, 03:53:52 pm »
This:
while (!IsExisting())
 

Here you are telling the while loop to work until the 'IsExiting()' function returns true, because you put the '!' mark, which negates the logical expression.

Inside the "IsExiting" function:
bool Game::IsExisting()
{
        if (_gameState == Game::Exiting)
                return false;
        else
                return true;
}

You tell the program as long as '_gameState' is not equal to Game::Exiting, the 'isExiting()' will always return true.
Because of the '!' inside the condition parameter for your while loop , it will negate the returning value of the 'isExiting()' function (i.e. true boolean value), making it false for the condition parameter.

The while loop will loop as long as condition is true. But the moment the condition is false, it will break the loop.

This is exactly what happens in your case. ;)
« Last Edit: July 28, 2015, 03:55:49 pm by Brax »

noaboa

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML Window Quiting when I start program
« Reply #2 on: July 28, 2015, 04:12:02 pm »
Thanks it worked