// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See LICENSE for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
CVS AUTO-GENERATED INFO
$Author: dauti $
$Date: 2006/03/16 20:37:17 $
$Name: rel-0-3-patches $
$Revision: 1.21 $
$State: Exp $

$Log: main.cpp,v $
Revision 1.21  2006/03/16 20:37:17  dauti
Include the name of the called executable in the help text. Just "blinkensisters" was wrong.

Revision 1.20  2006/03/16 10:47:53  cavac
Changes some .bmp to .jpg for smaller downloads
Re-compressed sisters_orient.ogg for smaller download
Updated .cpp files to reflect changes

Revision 1.19  2006/03/02 13:03:42  cavac
Re-formatted all .cpp and .h files a bit to get rid of the TAB/SPACE mess
that makes troubles with XCode's "re-format" feature

Revision 1.18  2006/03/02 12:43:32  cavac
Non-Releasemode modus now starts game directly (without the menu for the moment)

Revision 1.17  2006/02/27 15:46:30  cavac
Made errors with DIE more visible (added some figlet art)

Revision 1.16  2006/02/27 09:56:31  cavac
Removed old lua testbindings in scripting_lua*

Revision 1.15  2006/02/26 14:06:05  cavac
Added menu support

Revision 1.14  2006/02/23 20:19:58  dauti
Set a window title

Revision 1.13  2006/02/17 10:49:42  cavac
Added error-handling

Revision 1.12  2006/02/10 18:56:17  cavac
Code Audit: Fixed many 64Bit based problems (mostly untested)

Revision 1.11  2006/02/06 11:57:07  cavac
Command-line option "-v" now also output the most important compile-flags

Revision 1.10  2006/02/06 11:47:10  cavac
Changed command-line support in main.cpp to use cmdopts.*

Revision 1.9  2006/02/05 22:24:57  cavac
Added bool to sound.cpp to check for initSound()
ifdef'ed out calls to popt

Revision 1.8  2006/02/05 19:07:57  dauti
Added command line option support.

Revision 1.7  2006/02/02 13:35:29  cavac
Added file/basic framework for LUA scripting engine (untested)

Revision 1.6  2006/01/27 17:04:02  cavac
Updated VC6 workspace to current release
Added ogg/vorbis support for Win32
Added SDL_Image and SDL_ttf support
Changed build- and installer-system for easier maintenance

Revision 1.5  2006/01/23 19:19:45  cavac
Added define ENABLE_FULLSCREEN to enable/disable the availability
of the full-screen mode
Added define START_WITH_FULLSCREEN to set default screen mode when
ENABLE_FULLSCREEN is defined
User can now switch between windowed and fullscreen with the "f"-key

Revision 1.4  2006/01/23 18:47:55  cavac
Patches for unix make system for building on Linux  and other
Unix systems (thanks to: Dauti)
Tests for new colission detection (default: Use old one)
Added Joystick/Gamepad support (only tested with a cheap logitech gamepad)
Fixed some warnings on linux/gcc3
Added true-type fonts for planned menu system
Added library SDL_ttf in xcode for planned menu-system

Revision 1.3  2006/01/09 14:06:59  cavac
Disabled Sound and fullscreen mode for WIN32

Revision 1.2  2006/01/09 00:50:34  cavac
Bugfixed CVS autogenerated entries in background.cpp
Added CVS autogenerated entries to remaining .cpp and .h files
Updated copyright from "2005 Rene Schickbauer" to "2005-06 Rene Schickbauer"


--------------------------------------------------------------------*/
#endif //NOP_CODE

#include <stdlib.h>
#include "globals.h"
#include "intro.h"
#include "sound.h"
#include "engine.h"
#include "showtext.h"
#include "debug.h"
#include "levelhandler.h"
#include "showloading.h"
#include "monstersprites.h"
#include "gameengine.h"
#include "joystick.h"
#include "cmdopts.h"
#include "errorhandler.h"
#include "fonthandler.h"
#include "menu.h"


// Screen surface
SDL_Surface *gScreen = 0;

// Last iteration's tick value
#include <SDL_types.h>
Uint32 gLastTick;

#ifdef ENABLE_FULLSCREEN
#ifdef START_WITH_FULLSCREEN
bool isFullscreen = true;
#else // NO START_WITH_FULLSCREEN
bool isFullscreen = false;
#endif // START_WITH_FULLSCREEN
#endif // ENABLE_FULLSCREEN
#ifdef DISABLE_SOUND
bool soundOK = false;
#else // DISABLE_SOUND
bool soundOK = true;
#endif // DISABLE_SOUND

// Entry point
int main(int ARGC, char **ARGV)
{
	
	//DIE(ERROR_FILE_WRITE, "Just testing...");
	
	// Parse command-line options
	if(!initCmdOpt(ARGC, ARGV) || getCmdOptBool("-h")) {
		helpCmdOpt(ARGV[0]); // <- automatic exit(1)
	}
	
#ifdef ENABLE_FULLSCREEN
	if(getCmdOptBool("-f")) {
		isFullscreen = true;
	}
	if(getCmdOptBool("-w")) {
		isFullscreen = false;
	}	
#endif // ENABLE_FULLSCREEN
#ifndef DISABLE_SOUND
	if(getCmdOptBool("-s")) {
		soundOK = false;
	}	
#endif // DISABLE_SOUND
	
	if(getCmdOptBool("-v")) {
		printf("BlinkenSisters - Hunt for the Lost Pixels\nLostPixels %s \n",LONGVERSION);
		printf("\nCompile-Flags:\n");
#ifdef ENABLE_FULLSCREEN
		printf("  Fullscreen: ENABLED\n");
#else // NO ENABLE_FULLSCREEN
		printf("  Fullscreen: DISABLED\n");
#endif // ENABLE_FULLSCREEN
#ifndef DISABLE_SOUND
		printf("  Sound-Support: ENABLED\n");
#else // NO DISABLE_SOUND
		printf("  Sound-Support: DISABLED\n");
#endif // DISABLE_SOUND
		printf("  Resource-Path: %s\n", RESPATH);
		exit(0);
	}	
	// END processing command-line arguments
	
	// Initialize SDL's subsystems - in this case, only video.
#ifdef CAVAC_RELEASEMODE
	if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0 ) 
#else // NO CAVAC_RELEASEMODE
		if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE) < 0 ) 
#endif // CAVAC_RELEASEMODE
		{
			fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
			exit(1);
		}
			
			// Initialize sound subsystem
			initSound();
	initJoystick();
	initFontHandler();
	
	// Register SDL_Quit to be called at exit; makes sure things are
	// cleaned up when we quit.
	//atexit(deInitJoystick);
	atexit(deInitFontHandler);
	atexit(deInitSound);
	atexit(SDL_Quit);
	
	// Attempt to create a SCR_WIDTHxSCR_HEIGHT window with 32bit pixels.
	
#ifdef ENABLE_FULLSCREEN
	if(isFullscreen) {
		gScreen = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, 32, SDL_SWSURFACE | SDL_FULLSCREEN);
	} else {
		gScreen = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, 32, SDL_SWSURFACE);
	}
#else // NO ENABLE_FULLSCREEN
	gScreen = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, 32, SDL_SWSURFACE);
#endif // ENABLE_FULLSCREEN
	
	// Hide the mousepointer
	SDL_ShowCursor(0);
	/* Set a window title. */
	SDL_WM_SetCaption("blinkensisters", NULL); // Window title: blinkensisters, Icon title: none
	
	
	
	// Initialize loader screen and show it
	initShowLoading("loading.jpg");
	showLoading();
	loadMonsterSprites();
	
	// Inits that need an initialized screen
	initShowText();
	initFPSCounter();
	initMenu();
	
	// If we fail, return error.
	if ( gScreen == NULL ) 
	{
		fprintf(stderr, "Unable to set SCR_WIDTHxSCR_HEIGHT video: %s\n", SDL_GetError());
		exit(1);
	}
	
	
	// Display the Intro sequence
#ifdef CAVAC_RELEASEMODE
	displayIntro();
	
	
	while(menuDisplay()) {
		initGame();
		playGame();
	}
#else // NO CAVAC_RELEASEMODE
	initGame();
	playGame();
#endif // CAVAC_RELEASEMODE
	
	// Clean up
	deInitMenu();
	deInitShowLoading();
	unloadMonsterSprites();
	SDL_ShowCursor(1);
	return 0;
}
