
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// 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.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// main.cpp
// Project: Nostril (aka Postal)
//
// This module sets up the system and RSPiX.
//
// History:
//		11/19/96 MJR	Started.
//
//		02/04/97	JMI	Now defaults to using RSP_DOSYSTEM_SLEEP instead of
//							RSP_DOSYSTEM_HOGCPU for rspSetDoSystemMode().
//
//		02/19/97	JMI	rspInitBlue was being called before 
//							rspSetWin32Video/AudioType().
//
//		02/21/97	JMI	Now gets specific display settings from game settings(INI).
//
//		02/21/97	JMI	Now gets audio lag time from INI.
//
//		03/14/97	JMI	Now, under Win32, gets value from INI indicating whether to
//							manage Windows' GUI components when the screen is resized.
//
//		03/24/97	JMI	No longer exits if SetMode() fails.  This allows the game
//							to be played on systems without sound cards.
//
//		04/14/97	JMI	Now uses zeroinit flag to SmartHeap.
//
//		04/16/97 MJR	Added more advanced audio init that retries for several
//							seconds, and then asks the user whether to abort, retry,
//							or ignore.
//
//							If video mode can't be set, it now tries to figure out
//							the exact reason and reports it to the user.
//
//							Fixed a stupid bug that prevented audio from ever working.
//
//		04/17/97 MJR	Futher clarified video error messages.
//
//							Discovered that the whole suggest-vide-mode thing is
//							different than I expected, so I restructured everything.
//
//		05/20/97	JMI	Changed the #if block around the SmartHeap specific stuff
//							to include only Intel Processor or MAC OS.
//
//		06/03/97	JMI	Now, in the case an audio mode cannot be obtained, we
//							attempt to use a message specific to the error returned
//							by RMix::SetMode() for the message box displayed.
//
//		06/16/97	JMI	Now sets do system mode to highest cooperation level in
//							debug and lowest coop level in release.
//
//		07/05/97 MJR	Added <smrtheap.hpp> if NOT in debug mode so we could
//							properly set one of its options.  Not sure why this worked
//							without <smrtheap.hpp> on the PC, but it didn't on the mac.
//
//		07/06/97 MJR	Added call to new rspSetWin32StaticColors() so we'll have
//							a common set of colors across platforms.
//
//		07/07/97 MJR	Added RSPiX profiling stuff (in disabled form).
//
//		07/13/97	JMI	Added MAIN_VANILLA_AUDIO_* macro overrides to MAIN_AUDIO_*
//							defaults.  These should be used when the INI or default
//							audio mode fails.
//
//		07/26/97	JMI	We were using the video type as the audio type.  Whoopsee.
//
//		09/03/97 PPL	Added the pragma to turn off far data for the mempool
//							initialization flag so that the smartheap library can reach it.
//
//		09/05/97 BRH	Added #if defined(MAC) around pragmas which didn't compile
//							on the PC.
//
//		09/09/97	JMI	Added check of INI flag in setting of Blue Shield Cursor
//							Mode on PC.
//
//		09/25/97	JMI	Now, on the PC, when setting the video type, sets the 
//							rspLock/Unlock behavior to be strict even when in simpler
//							modes that don't require that level of behavior (like GDI).
//
//		10/09/97	JMI	Added g_pszVideoChangeDepthErrorUnderGDI_s in the event
//							the colordepth could not be changed and the user had 
//							specified to use GDI (i.e., not DirectX).
//
//		10/21/97	JMI	Put back the play movie hack.  Also, added ability to turn
//							it off via the INI.
//
//		10/21/97	JMI	Now disables RipCord static logo only if movie successfully 
//							plays.
//
//		10/24/97	JMI	Now switches the video mode back if AVI changes (this seems
//							to happen when we're in a DirectX mode and we launch the
//							AVI).
//
//		10/31/97	JMI	Now uses MixBits entry in INI (in section Audio) to
//							determine what bit depth to mix samples with (defaults to
//							the device depth).
//
//		01/05/98	JMI	Now the MixBits var defaults to 16.
//
//		01/21/98	JMI	No longer plays movie regardless of OS or INI setting.
//
////////////////////////////////////////////////////////////////////////////////
#include <kos.h>
#include <math.h>
#include <sys/time.h>
#include "RSPiX.h"
#include "WishPiX/Prefs/prefs.h"
#include "localize.h"
#include "game.h"
#include "main.h"
#include "menus.h"
#include "title.h"
#include "input.h"

//#define RSP_PROFILE_ON
#include "ORANGE/Debug/profile.h"

#include "softfloat.h"
#define START_MENU_ADDON_ITEM

extern int access(const char* pathname, int mode)
{
	struct stat st;
	if (stat(pathname, &st) < 0)
		return -1;
	return 0;
}

extern "C" {
	int unlink_r(const char* name) {
		return 0;
	}
}

////////////////////////////////////////////////////////////////////////////////
// Macros/types/etc.
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Variables/data
////////////////////////////////////////////////////////////////////////////////
int wideScreenWidth;

////////////////////////////////////////////////////////////////////////////////
// Function prototypes
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Set up video for the game.
////////////////////////////////////////////////////////////////////////////////
static int16_t SetupVideo(					// Returns 0 on success.
	int16_t	sUseCurrentDeviceDimensions,	// In:  1 to use current video area.
	int16_t	sDeviceWidth,						// In:  Desired video hardware width.
	int16_t	sDeviceHeight)						// In:  Desired video hardware height.
{
	int16_t	sResult = 0;

	// Try setting video mode using device size specified in prefs file
	sResult = rspSetVideoMode(
		MAIN_SCREEN_DEPTH,
		sDeviceWidth,
		sDeviceHeight,
		MAIN_WINDOW_WIDTH,
		MAIN_WINDOW_HEIGHT,
		0,
		MAIN_SCREEN_SCALING);

	return sResult;
}

////////////////////////////////////////////////////////////////////////////////
//
// Set up the system environment and if all goes well, start the game.
//
////////////////////////////////////////////////////////////////////////////////

int32_t playthroughMS = 0;

int main(void)
{	

	int16_t sResult = 0;	 	
	
	// Get video preferences
	int16_t sDeviceWidth = MAIN_SCREEN_MIN_WIDTH, sDeviceHeight = MAIN_SCREEN_MIN_HEIGHT, sUseCurrentDeviceDimensions = 1;

	// Get audio preferences
	int16_t	sAudioSamplesPerSec = MAIN_AUDIO_RATE, sDeviceBitsPerSample = 16, sBufTime = MAIN_AUDIO_BUFTIME, sMixBitsPerSample = 16;

	int32_t size =  999424;
	pvr_init_params_t params = {    	
       	{ PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16 },
        size, 
        0,
        0,
        0
    };	
	
    pvr_init(&params);
			
	int done = 0;


			//---------------------------------------------------------------------------
			// Init blue layer
			//---------------------------------------------------------------------------
			if (rspInitBlue() == 0)
			{								
				//------------------------------------------------------------------------
				// Set system stuff
				//------------------------------------------------------------------------
				
				rspSetDoSystemMode(RSP_DOSYSTEM_HOGCPU);
				//------------------------------------------------------------------------
				// Setup video
				//------------------------------------------------------------------------

				//todo?

				sResult = SetupVideo(				// Returns 0 on success.
					sUseCurrentDeviceDimensions,	// In:  1 to use current video area.
					640,						// In:  Desired video hardware width.
					480);					// In:  Desired video hardware height.

				if (sResult == 0)
				{					
					//---------------------------------------------------------------
					// Setup audio - TODO
					//---------------------------------------------------------------									
					sResult	= rspSetSoundOutMode(
						sAudioSamplesPerSec, 
						4, 
						MAIN_AUDIO_CHANNELS, 
						sBufTime, 
						MAIN_AUDIO_MAXBUFTIME, 
						0, 
					0L);

					if (sResult == 0)
					{										
						// Run the game
						TheGame();										
					}
				}		
			}
			else
			{
				// Can't init blue
				TRACE("main(): Error returned by rspInitBlue()!\n");
			}
				
	

	return 0;
}
////////////////////////////////////////////////////////////////////////////////
// EOF
/////////////////////////////////////////////////////////////////////////////////
