#include "stdafx.h"
#include "common.h"
#include "demo1.h"
#include "mmsystem.h"
#include "fmod/lib/minifmod.h"
extern FMUSIC_MODULE *mod;

int fxon=1;

#ifdef DEBUG
int nosnd=1;    // ***********************
#else
int nosnd=0;
#endif

extern int secret;
int secret=0;

int config=0;


//"c:\\unzip\\try.xm";//
unsigned char *moduleName=(unsigned char*)"squademo.xm";//"f:\\sound\\plm\\funky.s3m";
int currow;
int curord;
int curpat;
int curpos;


//W32TODO MIDASmodule module;                     /* Der Module */



void CALLBACK UpdateInfo(void)
{
#ifdef NOMUS
	curpos++;
	curord=curpos/64;
	curpat=curord;
	currow=curpos&63;
#else

	//moo
	//W32TODO 

	curord = FMUSIC_GetOrder(mod);
	currow = FMUSIC_GetRow(mod);
	curpat=curord;
	curpos = currow+curord*64;
	//mytime = (float)FMUSIC_GetTime(mod) / 1000.0f;
	/*
	static MIDASplayStatus status;
        if ( !MIDASgetPlayStatus(&status) )
                MIDASerror("play status");
	curord = status.position;
	curpat = status.pattern;
	currow = status.row;
	curpos = currow+curord*64;
	*/
#endif
}

void CALLBACK midas_timer(void)
{
	dofproc(1);	// oh dear midas :) this causes it to die in windo$e
	midastime++;
}

//unsigned char *MIDASCFG="midas.cfg";

void CloseFMOD();
void InitFMOD();
void PlaySong();

void initmusic()
{
#ifndef NOMUS
	InitFMOD();
	//W32TODO MIDASstartup();
	//MIDASloadConfig(MIDASCFG);
	if ( 1 )
	{
		//W32TODO MIDASdetectSoundCard();
		//W32TODO MIDASsetOption(MIDAS_OPTION_MIXRATE, 22050);

		//W32TODO if ( !MIDASconfig() )
//W32TODO 		{
//W32TODO if ( MIDASgetLastError() ) MIDASerror("config");
//W32TODO 			printf("[ abort ]\n");
//W32TODO 			exit (1);
//W32TODO 		}
	}
//W32TODO         if ( !MIDASinit() )     MIDASerror("init");
	//MIDASsaveConfig(MIDASCFG);

#endif
}

void loadmusic()
{
#ifndef NOMUS
	printf("loading music...\n");
//W32TODO         if ( (module = MIDASloadModule(moduleName)) == NULL )   MIDASerror("loadmusic");
	
#endif
}

int thetimer = 0;

void CALLBACK mytimeproc(
  UINT uID,      
  UINT uMsg,     
  DWORD dwUser,  
  DWORD dw1,     
  DWORD dw2      
)
{
	if (uID==thetimer)
	{
		dofproc(1);
		midastime++;
	}
}


void playmusic()
{
#ifndef NOMUS

	int sync;
	int refreshRate = 60000;
	/*
	int refreshRate = MIDASgetDisplayRefreshRate();
	if (refreshRate<58000 || refreshRate>62000)
	{
		printf("NOT SYNCING to video\n");
		refreshRate=60000;
		sync=FALSE;
	}
	else
	{
		printf("Syncing to video at %0.3fHz\n",refreshRate/1000.f);
		sync=TRUE;
	}
	*/
//W32TODO         if ( !MIDASsetTimerCallbacks(refreshRate, FALSE, NULL,&midas_timer, NULL) ) MIDASerror("timer callback");
//W32TODO         if ( (MIDASplayModule(module, 0)) == 0 )   MIDASerror("play");
        //if (!MIDASsetMusicSyncCallback(&SyncCallback)) MIDASerror();

	PlaySong();
	thetimer = timeSetEvent(1000/60,0,mytimeproc,0,TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
	

#endif
	resetfproc();
}

void stopmusic()
{
#ifndef NOMUS
//W32TODO         if ( !MIDASstopModule(module) )  MIDASerror("stop");
	timeKillEvent(thetimer);
	FMUSIC_StopSong(mod);
#endif
}


void closemusic()
{
#ifndef NOMUS
//W32TODO         if ( !MIDASfreeModule(module) )  MIDASerror("free");
//W32TODO         if ( !MIDASremoveTimerCallbacks() )   MIDASerror("remove timer");
//W32TODO         if ( !MIDASclose() )  MIDASerror("close");
#endif
}


extern void	monow(int ofs, unsigned char *str);
void	monow(int ofs, unsigned char *str)
{
	return;
	unsigned char *out=(unsigned char*)(0xb0000+ofs);
	while (*str)
	{
		*out++=*str++;
		*out++=7;
	}
}

#ifdef _DEBUG
#define CHECKEND {if (kbhit()) getch();}
#else
#define CHECKEND {if (kbhit()) goto theend2;}
#endif

void fixpal(unsigned char *fname, int nc, int fc)
{
	LFILE *fi=openf(fname);
	readf(fi,mypal[fc],3*nc);
	closef(fi);
}

void setplaypos(int i)
{
#ifndef NOMUS
//W32TODO         MIDASsetPosition(i);
	
#endif

}

int oldmain(int argc,unsigned char *argv[])
{

	

		int i,c1,c2;
		float uvscale=0;

		//unsigned char *ooops=new unsigned char[1024*1024*48];//*************************

		printf("demo (c) alex evans 1997\n\n");
//W32TODO 		printf("presented in MIDAS digital sound (c) 1997 housemarque inc\n");


		printf("start with /c to configure or /h for help\n\n");

		for (i=1; i<argc; i++)
		{
				  if (stricmp("dogshit",(char*)argv[i])==0) secret=1;
				  if (argv[i][0]='-') argv[i][0]='/';
				  if (!stricmp((char*)argv[i], "/?") || !stricmp((char*)argv[i], "/h"))
				  {
					  printf("demo by alex `statix' evans (c) 1997\n\n-h gives this screen\n-c to configure sound\n-r to switch off reverb/echo\n\npress and hold both shift keys to take a screenshot\n\nenjoy.\n");
					  exit(1);
				  }
				  if (!stricmp((char*)argv[i], "/q")) nosnd^=1;


				  if (!stricmp((char*)argv[i], "/c")) config^=1;
				  if (!stricmp((char*)argv[i], "/s")) config^=1;
		}
		if (nosnd) printf("nosound\n");


		//#ifndef DEBUG
		printf("ingesting...\n");
		if (openlibf((unsigned char*)"square.pak")) error((unsigned char*)"Can't open square.pak");
		//#endif


		calcmap();

		initdemo();
		initmusic();
		loadmusic();
		tracemap();

		vidmode(0x13);
		tweak(1,0);

		_sleep(700);

		shit_init();

		playmusic();

		resetfproc();
		addfproc(faderfproc,1);


		if (secret)
		{
		secretpart();
		goto theend;
		}


		calcsphere();

		//brainpart();

		//testvideo();	goto theend;

		//cubespart2();
		//ronipart2();
		//goto theend;

		//shit();
		//goto theend;


		//setplaypos(12);//**************
		//tub();CHECKEND;	// it was stoned...


		///////////////////////////////////////////////////
		// the demo starts here

		//setplaypos(0x1a);
		//runfpath();CHECKEND;
		//goto theend;

		//babypart();CHECKEND;




		//calcfpath();


		//runfpath();CHECKEND;
		

		//cubespart();CHECKEND;
		
            title();CHECKEND;
		makebjorkpath();CHECKEND;
		spherepart();CHECKEND;
		tub();CHECKEND;


		// part 2

		cubespart();CHECKEND;



		dnapart();CHECKEND;


		babypart();CHECKEND;

		//////////////////////////////////////////////////////
		// part 3
                
                //setplaypos(0x1a);


		//setplaypos(0x2);//********************
		//makebjorkpath();
		//spherepart();


		//

		//calcfpath();




		runfpath();CHECKEND;



		//setplaypos(0x24);//********************


		tunnelpart();CHECKEND;
		ronipart();CHECKEND;

		//setplaypos(0x2a);//********************
		brainpart(CUBES2START);CHECKEND;
		
		cubespart2();CHECKEND;
		ronipart2();CHECKEND;
		brainpart(TVSTART+8);CHECKEND;
theend1:
		testvideo();

		// fin

theend:

		removefproc(faderfproc);
		stopmusic();
forgetit:
		vidmode(3);
		closedemo();
#ifdef DEBUG
		//printf("Memory: %5dk / %5dk",curalloc/1024,maxalloc/1024);
		vidmode(3);	// just to clear the screen for windwos
#endif
		closemusic();
		if (secret) printf("ah, the secret zebou. we love you, simon\n");
		else printf("oh\n");
		return(0);

theend2:
		tweak(0,0);
		memset(screenbuf,0,320*240);
		swapscreens2();
		swapscreens2();

		while (kbhit()) getch();
		setplaypos(TVSTART/64);
		goto theend1;

}

int matherr( struct exception *err )
  {
	return(1);
  }
