#include <cstdlib>
#include <iostream>

#include "Engine.h"

#include "Controller.h"
#include "View.h"
#include "Model.h"
#include "Physics.h"
#include "EntityFactory.h"

#ifdef WIN32
#include <Windows.h>
#endif

using namespace std;

int main (int argc, char* argv[]) {
	cout << "Game Start" << endl;

#ifdef WIN32
	HWND hWnd = GetConsoleWindow();
	ShowWindow( hWnd, SW_HIDE );
#endif
	
	Engine::Engine engine;

	engine.SetEntityFactory (new asteroids::EntityFactory);
	engine.SetController (new asteroids::Controller);
	engine.SetModel (new asteroids::Model);
	engine.SetPhysics (new asteroids::Physics);
	engine.SetView (new asteroids::View);

	SetLogPrintLevel (Engine::LogLevelMessage);
	Engine::SetLogFilename ("game.log");

	if (engine.Init (argc, argv) != 0) {
		cout << "Could not start engine!" << endl;
		exit (-1);
	}

	// Load the icon
	Uint32          colorkey;
	SDL_Surface     *image = NULL;
	image = SDL_LoadBMP("./data/textures/icon.bmp");
	if (!image)
		Engine::LogWarning("Could not load icon: ./data/textures/icon.bmp");
	else
		SDL_WM_SetIcon(image,NULL);

	SDL_WM_SetCaption("Asteroids -BETA1-","Asteroids -BETA 1-");

	engine.GetView()->SetGridSize (8,8);
	dynamic_cast<asteroids::Physics*>(engine.GetPhysics())->SetWorldSize (26, 20);

	// run the default commands and load the configuration
	Engine::RunCommand ("exec asteroids.rc");
	Engine::RunCommand ("exec config.rc");

	engine.MainLoop ();

	// save the configuration
	std::ofstream config_file ("config.rc");
	config_file << "set effects_volume " << Engine::GetEffectsVolume() << std::endl;
	config_file << "set music_volume " << Engine::GetMusicVolume() << std::endl;
	config_file.close();

	SDL_WM_SetIcon(NULL,NULL);
	SDL_FreeSurface (image);

	engine.Destroy ();

	cout << "Game Quit" << endl;

	return 0;
}
