SFML community forums

Help => General => Topic started by: flachead on March 09, 2013, 11:46:39 pm

Title: Unhandled exception (Render window)
Post by: flachead on March 09, 2013, 11:46:39 pm
I've been banging my head off a wall for two days now with this.

Game.h

Quote
#pragma once
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"

class Game
{
public:
   static void Start();
   static sf::RenderWindow& GetWindow();
   const static sf::Event& GetInput();

   const static int vWidth = 800;
   const static int vHeight = 600;
   

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

   enum GameState { Uninitialized, Playing, Exiting };

   static GameState _gameState;

   static sf::RenderWindow _window;

};

Game.cpp
Quote
#include "stdafx.h"
#include "game.h"

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

   _window.create( sf::VideoMode( vWidth, vHeight, 32), "flaccy" );

   _gameState = Game::Playing;

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

   _window.close();
   
}

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

sf::RenderWindow& Game::GetWindow()
{
   return _window;
}

const sf::Event& Game::GetInput()
{
   sf::Event currentEvent;
   _window.pollEvent( currentEvent );
   return currentEvent;
}

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

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

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

      break;
   }
}

Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_window;

When I run this I get the error...
Quote
Unhandled exception at 0x76f015de in flaccy second.exe: 0xC0000005: Access violation writing location 0x00000004.

Debugger shows this
Quote
>   flaccy second.exe!`dynamic initializer for 'Game::_window''()  Line 65 + 0x28 bytes   C++

and its stopped on sf::renderwindow in the game.cpp file.

Sorry if this is stupid but I couldn't seem to find an answer anywhere.

Visual Studio 2012, SFML 2.0
Title: Re: Unhandled exception (Render window)
Post by: FRex on March 10, 2013, 12:17:54 am
You can't have global/static render windows.
Title: Re: Unhandled exception (Render window)
Post by: flachead on March 10, 2013, 12:29:51 am
Thanks for the reply!

So I can't have sf::RenderWindow in the Game class? Taking the static away breaks everything else. Sorry i'm just really confused, im a novice lol.
Title: Re: Unhandled exception (Render window)
Post by: FRex on March 10, 2013, 12:40:45 am
No, you just can't have them globally/statically. You're doing it very very wrong, nothing should be static(in here).
Title: Re: Unhandled exception (Render window)
Post by: flachead on March 10, 2013, 12:42:30 am
Ok thanks for the help, i'll start again lol