/***************************************************************************
 *   Copyright (C) 2004-2005 by                                            *
 *     Paolo Sacconier   <axa1981@tin.it>                                  *
 *     Francesco Tamagni <minchiahead@hacari.org>                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>

#include "main.h"
#include "sound.h"
#include "video.h"
#include "game.h"
#include "menu.h"
#include "event.h"

using namespace gillo;

int main( int argc, char* argv[] ) {
	try {
		int i, frame_ticks = 0;
		double dt = 0;	// in seconds
		bool quit = false;
		
		/* initialitiation */		
		srand(time(NULL));
		queue<Event>	equeue;
		Video		video(Video::WINDOW);
		Sound		sound;
		Game		game(video, sound, equeue);
		Menu		menu(video, sound, equeue);

		while(!quit) {
 			SDL_Delay(1);
			dt = frame_ticks/1000.0;
			if (dt > ODE_TIME_TSHOLD)
				dt = ODE_TIME_TSHOLD;
			frame_ticks = SDL_GetTicks();
			
			/* Process incoming events. */
			SDL_Event event;
			/* Grab all the events off the queue. */
			while( SDL_PollEvent( &event ) ) {
				game.react(&event);
				menu.react(&event);
			}
			while (!equeue.empty()) {
				Event e = equeue.front();
				equeue.pop();
				switch (e.rcpt) {
					case Event::GAME:
						game.react(e.type);
						break;
					case Event::MENU:
						menu.react(e.type);
						break;
					case Event::APP:
						switch(e.type) {
							case Event::QUIT:
								quit = true;
								break;
							default:
								throw Error::Event("Unhandled event");
								break;
						}
						break;
					default:
						throw Error::Event("Unhandled event");
						break;
				}
			}
			game.update(dt);
			sound.update(dt);
			video.update(dt);
			menu.update(dt);
			frame_ticks = SDL_GetTicks() - frame_ticks;
		}
	// handling fatal errors here
	} catch (Error::Event e) {
		printf("Fatal event error: %s\n", e.msg);
		return 1; // should close all correctly
	} catch (Error::Video e) {
		printf("Fatal video error: %s\n", e.msg);
		return 1; // should close all correctly
	} catch (Error::Game e) {
		printf("Fatal world error: %s\n", e.msg);
		return 1; //@fixme should close all correctly
	}
	printf("User asked to quit, exiting... bye!\n");
	return 0;

}
