GameSwitcher::GameSwitcher()
	: background(NULL)
	, background_image(NULL)
	, background_filename("")
	, fps_ticks(0)
	, last_fps(0)
{

	// The initial state is the intro cutscene and then title screen
	GameStateTitle *title=new GameStateTitle();
	GameStateCutscene *intro = new GameStateCutscene(title);

	currentState = intro;

	if (!intro->load("cutscenes/intro.txt")) {
		delete intro;
		currentState = title;
	}

	label_fps = new WidgetLabel();
	done = false;
	loadMusic();
	loadFPS();

	loadBackgroundList();

	if (currentState->has_background)
		loadBackgroundImage();
}
 void WatcherQtGUIConfig::loadBackgroundImage(void) {
     QString filename;
     filename=QFileDialog::getOpenFileName(this, "Choose a background image file", ".", ""); 
     if (!filename.isEmpty()) {
         if (!BackgroundImage::getInstance().loadImageFile(filename.toStdString())) {
             if (QMessageBox::Yes==QMessageBox::question(NULL, 
                         tr("Error loading file"), tr("Error loading the image file. Try again?"), QMessageBox::Yes, QMessageBox::No))
                 loadBackgroundImage();
         }
         else {
             toggleBackgroundImage(true);
             emit enableBackgroundImage(true);
         }
     }
 }
void GameSwitcher::logic() {
	// reset the mouse cursor
	curs->logic();

	// Check if a the game state is to be changed and change it if necessary, deleting the old state
	GameState* newState = currentState->getRequestedGameState();
	if (newState != NULL) {
		if (currentState->reload_backgrounds || render_device->reloadGraphics())
			loadBackgroundList();

		delete currentState;
		currentState = newState;
		currentState->load_counter++;

		// reload the fps meter position
		loadFPS();

		// if this game state does not provide music, use the title theme
		if (!currentState->hasMusic)
			if (!snd->isPlayingMusic())
				loadMusic();

		// if this game state shows a background image, load it here
		if (currentState->has_background)
			loadBackgroundImage();
		else
			freeBackground();
	}

	// resize background image when window is resized
	if (inpt->window_resized && currentState->has_background) {
		refreshBackground();
	}

	currentState->logic();

	// Check if the GameState wants to quit the application
	done = currentState->isExitRequested();

	if (currentState->reload_music) {
		loadMusic();
		currentState->reload_music = false;
	}
}