/******************************************************************************
*
* project name:   DOOM89
* file name:      Doom89.c
* initial date:   6/4/07
* author:         Aaron Miller
*
* description:    Doom for the 89
******************************************************************************/
#define KERNEL_FORMAT_RELOCS
#include <tigcclib.h>
#include "fat.h"
#include "extgraph.h"
#include "utils.h"
#include "indices1.h"
#include "indices2.h"
#include "indices3.h"
#include "indices4.h"
#include "indices5.h"
#include "indices6.h"
#include "indices7.h"
#include "rayline.h"
#include "levelinit.h"
#include "extrand.h"
#include "weapons.h"
#include "sys.h"

//=============================================================================
// as usual ... our main function
//=============================================================================

void _main(void) {
		
    FATCONFIG fc = {
        0,   // width of map
       	0,  // height of map
       	NULL,         // pointer to map data
        textures,    // pointer to textures array
        gg.num_sprites,           // number of sprites
        sprites,        // the sprite array
        96+3*64,     // start pos_x
        96,          // start pos_y
        ANGLE_SOUTH, // orientation
        NULL,        // destination plane0
        NULL,        // destination plane1
        NULL,        // background image
        0            // frame counter
    };
 

//=============================================================================


    //-------------------------------------------------------------------------
    // before we try to do anything else we have to initialize the engine
    // its only valid to proceed if FAT_LOADOKAY gets returned
    //-------------------------------------------------------------------------
    if ((result = FAT_LoadEngine()) != FAT_LOADOKAY) {
        switch(result) {
            case FAT_NOTGHOSTSPACE: ST_helpMsg("ERROR: client no exepacked!");break;
            case FAT_NOTFOUND:      ST_helpMsg("ERROR: no matching engine found");break;
            case FAT_LOCKFAILED:    ST_helpMsg("ERROR: locking engine failed");break;
            case FAT_OUTOFMEM:      ST_helpMsg("ERROR: out of memory");break;
            default:                ST_helpMsg("ERROR: unknown errorcode");break;
            ngetchx();
        }
        return;
    }
		
		AllocMapMem(&fc);
 		LoadTextures(&fc); 		
 		
 	
 		
 		if (!(bg_img = (unsigned short*)malloc(1152*2))) {
        PostGameCleanup(&fc,ERR_MEMORY);
    }
    
    SetBackgroundImage(&fc,background);
    


		texanim.dest       = &textures[56];
    texanim.sources    = &textures[TEXIDX_COMPUTER1];
    texanim.anim_steps = 2;
    texanim.anim_idx   = 0;
		
		texanim2.dest       = &textures[57];
    texanim2.sources    = &textures[TEXIDX_SCREEN1];
    texanim2.anim_steps = 6;
    texanim2.anim_idx   = 0;
    
    texanim3.dest       = &textures[58];
    texanim3.sources    = &textures[49];
    texanim3.anim_steps = 7;
    texanim3.anim_idx   = 0;
    
    texanim4.dest       = &textures[66];
    texanim4.sources    = &textures[67];
    texanim4.anim_steps = 3;
    texanim4.anim_idx   = 0;
    

    //-------------------------------------------------------------------------
    // add doors defined in map
    //-------------------------------------------------------------------------
    
		
    LCD_save(lcd);
    
    
 
    //-------------------------------------------------------------------------
    // allocate space for a background image and fill the lower part of the
    // light plane with 0xff to simulate a floor ...
    //-------------------------------------------------------------------------
    
    InstallDummyHandlerForINTs();

    //-------------------------------------------------------------------------
    // initialize grayscales, clear both planes and set the destination plane
    // addresses for the FAT-Engine
    //-------------------------------------------------------------------------
    if (!GrayOn()) {
        PostGameCleanup(&fc,ERR_MEMORY);
    }
    

		LoadConfig(&fc); // load configuration.
		
    //-------------------------------------------------------------------------
    // display the FAT-Engine's splashscreen
    //-------------------------------------------------------------------------
    FAT_DisplayLogo(100);

    fc.dest_plane0 = GetPlane(0)+30*2; // center vertically
    fc.dest_plane1 = GetPlane(1)+30*2; // center vertically
    

		//ShowTitleScreen(titlescreen);
    //-------------------------------------------------------------------------
    // setup calculator type specials
    //-------------------------------------------------------------------------
    if (TI89) {
        display_offset  = 0;    	
        display_offsetx = 0;
        display_offsety = 0;
    }
    else {
        // TI92p or V200
        display_offset   = 14*30+4;
        display_offsetx  = 40;
        display_offsety  = 14;
        fc.dest_plane0  += (14*30+4)/2;
        fc.dest_plane1  += (14*30+4)/2;
    }
		
		firstrun = 1;
		
		ShowTitleScreen();
    FAT_AllKeysReleased();
    
    LockHandleOfMapFile(&fc);
    ShowMenus(&fc);

		
    //-------------------------------------------------------------------------
    // initialize the GUI
    //-------------------------------------------------------------------------
    ClearGrayScreen();
    InitGUI();

    
		
		FAT_SetLowerHUD(hud1_plane1+288,hud1_plane2+288,hud1mask+288);
		ClearFacePlane();
   	UpdateFace(lookright1);
   	
   	
   	MessageEnqueue(21); // welcome message
   	
		//sprites[0].textures = cpos;
    //-------------------------------------------------------------------------
    // the main rendering loop ...
    //-------------------------------------------------------------------------
    do {
        FAT_Render(&fc); // render image    		
        CrossHairs(&fc);  // put crosshairs on screen
				HandleDoors(&fc);  // update door position
				HandleEnemies(&fc);  // AI and other functions
				HandleStaticSprites(&fc);	// Barrels and torches etc...
				HandleItems(&fc);		// Handle item events...
				HandleGunAnim(&fc);    // Animate first person HUDs...
				HandleFireBalls(&fc); // handle Imp's fireballs...
				HandleProjectiles(&fc);  // handle player projectiles...
				//WeaponStruct *curweapon = &weapon;
				
				UpdateMessages();
				
				if (gg.health <= 0) PlayerDeath(&fc); // checks if the player 
																							// has died during any part
																							// of the game...
				
				
				fc.nr_sprites = gg.num_sprites + gg.num_fireballs + gg.num_plr_proj; 
				
				FAT_UpdateAnimation(&texanim3);
				FAT_UpdateAnimation(&texanim4);
				if (gg.i == 5) {
				FAT_UpdateAnimation(&texanim);
				FAT_UpdateAnimation(&texanim2);
				UpdateFaceTimer();
				gg.i = 0;	
				}
				gg.i++;
				
				if (_keytest(RR_ENTER)) ShowMiniMap(&fc);

        if ((key = FAT_GetPadState())) {
        	
        		if (key & Controls.switch_weapon)
        		SwitchWeapon(0);
        		
        		if (key & Controls.contrast_up)
        		OSContrastUp();
        		if (key & Controls.contrast_dn)
						OSContrastDn();
						
            if (key & PADSTATE_ESC)
            {
            FAT_AllKeysReleased(); 
            ShowMenus(&fc);
            (key = FAT_GetPadState());
            FAT_AllKeysReleased();
            FAT_AllKeysReleased();
            }
            if (key & PADSTATE_UPDOWN) 
            {
            	if (key & Controls.run)
            	{
            		if (gg.shooting == 0)
            		UpdateHUDMovement();
            		PlayerMove(&fc,key & PADSTATE_UP,0,0);
            	}
            	else
            	{
            		if (gg.shooting == 0)
            		UpdateHUDMovement();
            		PlayerMove(&fc,key & PADSTATE_UP,0,1);
            	}
            			if (motion_background == 1)
									{
										if (background_number == 0)
										{
											SetBackgroundImage(&fc,dark_background);
										} 
										 else if (background_number == 1)
										{
											SetBackgroundImage(&fc,dark_background2);
										}
										 else if (background_number == 2)
										{
											SetBackgroundImage(&fc,dark_background);
										}
										 else if (background_number == 3)
										{
											SetBackgroundImage(&fc,dark_background2);
										}
										
										background_number++;
										if (background_number > 3)
										{
											background_number = 0;
										}
									}
            }

            if (key & PADSTATE_LEFTRIGHT) {
                // STRAFE KEY: DIAMOND (TI-89) / F2 (TI-92p)
                if (key & Controls.strafe) {
											PlayerMove(&fc,key&PADSTATE_RIGHT,1,0);
                }
                else {
                	if (key & Controls.run)
                	{
                		PlayerTurn(&fc,key&PADSTATE_RIGHT,ANGLE_015);
                	}
                	else
                	{
                		PlayerTurn(&fc,key&PADSTATE_RIGHT,ANGLE_015/2);
                	} 
                }
                if (motion_background == 1)
									{
										if (background_number == 0)
										{
											SetBackgroundImage(&fc,dark_background);
										} 
										 else if (background_number == 1)
										{
											SetBackgroundImage(&fc,dark_background2);
										}
										 else if (background_number == 2)
										{
											SetBackgroundImage(&fc,dark_background);
										}
										 else if (background_number == 3)
										{
											SetBackgroundImage(&fc,dark_background2);
										}
										
										background_number++;
										if (background_number > 3)
										{
											background_number = 0;
										}
									}				
            }
            if (key & Controls.action) {
                UpdateDoor(&fc,1);
                UpdateSwitch(&fc);
            }
            if (key & Controls.shoot && gg.shooting == 0) {
							gg.shooting = 1;
							gg.timer = 0;
            }
             if (key & Controls.strafe) {
               if (gg.Auto_Aim) AutoAim(&fc);
            }
            
        }
        
    }
    while(1);
    //-------------------------------------------------------------------------
    // some clean stuff ...
    //-------------------------------------------------------------------------
    PostGameCleanup(&fc,ERR_NORMAL_EXIT);
}

//#############################################################################
// Revision History
//#############################################################################
//
// $Log: fatd10.c,v $
// Revision 1.4  2002/05/08 15:35:13  tnussb
// (1) display of FAT splashscreen (FAT_DisplayLogo) added
// (2) GUI of demos completely revised
//
// Revision 1.3  2002/05/07 15:22:12  tnussb
// stupid double call of FAT_LoadEngine() fixed
//
// Revision 1.2  2002/04/30 17:41:44  tnussb
// using now defines from automatically generated headerfile indices.h for indices into
// our texture file
//
// Revision 1.1  2002/04/29 10:09:10  tnussb
// initial version
//
//
