#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <devices/timer.h>
#include <graphics/modeid.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <intuition/screens.h>
#include <cybergraphics/cybergraphics.h>
#include <proto/cybergraphics.h>

#include "cntrl.h"
#include "Shared/SH_view.h"
#include "Shared/SH_timer.h"
#include "TextureMapping/TM_Structures.h"
#include "TextureMapping/TM_Render.h"
#include "TextureMapping/TM_Reader.h"
#include "TextureMapping/TM_Animation.h"
#include "TextureMapping/TM_Sequence.h"
#include "TextureMapping/TM_precalc.h"
#include "BumpMapping/BM_precalc.h"
#include "Music/Music_play.h"
#include "data/incbiny.h"
#include "sequences.h"
#include "Tools.h"

#ifdef __SHOW_TIME__
#include <time.h>
#endif

IMPORT ULONG MULUTab[513];

UBYTE tmpTxt[256*256];

UWORD Phong[256*256];

UBYTE BmpBack[320*200];
UBYTE BumpTxt[256*256];
WORD BumpMapa[256*256];
UBYTE NeraShine[256*256];

UBYTE Fish[256*256];

UBYTE Back[320*200];
ULONG BlackPal[256*3+4];

ULONG PalRGB32[256*3+4];
ULONG PalRGB32Fish[256*3+4];
ULONG PalRGB32Nera[256*3+4];
ULONG PalRGB32Column[256*3+4];
UBYTE EndChunky[320*81];

UBYTE ColorTxt[256*256];
UBYTE PlasmeTxt[256*256];
UBYTE *Textures[2] = {&ColorTxt[256*128],&PlasmeTxt[256*128]};

UBYTE ColorMap[256*256];

UBYTE *EnvMaps[2] = {&BumpTxt[256*128],&ColorMap[256*128]};
LONG FPSFlag = 0L;

struct Library __far *CyberGfxBase=NULL;
#define CYBERGFXNAME     "cybergraphics.library"
#define CYBERGFXVERSION  40L

DVIEW *DView = NULL;

#ifdef _CXOFV
#undef _CXOFV
void __stdargs _CXOVF(void){return;}
#endif

#ifdef _CXFERR
#undef _CXFERR
void __stdargs _CXFERR(code) {return;}
#endif

IMPORT BOOL Cyber;
IMPORT TIMER *Timer;

VOID main(LONG argc, STRPTR *argv)
{
ULONG i,x,y;
#ifdef __SHOW_TIME__
time_t t1,t2;
#endif
ULONG Flags = 0;

  FreeArgs(ReadArgs("FPS/S", &FPSFlag, NULL));

      if ((CyberGfxBase=OpenLibrary(CYBERGFXNAME,CYBERGFXVERSION))!=NULL)
       {
          PutStr("CyberGFX Mode On!\n");
          Cyber = TRUE;
       }
      else 
       {
          PutStr("AGA Mode On.\n");
          Flags = DVF_DBUFF |DVF_INTERLEAVED | DVF_CENTER;
       }
     
     PutStr("Prepare Data. Please Wait!\n");
#ifdef __SHOW_TIME__
 time(&t1);
#endif

		Progress();
    PrepareRGB32Pal((PALRGB *)FishPal,PalRGB32Fish);

     if(InitTimer())
      {
#ifdef __DISK_TABLES__
       if(TM_PrecalcRotateTable(180, 1024))
        {
         if(TM_PrecalcPerspectiveTable(300, 1124))
          {
           if(TM_PrecalcArcSineTable())
            {
#else
        TM_PrecalcRotateTable();
        {
         TM_PrecalcPerspectiveTable();
          {
           TM_PrecalcArcSineTable();
            {
#endif

					     PrepareMULUTab(MULUTab);

               PrepareRGB32Pal((PALRGB *)Pal1,PalRGB32);
               ScaleUP(Texture1,tmpTxt,PalRGB32,FALSE);
								PrepareEnv(tmpTxt, Phong);

               PrepareRGB32Pal((PALRGB *)BumpPal,PalRGB32Nera);
               ScaleUP(BumpTexture,tmpTxt,PalRGB32Nera,FALSE);
								PrepareTxt(tmpTxt, BumpTxt);
               ScaleUP(BumpBack,tmpTxt,PalRGB32Nera,FALSE);
               ScaleChunky(tmpTxt,BmpBack,256,256,320,200);
							 TileUP(BumpSmall,tmpTxt,8);
								PrepareBump(tmpTxt, &BumpMapa[256*128]);

                for(y=0;y<96;y++)
                 for(x=0;x<65;x++)
                   tmpTxt[(y*128)+x] = FishTxt[(y*65)+x];

                ScaleUP(tmpTxt,NeraShine,PalRGB32Fish,TRUE);
								PrepareTxt(NeraShine, Fish);
                ScaleUP(FishBack,tmpTxt,PalRGB32Fish,FALSE);
                ScaleChunky(tmpTxt,Back,256,256,320,200);

								 PrepareColorTexture(tmpTxt);
                 PrepareTxt(tmpTxt,ColorMap);

               PrepareRGB32Pal((PALRGB *)ColumnPal,PalRGB32Column);
							 TileUP(ColumnTxt,tmpTxt,4);
								PrepareTxt(tmpTxt, PlasmeTxt);
							 TileUP(ColumnCol,tmpTxt,4);
								PrepareTxt(tmpTxt, ColorTxt);


               for(i=0;i<256*3;i++) BlackPal[1+i]=0x00000000;
               BlackPal[0] = 0x01000000;

#ifdef __DISK_SHADE__
	             MakeShadeTable(NeraShine,PalRGB32Nera,47,"data/Bump.shadepalette");
#else
	             MakeShadeTable(NeraShine,PalRGB32Nera,46);
#endif

							 PrepareChunky(320,81,TextPlane,EndChunky);
							 DoRamka(EndChunky,320,81);

#ifdef __SHOW_TIME__
 time(&t2);
 printf("\nTIME:\t%d\n",t2-t1);
#endif
							 PutStr("\n");

               if((DView = OpenView(320, 200, 8, PAL_MONITOR_ID|LORES_KEY, BlackPal,Flags))!=NULL)
                {
/* Explode Sequence stuff*/
                 ExplodeList[0].Parameter = (LONG)PalRGB32;
                 EXP_Sequence(ChinaScn,ExplodeList,Phong);

/*Nera Sequence stuff*/
                 NeraList[0].Parameter = (LONG)PalRGB32Nera;
                 NERA_Sequence(NeraScn,NeraList,Phong,BmpBack,BumpMapa,NeraShine,BumpTxt);

/*Fish Sequence stuff*/
                 FishList[0].Parameter = (LONG)PalRGB32Fish;
                 FishList[72].Parameter = (LONG)PalRGB32Fish;
                 FISH_Sequence(FishScn,FishList,Fish,Back);

/*Skull sequence stuff*/
                 SkullList[0].Parameter = (LONG)PalRGB32Nera;
								 SKUL_Sequence(SkullScn,SkullList,BmpBack,Phong,NeraShine);

/*Column sequence stuff*/
                 ColumnList[0].Parameter = (LONG)PalRGB32Column;
                 ColumnList[95].Parameter = (LONG)PalRGB32Column;
                 ColumnList[153].Parameter = (LONG)PalRGB32Column;
                 ColumnList[211].Parameter = (LONG)PalRGB32Column;
                 ColumnList[279].Parameter = (LONG)PalRGB32Column;
                 CLMNEndList[0].Parameter = (LONG)PalRGB32Column;
                 CLMN_Sequence(ColumnScn,ColumnList,CLMNEndList);
/*wypierdalac!*/
     	         StopMusic();
               CloseView(DView);
              }
#ifdef __DISK_TABLES__
             FreeVec(TM_ArcSineTable);
#endif
            }
#ifdef __DISK_TABLES__
           FreeVec(TM_PerspectiveTable);
#endif
          }
#ifdef __DISK_TABLES__
         FreeVec(TM_RotateTable);
#endif
        }
       RemoveTimer();
      }

     if(Cyber)
     {
      CloseLibrary(CyberGfxBase);
     }

  PutStr("All Done.\n");
}