


// **************************************************************************
// **************************************************************************
// **************************************************************************
// **                                                                      **
// ** SHELL.C                                                       MODULE **
// **                                                                      **
// ** Main shell code.                                                     **
// **                                                                      **
// ** Last modified : 12 Jan 1999 by Mike Knauer                           **
// ** Last modified : 03 Mar 1997 by John Brandwood                        **
// **                                                                      **
// **************************************************************************
// **************************************************************************
// **************************************************************************

#if 1	//---------------------   phil   --------------------------------------
#define ALL_PROCESSES	1	// 0- stubs,  1= old code


// Game's N64 header files.

#include "generic.h"
#include "gameinc.h"	

// John's N64 header files.

#include "teamdata.h"

#include "shellres.h"

#include "shell.h"
#include "stats.h"
#include "shellgfx.h"
#include "states.h"
#include "shadfont.h"

#include "popup.h"
#include "mempak.h"

#include "messages.h"
#include "help.h"
#include "audio.h"
#include "misc.h"
#include "define.h"

#define IN_2_SHELL_C_FILES 1	// functions in bball2 temp shell

#define MJK_KLUDGE	0	// 

#define ABORT_PROGRAM	exit(1)
//#define ABORT_PROGRAM	


#define GFX_LIST_SIZE ((8192+1024+2048+1024) * sizeof(Gfx))

extern dyn
	DBdata[];


// phil's audio stuff...

void
	*ShellFX;


#if defined (DEBUG)
global int dbr=DP_RGB(25);
global int dbg=DP_RGB(6);
global int dbb=DP_RGB(6);
global int dba=0xa0;	// for modifing colors in debugger
#endif

global int LeaveShellMethod;


global int demoTimeout;

//#include "res.id"	//
#include "res/shell.id"		// this has actual data (true sprite addresses)


// dummy globals so it links (where are these???)
//global int pauseTeam;
global int configUseCtrlPak;
global int configUseRumble;


global int ShellPlayerOnScreen;
int ControllerLockOnPlayer[MAX_CONTROLLERS];
global int WriteFRError = 0;	// for debugging without the debugger

global int EasterTeam[2] = {0,0};

//
//  conditional compile flags
//

#define  SHOW_FONT      0
#define  SHOW_LINES     1
#define  SHOW_SCREEN    0

#define SHOW_BG_COURT_LINES		0

//
//
//

global	UB                  aub__gCustomHeads [CUSTOM_HEADS][2] =
	{
	0, PLYR_HEAD_BLOCK,
	0, PLYR_HEAD_FLAT,
	1, PLYR_HEAD_THIN,
	1, PLYR_HEAD_HAIR,
	2, PLYR_HEAD_NORMAL,
	2, PLYR_HEAD_NORMAL,
	3, PLYR_HEAD_NORMAL,
	3, PLYR_HEAD_BLOCK,
	4, PLYR_HEAD_THIN,
	4, PLYR_HEAD_NORMAL,
	5, PLYR_HEAD_BLOCK,
	5, PLYR_HEAD_NORMAL,
	8, PLYR_HEAD_HAIR,
	8, PLYR_HEAD_BLOCK,
	6, PLYR_HEAD_HAIR,
	7, PLYR_HEAD_HAIR,
	7, PLYR_HEAD_NORMAL,
	6, PLYR_HEAD_NORMAL,
	3, PLYR_HEAD_NORMAL,
	3, PLYR_HEAD_NORMAL,
	};

//
//
//
extern int HintCounter;

extern	unsigned long int   ID_FNT_TINY10;
extern	unsigned long int   ID_FNT_BLKBT18;
extern	unsigned long int   ID_FNT_BLKCN18;
extern	unsigned long int   ID_FNT_BLKBT24;
//extern	unsigned long int   ID_FNT_BLKBT22;
//extern	unsigned long int   ID_FNT_BLKCN22;
//extern	unsigned long int   ID_FNT_BLKCN24;
//extern	unsigned long int   ID_FNT_BLKCN50;
//extern	unsigned long int   ID_FNT_BLKEX26;
//extern	unsigned long int   ID_FNT_EXBT18;

//#define	FONT_TINY10         1
//#define	FONT_BLKBT18        2
//#define	FONT_BLKCN18        3
//#define	FONT_BLKBT24        4


static	unsigned long *     apul_sFntResShell [] =
	{
	&ID_FNT_TINY10,
	&ID_FNT_BLKBT18,
	&ID_FNT_BLKCN18,
	&ID_FNT_BLKBT24,
//	&ID_FNT_BLKBT22,
//	&ID_FNT_BLKCN22,
//	&ID_FNT_BLKCN24,
//	&ID_FNT_BLKCN50,
//	&ID_FNT_BLKEX26,
//	&ID_FNT_EXBT18,
	NULL
	};

/*********
#define	FPAL_BLACK          1
#define	FPAL_WHITE          2
#define	FPAL_GREY           3
#define	FPAL_RED            4
#define	FPAL_YELLOW         5
#define	FPAL_BLUE           6
#define	FPAL_GREEN          7
#define	FPAL_BENCH          8
#define	FPAL_PERSIL         9
#define	FPAL_LTGREY         10
#define	FPAL_BRIGHT_WHITE	11
******/
static	FNTPAL_T            acl__sFntPalShell [] =
	{
	{ 0x00, 0x00, 0x00, 0x00 },		// FFLG_LOLITE// 0 -
	{ 0x00, 0x00, 0x00, 0x00 },		// normal
	{ 0x00, 0x00, 0x00, 0x00 },		// FFLG_HILITE

	{ 0x10, 0x10, 0x10, 0x00 },		// 1 - Black
	{ 0x10, 0x10, 0x10, 0x00 },
	{ 0x10, 0x10, 0x10, 0x00 },

	{ 0x4C, 0x4C, 0x4C, 0x00 },		// 2 - White
	{ 0x98, 0x98, 0x98, 0x00 },
	{ 0x10, 0x10, 0x10, 0x00 },

	{ 0x20, 0x20, 0x20, 0x00 },		// 3 - Grey
	{ 0x4C, 0x4C, 0x4C, 0x00 },
	{ 0x60, 0x20, 0x20, 0x00 },

	{ 0x90, 0x1C, 0x1C, 0x00 },		// 4 - Red
	{ 0x58, 0x10, 0x10, 0x00 },
	{ 0xA0, 0x80, 0x50, 0x00 },

	{ 0x00, 0x00, 0x00, 0x00 },		// 5 - Yellow (with red highlight)
	{ 0xA0, 0x80, 0x50, 0x00 },
	{ 0x58, 0x10, 0x10, 0x00 },

	{ 0x00, 0x00, 0x00, 0x00 },		// 6 - Blue
	{ 0x30, 0x88, 0x90, 0x00 },
	{ 0x00, 0x00, 0x00, 0x00 },

	{ 0x00, 0x00, 0x00, 0x00 },		// 7 - Green
	{ 0x00, 0x90, 0x00, 0x00 },
	{ 0x00, 0x00, 0x00, 0x00 },

	{ 0x00, 0x00, 0x00, 0x00 },		// 8 - Bench
	{ 0x7A, 0x51, 0x3A, 0x00 },
	{ 0x00, 0x00, 0x00, 0x00 },

	{ 0x00, 0x00, 0x00, 0x00 },		// 9 - Persil - full white
	{ 0xFF, 0xFF, 0xFF, 0x00 },
	{ 0x00, 0x00, 0x00, 0x00 },

	{ 0x4C, 0x4C, 0x4C, 0x00 },		// 10 - LtGrey
	{ 0x80, 0x80, 0x80, 0x00 },
	{ 0x10, 0x10, 0x10, 0x00 },

	{ 0x4C, 0x4C, 0x4C, 0x00 },		// 11 - Bright White
	{ 0xc8, 0xc8, 0xc8, 0x00 },
	{ 0x10, 0x10, 0x10, 0x00 },

	{ DP_RGB(64), DP_RGB(23), DP_RGB(9), 0x00 },	// 12 - Orange
	{ DP_RGB(64), DP_RGB(23), DP_RGB(9), 0x00 },
	{ DP_RGB(64), DP_RGB(23), DP_RGB(9), 0x00 },


	};

//--------------------------------------------------------------------------
// NOTE: redefine our SPRITE_LIST to fit into the following structure
//  (i.e. concatenate the name to get the ID number

#define sm(name)	{ NULL, &ID_##name},

global	SPR_LIST_T          acl__gShellSpr [] =
	{
	{ NULL, NULL                },		//  0

	SPRITE_LIST

/***	
	{ NULL, &ID_SPR_ATITLE      },		// (1+ 0)
	{ NULL, &ID_SPR_BTITLE      },		// (1+ 1)
	{ NULL, &ID_SPR_ETITLE0     },		// (1+ 2)
	{ NULL, &ID_SPR_ETITLE1     },		// (1+ 3)
	{ NULL, &ID_SPR_ETITLE2     },		// (1+ 4)
	{ NULL, &ID_SPR_EBITS       },		// (1+ 5)
	{ NULL, &ID_SPR_EBOXL       },		// (1+ 6)
	{ NULL, &ID_SPR_EBOXR       },		// (1+ 7)
	{ NULL, &ID_SPR_ESTATGAM    },		// (1+ 8)
	{ NULL, &ID_SPR_ESTATNBA    },		// (1+ 9)
	{ NULL, &ID_SPR_ESTATSSN    },		// (1+10)
	{ NULL, &ID_SPR_ESTATPLY    },		// (1+11)
	{ NULL, &ID_SPR_FTITLE      },		// (1+12)
	{ NULL, &ID_SPR_GTITLE      },		// (1+13)
	{ NULL, &ID_SPR_GTITLE1     },		// (1+14)
	{ NULL, &ID_SPR_GUSER       },		// (1+15)
	{ NULL, &ID_SPR_GCPU        },		// (1+16)
	{ NULL, &ID_SPR_HTITLE      },		// (1+17)
	{ NULL, &ID_SPR_HTITLE1     },		// (1+18)
	{ NULL, &ID_SPR_ITITLE      },		// (1+19)
	{ NULL, &ID_SPR_ITTL1NBA    },		// (1+20)
	{ NULL, &ID_SPR_ITTL1SSN    },		// (1+21)
	{ NULL, &ID_SPR_ITTL2GAM    },		// (1+22)
	{ NULL, &ID_SPR_ITTL2NBA    },		// (1+23)
	{ NULL, &ID_SPR_ITTL2PLY    },		// (1+24)
	{ NULL, &ID_SPR_ITTL2SSN    },		// (1+25)
	{ NULL, &ID_SPR_ITTL3GAM    },		// (1+26)
	{ NULL, &ID_SPR_ITTL3ATR    },		// (1+27)
	{ NULL, &ID_SPR_ITTL3MVP    },		// (1+28)
	{ NULL, &ID_SPR_ITTL4NBA    },		// (1+29)
	{ NULL, &ID_SPR_ITTL4PLY    },		// (1+30)
	{ NULL, &ID_SPR_ITTL4SSN    },		// (1+31)
	{ NULL, &ID_SPR_IMVP        },		// (1+32)
	{ NULL, &ID_SPR_JTITLE      },		// (1+33)
	{ NULL, &ID_SPR_KEAST       },		// (1+34)
	{ NULL, &ID_SPR_KWEST       },		// (1+35)
	{ NULL, &ID_SPR_KTEAMUSR    },		// (1+36)
	{ NULL, &ID_SPR_KTEAMCPU    },		// (1+37)
	{ NULL, &ID_SPR_KTEAMNUL    },		// (1+38)
	{ NULL, &ID_SPR_LTITLE      },		// (1+39)
	{ NULL, &ID_SPR_LBITS       },		// (1+40)
	{ NULL, &ID_SPR_LTITLE1     },		// (1+41)
	{ NULL, &ID_SPR_PTITLE      },		// (1+42)
	{ NULL, &ID_SPR_QTITLE      },		// (1+43)
	{ NULL, &ID_SPR_RTITLE      },		// (1+44)
	{ NULL, &ID_SPR_STITLE      },		// (1+45)
	{ NULL, &ID_SPR_STITLE1     },		// (1+46)
	{ NULL, &ID_SPR_STITLE2     },		// (1+47)
	{ NULL, &ID_SPR_SMALLBTN    },		// (1+48)
	{ NULL, &ID_SPR_CONTROL1    },		// (1+49)
	{ NULL, &ID_SPR_CONTROL2    },		// (1+50)
	{ NULL, &ID_SPR_CONTROL3    },		// (1+51)
	{ NULL, &ID_SPR_CONTROL4    },		// (1+52)
	{ NULL, &ID_SPR_TITLE_HT    },		// (1+53)
	{ NULL, &ID_SPR_TITLE_FT    },		// (1+54)
	{ NULL, &ID_SPR_MAINOPT0    },		// (1+55)
	{ NULL, &ID_SPR_MAINOPT1    },		// (1+56)
	{ NULL, &ID_SPR_MAINOPT2    },		// (1+57)
	{ NULL, &ID_SPR_MAINOPT3    },		// (1+58)
	{ NULL, &ID_SPR_MAINOPT4    },		// (1+59)
	{ NULL, &ID_SPR_MAINOPT5    },		// (1+60)
	{ NULL, &ID_SPR_MSEASFN0    },		// (1+61)
	{ NULL, &ID_SPR_MSEASFN1    },		// (1+62)
	{ NULL, &ID_SPR_MSEASFN2    },		// (1+63)
	{ NULL, &ID_SPR_MPOFFFN0    },		// (1+64)
	{ NULL, &ID_SPR_MPOFFFN1    },		// (1+65)
	{ NULL, &ID_SPR_MPOFFFN2    },		// (1+66)
	{ NULL, &ID_SPR_MROSTFN0    },		// (1+67)
	{ NULL, &ID_SPR_MROSTFN1    },		// (1+68)
	{ NULL, &ID_SPR_MROSTFN2    },		// (1+69)
	{ NULL, &ID_SPR_MROSTFN3    },		// (1+70)
	{ NULL, &ID_SPR_MSTATFN0    },		// (1+71)
	{ NULL, &ID_SPR_MSTATFN1    },		// (1+72)
	{ NULL, &ID_SPR_MSTATFN2    },		// (1+73)
	{ NULL, &ID_SPR_MSTATFN3    },		// (1+74)

//  below new for 1999	
	{ NULL, &ID_SPR_MENU_HL },		
	{ NULL, &ID_SPR_MENULBAR},	// colorful "courtside" - text is separate(atitle, ititle, etc.)


	{ NULL, &ID_SPR_CNBUT1C},	//			(1+77)	// home 
	{ NULL, &ID_SPR_CNBUT1A},	//			(1+78)   //away
	{ NULL, &ID_SPR_CNBUT1B},	//			(1+79)

	{ NULL, &ID_SPR_CNBUT2C},	//			(1+80) // home 
	{ NULL, &ID_SPR_CNBUT2A},	//			(1+81)   //away
	{ NULL, &ID_SPR_CNBUT2B},	//			(1+82)

	{ NULL, &ID_SPR_CNBUT3C},	//			(1+83)	// home 
	{ NULL, &ID_SPR_CNBUT3A},	//			(1+84)   //away
	{ NULL, &ID_SPR_CNBUT3B},	//			(1+85)

	{ NULL, &ID_SPR_CNBUT4C},	//			(1+86)	// home 
	{ NULL, &ID_SPR_CNBUT4A},	//			(1+87)   //away
	{ NULL, &ID_SPR_CNBUT4B},	//			(1+88)


****/


// end "new" sprites for 1999
	
	{ NULL, &ID_SPR_FLATNUL     },		// (SPR_FLATS+ 0)
	{ NULL, &ID_SPR_FLATATL     },		// (SPR_FLATS+ 1)
	{ NULL, &ID_SPR_FLATBOS     },		// (SPR_FLATS+ 2)
	{ NULL, &ID_SPR_FLATCHA     },		// (SPR_FLATS+ 3)
	{ NULL, &ID_SPR_FLATCHI     },		// (SPR_FLATS+ 4)
	{ NULL, &ID_SPR_FLATCLE     },		// (SPR_FLATS+ 5)
	{ NULL, &ID_SPR_FLATDAL     },		// (SPR_FLATS+ 6)
	{ NULL, &ID_SPR_FLATDEN     },		// (SPR_FLATS+ 7)
	{ NULL, &ID_SPR_FLATDET     },		// (SPR_FLATS+ 8)
	{ NULL, &ID_SPR_FLATGS      },		// (SPR_FLATS+ 9)
	{ NULL, &ID_SPR_FLATHOU     },		// (SPR_FLATS+10)
	{ NULL, &ID_SPR_FLATIND     },		// (SPR_FLATS+11)
	{ NULL, &ID_SPR_FLATLAC     },		// (SPR_FLATS+12)
	{ NULL, &ID_SPR_FLATLAL     },		// (SPR_FLATS+13)
	{ NULL, &ID_SPR_FLATMIA     },		// (SPR_FLATS+14)
	{ NULL, &ID_SPR_FLATMIL     },		// (SPR_FLATS+15)
	{ NULL, &ID_SPR_FLATMIN     },		// (SPR_FLATS+16)
	{ NULL, &ID_SPR_FLATNJ      },		// (SPR_FLATS+17)
	{ NULL, &ID_SPR_FLATNY      },		// (SPR_FLATS+18)
	{ NULL, &ID_SPR_FLATORL     },		// (SPR_FLATS+19)
	{ NULL, &ID_SPR_FLATPHI     },		// (SPR_FLATS+20)
	{ NULL, &ID_SPR_FLATPHO     },		// (SPR_FLATS+21)
	{ NULL, &ID_SPR_FLATPOR     },		// (SPR_FLATS+22)
	{ NULL, &ID_SPR_FLATSAC     },		// (SPR_FLATS+23)
	{ NULL, &ID_SPR_FLATSA      },		// (SPR_FLATS+24)
	{ NULL, &ID_SPR_FLATSEA     },		// (SPR_FLATS+25)
	{ NULL, &ID_SPR_FLATTOR     },		// (SPR_FLATS+26)
	{ NULL, &ID_SPR_FLATUTA     },		// (SPR_FLATS+27)
	{ NULL, &ID_SPR_FLATVAN     },		// (SPR_FLATS+28)
	{ NULL, &ID_SPR_FLATWAS     },		// (SPR_FLATS+29)
	{ NULL, &ID_SPR_FLATEAS     },		// (SPR_FLATS+30)
	{ NULL, &ID_SPR_FLATWES     },		// (SPR_FLATS+31)
	{ NULL, &ID_SPR_FLATLFP     },		// (SPR_FLATS+32)
	{ NULL, &ID_SPR_FLATNIN     },		// (SPR_FLATS+33)
	{ NULL, &ID_SPR_FLATNOA     },		// (SPR_FLATS+34)

	{ NULL, &ID_SPR_LOGONUL_000 },		// (SPR_LOGOS+0x0000)
	{ NULL, &ID_SPR_LOGONUL_001 },		// (SPR_LOGOS+0x0001)
	{ NULL, &ID_SPR_LOGONUL_002 },		// (SPR_LOGOS+0x0002)
	{ NULL, &ID_SPR_LOGONUL_003 },		// (SPR_LOGOS+0x0003)
	{ NULL, &ID_SPR_LOGONUL_004 },		// (SPR_LOGOS+0x0004)
	{ NULL, &ID_SPR_LOGONUL_005 },		// (SPR_LOGOS+0x0005)
	{ NULL, &ID_SPR_LOGONUL_006 },		// (SPR_LOGOS+0x0006)
	{ NULL, &ID_SPR_LOGONUL_007 },		// (SPR_LOGOS+0x0007)

	{ NULL, &ID_SPR_LOGOATL_000 },		// (SPR_LOGOS+0x0008)
	{ NULL, &ID_SPR_LOGOATL_001 },		// (SPR_LOGOS+0x0009)
	{ NULL, &ID_SPR_LOGOATL_002 },		// (SPR_LOGOS+0x000A)
	{ NULL, &ID_SPR_LOGOATL_003 },		// (SPR_LOGOS+0x000B)
	{ NULL, &ID_SPR_LOGOATL_004 },		// (SPR_LOGOS+0x000C)
	{ NULL, &ID_SPR_LOGOATL_005 },		// (SPR_LOGOS+0x000D)
	{ NULL, &ID_SPR_LOGOATL_006 },		// (SPR_LOGOS+0x000E)
	{ NULL, &ID_SPR_LOGOATL_007 },		// (SPR_LOGOS+0x000F)

	{ NULL, &ID_SPR_LOGOBOS_000 },		// (SPR_LOGOS+0x0010)
	{ NULL, &ID_SPR_LOGOBOS_001 },		// (SPR_LOGOS+0x0011)
	{ NULL, &ID_SPR_LOGOBOS_002 },		// (SPR_LOGOS+0x0012)
	{ NULL, &ID_SPR_LOGOBOS_003 },		// (SPR_LOGOS+0x0013)
	{ NULL, &ID_SPR_LOGOBOS_004 },		// (SPR_LOGOS+0x0014)
	{ NULL, &ID_SPR_LOGOBOS_005 },		// (SPR_LOGOS+0x0015)
	{ NULL, &ID_SPR_LOGOBOS_006 },		// (SPR_LOGOS+0x0016)
	{ NULL, &ID_SPR_LOGOBOS_007 },		// (SPR_LOGOS+0x0017)

	{ NULL, &ID_SPR_LOGOCHA_000 },		// (SPR_LOGOS+0x0018)
	{ NULL, &ID_SPR_LOGOCHA_001 },		// (SPR_LOGOS+0x0019)
	{ NULL, &ID_SPR_LOGOCHA_002 },		// (SPR_LOGOS+0x001A)
	{ NULL, &ID_SPR_LOGOCHA_003 },		// (SPR_LOGOS+0x001B)
	{ NULL, &ID_SPR_LOGOCHA_004 },		// (SPR_LOGOS+0x001C)
	{ NULL, &ID_SPR_LOGOCHA_005 },		// (SPR_LOGOS+0x001D)
	{ NULL, &ID_SPR_LOGOCHA_006 },		// (SPR_LOGOS+0x001E)
	{ NULL, &ID_SPR_LOGOCHA_007 },		// (SPR_LOGOS+0x001F)

	{ NULL, &ID_SPR_LOGOCHI_000 },		// (SPR_LOGOS+0x0020)
	{ NULL, &ID_SPR_LOGOCHI_001 },		// (SPR_LOGOS+0x0021)
	{ NULL, &ID_SPR_LOGOCHI_002 },		// (SPR_LOGOS+0x0022)
	{ NULL, &ID_SPR_LOGOCHI_003 },		// (SPR_LOGOS+0x0023)
	{ NULL, &ID_SPR_LOGOCHI_004 },		// (SPR_LOGOS+0x0024)
	{ NULL, &ID_SPR_LOGOCHI_005 },		// (SPR_LOGOS+0x0025)
	{ NULL, &ID_SPR_LOGOCHI_006 },		// (SPR_LOGOS+0x0026)
	{ NULL, &ID_SPR_LOGOCHI_007 },		// (SPR_LOGOS+0x0027)

	{ NULL, &ID_SPR_LOGOCLE_000 },		// (SPR_LOGOS+0x0028)
	{ NULL, &ID_SPR_LOGOCLE_001 },		// (SPR_LOGOS+0x0029)
	{ NULL, &ID_SPR_LOGOCLE_002 },		// (SPR_LOGOS+0x002A)
	{ NULL, &ID_SPR_LOGOCLE_003 },		// (SPR_LOGOS+0x002B)
	{ NULL, &ID_SPR_LOGOCLE_004 },		// (SPR_LOGOS+0x002C)
	{ NULL, &ID_SPR_LOGOCLE_005 },		// (SPR_LOGOS+0x002D)
	{ NULL, &ID_SPR_LOGOCLE_006 },		// (SPR_LOGOS+0x002E)
	{ NULL, &ID_SPR_LOGOCLE_007 },		// (SPR_LOGOS+0x002F)

	{ NULL, &ID_SPR_LOGODAL_000 },		// (SPR_LOGOS+0x0030)
	{ NULL, &ID_SPR_LOGODAL_001 },		// (SPR_LOGOS+0x0031)
	{ NULL, &ID_SPR_LOGODAL_002 },		// (SPR_LOGOS+0x0032)
	{ NULL, &ID_SPR_LOGODAL_003 },		// (SPR_LOGOS+0x0033)
	{ NULL, &ID_SPR_LOGODAL_004 },		// (SPR_LOGOS+0x0034)
	{ NULL, &ID_SPR_LOGODAL_005 },		// (SPR_LOGOS+0x0035)
	{ NULL, &ID_SPR_LOGODAL_006 },		// (SPR_LOGOS+0x0036)
	{ NULL, &ID_SPR_LOGODAL_007 },		// (SPR_LOGOS+0x0037)

	{ NULL, &ID_SPR_LOGODEN_000 },		// (SPR_LOGOS+0x0038)
	{ NULL, &ID_SPR_LOGODEN_001 },		// (SPR_LOGOS+0x0039)
	{ NULL, &ID_SPR_LOGODEN_002 },		// (SPR_LOGOS+0x003A)
	{ NULL, &ID_SPR_LOGODEN_003 },		// (SPR_LOGOS+0x003B)
	{ NULL, &ID_SPR_LOGODEN_004 },		// (SPR_LOGOS+0x003C)
	{ NULL, &ID_SPR_LOGODEN_005 },		// (SPR_LOGOS+0x003D)
	{ NULL, &ID_SPR_LOGODEN_006 },		// (SPR_LOGOS+0x003E)
	{ NULL, &ID_SPR_LOGODEN_007 },		// (SPR_LOGOS+0x003F)

	{ NULL, &ID_SPR_LOGODET_000 },		// (SPR_LOGOS+0x0040)
	{ NULL, &ID_SPR_LOGODET_001 },		// (SPR_LOGOS+0x0041)
	{ NULL, &ID_SPR_LOGODET_002 },		// (SPR_LOGOS+0x0042)
	{ NULL, &ID_SPR_LOGODET_003 },		// (SPR_LOGOS+0x0043)
	{ NULL, &ID_SPR_LOGODET_004 },		// (SPR_LOGOS+0x0044)
	{ NULL, &ID_SPR_LOGODET_005 },		// (SPR_LOGOS+0x0045)
	{ NULL, &ID_SPR_LOGODET_006 },		// (SPR_LOGOS+0x0046)
	{ NULL, &ID_SPR_LOGODET_007 },		// (SPR_LOGOS+0x0047)

	{ NULL, &ID_SPR_LOGOGS_000  },		// (SPR_LOGOS+0x0048)
	{ NULL, &ID_SPR_LOGOGS_001  },		// (SPR_LOGOS+0x0049)
	{ NULL, &ID_SPR_LOGOGS_002  },		// (SPR_LOGOS+0x004A)
	{ NULL, &ID_SPR_LOGOGS_003  },		// (SPR_LOGOS+0x004B)
	{ NULL, &ID_SPR_LOGOGS_004  },		// (SPR_LOGOS+0x004C)
	{ NULL, &ID_SPR_LOGOGS_005  },		// (SPR_LOGOS+0x004D)
	{ NULL, &ID_SPR_LOGOGS_006  },		// (SPR_LOGOS+0x004E)
	{ NULL, &ID_SPR_LOGOGS_007  },		// (SPR_LOGOS+0x004F)

	{ NULL, &ID_SPR_LOGOHOU_000 },		// (SPR_LOGOS+0x0050)
	{ NULL, &ID_SPR_LOGOHOU_001 },		// (SPR_LOGOS+0x0051)
	{ NULL, &ID_SPR_LOGOHOU_002 },		// (SPR_LOGOS+0x0052)
	{ NULL, &ID_SPR_LOGOHOU_003 },		// (SPR_LOGOS+0x0053)
	{ NULL, &ID_SPR_LOGOHOU_004 },		// (SPR_LOGOS+0x0054)
	{ NULL, &ID_SPR_LOGOHOU_005 },		// (SPR_LOGOS+0x0055)
	{ NULL, &ID_SPR_LOGOHOU_006 },		// (SPR_LOGOS+0x0056)
	{ NULL, &ID_SPR_LOGOHOU_007 },		// (SPR_LOGOS+0x0057)

	{ NULL, &ID_SPR_LOGOIND_000 },		// (SPR_LOGOS+0x0058)
	{ NULL, &ID_SPR_LOGOIND_001 },		// (SPR_LOGOS+0x0059)
	{ NULL, &ID_SPR_LOGOIND_002 },		// (SPR_LOGOS+0x005A)
	{ NULL, &ID_SPR_LOGOIND_003 },		// (SPR_LOGOS+0x005B)
	{ NULL, &ID_SPR_LOGOIND_004 },		// (SPR_LOGOS+0x005C)
	{ NULL, &ID_SPR_LOGOIND_005 },		// (SPR_LOGOS+0x005D)
	{ NULL, &ID_SPR_LOGOIND_006 },		// (SPR_LOGOS+0x005E)
	{ NULL, &ID_SPR_LOGOIND_007 },		// (SPR_LOGOS+0x005F)

	{ NULL, &ID_SPR_LOGOLAC_000 },		// (SPR_LOGOS+0x0060)
	{ NULL, &ID_SPR_LOGOLAC_001 },		// (SPR_LOGOS+0x0061)
	{ NULL, &ID_SPR_LOGOLAC_002 },		// (SPR_LOGOS+0x0062)
	{ NULL, &ID_SPR_LOGOLAC_003 },		// (SPR_LOGOS+0x0063)
	{ NULL, &ID_SPR_LOGOLAC_004 },		// (SPR_LOGOS+0x0064)
	{ NULL, &ID_SPR_LOGOLAC_005 },		// (SPR_LOGOS+0x0065)
	{ NULL, &ID_SPR_LOGOLAC_006 },		// (SPR_LOGOS+0x0066)
	{ NULL, &ID_SPR_LOGOLAC_007 },		// (SPR_LOGOS+0x0067)

	{ NULL, &ID_SPR_LOGOLAL_000 },		// (SPR_LOGOS+0x0068)
	{ NULL, &ID_SPR_LOGOLAL_001 },		// (SPR_LOGOS+0x0069)
	{ NULL, &ID_SPR_LOGOLAL_002 },		// (SPR_LOGOS+0x006A)
	{ NULL, &ID_SPR_LOGOLAL_003 },		// (SPR_LOGOS+0x006B)
	{ NULL, &ID_SPR_LOGOLAL_004 },		// (SPR_LOGOS+0x006C)
	{ NULL, &ID_SPR_LOGOLAL_005 },		// (SPR_LOGOS+0x006D)
	{ NULL, &ID_SPR_LOGOLAL_006 },		// (SPR_LOGOS+0x006E)
	{ NULL, &ID_SPR_LOGOLAL_007 },		// (SPR_LOGOS+0x006F)

	{ NULL, &ID_SPR_LOGOMIA_000 },		// (SPR_LOGOS+0x0070)
	{ NULL, &ID_SPR_LOGOMIA_001 },		// (SPR_LOGOS+0x0071)
	{ NULL, &ID_SPR_LOGOMIA_002 },		// (SPR_LOGOS+0x0072)
	{ NULL, &ID_SPR_LOGOMIA_003 },		// (SPR_LOGOS+0x0073)
	{ NULL, &ID_SPR_LOGOMIA_004 },		// (SPR_LOGOS+0x0074)
	{ NULL, &ID_SPR_LOGOMIA_005 },		// (SPR_LOGOS+0x0075)
	{ NULL, &ID_SPR_LOGOMIA_006 },		// (SPR_LOGOS+0x0076)
	{ NULL, &ID_SPR_LOGOMIA_007 },		// (SPR_LOGOS+0x0077)

	{ NULL, &ID_SPR_LOGOMIL_000 },		// (SPR_LOGOS+0x0078)
	{ NULL, &ID_SPR_LOGOMIL_001 },		// (SPR_LOGOS+0x0079)
	{ NULL, &ID_SPR_LOGOMIL_002 },		// (SPR_LOGOS+0x007A)
	{ NULL, &ID_SPR_LOGOMIL_003 },		// (SPR_LOGOS+0x007B)
	{ NULL, &ID_SPR_LOGOMIL_004 },		// (SPR_LOGOS+0x007C)
	{ NULL, &ID_SPR_LOGOMIL_005 },		// (SPR_LOGOS+0x007D)
	{ NULL, &ID_SPR_LOGOMIL_006 },		// (SPR_LOGOS+0x007E)
	{ NULL, &ID_SPR_LOGOMIL_007 },		// (SPR_LOGOS+0x007F)

	{ NULL, &ID_SPR_LOGOMIN_000 },		// (SPR_LOGOS+0x0080)
	{ NULL, &ID_SPR_LOGOMIN_001 },		// (SPR_LOGOS+0x0081)
	{ NULL, &ID_SPR_LOGOMIN_002 },		// (SPR_LOGOS+0x0082)
	{ NULL, &ID_SPR_LOGOMIN_003 },		// (SPR_LOGOS+0x0083)
	{ NULL, &ID_SPR_LOGOMIN_004 },		// (SPR_LOGOS+0x0084)
	{ NULL, &ID_SPR_LOGOMIN_005 },		// (SPR_LOGOS+0x0085)
	{ NULL, &ID_SPR_LOGOMIN_006 },		// (SPR_LOGOS+0x0086)
	{ NULL, &ID_SPR_LOGOMIN_007 },		// (SPR_LOGOS+0x0087)

	{ NULL, &ID_SPR_LOGONJ_000  },		// (SPR_LOGOS+0x0088)
	{ NULL, &ID_SPR_LOGONJ_001  },		// (SPR_LOGOS+0x0089)
	{ NULL, &ID_SPR_LOGONJ_002  },		// (SPR_LOGOS+0x008A)
	{ NULL, &ID_SPR_LOGONJ_003  },		// (SPR_LOGOS+0x008B)
	{ NULL, &ID_SPR_LOGONJ_004  },		// (SPR_LOGOS+0x008C)
	{ NULL, &ID_SPR_LOGONJ_005  },		// (SPR_LOGOS+0x008D)
	{ NULL, &ID_SPR_LOGONJ_006  },		// (SPR_LOGOS+0x008E)
	{ NULL, &ID_SPR_LOGONJ_007  },		// (SPR_LOGOS+0x008F)

	{ NULL, &ID_SPR_LOGONY_000  },		// (SPR_LOGOS+0x0090)
	{ NULL, &ID_SPR_LOGONY_001  },		// (SPR_LOGOS+0x0091)
	{ NULL, &ID_SPR_LOGONY_002  },		// (SPR_LOGOS+0x0092)
	{ NULL, &ID_SPR_LOGONY_003  },		// (SPR_LOGOS+0x0093)
	{ NULL, &ID_SPR_LOGONY_004  },		// (SPR_LOGOS+0x0094)
	{ NULL, &ID_SPR_LOGONY_005  },		// (SPR_LOGOS+0x0095)
	{ NULL, &ID_SPR_LOGONY_006  },		// (SPR_LOGOS+0x0096)
	{ NULL, &ID_SPR_LOGONY_007  },		// (SPR_LOGOS+0x0097)

	{ NULL, &ID_SPR_LOGOORL_000 },		// (SPR_LOGOS+0x0098)
	{ NULL, &ID_SPR_LOGOORL_001 },		// (SPR_LOGOS+0x0099)
	{ NULL, &ID_SPR_LOGOORL_002 },		// (SPR_LOGOS+0x009A)
	{ NULL, &ID_SPR_LOGOORL_003 },		// (SPR_LOGOS+0x009B)
	{ NULL, &ID_SPR_LOGOORL_004 },		// (SPR_LOGOS+0x009C)
	{ NULL, &ID_SPR_LOGOORL_005 },		// (SPR_LOGOS+0x009D)
	{ NULL, &ID_SPR_LOGOORL_006 },		// (SPR_LOGOS+0x009E)
	{ NULL, &ID_SPR_LOGOORL_007 },		// (SPR_LOGOS+0x009F)

	{ NULL, &ID_SPR_LOGOPHI_000 },		// (SPR_LOGOS+0x00A0)
	{ NULL, &ID_SPR_LOGOPHI_001 },		// (SPR_LOGOS+0x00A1)
	{ NULL, &ID_SPR_LOGOPHI_002 },		// (SPR_LOGOS+0x00A2)
	{ NULL, &ID_SPR_LOGOPHI_003 },		// (SPR_LOGOS+0x00A3)
	{ NULL, &ID_SPR_LOGOPHI_004 },		// (SPR_LOGOS+0x00A4)
	{ NULL, &ID_SPR_LOGOPHI_005 },		// (SPR_LOGOS+0x00A5)
	{ NULL, &ID_SPR_LOGOPHI_006 },		// (SPR_LOGOS+0x00A6)
	{ NULL, &ID_SPR_LOGOPHI_007 },		// (SPR_LOGOS+0x00A7)

	{ NULL, &ID_SPR_LOGOPHO_000 },		// (SPR_LOGOS+0x00A8)
	{ NULL, &ID_SPR_LOGOPHO_001 },		// (SPR_LOGOS+0x00A9)
	{ NULL, &ID_SPR_LOGOPHO_002 },		// (SPR_LOGOS+0x00AA)
	{ NULL, &ID_SPR_LOGOPHO_003 },		// (SPR_LOGOS+0x00AB)
	{ NULL, &ID_SPR_LOGOPHO_004 },		// (SPR_LOGOS+0x00AC)
	{ NULL, &ID_SPR_LOGOPHO_005 },		// (SPR_LOGOS+0x00AD)
	{ NULL, &ID_SPR_LOGOPHO_006 },		// (SPR_LOGOS+0x00AE)
	{ NULL, &ID_SPR_LOGOPHO_007 },		// (SPR_LOGOS+0x00AF)

	{ NULL, &ID_SPR_LOGOPOR_000 },		// (SPR_LOGOS+0x00B0)
	{ NULL, &ID_SPR_LOGOPOR_001 },		// (SPR_LOGOS+0x00B1)
	{ NULL, &ID_SPR_LOGOPOR_002 },		// (SPR_LOGOS+0x00B2)
	{ NULL, &ID_SPR_LOGOPOR_003 },		// (SPR_LOGOS+0x00B3)
	{ NULL, &ID_SPR_LOGOPOR_004 },		// (SPR_LOGOS+0x00B4)
	{ NULL, &ID_SPR_LOGOPOR_005 },		// (SPR_LOGOS+0x00B5)
	{ NULL, &ID_SPR_LOGOPOR_006 },		// (SPR_LOGOS+0x00B6)
	{ NULL, &ID_SPR_LOGOPOR_007 },		// (SPR_LOGOS+0x00B7)

	{ NULL, &ID_SPR_LOGOSAC_000 },		// (SPR_LOGOS+0x00B8)
	{ NULL, &ID_SPR_LOGOSAC_001 },		// (SPR_LOGOS+0x00B9)
	{ NULL, &ID_SPR_LOGOSAC_002 },		// (SPR_LOGOS+0x00BA)
	{ NULL, &ID_SPR_LOGOSAC_003 },		// (SPR_LOGOS+0x00BB)
	{ NULL, &ID_SPR_LOGOSAC_004 },		// (SPR_LOGOS+0x00BC)
	{ NULL, &ID_SPR_LOGOSAC_005 },		// (SPR_LOGOS+0x00BD)
	{ NULL, &ID_SPR_LOGOSAC_006 },		// (SPR_LOGOS+0x00BE)
	{ NULL, &ID_SPR_LOGOSAC_007 },		// (SPR_LOGOS+0x00BF)

	{ NULL, &ID_SPR_LOGOSA_000  },		// (SPR_LOGOS+0x00C0)
	{ NULL, &ID_SPR_LOGOSA_001  },		// (SPR_LOGOS+0x00C1)
	{ NULL, &ID_SPR_LOGOSA_002  },		// (SPR_LOGOS+0x00C2)
	{ NULL, &ID_SPR_LOGOSA_003  },		// (SPR_LOGOS+0x00C3)
	{ NULL, &ID_SPR_LOGOSA_004  },		// (SPR_LOGOS+0x00C4)
	{ NULL, &ID_SPR_LOGOSA_005  },		// (SPR_LOGOS+0x00C5)
	{ NULL, &ID_SPR_LOGOSA_006  },		// (SPR_LOGOS+0x00C6)
	{ NULL, &ID_SPR_LOGOSA_007  },		// (SPR_LOGOS+0x00C7)

	{ NULL, &ID_SPR_LOGOSEA_000 },		// (SPR_LOGOS+0x00C8)
	{ NULL, &ID_SPR_LOGOSEA_001 },		// (SPR_LOGOS+0x00C9)
	{ NULL, &ID_SPR_LOGOSEA_002 },		// (SPR_LOGOS+0x00CA)
	{ NULL, &ID_SPR_LOGOSEA_003 },		// (SPR_LOGOS+0x00CB)
	{ NULL, &ID_SPR_LOGOSEA_004 },		// (SPR_LOGOS+0x00CC)
	{ NULL, &ID_SPR_LOGOSEA_005 },		// (SPR_LOGOS+0x00CD)
	{ NULL, &ID_SPR_LOGOSEA_006 },		// (SPR_LOGOS+0x00CE)
	{ NULL, &ID_SPR_LOGOSEA_007 },		// (SPR_LOGOS+0x00CF)

	{ NULL, &ID_SPR_LOGOTOR_000 },		// (SPR_LOGOS+0x00D0)
	{ NULL, &ID_SPR_LOGOTOR_001 },		// (SPR_LOGOS+0x00D1)
	{ NULL, &ID_SPR_LOGOTOR_002 },		// (SPR_LOGOS+0x00D2)
	{ NULL, &ID_SPR_LOGOTOR_003 },		// (SPR_LOGOS+0x00D3)
	{ NULL, &ID_SPR_LOGOTOR_004 },		// (SPR_LOGOS+0x00D4)
	{ NULL, &ID_SPR_LOGOTOR_005 },		// (SPR_LOGOS+0x00D5)
	{ NULL, &ID_SPR_LOGOTOR_006 },		// (SPR_LOGOS+0x00D6)
	{ NULL, &ID_SPR_LOGOTOR_007 },		// (SPR_LOGOS+0x00D7)

	{ NULL, &ID_SPR_LOGOUTA_000 },		// (SPR_LOGOS+0x00D8)
	{ NULL, &ID_SPR_LOGOUTA_001 },		// (SPR_LOGOS+0x00D9)
	{ NULL, &ID_SPR_LOGOUTA_002 },		// (SPR_LOGOS+0x00DA)
	{ NULL, &ID_SPR_LOGOUTA_003 },		// (SPR_LOGOS+0x00DB)
	{ NULL, &ID_SPR_LOGOUTA_004 },		// (SPR_LOGOS+0x00DC)
	{ NULL, &ID_SPR_LOGOUTA_005 },		// (SPR_LOGOS+0x00DD)
	{ NULL, &ID_SPR_LOGOUTA_006 },		// (SPR_LOGOS+0x00DE)
	{ NULL, &ID_SPR_LOGOUTA_007 },		// (SPR_LOGOS+0x00DF)

	{ NULL, &ID_SPR_LOGOVAN_000 },		// (SPR_LOGOS+0x00E0)
	{ NULL, &ID_SPR_LOGOVAN_001 },		// (SPR_LOGOS+0x00E1)
	{ NULL, &ID_SPR_LOGOVAN_002 },		// (SPR_LOGOS+0x00E2)
	{ NULL, &ID_SPR_LOGOVAN_003 },		// (SPR_LOGOS+0x00E3)
	{ NULL, &ID_SPR_LOGOVAN_004 },		// (SPR_LOGOS+0x00E4)
	{ NULL, &ID_SPR_LOGOVAN_005 },		// (SPR_LOGOS+0x00E5)
	{ NULL, &ID_SPR_LOGOVAN_006 },		// (SPR_LOGOS+0x00E6)
	{ NULL, &ID_SPR_LOGOVAN_007 },		// (SPR_LOGOS+0x00E7)

	{ NULL, &ID_SPR_LOGOWAS_000 },		// (SPR_LOGOS+0x00E8)
	{ NULL, &ID_SPR_LOGOWAS_001 },		// (SPR_LOGOS+0x00E9)
	{ NULL, &ID_SPR_LOGOWAS_002 },		// (SPR_LOGOS+0x00EA)
	{ NULL, &ID_SPR_LOGOWAS_003 },		// (SPR_LOGOS+0x00EB)
	{ NULL, &ID_SPR_LOGOWAS_004 },		// (SPR_LOGOS+0x00EC)
	{ NULL, &ID_SPR_LOGOWAS_005 },		// (SPR_LOGOS+0x00ED)
	{ NULL, &ID_SPR_LOGOWAS_006 },		// (SPR_LOGOS+0x00EE)
	{ NULL, &ID_SPR_LOGOWAS_007 },		// (SPR_LOGOS+0x00EF)

	{ NULL, &ID_SPR_LOGOEAS_000 },		// (SPR_LOGOS+0x00F0)
	{ NULL, &ID_SPR_LOGOEAS_001 },		// (SPR_LOGOS+0x00F1)
	{ NULL, &ID_SPR_LOGOEAS_002 },		// (SPR_LOGOS+0x00F2)
	{ NULL, &ID_SPR_LOGOEAS_003 },		// (SPR_LOGOS+0x00F3)
	{ NULL, &ID_SPR_LOGOEAS_004 },		// (SPR_LOGOS+0x00F4)
	{ NULL, &ID_SPR_LOGOEAS_005 },		// (SPR_LOGOS+0x00F5)
	{ NULL, &ID_SPR_LOGOEAS_006 },		// (SPR_LOGOS+0x00F6)
	{ NULL, &ID_SPR_LOGOEAS_007 },		// (SPR_LOGOS+0x00F7)

	{ NULL, &ID_SPR_LOGOWES_000 },		// (SPR_LOGOS+0x00F8)
	{ NULL, &ID_SPR_LOGOWES_001 },		// (SPR_LOGOS+0x00F9)
	{ NULL, &ID_SPR_LOGOWES_002 },		// (SPR_LOGOS+0x00FA)
	{ NULL, &ID_SPR_LOGOWES_003 },		// (SPR_LOGOS+0x00FB)
	{ NULL, &ID_SPR_LOGOWES_004 },		// (SPR_LOGOS+0x00FC)
	{ NULL, &ID_SPR_LOGOWES_005 },		// (SPR_LOGOS+0x00FD)
	{ NULL, &ID_SPR_LOGOWES_006 },		// (SPR_LOGOS+0x00FE)
	{ NULL, &ID_SPR_LOGOWES_007 },		// (SPR_LOGOS+0x00FF)

	{ NULL, &ID_SPR_LOGOLFP_000 },		// (SPR_LOGOS+0x0100)
	{ NULL, &ID_SPR_LOGOLFP_001 },		// (SPR_LOGOS+0x0101)
	{ NULL, &ID_SPR_LOGOLFP_002 },		// (SPR_LOGOS+0x0102)
	{ NULL, &ID_SPR_LOGOLFP_003 },		// (SPR_LOGOS+0x0103)
	{ NULL, &ID_SPR_LOGOLFP_004 },		// (SPR_LOGOS+0x0104)
	{ NULL, &ID_SPR_LOGOLFP_005 },		// (SPR_LOGOS+0x0105)
	{ NULL, &ID_SPR_LOGOLFP_006 },		// (SPR_LOGOS+0x0106)
	{ NULL, &ID_SPR_LOGOLFP_007 },		// (SPR_LOGOS+0x0107)

	{ NULL, &ID_SPR_LOGONIN_000 },		// (SPR_LOGOS+0x0108)
	{ NULL, &ID_SPR_LOGONIN_001 },		// (SPR_LOGOS+0x0109)
	{ NULL, &ID_SPR_LOGONIN_002 },		// (SPR_LOGOS+0x010A)
	{ NULL, &ID_SPR_LOGONIN_003 },		// (SPR_LOGOS+0x010B)
	{ NULL, &ID_SPR_LOGONIN_004 },		// (SPR_LOGOS+0x010C)
	{ NULL, &ID_SPR_LOGONIN_005 },		// (SPR_LOGOS+0x010D)
	{ NULL, &ID_SPR_LOGONIN_006 },		// (SPR_LOGOS+0x010E)
	{ NULL, &ID_SPR_LOGONIN_007 },		// (SPR_LOGOS+0x010F)

	{ NULL, &ID_SPR_LOGONOA_000 },		// (SPR_LOGOS+0x0110)
	{ NULL, &ID_SPR_LOGONOA_001 },		// (SPR_LOGOS+0x0111)
	{ NULL, &ID_SPR_LOGONOA_002 },		// (SPR_LOGOS+0x0112)
	{ NULL, &ID_SPR_LOGONOA_003 },		// (SPR_LOGOS+0x0113)
	{ NULL, &ID_SPR_LOGONOA_004 },		// (SPR_LOGOS+0x0114)
	{ NULL, &ID_SPR_LOGONOA_005 },		// (SPR_LOGOS+0x0115)
	{ NULL, &ID_SPR_LOGONOA_006 },		// (SPR_LOGOS+0x0116)
	{ NULL, &ID_SPR_LOGONOA_007 },		// (SPR_LOGOS+0x0117)

	{ NULL, NULL                },
};


#undef sm

global	UB                  aub__gShellSpr [(TOTAL_SPRITES+15)&~15];
#define BG_IN_RESMAN	0	// 9/199/99 - backgrounds not in resource manager

#if BG_IN_RESMAN


global	unsigned long *     apul_gTeamBackground [] =
	{
	&ID_SPR_BACK_KB1,	//placeholder
	&ID_SPR_BACKATL,
	&ID_SPR_BACKBOS,
	&ID_SPR_BACKCHA,
	&ID_SPR_BACKCHI,
	&ID_SPR_BACKCLE,
	&ID_SPR_BACKDAL,
	&ID_SPR_BACKDEN,
	&ID_SPR_BACKDET,
	&ID_SPR_BACKGS,
	&ID_SPR_BACKHOU,
	&ID_SPR_BACKIND,
	&ID_SPR_BACKLAC,
	&ID_SPR_BACKLAL,
	&ID_SPR_BACKMIA,
	&ID_SPR_BACKMIL,
	&ID_SPR_BACKMIN,
	&ID_SPR_BACKNJ,
	&ID_SPR_BACKNY,
	&ID_SPR_BACKORL,
	&ID_SPR_BACKPHI,
	&ID_SPR_BACKPHO,
	&ID_SPR_BACKPOR,
	&ID_SPR_BACKSAC,
	&ID_SPR_BACKSA,
	&ID_SPR_BACKSEA,
	&ID_SPR_BACKTOR,
	&ID_SPR_BACKUTA,
	&ID_SPR_BACKVAN,
	&ID_SPR_BACKWAS,

	&ID_SPR_KEAST,	// same as screen h1 bg
	&ID_SPR_KWEST,	// same as screen h1 bg
//	&ID_SPR_BACKEAS,
//	&ID_SPR_BACKWES,

	&ID_SPR_BACK_KB4,	//lfp
	&ID_SPR_BACK_KB2,	// nin 1
	&ID_SPR_BACK_KB3,	// nin 2
	&ID_SPR_BACK_KB1,	// 3 pt

	&ID_SPR_BACK_KB5,	// safety placeholder
	&ID_SPR_BACK_KB5,	// safety placeholder
	&ID_SPR_BACK_KB5,	// safety placeholder

//	&ID_SPR_BACKLFP,
//	&ID_SPR_BACKNIN,	// "Nintendo" in oval
//	&ID_SPR_BACKNIN
};


#define NUM_KOBE_BGS	5
global	unsigned long *KobeBGs[] = 
{
	&ID_SPR_BACK_KB1,
	&ID_SPR_BACK_KB2,
	&ID_SPR_BACK_KB3,
	&ID_SPR_BACK_KB4,
	&ID_SPR_BACK_KB5,
};



#define NUM_CYCLE_BACKGROUNDS	(29*2)

global	unsigned long *     apul_gCycleBackground [] =
{						// used for cycling backgrounds
	&ID_SPR_BACK_KB1,	
	&ID_SPR_BACKATL,
	&ID_SPR_BACK_KB2,
	&ID_SPR_BACKBOS,
	&ID_SPR_BACK_KB3,
	&ID_SPR_BACKCHA,
	&ID_SPR_BACK_KB4,
	&ID_SPR_BACKCHI,
	&ID_SPR_BACK_KB5,
	&ID_SPR_BACKCLE,
	&ID_SPR_BACK_KB4,
	&ID_SPR_BACKDAL,
	&ID_SPR_BACK_KB3,
	&ID_SPR_BACKDEN,
	&ID_SPR_BACK_KB2,
	&ID_SPR_BACKDET,
	&ID_SPR_BACK_KB1,
	&ID_SPR_BACKGS,
	&ID_SPR_BACK_KB3,
	&ID_SPR_BACKHOU,
	&ID_SPR_BACK_KB5,
	&ID_SPR_BACKIND,
	&ID_SPR_BACK_KB2,
	&ID_SPR_BACKLAC,
	&ID_SPR_BACK_KB4,
	&ID_SPR_BACKLAL,
	&ID_SPR_BACK_KB1,
	&ID_SPR_BACKMIA,
	&ID_SPR_BACK_KB2,
	&ID_SPR_BACKMIL,
	&ID_SPR_BACK_KB3,
	&ID_SPR_BACKMIN,
	&ID_SPR_BACK_KB4,
	&ID_SPR_BACKNJ,
	&ID_SPR_BACK_KB5,
	&ID_SPR_BACKNY,
	&ID_SPR_BACK_KB1,
	&ID_SPR_BACKORL,
	&ID_SPR_BACK_KB2,
	&ID_SPR_BACKPHI,
	&ID_SPR_BACK_KB3,
	&ID_SPR_BACKPHO,
	&ID_SPR_BACK_KB4,
	&ID_SPR_BACKPOR,
	&ID_SPR_BACK_KB5,
	&ID_SPR_BACKSAC,
	&ID_SPR_BACK_KB1,
	&ID_SPR_BACKSA,
	&ID_SPR_BACK_KB2,
	&ID_SPR_BACKSEA,
	&ID_SPR_BACK_KB3,
	&ID_SPR_BACKTOR,
	&ID_SPR_BACK_KB4,
	&ID_SPR_BACKUTA,
	&ID_SPR_BACK_KB5,
	&ID_SPR_BACKVAN,
	&ID_SPR_BACK_KB3,
	&ID_SPR_BACKWAS,

};



int GetRandomKobeBG(void)
{

	return( *(KobeBGs[ReadRandomByte()%NUM_KOBE_BGS]) );

}

#endif


global	SPR_LIST_T          acl__gShellPal [] =
	{
	{ NULL, NULL                },		//  0

	{ NULL, &ID_PAL_MAINPAL2    },		//  1 - user/cpu boxes
	{ NULL, &ID_PAL_MAINPALH    },		//  2 - most stuff - rainbow palette
	{ NULL, &ID_PAL_CTRLPAL     },		//  3
	{ NULL, &ID_PAL_MAINPAL3     },		//  4

//	{ NULL, &ID_PAL_KEAST       },		//  5	bg of playoff screen
//	{ NULL, &ID_PAL_KWEST       },		//  6	bg of playoff screen

	{ NULL, &ID_PAL_LOGONUL     },		//  7	#define FIRST_TEAM_PAL	(free agent team)

	{ NULL, &ID_PAL_LOGOATL     },		//  x
	{ NULL, &ID_PAL_LOGOBOS     },		//  x
	{ NULL, &ID_PAL_LOGOCHA     },		//  x
	{ NULL, &ID_PAL_LOGOCHI     },		//  x
	{ NULL, &ID_PAL_LOGOCLE     },		//  x
	{ NULL, &ID_PAL_LOGODAL     },		//  x
	{ NULL, &ID_PAL_LOGODEN     },		//  x
	{ NULL, &ID_PAL_LOGODET     },		//  x
	{ NULL, &ID_PAL_LOGOGS      },		//  x
	{ NULL, &ID_PAL_LOGOHOU     },		//  x
	{ NULL, &ID_PAL_LOGOIND     },		//  x
	{ NULL, &ID_PAL_LOGOLAC     },		//  x
	{ NULL, &ID_PAL_LOGOLAL     },		//  x
	{ NULL, &ID_PAL_LOGOMIA     },		//  x
	{ NULL, &ID_PAL_LOGOMIL     },		//  x
	{ NULL, &ID_PAL_LOGOMIN     },		//  x
	{ NULL, &ID_PAL_LOGONJ      },		//  x
	{ NULL, &ID_PAL_LOGONY      },		//  x
	{ NULL, &ID_PAL_LOGOORL     },		//  x
	{ NULL, &ID_PAL_LOGOPHI     },		//  x
	{ NULL, &ID_PAL_LOGOPHO     },		//  x
	{ NULL, &ID_PAL_LOGOPOR     },		//  x
	{ NULL, &ID_PAL_LOGOSAC     },		//  x
	{ NULL, &ID_PAL_LOGOSA      },		//  x
	{ NULL, &ID_PAL_LOGOSEA     },		//  x
	{ NULL, &ID_PAL_LOGOTOR     },		//  x
	{ NULL, &ID_PAL_LOGOUTA     },		//  x
	{ NULL, &ID_PAL_LOGOVAN     },		//  x
	{ NULL, &ID_PAL_LOGOWAS     },		//  x
	{ NULL, &ID_PAL_LOGOEAS     },		//  x
	{ NULL, &ID_PAL_LOGOWES     },		//  x
	{ NULL, &ID_PAL_LOGOLFP     },		//  x
	{ NULL, &ID_PAL_LOGONIN     },		//  x
	{ NULL, &ID_PAL_LOGONOA     },		//  x

	{ NULL, NULL                },
	};


global	SPR_LIST_T          acl__gShellFromPausePal [] =
	{
	{ NULL, NULL                },		//  0

	{ NULL, &ID_PAL_MAINPAL2    },		//  1 - user/cpu boxes
	{ NULL, &ID_PAL_MAINPALH    },		//  2 - most stuff - rainbow palette
	{ NULL, &ID_PAL_CTRLPAL     },		//  3
	{ NULL, &ID_PAL_MAINPAL3    },		//  4 - no more rainbow


/**********

	{ NULL, &ID_PAL_KEAST       },		//  4	bg of playoff screen
	{ NULL, &ID_PAL_KWEST       },		//  5	bg of playoff screen

	{ NULL, &ID_PAL_LOGONUL     },		//  6	#define FIRST_TEAM_PAL	6

	{ NULL, &ID_PAL_LOGOATL     },		//  x
	{ NULL, &ID_PAL_LOGOBOS     },		//  x
	{ NULL, &ID_PAL_LOGOCHA     },		//  x
	{ NULL, &ID_PAL_LOGOCHI     },		//  x
	{ NULL, &ID_PAL_LOGOCLE     },		//  x
	{ NULL, &ID_PAL_LOGODAL     },		//  x
	{ NULL, &ID_PAL_LOGODEN     },		//  x
	{ NULL, &ID_PAL_LOGODET     },		//  x
	{ NULL, &ID_PAL_LOGOGS      },		//  x
	{ NULL, &ID_PAL_LOGOHOU     },		//  x
	{ NULL, &ID_PAL_LOGOIND     },		//  x
	{ NULL, &ID_PAL_LOGOLAC     },		//  x
	{ NULL, &ID_PAL_LOGOLAL     },		//  x
	{ NULL, &ID_PAL_LOGOMIA     },		//  x
	{ NULL, &ID_PAL_LOGOMIL     },		//  x
	{ NULL, &ID_PAL_LOGOMIN     },		//  x
	{ NULL, &ID_PAL_LOGONJ      },		//  x
	{ NULL, &ID_PAL_LOGONY      },		//  x
	{ NULL, &ID_PAL_LOGOORL     },		//  x
	{ NULL, &ID_PAL_LOGOPHI     },		//  x
	{ NULL, &ID_PAL_LOGOPHO     },		//  x
	{ NULL, &ID_PAL_LOGOPOR     },		//  x
	{ NULL, &ID_PAL_LOGOSAC     },		//  x
	{ NULL, &ID_PAL_LOGOSA      },		//  x
	{ NULL, &ID_PAL_LOGOSEA     },		//  x
	{ NULL, &ID_PAL_LOGOTOR     },		//  x
	{ NULL, &ID_PAL_LOGOUTA     },		//  x
	{ NULL, &ID_PAL_LOGOVAN     },		//  x
	{ NULL, &ID_PAL_LOGOWAS     },		//  x
	{ NULL, &ID_PAL_LOGOEAS     },		//  x
	{ NULL, &ID_PAL_LOGOWES     },		//  x
	{ NULL, &ID_PAL_LOGOLFP     },		//  x
	{ NULL, &ID_PAL_LOGONIN     },		//  x
	{ NULL, &ID_PAL_LOGONOA     },		//  x
********/
	{ NULL, NULL                },
	};

global	SPR_LIST_T          acl__gPalGlobal [] =
	{
	{ NULL, &ID_PAL_PALETTE     },		//  0
	{ NULL, NULL                }
	};


static	UB                  aub__sSprGlobal [] =
	{
	SPR_SMALLBTN,

	SPR_FLATNUL,
	SPR_FLATATL,
	SPR_FLATBOS,
	SPR_FLATCHA,
	SPR_FLATCHI,
	SPR_FLATCLE,
	SPR_FLATDAL,
	SPR_FLATDEN,
	SPR_FLATDET,
	SPR_FLATGS,
	SPR_FLATHOU,
	SPR_FLATIND,
	SPR_FLATLAC,
	SPR_FLATLAL,
	SPR_FLATMIA,
	SPR_FLATMIL,
	SPR_FLATMIN,
	SPR_FLATNJ,
	SPR_FLATNY,
	SPR_FLATORL,
	SPR_FLATPHI,
	SPR_FLATPHO,
	SPR_FLATPOR,
	SPR_FLATSAC,
	SPR_FLATSA,
	SPR_FLATSEA,
	SPR_FLATTOR,
	SPR_FLATUTA,
	SPR_FLATVAN,
	SPR_FLATWAS,
	SPR_FLATEAS,
	SPR_FLATWES,
	SPR_FLATLFP,
	SPR_FLATNIN,
	SPR_FLATNOA,

	SPR_MENULBAR,	// where sideways font is
	SPR_MBARSHAD,	// shadow on above	
	SPR_MENU_HL,	// menu selector (rainbow bar)- no more rainbow 8/13/99 - it's a basketball
	SPR_SMALLCRV,	// red rc on menus
	SPR_TM_CRNR,	// team corner - printbigteamname
	SPR_AT_BOX,		// away "AT" home


	SPR_TRLNTL,	// new more-rounded lines
	SPR_TRLNTR,
	SPR_TRLNBL,
	SPR_TRLNBR,

	SPR_SCBLUR11,
	SPR_SCBLUL11,
	SPR_SCORAR11,	// corners for cursor bars (middle of cursor is tbox)
	SPR_SCORAL11,
	0
	};



static	UB                  aub__sSprShellPauseMenu [] =
	{
	SPR_SMALLBTN,
/*******
	SPR_FLATNUL,
	SPR_FLATATL,
	SPR_FLATBOS,
	SPR_FLATCHA,
	SPR_FLATCHI,
	SPR_FLATCLE,
	SPR_FLATDAL,
	SPR_FLATDEN,
	SPR_FLATDET,
	SPR_FLATGS,
	SPR_FLATHOU,
	SPR_FLATIND,
	SPR_FLATLAC,
	SPR_FLATLAL,
	SPR_FLATMIA,
	SPR_FLATMIL,
	SPR_FLATMIN,
	SPR_FLATNJ,
	SPR_FLATNY,
	SPR_FLATORL,
	SPR_FLATPHI,
	SPR_FLATPHO,
	SPR_FLATPOR,
	SPR_FLATSAC,
	SPR_FLATSA,
	SPR_FLATSEA,
	SPR_FLATTOR,
	SPR_FLATUTA,
	SPR_FLATVAN,
	SPR_FLATWAS,
	SPR_FLATEAS,
	SPR_FLATWES,
	SPR_FLATLFP,
	SPR_FLATNIN,
	SPR_FLATNOA,
********/
	SPR_MENULBAR,	// where sideways font is
	SPR_MBARSHAD,	// shadow on above	
	SPR_MENU_HL,	// menu selector (rainbow bar)
	SPR_SMALLCRV,	// red rc on menus (left side of menu choices) & player stats
	SPR_TM_CRNR,	// team corner - yellow for team stats,player stats
//	SPR_AT_BOX,		// away "AT" home - controller screen

	SPR_TRLNTL,	// new more-rounded lines
	SPR_TRLNTR,
	SPR_TRLNBL,
	SPR_TRLNBR,
	
	// corners for cursor bars (middle is tbox)
	SPR_SCBLUR11,
	SPR_SCBLUL11,
	SPR_SCORAR11,
	SPR_SCORAL11,

	0
	};




//
//
//


global	UW *                puw__gPalette;

global	UW *                puw__gMainPal2;
global	UW *                puw__gMainPalH;
global	UW *                puw__gContPal;
global	UW *                puw__gMainPal3;

global	UW *                puw__gPhoto0;
global	UW *                puw__gPhoto1;

global	long                sl___Memory;

//1
//
//

#if SHOW_SCREEN
static	UW *                puw__gScreen = NULL;

static	unsigned long *     apui_gScrLst [] =
	{
	&ID_IMG_TEST,
	NULL
	};

static	unsigned long **    ppui_gScrLst = &apui_gScrLst[0];
#endif

//
//
//

global	int                 fl___gShowingHelp;

global	int                 fl___gDemoFlag;
global	int                 si___gDemoTimer;

global	char *              pcz__gPopupTtl;
global	char *              pcz__gPopupMsg;
global	UB *                pub__gPopupPal;
global	SI                  si___gPopupVal;

global	SI                  fl___gShellOverlay;	// 0 if normal shell, 1 if from PAUSE screen
global	SB                  sb___gShellTune;

global	UI                  ui___gFrameDrawn;
global	SI                  si___gFramePause;

global	struct openSet *    pcl__ShellRes;

global	signed int          asi__gJoyPad [4];
global	signed int          asi__gJoyBtn [4];

static	SB                  asb__sJoyRpt [4];
static	SB                  asb__sJoyClk [4][4];

global	signed int          asi__gTeamPad [5];	// 0=Home 1=Away 2=None 3=Both 4=All
global	signed int          asi__gTeamBtn [5];	// 0=Home 1=Away 2=None 3=Both 4=All

//
// DEFINITIONS
//

#define	ALPHA_LIMIT         128

#define	NEAR_Z              50.0
#define	FAR_Z               16384.0


#define FOV_Y               33.0


#if SHOW_BG_COURT_LINES	//---------------------------------------


#define	NUM_LTILES          3

#define CC_LINEMODE         0,0,0,PRIMITIVE,0,0,0,PRIMITIVE
#define CC_LINEMODE2        0,0,0,PRIMITIVE,0,0,0,PRIMITIVE
#define CC_ADDMODE          0,0,0,TEXEL0,0,0,0,PRIMITIVE
#define CC_ADDMODE2         0,0,0,TEXEL0,0,0,0,PRIMITIVE
#define CC_XLU              0,0,0,SHADE,0,0,0,SHADE
#define CC_XLU2             0,0,0,SHADE,0,0,0,SHADE

#define MAX_ACTIVE_LINES    3

typedef struct _activeLine
	{
	int                 flags;
	int                 alpha;
	int                 camX;
	int                 camY;
	int                 camZ;
	int                 camDist;
	int                 camDX;
	int                 camDY;
	int                 camDZ;
	int                 camP;
	int                 camH;
	int                 camDH;
} activeLine;



static	activeLine          activeLines [MAX_ACTIVE_LINES];

global	n64Script *         titleLineScript;

#endif	//-------------------------------------------------------------

//
// GLOBAL VARIABLES
//

//global char *gLeftSideText = NULL;


static	int                 titleFrame;

//global	UI			MainBGSlot;
//global	UI				ShellBGMode;
global	UI				ShellBackBG;
global	UI				ShellForeBG;
global	UI				BGCycleIndex;

//global	UI	displayrainbow=1;

static	UI                  ui___sBackRes[2];
global	SprTexture_T *      pcl__gBackSpr[2];

//global	SprTexture_T *      pcl__gForeSpr;

global	UI		              BackgroundScrollMode;

global void *SideShadFont=NULL;
global void *MenuShadFont=NULL;
global void *LargeShadFont=NULL;
global void *SmallShadFont=NULL;
global void *TinyShadFont=NULL;
//
//
//

#define aiffID(ch0, ch1, ch2, ch3) \
		((UD)(UB)(ch3)        | \
		((UD)(UB)(ch2) <<  8) | \
		((UD)(UB)(ch1) << 16) | \
		((UD)(UB)(ch0) << 24 ))

global	UL                  ul___gInfoFileRom;

global	SL                  sl___gInfoVersPos;
global	SL                  sl___gInfoVersLen;
global	SL                  sl___gInfoCstrPos;
global	SL                  sl___gInfoCstrLen;
global	SL                  sl___gInfoTeamPos;
global	SL                  sl___gInfoTeamLen;
global	SL                  sl___gInfoRostPos;
global	SL                  sl___gInfoRostLen;
global	SL                  sl___gInfoPlyrPos;
global	SL                  sl___gInfoPlyrLen;
global	SL                  sl___gInfoDatePos;
global	SL                  sl___gInfoDateLen;

global	UL                  ul___gDataFileRom;

global	SL                  sl___gDataVersPos;
global	SL                  sl___gDataVersLen;
global	SL                  sl___gDataPtexPos;
global	SL                  sl___gDataPtexLen;
global	SL                  sl___gDataPimgPos;
global	SL                  sl___gDataPimgLen;
global	SL                  sl___gDataSpalPos;
global	SL                  sl___gDataSpalLen;

global	UL                  ul___gCommFileRom;

global	SL                  sl___gCommVersPos;
global	SL                  sl___gCommVersLen;
global	SL                  sl___gCommPnamPos;
global	SL                  sl___gCommPnamLen;
global	SL                  sl___gCommTalkPos;
global	SL                  sl___gCommTalkLen;

//
//
//

global	FH                  pcl__gInfoFile;
global	FH                  pcl__gDataFile;
global	FH                  pcl__gCommFile;

global	UB *                pub__gInfoFile;

global	UB *                pub__gInfoVers;
global	UB *                pub__gInfoCstr;
global	UB *                pub__gInfoTeam;
global	UB *                pub__gInfoRost;
global	UB *                pub__gInfoPlyr;
global	UB *                pub__gInfoDate;

global	UD *                pud__gIndxPtex;
global	UD *                pud__gIndxPimg;
global	UD *                pud__gIndxPnam;
global	UD *                pud__gIndxTalk;

global	UW *                puw__gSuperPal;
global	SL                  sl___gSuperPal;

//global	int                 si___gEepromNeed;
global	int                 si___gMemPakNeed;


global	int       BGWidth=0;
global	int       BGWidthInc = BG_WIDTH_INC_VALUE;


//
//
//

//static	Vp                  vp =
global	Vp                  vp =
	{
	0, 0, G_MAXZ/2, 0,	/* scale */
	0, 0, G_MAXZ/2, 0,	/* translate */
	};



static	u16                 nmlz;

static Mtx			titleFlipMatrix=
	{
	0x00010000,	0x00000000,
	0x00000001,	0x00000000,
	0x00000000,	0xFFFF0000,
	0x00000000,	0x00000001,
	0x00000000,	0x00000000,
	0x00000000,	0x00000000,
	0x00000000,	0x00000000,
	0x00000000,	0x00000000
	};


//
// STATIC VARIABLES
//

//
// STATIC FUNCTION PROTOTYPES
//

static	void                AudioOpenShell          (void);
static	void                AudioCloseShell         (void);

#if SHOW_BG_COURT_LINES
static	void                AddLine                 (void);
static	Gfx *               DrawLines               (Gfx *, dyn *);
#endif

extern	UW *                LoadScreenImg           (
								struct openSet *    pcl__Res,
								unsigned long int   ui___ID);

extern	Gfx *               DrawScreenImg           (
								Gfx *               gp,
								UW *                puw__Src);

extern	void                LoadFontTest            (void);

extern	Gfx *               RenderScreenFnt         (
								Gfx *               gp,
								UB *                pub__Src);

extern	UB                  pub__Tex [];

extern	UW *                LoadPhoto               (
								unsigned long int   ui___ID);

extern	SprTexture_T *      LoadSprite              (
								unsigned long int   ui___ID);


extern void ChangeShellBG(void);

//
//
//

#define	RM_XLU_FONT(clk)					\
	IM_RD | CVG_DST_FULL | FORCE_BL | ZMODE_OPA | \
	GBL_c##clk(G_BL_CLR_IN, G_BL_A_IN, G_BL_CLR_MEM, G_BL_1MA)

//
//
//



// **************************************************************************
// **************************************************************************
// **************************************************************************
//	GLOBAL FUNCTIONS
// **************************************************************************
// **************************************************************************
// **************************************************************************

#define BG_S3TC_CHUNKS ((512/4) * (240/4))
#define BG_S3TC_SIZE (BG_S3TC_CHUNKS * 8)
#define BG_RGBA_SIZE (512 * 240 * 2)


static RGBA16
	*BGs[2];

static s8
	BG_DoAuto = FALSE;


void BG_Auto( const s32 a )
{
	BG_DoAuto = a;
}


Gfx *BG_Draw( Gfx *gp, const u32 slot, s32 x, const s32 half, const u8 shade )
{
	RGBA16
		*bg;
	s32
		y,
		t,
		s,
		w;

	bg = BGs[slot];

	if (bg)
	{
		gDPPipeSync( gp++ );
		gDPSetTextureLUT( gp++, G_TT_NONE );
		gDPSetCombineMode( gp++, G_CC_MODULATEI_PRIM, G_CC_MODULATEI_PRIM );
		gDPSetPrimColor( gp++, 0, 0, shade, shade, shade, 255 );

		t = 0;

		if (half)
		{
			s = (2 << 10);
			w = 512/2;
		}
		else
		{
			s = (1 << 10);
			w = 512;
		}

		if (x >= 512 || (x+w) <= 0)
			return( gp );

		if (x < 0)
		{
			w += x;
			t = -x << 5;
			if (half)
				t <<= 1;
			x = 0;
		}

		if ((x+w) > 512)
			w -= (x+w) - 512;

		for (y=0; y<240; y+=4)
		{
			gDPLoadTextureBlock( gp++,
													 bg,
													 G_IM_FMT_RGBA,
													 G_IM_SIZ_16b,
													 512,
													 4,
													 0,
													 G_TX_CLAMP,
													 G_TX_CLAMP,
													 10, 3,
													 G_TX_NOLOD,
													 G_TX_NOLOD );

			gSPTextureRectangle( gp++,
													 x << 2,			// ulx  (10.2)
													 y << 2,			// uly  (10.2)
													 (x+w) << 2,		// lrx  (10.2)
													 (y+4) << 2,		// lry  (10.2)
													 G_TX_RENDERTILE,	// tile
													 t,					// s    (s10.5)
													 0,					// t    (s10.5)
													 s,			// dsdx (s5.10)
													 1 << 10);			// dtdy (s5.10)

			bg += (4 * 512);
		}
	}

	return( gp );
}


void BG_Free( const u32 slot )
{
	if (BGs[slot])
	{
		MemFree( BGs[slot] );
		BGs[slot] = NULL;
	}
}


void BG_Load( const u32 id, const u32 slot )
{
	FH
		file;

	if (!BGs[slot])
		BGs[slot] = MemMalloc( BG_RGBA_SIZE, MEM_ANY );


	Gfx_Wait();


	file = FileOpen( "BackGnds.S3T", "rb" );

	FileSeek( file, id * BG_S3TC_SIZE, SEEK_SET );
	FileRead( dfb, BG_S3TC_SIZE, 1, file );

	FileClose( file );


	S3TC_Decompress( dfb, BG_S3TC_CHUNKS, BGs[slot], 512 );

	osWritebackDCache( BGs[slot], BG_RGBA_SIZE );
}


void BG_Init()
{
	BGs[0] = NULL;
	BGs[1] = NULL;
}


void BG_Deinit()
{
	BG_Free( 0 );
	BG_Free( 1 );
}


static void AllocGfxLists()
{
	// Allocate gfx lists...

	if (!DBdata[0].ShellList)
		DBdata[0].ShellList = MemMalloc( GFX_LIST_SIZE, MEM_ANY );

	if (!DBdata[1].ShellList)
		DBdata[1].ShellList = MemMalloc( GFX_LIST_SIZE, MEM_ANY );
}


static void DeallocGfxLists()
{
	// Free gfx lists...

	if (DBdata[1].ShellList)
	{
		MemFree( DBdata[1].ShellList );
		DBdata[1].ShellList = NULL;
	}

	if (DBdata[0].ShellList)
	{
		MemFree( DBdata[0].ShellList );
		DBdata[0].ShellList = NULL;
	}
}



void SetAudioVolumesFromOptions(void)
{

	Audio_MusicVolume( cl___gPreferences.sb___VolMusic );
	SfxVolume = cl___gPreferences.sb___VolSound;
	Crowd_Volume( cl___gPreferences.sb___VolCrowd );
	Speech_Volume( cl___gPreferences.sb___VolSpeech );

}


#if ALL_PROCESSES

// **************************************************************************
// * ShellOpen ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  UD *    Ptr to input queue                                     *
// *         FL      Repeat flag (TRUE if you want auto-repeat)             *
// *                                                                        *
// * Output  UD      Composite of all joypads' buttons just pressed         *
// **************************************************************************

global	void                ShellOpen               (
								void *              p)

	{
	// Resource ID numbers.

//	extern int ID_STF_BACKONE;
//	extern int ID_SHP_TLINES;
//	extern int ID_SQ3_GLOUSTER;


	// Local Variables.

	int                 i;
	int                 j;
	int                 k;
	int                 l;
	int                 m;
	unsigned short *    backImage2;

	SPR_LIST_T *        pcl__SprLst;

	DIR_INFO_T *        pcl__DirInfo;

	//

	ExitPopup();

	#ifdef DEBUG
		#if 0
			PrintToFile("\nStarting shell\n");
			MemDump();
			MemMarkSet();
		#endif
	#endif


	AllocGfxLists();


	// Signal stand-alone mode.

	ShellPlayerOnScreen = fl___gShellOverlay = 0;

	// Signal that no tune is loaded.

	demoTimeout=0;
	sb___gShellTune = 0;

	// Initialize joypads.

	for (i = 0; i < 4; i++)	{	asi__gJoyPad[i] = -1; asi__gJoyBtn[i] =  0;	}

	// Initialize preferences.

	LoadPreferences(&cl___gPreferences);	// from Flash Rom
	SetAudioVolumesFromOptions();


	LoadCustomUsers();		// from Flash Rom

	// Initialize options.

	acl__gOptions[SOURCE_PRE].sb___OptionSrc = SOURCE_PRE;
	acl__gOptions[SOURCE_SSN].sb___OptionSrc = SOURCE_SSN;
	acl__gOptions[SOURCE_PLY].sb___OptionSrc = SOURCE_PLY;

	// Load up permanent data (if it isn't already loaded).

	ShellLoadPermData();
	UnstuffRegisterSuperPals(sl___gSuperPal/512,puw__gSuperPal);	// allows player photos to work-2/16/99 mjk

	// Load up the complete team/player/schedule information.
	ShellLoadTeamInfo();	// sets default NBA roster

	MenuShadFont = ShadFont_Load("butnshel.fnt");
	SideShadFont = ShadFont_Load("sideshel.fnt");
	LargeShadFont= ShadFont_Load("largshel.fnt");	//currently uses Teamfnt.tga (not largefnt.tga)
	SmallShadFont= ShadFont_Load("smalshel.fnt");
	TinyShadFont = ShadFont_Load("tinyshel.fnt");

//	rep_buf_sz = REP_BUF_SZ;
//	rep_buf_wid = REP_BUF_WID;


	// Init the player statistics simulation.

	ShellInitSimulation();

	// Init rosters ?

	if (*apuw_gRoster[SOURCE_SSN] == 0)
	{	// if league season roster not setup, copy default(NBA) to season roster
		// could have been previously set up (before 1st shell open) my loading from memcard
		wordcpy(apuw_gRoster[SOURCE_SSN], apuw_gRoster[SOURCE_NBA], sl___gInfoRostLen);	// dest,src,size
	}

	if (*apuw_gRoster[SOURCE_GAM] == 0)
	{	// if league GAME roster not setup, init from season roster
		wordcpy(apuw_gRoster[SOURCE_GAM], apuw_gRoster[SOURCE_SSN], sl___gInfoRostLen);
	}

	puw__gRoster = apuw_gRoster[SOURCE_GAM];	// usually always points to this (except in demo mode)

	// Link in the custom plyr structures.

	LinkCustomPlyrs();
	FixPlyrRomTeams();

	// Open resource set.
	if (ResOpenSet("shell", &pcl__ShellRes) != 0)
	{
		ABORT_PROGRAM;
	}	

	// Load up the shell palette(s).

	LoadSprPalettes(&acl__gShellPal[1]);

	puw__gMainPal2 = (UW *) acl__gShellPal[1].pbf__Res; //
	puw__gMainPalH = (UW *) acl__gShellPal[2].pbf__Res;	// menu highlite palette - rainbox
	puw__gContPal  = (UW *) acl__gShellPal[3].pbf__Res;	// controller select
	puw__gMainPal3 = (UW *) acl__gShellPal[4].pbf__Res; //new pal 8/13/99 - no more rainbow

	// Initialize the dynamic character drawing.

	InitChrList();

	// Initialize the dynamic sprite loading system.

	InitAllSprites();

	// Load up the generic sprites.

	LoadSpriteList(aub__sSprGlobal);

#if SHOW_BG_COURT_LINES	//---------------------------------------

	// Load up the court lines.

	titleLineScript = (n64Script *) ResOpenID(pcl__ShellRes, ID_SHP_TLINES);
#endif


//	LoadKobePhoto();

	// Load background image.

//	MainBGSlot=0;
//	ShellBGMode = -1;	// don't draw back background yet
	ShellBackBG = ShellForeBG = 0;

	BGWidth=0;
//	ShellBGMode = BG_MODE_NEED_INIT;

//	LoadShellBackgroundSpr(ID_SPR_BACKNBA);
	//LoadShellForegroundSpr(ID_SPR_BLUBALL);

	BG_Init();
	BG_Auto( TRUE );

	// Load the game sound effects.

	ShellFX = Audio_LoadBank( "ShellFX.BNK" );

	AudioOpenShell();

	titleFrame=0;

	// Start lines off.

	InitRnd250(12345);

#if SHOW_BG_COURT_LINES

	memset(activeLines, 0, sizeof(activeLines));
	AddLine();


#endif

	// --------------- Decide what state to start off in --------------.

	BackgroundScrollMode = 0;

	si___gOldState = STATE_NULL;
	si___gCurState = STATE_NULL;
//	si___gNxtState = STATE_NULL;

	si___gNxtState = STATE_SHELL_ENTRY;

/************

	// Are we returning from a demo ?

	if (fl___gDemoFlag)
	{
		fl___gDemoFlag = 0;

		if (pcl__gMatch != NULL)
		{
			DoneMatchStruct(pcl__gMatch);
		}

		pcl__gMatch = NULL;

		si___gNxtState = STATE_MAIN_MENU;

		pcl__gPreferences = &cl___gPreferences;
		pcl__gOptions     = &acl__gOptions[SOURCE_PRE];
		pcl__gMatch       = &cl___gMatch;

		if (pcl__gMatch != NULL)
		{
			DoneMatchStruct(pcl__gMatch);
		}

	}
//	if (pcl__gMatch == NULL)		// Are we returning from a match ?
	else if (pcl__gMatch == NULL)		// Are we returning from a match ?
	{
		// No, we must have just been reset.

		pcl__gPreferences = &cl___gPreferences;
		pcl__gOptions     = &acl__gOptions[SOURCE_PRE];
		pcl__gMatch       = &cl___gMatch;

#ifdef DEBUG
		si___gNxtState = STATE_MEMPAK_CONFIG;
#else
		si___gNxtState = STATE_MAIN_MENU;
#endif
		si___gNxtState = STATE_MAIN_MENU;
		si___gNxtState = STATE_MEMPAK_CONFIG;

		if (pcl__gMatch != NULL)
		{
			DoneMatchStruct(pcl__gMatch);
		}
	
	}
	else if(C3PT)
	{		// returning from 3 point contest

		si___gNxtState = STATE_3PT_ROUNDS;
		Restart3PtContestAfterMatch();	// sets up scores from last match

	}
	else
	{
		// Yes, and so we'd better fix up the plyr time-on-court stats.

		XvertPlyrTimeOnCourt();

		// Why have we returned ?

		pcl__gOptions = pcl__gMatch->pcl__mOptions;

		switch (pcl__gMatch->sb___mQuarter)
		{
			case -1: // Match was quit.
			{
				si___gNxtState = STATE_MAIN_MENU;
				DoneMatchStruct(pcl__gMatch);

				break;
			}
			case  0: // End of 1st quarter.
			{
				si___gNxtState = STATE_ENDOFQTR;
				break;
			}
			case  1: // End of 2nd quarter.
			{
				si___gNxtState = STATE_HALFTIME;
				break;
			}
			case  2: // End of 3rd quarter.
			{
				si___gNxtState = STATE_ENDOFQTR;
				break;
			}
		
			default: // End of 4th quarter or overtime.
			{
				if (pcl__gMatch->apcl_mTeamRam[HOME_TEAM]->uw___tPoints ==
					pcl__gMatch->apcl_mTeamRam[AWAY_TEAM]->uw___tPoints)
				{
					// Not over yet ...

					si___gNxtState = STATE_ENDOFQTR;
				}
				else
				{
					// Match finished ...

					// in Kobe 1, Post game was a part of game(overlayed), now it's in shell
					//  so ALL games go direct to PostGame,
					// PostGame Exit will then figure where to go
					si___gNxtState = STATE_POST_GAME_MENU;	//STATE_POST_GAME_TEAM_STATS;	

					if ((pcl__gOptions->sw___PlayoffIndx == 0) &&
						(pcl__gOptions->sw___SeasonMatch == 0))
					{
						// It was a pre-season match.

//						si___gNxtState = STATE_B;
//						si___gNxtState = STATE_MAIN_MENU;	// 1999 - could have been quick play
						
					}
					else
					{
						// Was it a season or playoff match.

						if (pcl__gOptions->sw___PlayoffIndx != 0)
						{
							// Continue the playoffs.

							UpdatePlyMatchStats(0);		// just complete true game (not sim)

							NxtPlayoffGame();

//							si___gNxtState = STATE_H1;
						}
						else
						{
							// Continue the season.

							UpdateSsnMatchStats(0);	// just complete true game (0 = not sim)

							pcl__gOptions->sw___SeasonMatch += 1;

//							si___gNxtState = STATE_H;
						}

						// Flag that we need to save data to backup memory.

						sb___gNeedToSave = pcl__gOptions->sb___OptionSrc + 1;
					}

					// Force the fulltime stats screen.
					//
					// si___gNxtState = STATE_FULLTIME;
				}
				break;
			}
		}

	// Free up the match data that we got back from the arcade.
	// NO - no yet for Kobe 2 - still need to do stats screens
	//	DoneMatchStruct(pcl__gMatch);

	}
****************/

	//

	ui___gFrameDrawn = 0;
	si___gFramePause = 0;

	/***********************************
	// Test speed of GSM 06.10 decompression.
	{
	UB * addr;
	long size;

	SW   buff [320];
	int  t[16];

	addr = NULL;
	size = 0;

	LoadFile("PIPPEN.BIN", &addr, &size);

	for (i = 0; i < 4; i++)
		{
		DecodeGSMPacket((struct resSample *) addr, i, buff);
		}

	for (i = 0; i < 16; i++)
		{
		t[i] = osGetCount();
		for (j = 0; j < 8; j++)
			{
			DecodeGSMPacket((struct resSample *) addr, 4+j, buff);
			}
		t[i] = osGetCount() - t[i];
		}
	}	
	**********************************************/

	ClearCheatModes();

	// All done.

	return;
}


// **************************************************************************
// *			Shell   From    Game   - PAUSE screen
// **************************************************************************
int SavePads[4];
int SaveRPM[4];
int SaveContDiff[4];

global	void ShellFromGameInitMenu(void)
{
	// Load up the shell palette(s).

//	LoadSprPalettes(&acl__gShellPal[1]);
	LoadSprPalettes(&acl__gShellFromPausePal[1]);
	puw__gMainPal2 = (UW *) acl__gShellFromPausePal[1].pbf__Res;	 //
	puw__gMainPalH = (UW *) acl__gShellFromPausePal[2].pbf__Res;	// menu highlite palette - rainbox
	puw__gContPal  = (UW *) acl__gShellFromPausePal[3].pbf__Res;	// controller select
	puw__gMainPal3  = (UW *) acl__gShellFromPausePal[4].pbf__Res;	//new pal 8/13/99 - no more rainbow

//	loadSpriteList(aub__sSprGlobal);

}

global	void ShellFromGameOpen(void)
{
int i;

	MenuShadFont = ShadFont_Load("butnshel.fnt");	// 70's style font used only for MENUS
	SideShadFont = ShadFont_Load("sideshel.fnt");
	LargeShadFont= ShadFont_Load("largshel.fnt");	//currently use Teamfnt.tga (not largefnt.tga)
	SmallShadFont= ShadFont_Load("smalshel.fnt");
	TinyShadFont = ShadFont_Load("tinyshel.fnt");

	ShellFX = Audio_LoadBank( "ShellFX.BNK" );

	if (ResOpenSet("shell", &pcl__ShellRes) != 0)
	{
		ABORT_PROGRAM;
	}


	// Load up the shell palette(s).

	ShellFromGameInitMenu();

/*******
	LoadSprPalettes(&acl__gShellFromPausePal[1]);
	puw__gMainPal2 = (UW *) acl__gShellFromPausePal[1].pbf__Res; //
	puw__gMainPalH = (UW *) acl__gShellFromPausePal[2].pbf__Res;	// menu highlite palette - rainbox
	puw__gContPal  = (UW *) acl__gShellFromPausePal[3].pbf__Res;	// controller select
	puw__gMainPal3 = (UW *) acl__gShellFromPausePal[4].pbf__Res; //new pal 8/13/99 - no more rainbow
**********/

	BG_Init();

	// Load up the generic sprites.

	LoadSpriteList(aub__sSprShellPauseMenu);

	ui___gFrameDrawn = 0;
	si___gFramePause = 0;

	// save current config of controllers
	for(i=0; i<4; i++)
	{
		SavePads[i] = pcl__gMatch->asb__mPads[i];
		SaveRPM[i] = ControllerLockOnPlayer[i];
		SaveContDiff[i] = ControllerGameMode[i]; 
	}
}



global	void ShellFromGameClose(void)
{
int i;

	// Free whole sprite table.
	FreeAllSprites();

	// Free background image.

	BG_Deinit();

	// Free up the shell palettes.

	FreeSprPalettes(&acl__gShellPal[1]);

	ResCloseSet(pcl__ShellRes);

	Audio_StopAll();
	Audio_Process();
	Audio_Process();
	Audio_FreeBank( ShellFX );

	ShadFont_Free(TinyShadFont);
	ShadFont_Free(SmallShadFont);
	ShadFont_Free(LargeShadFont);
	ShadFont_Free(SideShadFont);
	ShadFont_Free(MenuShadFont);


	DeallocGfxLists();


// 
	for(i=0; i<4; i++)	// controllers changed???
	{
		if(SavePads[i] != pcl__gMatch->asb__mPads[i] ||
			SaveRPM[i] != ControllerLockOnPlayer[i]  ||
			SaveContDiff[i] != ControllerGameMode[i]
		  ) 
		{
			init_controls();
			break;
		}
	}
}



// **************************************************************************
// * ShellClose ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  UD *    Ptr to input queue                                     *
// *         FL      Repeat flag (TRUE if you want auto-repeat)             *
// *                                                                        *
// * Output  UD      Composite of all joypads' buttons just pressed         *
// **************************************************************************

global	void                ShellClose              (
								void *              p)

	{
	// Resource ID numbers.

//	extern int ID_STF_BACKONE;
//	extern int ID_SHP_TLINES;
//	extern int ID_SQ3_STILTON;

	// Local Variables.

	int                 i;

	SPR_LIST_T *        pcl__SprLst;

	// Free up the match.

//	DoneMatchStruct(pcl__gMatch);
//
//	pcl__gMatch = NULL;

	// Close sound effects.

	AudioCloseShell();

	//

	#if SHOW_FONT
//		LoadFontTest();
	#endif

//	FreeKobePhoto();

	// Free whole sprite table.

	FreeAllSprites();

	// Free background image.

	BG_Deinit();

#if SHOW_BG_COURT_LINES	//---------------------------------------
	// Free up the court lines.
	ResPurgeID(pcl__ShellRes, ID_SHP_TLINES);
#endif


	// Free up the shell palettes.

	FreeSprPalettes(&acl__gShellPal[1]);

	// Close resource sets (freeing up the palettes and any other rubbish).

	ResCloseSet(pcl__ShellRes);

	// Free the player statistics simulation.

	ShellFreeSimulation();

	// Free up the complete team/player/schedule information.
	//
	// N.B. this leaves the current match information intact.

	ShellFreeTeamInfo();

	Audio_FreeBank( ShellFX );

	ShadFont_Free(TinyShadFont);
	ShadFont_Free(SmallShadFont);
	ShadFont_Free(LargeShadFont);
	ShadFont_Free(SideShadFont);
	ShadFont_Free(MenuShadFont);

	//

#if 0
	if (sl___Memory != sl___gCurMemUsed)
	{
		DIAGNOSE(("Leaking Memory !!!"));
		while(1);
	}
#endif
	// Free decompressed photographs.

	MemFree(puw__gPhoto0); puw__gPhoto0 = NULL;
	MemFree(puw__gPhoto1); puw__gPhoto1 = NULL;

	// Finally, load up the team graphics before starting the arcade game.

	if(LeaveShellMethod == 	LEAVE_SHELL_TO_GAME)
	{
		if(pcl__gMatch)
		{
			LoadMatchTeam(pcl__gMatch->apcl_mTeamRam[HOME_TEAM]);
			LoadMatchTeam(pcl__gMatch->apcl_mTeamRam[AWAY_TEAM]);

			// Signal arcade overlay mode.

			fl___gShellOverlay = 1;
	
			// Signal those screens that want to know, that we're in a game.

			pcl__gMatch->pcl__mOptions->sb___OptionTyp = SOURCE_GAM;
		}
		else  
		{
#if DEBUG
//		BRK;
#endif
		}
	
	}

	DeallocGfxLists();


	#ifdef DEBUG
		#if 0
			PrintToFile("Leaving shell\n");
			MemDump();
			MemMarkSet();
		#endif
	#endif


#if 1
	if (MemTest() < 0)
	{
		DIAGNOSE("Memory corruption detected !\n");
		MemDump();
		while(1);
//		return (NULL);
	}
#endif


	// All done.

	return;
}



// **************************************************************************
// * ShellLogic ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  UD *    Ptr to input queue                                     *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ShellLogic              (
								signed int *        c)

	{
	// Local Variables.

	int                 i;
	int                 j;
	int                 k;

	// update frame counter

	titleFrame++;

	// Execute the actual gut of the state-driven shell logic.

	DoShellExec(c);

	// Switch on lines.

/*****
	if (asi__gJoyBtn[0] & CTRL_SHOULDER_L)
	{
		BackgroundScrollMode ^= 1;
	}
***/
	
	// Switch to the arcade.

	if (si___gCurState == STATE_EXIT_SHELL)
	{
		currentProcess->status = PROCESS_FREEING;
	}

	// All done.

	return;
	}



// **************************************************************************
// * ShellRender ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  dyn *   ?                                                      *
// *         int     ?                                                      *
// *         Gfx **  ?                                                      *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ShellRender             (
								dyn *               d,
								int                 draw_buffer,
								Gfx **              gl)

{
	// Local Variables.

	Gfx *               gp;

	Mtx                 tmp1;

	// Initialize drawing window.

	dbuf = draw_buffer;

	si___gWindowX = 0;
	si___gWindowY = 0;
	si___gWindowW = 512;
	si___gWindowH = 240;

	// Get display list ptr and pipe-sync.

	AllocGfxLists();
	gp = d->ShellList;

	gDPPipeSync(gp++);

//goto allDone;

	//

	MakeMat64PRH((long *)(&d->identity), 0, 0, 0, 0, 0, 0, 256);

	// Set up projection matrices.

	guPerspective(&tmp1,
		&nmlz,
		FOV_Y,
		320.0/240.0,
		NEAR_Z,
		FAR_Z,
		1.0);

	guMtxCatL(
		&titleFlipMatrix,
		&tmp1,
		&d->shellFlip);

	guOrtho(&tmp1,
		-512/2,
		 512/2,
		-240/2,
		 240/2,
		NEAR_Z,
		FAR_Z,
		1.0);

	guMtxCatL(
		&titleFlipMatrix,
		&tmp1,
		&d->shellOrtho);

	// Init RSP and RDP.

	gSPSegment(gp++, 0, 0x0);
	gSPDisplayList(gp++, DLInitRDP);
	gSPDisplayList(gp++, DLInitRSP);
	gSPPerspNormalize(gp++, nmlz);

	// Signal that the display code has been executed.

//	if (fl___gShellOverlay == 0)	// normal shell??
	{
		if (ShellStopDrawing() != 0)
		{
			goto allDone;
		}
	}

	// Set scissor and viewport manually - may change with resolution.

	gDPSetScissor(gp++,
		G_SC_NON_INTERLACE,
		0,
		0,
		512,
		240);

	vp.vp.vscale[0] = 512*2;
	vp.vp.vscale[1] = 240*2;
	vp.vp.vtrans[0] = 512*2;
	vp.vp.vtrans[1] = 240*2;

	gSPViewport(gp++, &vp);

///	gSPClipRatio(gp++,FRUSTRATIO_2);	//mjk  - ???

//	if (fl___gShellOverlay == 0)	// normal shell 
	{
		// Clear z-buffer.

		gDPPipeSync(gp++);

	if(ShellPlayerOnScreen)
	{
		gDPSetDepthImage(gp++,
			OS_K0_TO_PHYSICAL(dfb));

		gDPPipeSync(gp++);

		gDPSetCycleType(gp++,
			G_CYC_FILL);

		gDPSetColorImage(gp++,
       		G_IM_FMT_RGBA,
			G_IM_SIZ_16b,
			512,
			OS_K0_TO_PHYSICAL(dfb));

		gDPSetFillColor(gp++,
			(GPACK_ZDZ(G_MAXFBZ,0)<<16) | (GPACK_ZDZ(G_MAXFBZ,0)));
	
		gDPFillRectangle(gp++,
			0,
			0,
			512-1,
			512-1);
	}
		// Setup color framebuffer.

		gDPSetColorImage(gp++,
			G_IM_FMT_RGBA,
			G_IM_SIZ_16b,
			512,
			OS_K0_TO_PHYSICAL(cfb[draw_buffer]));

		// Clear color framebuffer.
/*******
// hack here by phil..
gDPSetFillColor(gp++,
//	GPACK_RGBA5551(0,0,0,1) << 16 | GPACK_RGBA5551(0,0,0,1));			// black
//	GPACK_RGBA5551(25,33,66,1) << 16 | GPACK_RGBA5551(25,33,66,1));	// try to match BG - too blue	
//	GPACK_RGBA5551(18,26,41,1) << 16 | GPACK_RGBA5551(18,26,41,1));// try to match BG 
	GPACK_RGBA5551(21,29,53,1) << 16 | GPACK_RGBA5551(21,29,53,1));// try to match BG 

gDPFillRectangle(gp++,
	0,
	0,
	currentProcess->ScreenX-1,
	currentProcess->ScreenY-1);
//******* end hack
*******/

//		if (si___gCurState <  STATE_STOP_SHELL)
		if (si___gCurState <= STATE_STOP_SHELL || si___gCurState == STATE_SHELL_ENTRY)
		{	// only black (i.e no BG) if leaving shell
			gDPPipeSync(gp++);

				gDPSetCycleType(gp++,
					G_CYC_FILL);

		  		gDPSetFillColor(gp++,
					GPACK_RGBA5551(0,0,0,1) << 16 | GPACK_RGBA5551(0,0,0,1));

				gDPFillRectangle(gp++,
					0,
					0,
					512-1,
					240-1);
		}
		else
		{
				gDPSetCycleType(gp++,G_CYC_1CYCLE);
				gSPSetGeometryMode(gp++,G_CULL_BACK);
				gDPSetColorDither(gp++,G_CD_MAGICSQ);

				gSPTexture( gp++, 0x8000, 0x8000, 0, G_TX_RENDERTILE, G_ON );
				gDPSetTexturePersp( gp++, G_TP_NONE );
				gDPSetTextureFilter( gp++, G_TF_BILERP );
				gDPSetAlphaCompare( gp++, G_AC_NONE );
				gDPSetRenderMode( gp++, G_RM_OPA_SURF, G_RM_OPA_SURF2 );

				// Draw background.

				if (BG_DoAuto)
					gp = BG_Draw( gp, 0, 0, FALSE, 255 );
/*
			{
				gDPSetFillColor(gp++,
				//	GPACK_RGBA5551(25,33,66,1) << 16 | GPACK_RGBA5551(25,33,66,1));	// try to match BG - too blue	
				//	GPACK_RGBA5551(18,26,41,1) << 16 | GPACK_RGBA5551(18,26,41,1));// try to match BG 
					GPACK_RGBA5551(21,29,53,1) << 16 | GPACK_RGBA5551(21,29,53,1));// try to match BG 

				gDPFillRectangle(gp++,
					0,
					0,
					512-1,
					240-1);
			}
*/
		}
	}

	// Reset projection and modelview matrices for 2D drawing.

	gDPPipeSync(gp++);

	gDPSetCycleType(gp++,
		G_CYC_1CYCLE);

	gSPMatrix(gp++,
		OS_K0_TO_PHYSICAL(&(d->shellOrtho)),
		G_MTX_PROJECTION | G_MTX_LOAD | G_MTX_NOPUSH);

	gSPMatrix(gp++,
		OS_K0_TO_PHYSICAL(&(d->identity)),
		G_MTX_MODELVIEW  | G_MTX_LOAD | G_MTX_NOPUSH);

	// Reset shell drawing.

	gp = ResetRenderStatus(gp);

	// Draw test font.

	#if SHOW_FONT
		gp = DrawFontTest(gp);
		goto allDone;
	#endif

	// Draw test screen.

	#if SHOW_SCREEN
		if (puw__gScreen != NULL)
			{
			gp = RenderScreenImg(gp, puw__gScreen);
			goto allDone;
			}
	#endif

	// Reset vertex list pointer.

	NextVtxList();

	// Execute current state's drawing code.

	gp = DrawShellState(si___gCurState, gp);

	gp = DrawLeftSideBar(gp, NULL,0,0);

	// Execute popup message drawing code.

//phil
	gp = DrawPopup(gp);

	// Write display list ptr back.

//	if(!displayrainbow)
//	{	// tone down everything
//		gp = RenderTBOX(gp, 0,  0, 512, 240, RGB_TRANS_DKGRY);

//		gp = RenderTBOX(gp, 0,  0, 512, 240, 0,0,0,25);
//	}

	allDone:

	gDPPipeSync(gp++);
	gSPEndDisplayList( gp++ );

	if (((u32)gp - (u32)d->ShellList) > GFX_LIST_SIZE)
		FatalError( "Shell Gfx list too small", __FILE__, __LINE__ );

	gp = *gl;
	gSPDisplayList( gp++, d->ShellList );
	*gl = gp;

	// All done.

	return;
}




static u32 CurrentSong = 0; // This should never be negative.
//			which = 0,
//				check = 0;


u32 ChangeSong( const s32 change )
{
#ifndef DEBUG	// keep same so we can do valid (consistant)  memory tests

	if (change)
	{
		CurrentSong++;

		if (CurrentSong >= 6)
		{		// overflow & error check
			CurrentSong = ZERO;
		}
	}

#endif

	return( CurrentSong );
}


#define MUSIC_LOAD	0
#define MUSIC_PLAY	1
#define MUSIC_FREE	2
#define MUSIC_WAIT	3


static void
	*MusicSong = NULL;

static u8
	PlayMusic = FALSE,
	MusicWait,
	MusicState;


static void ProcessMusic()
{
	if (PlayMusic)
	{
		switch (MusicState)
		{
			case MUSIC_LOAD:
				ChangeSong( TRUE );

				MusicSong = Audio_LoadMusic( "Shell.MUS", "Shell.BNK", CurrentSong );
#ifdef DEBUG 
				if (MemTest() < 0)
				{
					while(1);
				}
#endif
				
				Audio_MusicPlay( MusicSong, CurrentSong );
#ifdef DEBUG 
				if (MemTest() < 0)
				{
					while(1);
				}
#endif
				MusicWait = 2;
				MusicState++;
				break;

			case MUSIC_PLAY:
				if (MusicWait)
					MusicWait--;
				else
				{
					if (!Audio_MusicActive())
					{
						Audio_MusicStop();
						MusicWait = 2;
						MusicState++;
					}
				}
				break;

			case MUSIC_FREE:
				if (MusicWait)
					MusicWait--;
				else
				{
					if (MusicSong)
					{
						Audio_FreeMusic( MusicSong );
						MusicSong = NULL;
					}

					MusicWait = 2*30;
					MusicState++;
				}
				break;

			case MUSIC_WAIT:
				if (MusicWait)
					MusicWait--;
				else
					MusicState = MUSIC_LOAD;
				break;
		}
	}
}


// **************************************************************************
// * DoShellExec ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  UD *    Ptr to input queue                                     *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

#define	RGB_HELP_BOXTL        0,100, 88,255
#define	RGB_HELP_BOXTR        0, 85, 77,255
#define	RGB_HELP_BOXBL        0, 47, 43,255
#define	RGB_HELP_BOXBR        0, 35, 30,255
#define	RGB_HELP_EDGE        32,128, 80,255
#define	RGB_HELP_BUTTON     255,255,255,128
#define	RGB_HELP_HILITE     255,255,255,255

global	UB                  aub__gHelpColours [] =
	{
	RGB_HELP_BOXTL,			// BoxTL
	RGB_HELP_BOXTR,			// BoxTR
	RGB_HELP_BOXBL,			// BoxBL
	RGB_HELP_BOXBR,			// BoxBR
	RGB_HELP_EDGE, 			// Edge
	RGB_MESSAGE_BUTTON, 	// Button (normal)
	RGB_MESSAGE_HILITE 		// Button (hilite)
	};

global	int                 DoShellExec             (
								signed int *        controllerdata)

	{
	// Local Variables.

	int                 i;
	int                 j;
	int                 k;

	// Auto-change the music ?

	ProcessMusic();

	// Process joypads with auto-repeat.

	ProcessJoypads(controllerdata, 1);

	// Are we trying to save ?

	if (sb___gWantToSave != 0)
		{
		ProcMemPakSave();
		}
	else
		{
		// Init popup message ?

		if (pcz__gPopupMsg != NULL)
			{
			InitPopup(pcz__gPopupTtl, pcz__gPopupMsg, pub__gPopupPal);

			pcz__gPopupMsg = NULL;
			}

		// Process any existing popup message.

		i = ProcPopup();

		if (i != 0)
			{
			si___gPopupVal = i;
			}

		// Exit popup message ?

		if (i >= 0)
			{
			ExitPopup();
			}

		// Execute normal state code if we're not displaying a popup message.

		if (i == 0)
			{
			// Were we viewing a help screen ?

			if (fl___gShowingHelp)
				{
				fl___gShowingHelp = 0;
				si___gPopupVal    = 0;
				}

			// Are we waiting to change state ?

			if (si___gNxtState != si___gCurState)
			{

			// Wait for the display to stabilize.

				if (ShellLockDisplay() == 0) goto allDone;

				// When the display pause has finished, complete the state change.

				i = ExitShellState(si___gCurState);		// why store @ i??????

				while ((i = InitShellState(si___gNxtState)) != si___gNxtState)
				{
					si___gNxtState = i;
				}

				si___gOldState = si___gCurState;
				si___gCurState = si___gNxtState;

				// Unlock the display and continue.

				ShellUnlockDisplay();
			}

			// Bring up the help message ?

//			if ((asi__gTeamBtn[EVERYBODY] & (CTRL_SHOULDER_L | CTRL_SHOULDER_R)) && (si___gPopupVal == 0))
			if ((asi__gTeamBtn[EVERYBODY] & (CTRL_SHOULDER_R)) && (si___gPopupVal == 0))
			{
				int si___Sound;

				pcz__gPopupMsg = apcz_gStateHelpMsg[si___gCurState];

				if (si___gCurState == STATE_B)
				{
					if (cl___gStatus.sb___BView != 0)
						pcz__gPopupMsg = acz__gXxxHelpScreenB;
				}
/***				else
				if (si___gCurState == STATE_G)
				{
					if (cl___gStatus.sb___BView != 0)
						pcz__gPopupMsg = acz__gXxxHelpScreenG;
				}
				else
				if (si___gCurState == STATE_G1)
				{
					if (cl___gStatus.sb___BView != 0)
						pcz__gPopupMsg = acz__gXxxHelpScreenG1;
				}*******************/
				/*****************
				else
				if ((si___gCurState == STATE_E) ||
					(si___gCurState == PAUSE_E) ||
					(si___gCurState == STATE_Q1))
				{
					if (sb___gSubShown)
						pcz__gPopupMsg = acz__gXxxHelpScreenE;
				} *************/
				else
				if ((si___gCurState == STATE_Q) ||
					(si___gCurState == STATE_R))
				{
					if (sb___gSubShown)
						pcz__gPopupMsg = acz__gXxxHelpScreenI3;
				}


				if (pcz__gPopupMsg != NULL)
				{
					fl___gShowingHelp = 1;

					si___gPopupVal = 0;
					pcz__gPopupTtl = acz__gHelpTitle;
					pub__gPopupPal = aub__gHelpColours;

					si___Sound = SFX_SHELL_BTNA;
				}
				else
				{
					si___Sound = SFX_SHELL_NOMV;
				}

				AudioPlaySfx(si___Sound, 11025, 255, 7);
			}
			else
			{
				// Execute state code if we're not displaying a popup message.

				si___gNxtState = ProcShellState(si___gCurState);
			
				si___gPopupVal = 0;
/*******
				if(si___gNxtState == STATE_D)	// mjk test - exit back to phil's bball2
				{
					if (controllerdata[0] & CTRL_START)
					{
				//		currentProcess->status = PROCESS_FREEING;
					}
				}			
******/
			}
		}
	}

	// Signal that a new frame should be drawn.

	ui___gFrameDrawn = 0;


#if SHOW_BG_COURT_LINES		

	// Process background lines.

	if (fl___gShellOverlay == 0)
	{
		if (ShellLocking() == 0)
		{
			for (i = 0; i < MAX_ACTIVE_LINES; i++)
			{
				if(!activeLines[i].flags) continue;

				if(activeLines[i].flags & 2)
				{
					activeLines[i].alpha--;
					if(activeLines[i].alpha<=0)
					{
						activeLines[i].flags=0;
						break;
					}
				}
				else
				{
					activeLines[i].alpha++;
					if(activeLines[i].alpha>=ALPHA_LIMIT)
					{
						activeLines[i].alpha=ALPHA_LIMIT;
						activeLines[i].flags |= 2;
						AddLine();
					}
				}

				activeLines[i].camH+=activeLines[i].camDH;
				activeLines[i].camX+=activeLines[i].camDX;
				activeLines[i].camY+=activeLines[i].camDY;
				activeLines[i].camZ+=activeLines[i].camDZ;
			}
		}
	}
#endif

	// All done.

	allDone:

	return (si___gCurState);
	}



#endif	// ALL_PROCESSES

	
	
// **************************************************************************
// * ShellLockDisplay ()                                                    *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  int    NZ if the caller should stop drawing                    *
// **************************************************************************
int FPWait = 5;	// for relatime debugging

global	int                 ShellLockDisplay        (void)
{
	// Signal the display code to pause drawing.

	if (si___gFramePause < 0)
	{	// give it enough time so all renders are done
//		si___gFramePause = 1;
//		si___gFramePause = 3;
//		si___gFramePause = 5;
		si___gFramePause = FPWait;

		return (0);
	}

	// Has the display finished its pause ?

	if (si___gFramePause > 0)
	{
		return (0);
	}

	// Signal that the display is locked.

	return (1);
}



// **************************************************************************
// * ShellUnlockDisplay ()                                                  *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  int    NZ if the caller should stop drawing                    *
// **************************************************************************

global	void                ShellUnlockDisplay      (void)

	{
	// When the display pause has finished, allow things to start up again.

	si___gFramePause = -1;

	// Allow drawing to continue.

	return;
	}



// **************************************************************************
// * ShellLocking ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  int    NZ if the caller should stop drawing                    *
// **************************************************************************

global	int                 ShellLocking            (void)

	{
	if (si___gFramePause < 0)
		{
		return (0);
		}

	return (1);
	}




// **************************************************************************
// * ShellStopDrawing ()                                                    *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  int    NZ if the caller should stop drawing                    *
// **************************************************************************

global	int                 ShellStopDrawing        (void)

	{
	// Pause the display to allow the logic to load up new graphics.

	if (si___gFramePause >= 0)
		{
		if (si___gFramePause >= 1)
			{
			si___gFramePause -= 1;
			}

		if (si___gFramePause <= 2)
			{
			// Signal the render function to stop drawing.

			return (1);
			}
		}

	// Signal the render function to continue drawing.

	ui___gFrameDrawn += 1;

	return (0);
	}



	
// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * ReadJoypads ()                                                         *
// **************************************************************************
// * Read current joypad values                                             *
// **************************************************************************
// * Inputs  int *   Ptr to input queue                                     *
// *         FL      Repeat flag (TRUE if you want auto-repeat)             *
// *                                                                        *
// * Output  -                                                              *
// *                                                                        *
// * N.B.    Updates the current joypad status.                             *
// **************************************************************************

#define	REPT_VAL            0x16
#define	REPT_NXT            0x10

global	void                ProcessJoypads          (
								signed int *        pasi_Joypad,
								int                 fl___Repeat)

{
	// Local Variables.

int                 i;
int                 j;
int                 k;

	// Process each individual joypad.

	for (i = 0; i < 4; i++)
	{
		// Get the current joypad value.

		j = pasi_Joypad[i];

		// Assign digital values to the analog controller.

		if ((j & CTRL_UP)    && ((j & 0xC0000000u) == 0xC0000000u)) j |= CTRL_PAD_UP;
		if ((j & CTRL_DOWN)  && ((j & 0xC0000000u) == 0xC0000000u)) j |= CTRL_PAD_DOWN;
		if ((j & CTRL_LEFT)  && ((j & 0x0C000000u) == 0x0C000000u)) j |= CTRL_PAD_LEFT;
		if ((j & CTRL_RIGHT) && ((j & 0x0C000000u) == 0x0C000000u)) j |= CTRL_PAD_RIGHT;

		// Store results.

		asi__gJoyBtn[i] = j & ~asi__gJoyPad[i];
		asi__gJoyPad[i] = j;

		// Process auto-repeat.

		if (fl___Repeat == 0)
		{
			// Disabled.

			asb__sJoyRpt[i] = 0;
		}
		else
		{
			// Handle boundary conditions when autorepeat is started.

			if (asb__sJoyRpt[i] == 0)
			{
				asb__sJoyRpt[i] = 1;
			}

			if (asb__sJoyRpt[i] == 1)
			{
				if (asi__gJoyPad[i] == 0)
				{
					asb__sJoyRpt[i] = 2;
					*((UD *) &asb__sJoyClk[i][0]) = 0;
				}
			}

			// Process autorepeat of the 4 direction buttons.

			if (asb__sJoyRpt[i] == 2)
			{
				k = CTRL_PAD_UP;

				for (j = 0; j < 4; j++)
				{
					if (asi__gJoyPad[i] & k)
					{
						if (++asb__sJoyClk[i][j] > ((SB) REPT_VAL))
						{
							asb__sJoyClk[i][j] = REPT_NXT;
							asi__gJoyBtn[i]   |= k;
						}
					}
					else
					{
						asb__sJoyClk[i][j] = 0;
					}

					k = k << 1;
				}
			}
		}
	}

	// Asign joypads to teams (with an error check).

	for (i = HOME_TEAM; i < BOTH_TEAM; i++)
	{
		asi__gTeamPad[i] = 0;
		asi__gTeamBtn[i] = 0;
	}

	if (pcl__gMatch != NULL)
	{
		for (i = 0; i < 4; i++)
		{
			j = pcl__gMatch->asb__mPads[i];

			if ((j >= HOME_TEAM) && (j <= NULL_TEAM))
			{
				asi__gTeamPad[j] |= asi__gJoyPad[i];
				asi__gTeamBtn[j] |= asi__gJoyBtn[i];
			}
		}
	}

	// Create composite joypad values.

	asi__gTeamPad[BOTH_TEAM] =	asi__gTeamPad[HOME_TEAM] | asi__gTeamPad[AWAY_TEAM];

	asi__gTeamBtn[BOTH_TEAM] =	asi__gTeamBtn[HOME_TEAM] | asi__gTeamBtn[AWAY_TEAM];

	asi__gTeamPad[EVERYBODY] =	asi__gJoyPad[0] | asi__gJoyPad[1] | asi__gJoyPad[2] | asi__gJoyPad[3];

	asi__gTeamBtn[EVERYBODY] =	asi__gJoyBtn[0] | asi__gJoyBtn[1] | asi__gJoyBtn[2] | asi__gJoyBtn[3];

	// All Done.

	return;
}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************


// **************************************************************************
// * AudioOpenShell ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                AudioOpenShell          (void)
{
	PlayMusic = TRUE;
	MusicState = MUSIC_LOAD;
}



// **************************************************************************
// * AudioCloseShell ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                AudioCloseShell         (void)
{
	Audio_MusicStop();
	Audio_Process();
	Audio_Process();

	if (PlayMusic)
	{
		PlayMusic = FALSE;

		if (MusicSong)
			Audio_FreeMusic( MusicSong );

		MusicSong = NULL;
	}
}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * LoadSprPalettes ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  int     0 if OK, -ve if an error                               *
// **************************************************************************

global	int                 LoadSprPalettes         (
								SPR_LIST_T *        pcl__List)

	{
	// Load up the whole palette table.

	while (pcl__List->pul__ResID != NULL)
	{
		pcl__List->pbf__Res =
			(void *) ResOpenID(pcl__ShellRes, *pcl__List->pul__ResID);

		pcl__List += 1;
	}

	// All done, return OK.

	return (0);
	}



// **************************************************************************
// * FreeSprPalettes ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  int     0 if OK, -ve if an error                               *
// **************************************************************************

global	void                FreeSprPalettes         (
								SPR_LIST_T *        pcl__List)

	{
	// Free up the whole palette table.

	while (pcl__List->pul__ResID != NULL)
		{
		ResPurgeID(pcl__ShellRes, *pcl__List->pul__ResID);

		pcl__List->pbf__Res = NULL;

		pcl__List += 1;
		}

	// All done.

	return;
	}



// **************************************************************************
// * LoadMatchLogoPalettes ()                                               *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                LoadMatchLogoPalettes   (void)

	{
	// Local Variables.

	int                 i;

	SPR_LIST_T *        pcl__List;

	// Load up the logo palettes for the match teams.
	//
	// N.B. Only needed if we're an overlay, because they're permanently
	//      loaded during the shell proper.

	for (i = 0; i < 2; i += 1)
	{
		pcl__List = &acl__gShellPal[(FIRST_TEAM_PAL + pcl__gMatch->asb__mTeam[i])];

		if (pcl__List->pbf__Res == NULL)
		{
			pcl__List->pbf__Res =
				(void *) ResOpenID(pcl__ShellRes, *pcl__List->pul__ResID);
		}
	}

	// All Done.

	return;
}



// **************************************************************************
// * FreeMatchLogoPalettes ()                                               *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                FreeMatchLogoPalettes   (void)

	{
	// Local Variables.

	int                 i;

	SPR_LIST_T *        pcl__List;

	// Free up the logo palettes for the match teams.
	//
	// N.B. Only needed if we're an overlay, because they're permanently
	//      loaded during the shell proper.

	for (i = 0; i < 2; i += 1)
	{
		pcl__List = &acl__gShellPal[(FIRST_TEAM_PAL + pcl__gMatch->asb__mTeam[i])];

		if (pcl__List->pbf__Res != NULL)
		{
			ResPurgeID(pcl__ShellRes, *pcl__List->pul__ResID);

			pcl__List->pbf__Res = NULL;
		}
	}

	// All Done.

	return;
}



// **************************************************************************
// * InitAllSprites ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  UB *    Ptr to list of sprite numbers                          *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                InitAllSprites          (void)

	{
	// Local Variables.

	SPR_LIST_T *        pcl__List;
	UB *                pub__List;

	// Init whole sprite table.

	pcl__List = &acl__gShellSpr[1];
	pub__List = &aub__gShellSpr[1];

	while (pcl__List->pul__ResID != NULL)
		{
		pcl__List->pbf__Res = NULL;
		pub__List[0]        = 0;

		pcl__List += 1;
		pub__List += 1;
		}

	// All done.

	return;
	}



// **************************************************************************
// * FreeAllSprites ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  UB *    Ptr to list of sprite numbers                          *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                FreeAllSprites          (void)

	{
	// Local Variables.

	SPR_LIST_T *        pcl__List;
	UB *                pub__List;

	// Free whole sprite table.

	pcl__List = &acl__gShellSpr[1];
	pub__List = &aub__gShellSpr[1];

	while (pcl__List->pul__ResID != NULL)
		{
		MemFree(pcl__List->pbf__Res);

		pcl__List->pbf__Res = NULL;
		pub__List[0]        = 0;

		pcl__List += 1;
		pub__List += 1;
		}

	// All done.

	return;
	}



// **************************************************************************
// * LoadSpriteList ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  UB *    Ptr to list of sprite numbers                          *
// *                                                                        *
// * Output  int     0 if OK, -ve if an error                               *
// **************************************************************************

global	int                 LoadSpriteList          (
								UB *                pub__List)

	{
	// Local Variables.

	int                 i;

	SPR_LIST_T *        pcl__List;

	// Load whole sprite list.

	while ((i = *pub__List++) != 0)
		{
		// Increment usage count.

		aub__gShellSpr[i] += 1;

		// Load the sprite if it isn't already loaded.

		pcl__List = &acl__gShellSpr[i];

		if (pcl__List->pbf__Res == NULL)
			{
			pcl__List->pbf__Res = LoadSprite(*pcl__List->pul__ResID);
			}
		}

	// All done, return OK.

	return (0);
	}



// **************************************************************************
// * FreeSpriteList ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  UB *    Ptr to list of sprite numbers                          *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                FreeSpriteList          (
								UB *                pub__List)

	{
	// Local Variables.

	int                 i;

	SPR_LIST_T *        pcl__List;

	// Free whole sprite list.

	while ((i = *pub__List++) != 0)
		{
		// Already free ?

		if (aub__gShellSpr[i] != 0)
			{
			// Decrement usage count.

			if (--aub__gShellSpr[i] == 0)
				{
				// Free the sprite when it's usage count goes to zero.

				pcl__List = &acl__gShellSpr[i];

				MemFree(pcl__List->pbf__Res);

				pcl__List->pbf__Res = NULL;
				}
			}
		}

	// If we're overlayed, then purge everything.

	if (fl___gShellOverlay)	// from PAUSE menu during game
	{
		// NO NO- in Kobe 2 all shell pause screen are isolated in a menu 
		// and shell data is purged when we leave that menu to return to the game
		// so don't kill everything after each screen
//		ResPurgeAll(pcl__ShellRes);
	}

	// All done.

	return;
	}



// **************************************************************************
// * LoadTeamLogo ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  int     Team number                                            *
// *                                                                        *
// * Output  int     0 if OK, -ve if an error                               *
// **************************************************************************

global	int                 LoadTeamLogo            (
								int                 si___Team,
								int                 si___Count)

	{
	// Local Variables.

	int                 i;

	SPR_LIST_T *        pcl__List;
	UB *                pub__List;

	// Limit teams to ones valid in the current build.

//	si___Team = aub__gValidTeam[si___Team];
//
//	if (si___Team == 0) return (0);

	// Load the 8 logo sprites.

	si___Team = (si___Team << 3) + SPR_LOGOS;

	pcl__List = &acl__gShellSpr[si___Team];
	pub__List = &aub__gShellSpr[si___Team];

	if (si___Count == 1)
		{
		// If we're only loading one frame, make sure its the correct one.

		pcl__List += 1;
		pub__List += 1;
		}

	while (si___Count--)
		{
		// Increment usage count.

		*pub__List += 1;

		// Load the sprite if it isn't already loaded.

		if (pcl__List->pbf__Res == NULL)
			{
			pcl__List->pbf__Res = LoadSprite(*pcl__List->pul__ResID);
			}

		// Increment pointers.

		pcl__List += 1;
		pub__List += 1;
		}

	// All done, return OK.

	return (0);
	}



// **************************************************************************
// * FreeTeamLogo ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  int     Team number                                            *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void	FreeTeamLogo	(int	si___Team)

	{
	// Local Variables.

	int                 i;

	SPR_LIST_T *        pcl__List;
	UB *                pub__List;

	// Limit teams to ones valid in the current build.

//	si___Team = aub__gValidTeam[si___Team];
//
//	if (si___Team == 0) return;

	// Free the 8 logo sprites.

	si___Team = (si___Team << 3) + SPR_LOGOS;

	pcl__List = &acl__gShellSpr[si___Team];
	pub__List = &aub__gShellSpr[si___Team];

	for (i = 8; i != 0; i -= 1)
		{
		// Already free ?

		if (*pub__List != 0)
			{
			// Decrement usage count.

			*pub__List -= 1;

			// Free the sprite if it isn't already loaded.

			if (*pub__List == 0)
				{
				if (pcl__List->pbf__Res != NULL)
					{
					MemFree(pcl__List->pbf__Res);

					pcl__List->pbf__Res  = NULL;
					}
				}
			}

		// Increment pointers.

		pcl__List += 1;
		pub__List += 1;
		}

	// All done, return OK.

	return;
	}



// **************************************************************************
// * TestTeamLogo ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  int     Team number                                            *
// *                                                                        *
// * Output  int     0 if not loaded, 1 if loaded                           *
// **************************************************************************

global	int                 TestTeamLogo            (
								int                 si___Team)

	{
	// Local Variables.

	UB *                pub__List;

	// Limit teams to ones valid in the current build.

//	si___Team = aub__gValidTeam[si___Team];
//
//	if (si___Team == 0) return (0);

	// Return status of the 1st of the 8 logo sprites.

	si___Team = (si___Team << 3) + SPR_LOGOS;

	pub__List = &aub__gShellSpr[si___Team];

	return (*pub__List);
	}



// **************************************************************************
// * FreeTeamLogos ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                FreeTeamLogos           (void)

	{
	// Local Variables.

	SI                  i;

	// Free up all the team logos.

	for (i = 0; i <= TEAM_END; i++)
		{
		FreeTeamLogo(i);
		}

	// All done.

	return;
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * LoadPreferences ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************





/****************
global	void                LoadPreferences         (
								Preferences_T *     pcl__Pref)

	{
	// Local Variables.

	Preferences_T *     pcl__Temp;

	// Allocate aligned workspace.

	pcl__Temp = (Preferences_T *) MemMalloc(24, MEM_ANY);
//	pcl__Temp = (Preferences_T *) MemMalloc(2048, MEM_ANY);

	// Read the preferences from eeprom.

	CtrlEepromReadBuffer(0, (UB *) pcl__Temp, (24>>3));
//	CtrlEepromReadBuffer(0, (UB *) pcl__Temp, (2048>>3));

	// Test the file version and checksum.

	if ((pcl__Temp->sb___PrefVersion == LFP_FILE_VERSION) &&
		(MakeChecksum((UD *) pcl__Temp, 24) == 0))
		{
		// Copy the data to its destination.

		wordcpy(pcl__Pref, pcl__Temp, sizeof(Preferences_T));
		}

	// Test for eeprom corruption.

//	ReadEepromFile(((UB *) pcl__Temp) + 24);

	// Free up the temporary workspace.

	MemFree(pcl__Temp);

	// All Done.

	return;
	}
*******************/

global	void	LoadPreferences(Preferences_T *     pcl__Pref)		// from Flash Rom
{
	// Local Variables.

Preferences_T *     pcl__Temp;
int error;

	// Allocate aligned workspace.

	pcl__Temp = (Preferences_T *) MemCalloc(FR_PAGE_SIZE_BYTES, MEM_ANY);

	// Read the preferences from Flash ROM.

	error = ReadFlashRom(PREFERENCES_FLASH_PAGE_NUM, 1, (char *) pcl__Temp);

//	CtrlEepromReadBuffer(0, (UB *) pcl__Temp, (24>>3));
//	CtrlEepromReadBuffer(0, (UB *) pcl__Temp, (2048>>3));

	// Test the file version and checksum.
	//Note: will use default data if error on load or test 

	if ((pcl__Temp->sb___PrefVersion == LFP_FILE_VERSION) &&
		(MakeChecksum((UD *) pcl__Temp, 24) == 0))
	{
		// Copy the data to its destination.

		wordcpy(pcl__Pref, pcl__Temp, sizeof(Preferences_T));
	}

	// Test for eeprom corruption.

//	ReadEepromFile(((UB *) pcl__Temp) + 24);

	// Free up the temporary workspace.

	MemFree(pcl__Temp);

	// All Done.

	return;
	}

// **************************************************************************
// * SavePreferences ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void  SavePreferences (Preferences_T *     pcl__Pref)	// to flash rom

{
	// Local Variables.

Preferences_T *     pcl__Temp;
//int error;

	if(sb___gSaveDisabled)
		return;

	// Setup the file version and checksum.

	pcl__Pref->ud___PrefChecksum = 0;
	pcl__Pref->sb___PrefVersion  = LFP_FILE_VERSION;
	pcl__Pref->sb___PrefRandom   = ReadRandomByte();
	pcl__Pref->ud___PrefChecksum = MakeChecksum((UD *) pcl__Pref, 24);


	// Allocate 1 page of Flash ROM aligned workspace.

	pcl__Temp = (Preferences_T *) MemCalloc(FR_PAGE_SIZE_BYTES, MEM_ANY);

	// Copy the data to the workspace.

	wordcpy(pcl__Temp, pcl__Pref, sizeof(Preferences_T));

	// Write out the preferences to Flash Rom.

//	CtrlEepromWriteBuffer(0, (UB *) pcl__Temp, (24>>3));	// preferences
	WriteFRError = WriteFlashRom(PREFERENCES_FLASH_PAGE_NUM, 1, (char *) pcl__Temp);


/*************	
	// Allocate aligned workspace.

	pcl__Temp = (Preferences_T *) MemMalloc(24, MEM_ANY);

	// Copy the data to the workspace.

	wordcpy(pcl__Temp, pcl__Pref, sizeof(Preferences_T));

	// Write out the preferences to eeprom.

	CtrlEepromWriteBuffer(0, (UB *) pcl__Temp, (24>>3));	// preferences
********/
	// Free up the temporary workspace.

	MemFree(pcl__Temp);

	// Test for eeprom corruption.

//	LoadPreferences(pcl__Pref);

	// All Done, return OK.

	return;
}





// **************************************************************************
// *				Save  Custom   Users                                    *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************


void SaveCustomUserData(void)
{
saveCUData_T  * ptr;		
int filesize,datasize,allocbytes;
int pagesneeded;
int error;
int step =0;
	
	if(NeedCustomUserSave && !sb___gSaveDisabled)
	{
		NeedCustomUserSave = 0;

		filesize = sizeof(saveCUData_T);
		pagesneeded = (filesize + FR_PAGE_SIZE_BYTES-1) >> FR_PAGE_SIZE_SHIFT;
		allocbytes = pagesneeded << FR_PAGE_SIZE_SHIFT;
	
		ptr = (saveCUData_T *) MemCalloc(allocbytes, MEM_ANY);

		ptr->ud___Checksum = 0;
		ptr->sb___Version  = LFP_FILE_VERSION;
		ptr->sb___Random   = ReadRandomByte();
		ptr->sb___Pad0 = 0;
		ptr->sb___Pad1 = 0;
	
		datasize = sizeof(UserTeamInfo_T) * MAX_CUSTOM_USERS;
		wordcpy(&ptr->UserTeams,UserTeams,datasize);
	
		ptr->ud___Checksum = MakeChecksum((UD *) ptr, filesize);

		WriteFRError = WriteFlashRom(CUSTOM_USERS_FLASH_PAGE_NUM, pagesneeded, (char *) ptr);

		MemFree(ptr);

	}
}


int DecodeStep = 10;	// printed on FR test screen


global	void	LoadCustomUsers(void)		// from Flash Rom - only on power up
{
	// Local Variables.

saveCUData_T  * ptr;		
int filesize,datasize,allocbytes;
int pagesneeded;
int error;

	// Allocate aligned workspace.

	filesize = sizeof(saveCUData_T);
	pagesneeded = (filesize + FR_PAGE_SIZE_BYTES -1 ) >> FR_PAGE_SIZE_SHIFT;
	allocbytes = pagesneeded << FR_PAGE_SIZE_SHIFT;
	
	// alloc space for # pages
	ptr = (saveCUData_T *) MemCalloc(allocbytes, MEM_ANY);

	// Read the data from Flash ROM.

	error = ReadFlashRom(CUSTOM_USERS_FLASH_PAGE_NUM, pagesneeded, (char *) ptr);

	// Test the file version and checksum.
	//Note: will use default data if error on load or test 

	DecodeStep = 0;

	if ( ptr->sb___Version == LFP_FILE_VERSION)
	{
		DecodeStep = 1;
		if(MakeChecksum((UD *) ptr, filesize) == 0)
		{
			DecodeStep = 2;
		// Copy the user data to its destination.
			datasize = sizeof(UserTeamInfo_T) * MAX_CUSTOM_USERS;
			wordcpy(UserTeams,&ptr->UserTeams, datasize);

		}
	}

	// Free up the temporary workspace.

	MemFree(ptr);


	return;
}




global	void ShrinkTeamPlayoffStats(TeamRamStat_T *     pcl__Src,TeamRamStat_T *     pcl__Dst);


#if 0	// --- for Kobe 2 (1999) - Flash Rom & MemPak use same data formatting (i.e. no eeprom)

/**************************************************************************
 * MakeEepromFile ()                                                      *
 **************************************************************************
 *                                                                        *
 **************************************************************************
 * Inputs  -                                                              *
 *                                                                        *
 * Output  -                                                              *

	here's what's saved in EEPROM:		(as of 6/4/99 from Kobe 1 - mjk)	
	
	 12			Header			4bytes=filesize,4bytes=cksum,4bytes=LFP_FILE_VERSION;
	108			Options							(3optiontypes * 36)
	 16			Season's playoff teams			(asb__gSsnTopTeams, 16)
	 64			Playoff tree.					(asb__gPlayoffTree, 64);
	932(930)	apuw_gRoster[SOURCE_SSN]		sl___gInfoRostLen (padded to 4th byte)
    240			4 custom players					( 4plyrs*60)
	406			team season-statistics(minimal)		(29teams*14)
	224			team playoff-statistics(minimal)	(16teams*14bytes)
	  6			pad to 8 byte boundary
	-----
   2008	total bytes
	NOTE: first 24 bytes of EEPROM is where preferences are stored
 
 **************************************************************************/

/*************
global	int                 MakeEepromFile          (			 deleted for Kobe 2
								char **             ppub_Addr)

	{
	// Local Variables.

	int                 i;

	int                 si___Size;
	UB *                pub__Addr;
	UB *                pub__Temp;

	TeamRamStat_T       acl__Stat [16];

	// Allocate a buffer for the file.

	if (*ppub_Addr != NULL)
	{
		pub__Addr = *ppub_Addr;
	}
	else
	{
		pub__Addr = (UB *) MemMalloc(2048, MEM_ANY);
	}

	pub__Temp = &pub__Addr[12];

	// Save the current options.

	for (i = SOURCE_PRE; i <= SOURCE_PLY; i += 1)
	{
		wordcpy(pub__Temp, &acl__gOptions[i], sizeof(Options_T));

		pub__Temp += (~3 & (3 + sizeof(Options_T)));
	}

	// Save the season's playoff teams.

	wordcpy(pub__Temp, asb__gSsnTopTeams, 16);

	pub__Temp += 16;

	// Save the playoff tree.

	wordcpy(pub__Temp, asb__gPlayoffTree, 64);

	pub__Temp += 64;

	// Save the ssn-rosters.

	wordcpy(pub__Temp, apuw_gRoster[SOURCE_SSN], sl___gInfoRostLen);

	pub__Temp += (~3 & (3 + sl___gInfoRostLen));

	// Save the custom plyrs.

	for (i = 0; i < 4; i += 1)
	{
		bytecpy(pub__Temp, &((apcl_gCustom[SOURCE_SSN])[i]).ub___pTeam, 60);

		pub__Temp += 60;
	}

	// Save the team ssn-statistics.

	for (i = 0; i < si___gTeamSsnStatCnt; i += 1)
	{
		bytecpy(pub__Temp, &pacl_gTeamSsnStat[i], sizeof(TeamEepStat_T));

		pub__Temp += sizeof(TeamEepStat_T);
	}

	// Save the team ply-statistics.
	// Save the team PLAYOFF statistics. (please SPELL it out!!!!) (ply = player???)

	ShrinkTeamPlayoffStats(pacl_gTeamPlyStat, acl__Stat);

	for (i = 0; i < 16; i += 1)
	{
		bytecpy(pub__Temp, &acl__Stat[i], sizeof(TeamEepStat_T));

		pub__Temp += sizeof(TeamEepStat_T);
	}

//	while ((pub__Temp - pub__Addr) & 3) *pub__Temp++ = 0;

	// Save the plyr season-statistics.	- nope, not in 2K

	// Save the plyr playoff-statistics.- nope, not in 2K


	// Pad out to the next 7 byte boundary.
	si___Size = pub__Temp - pub__Addr;

	while (si___Size & 7)
	{
		*pub__Temp = 0;
		pub__Temp += 1;
		si___Size += 1;
	}

	// Calculate the file checksum.

	((UD *) pub__Addr)[0] = si___Size;
	((UD *) pub__Addr)[1] = 0;
	((UD *) pub__Addr)[2] = LFP_FILE_VERSION;

	((UD *) pub__Addr)[1] = MakeChecksum(((UD *) pub__Addr), si___Size);

	// All Done, return the filesize.

	*ppub_Addr = pub__Addr;

	return (si___Size);
	}



// **************************************************************************
// * ReadEepromFile ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -    ZERO if no errors                                           *
// **************************************************************************

global	int                 ReadEepromFile          (
								char *              pub__Addr)

	{
	// Local Variables.

	int                 i;

	UD *                pud__Addr;
	UB *                pub__Temp;

	TeamRamStat_T       acl__Stat [16];

	// Allocate a buffer for the file.

	pud__Addr = (UD *) pub__Addr;

	pub__Temp = &pub__Addr[12];

	// Test the file version and checksum.

	if ((pud__Addr[2] != LFP_FILE_VERSION) ||
		(MakeChecksum(pud__Addr, pud__Addr[0]) != 0))
	{
		return (-1);
	}

	// Read the current options.

	for (i = SOURCE_PRE; i <= SOURCE_PLY; i += 1)
	{
		wordcpy(&acl__gOptions[i], pub__Temp, sizeof(Options_T));

		pub__Temp += (~3 & (3 + sizeof(Options_T)));
	}

	// Read the season's playoff teams.

	wordcpy(asb__gSsnTopTeams, pub__Temp, 16);

	pub__Temp += 16;

	// Read the playoff tree.

	wordcpy(asb__gPlayoffTree, pub__Temp, 64);

	pub__Temp += 64;

	// Read the ssn-rosters.

	wordcpy(apuw_gRoster[SOURCE_SSN], pub__Temp, sl___gInfoRostLen);

	pub__Temp += (~3 & (3 + sl___gInfoRostLen));

	// Read the custom plyrs.

	wordclr(apcl_gCustom[SOURCE_SSN], CUSTOM_PLYRS * sizeof(PlyrRomData_T));

	for (i = 0; i < 4; i += 1)
	{
		bytecpy(&((apcl_gCustom[SOURCE_SSN])[i]).ub___pTeam, pub__Temp, 60);

		pub__Temp += 60;
	}

	wordcpy(pacl_gCustom, apcl_gCustom[SOURCE_SSN], CUSTOM_PLYRS * sizeof(PlyrRomData_T));

	// Read the team ssn-statistics.

	for (i = 0; i < si___gTeamSsnStatCnt; i += 1)
	{
		wordclr(&pacl_gTeamSsnStat[i], sizeof(TeamRamStat_T));
		memcpy(&pacl_gTeamSsnStat[i], pub__Temp, sizeof(TeamEepStat_T));

		pub__Temp += sizeof(TeamEepStat_T);
	}

	// Read the team ply-statistics.

	for (i = 0; i < 16; i += 1)
	{
		wordclr(&acl__Stat[i], sizeof(TeamRamStat_T));
		memcpy(&acl__Stat[i], pub__Temp, sizeof(TeamEepStat_T));

		pub__Temp += sizeof(TeamEepStat_T);
	}

	ExpandTeamPlyStats(acl__Stat, pacl_gTeamPlyStat);

	// CLEAR the plyr ssn-statistics.

	for (i = 0; i < si___gPlyrSsnStatCnt; i += 1)
	{
		wordclr(&pacl_gPlyrSsnStat[i], sizeof(PlyrRamStat_T));
	}

	// CLEAR  the plyr ply-statistics.

	for (i = 0; i < si___gPlyrPlyStatCnt; i += 1)
	{
		wordclr(&pacl_gPlyrPlyStat[i], sizeof(PlyrRamStat_T));
	}

	// All Done, return OK.

	return (0);
	}

*******/
#endif	//--------------- no Eeprom in Kobe 2 -----------------------------------


// **************************************************************************
// *			MakeMemPakFile ()                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
/* Output  -   size of data                                                 *	

	here's what's saved on Controller Paks:		(as of 6/4/99 (from Kobe 1 - mjk)	
	
	 12			Header			4bytes=filesize,4bytes=cksum,4bytes=LFP_FILE_VERSION;
	108			Options							(3optiontypes * 36)
	 16			Season's playoff teams			(asb__gSsnTopTeams, 16)
	 64			Playoff tree.					(asb__gPlayoffTree, 64);
	932(930)	apuw_gRoster[SOURCE_SSN]		sl___gInfoRostLen (padded to 4th byte)
   1200			20 custom playrs					(20plyrs*60)
	812			team season-statistics(full)		(29teams*28) (padded to 4th byte)
	448			team playoff-statistics(full)		(16teams*28bytes)(padded to 4th byte)
  12416			player season-statistics(full)		((12*29plyrs+40)*32) (padded to 4th byte)
  12416			player playoff-statistics(full)		((12*29plyrs+40)*32) (padded to 4th byte)
			28424 - subtotal
	 24			pad to 32 byte boundary
	-----
  28448 	total bytes (max = 32768)


**************************************************************************/

global	int			MakeMemPakFile	(char **  ppub_Addr)
{
	// Local Variables.
int                 i;
int                 si___Size;
UD *                pud__Addr;
UB *                pub__Temp;
TeamRamStat_T       acl__Stat [16];


	// Allocate a buffer for the file.

	if (*ppub_Addr != NULL)
	{
		pud__Addr = (UD *) *ppub_Addr;
	}
	else
	{
		pud__Addr = (UD *) MemMalloc(32768, MEM_ANY);
	}

	pub__Temp = (UB *) &pud__Addr[3];

	// Save the current options.

	for (i = SOURCE_PRE; i <= SOURCE_PLY; i += 1)
	{
		wordcpy(pub__Temp, &acl__gOptions[i], sizeof(Options_T));

		pub__Temp += (~3 & (3 + sizeof(Options_T)));
	}

	// Save the season's playoff teams.

	wordcpy(pub__Temp, asb__gSsnTopTeams, 16);

	pub__Temp += 16;

	// Save the playoff tree.

	wordcpy(pub__Temp, asb__gPlayoffTree, 64);

	pub__Temp += 64;

	// Save the ssn-rosters.

	wordcpy(pub__Temp, apuw_gRoster[SOURCE_SSN], sl___gInfoRostLen);

	pub__Temp += (~3 & (3 + sl___gInfoRostLen));

	// Save the custom plyrs.

	for (i = 0; i < CUSTOM_PLYRS; i += 1)
	{
		bytecpy(pub__Temp, &((apcl_gCustom[SOURCE_SSN])[i]).ub___pTeam, CREATED_PLAYER_SAVED_BYTES);

		pub__Temp += CREATED_PLAYER_SAVED_BYTES;
	}

	// Save the custom players stats (only used for Rookie RolePlayers).

	for (i = 0; i < CUSTOM_PLYRS; i += 1)
	{		//112 x 20 = 2240 bytes
		bytecpy(pub__Temp, &CustomPlyrStatsData[i], sizeof(playerRPstatdata_t));

		pub__Temp += sizeof(playerRPstatdata_t);
	}


	// Save the team season-statistics.

	for (i = 0; i < si___gTeamSsnStatCnt; i += 1)
	{
		wordcpy(pub__Temp, &pacl_gTeamSsnStat[i], sizeof(TeamRamStat_T));

		pub__Temp += (~3 & (3 + sizeof(TeamRamStat_T)));
	}

	// Save the team ply-statistics.
	// Save the team PLAYOFF statistics. (please SPELL it out!!!!) (ply = player???)

	ShrinkTeamPlayoffStats(pacl_gTeamPlyStat, acl__Stat);

	for (i = 0; i < 16; i += 1)
	{
		wordcpy(pub__Temp, &acl__Stat[i], sizeof(TeamRamStat_T));

		pub__Temp += (~3 & (3 + sizeof(TeamRamStat_T)));
	}

	// Save the plyr season-statistics.

	for (i = 0; i < si___gPlyrSsnStatCnt; i += 1)
	{
		wordcpy(pub__Temp, &pacl_gPlyrSsnStat[i], sizeof(PlyrRamStat_T));

		pub__Temp += (~3 & (3 + sizeof(PlyrRamStat_T)));
	}

	// Save the plyr playoff-statistics.

	for (i = 0; i < si___gPlyrPlyStatCnt; i += 1)
	{
		wordcpy(pub__Temp, &pacl_gPlyrPlyStat[i], sizeof(PlyrRamStat_T));
		pub__Temp += (~3 & (3 + sizeof(PlyrRamStat_T)));
	}

/*********
	// Save the user team info.
	for (i = 0; i < MAX_CUSTOM_USERS; i += 1)
	{	
		wordcpy(pub__Temp, &UserTeams[i], sizeof(UserTeamInfo_T));
		pub__Temp += (~3 & (3 + sizeof(UserTeamInfo_T)));
	}
********/

	// Pad out to the next 32 byte boundary - 128 bytes for 1999

	si___Size = pub__Temp - ((UB *) pud__Addr);

//	while (si___Size & 31)
	while (si___Size & 127)
	{
		*pub__Temp = 0;
		pub__Temp += 1;
		si___Size += 1;
	}

	// Calculate the file checksum.

	pud__Addr[0] = si___Size;
	pud__Addr[1] = 0;
	pud__Addr[2] = LFP_FILE_VERSION;

	pud__Addr[1] = MakeChecksum(pud__Addr, si___Size);

	// All Done, return the filesize.

	*ppub_Addr = (char *) pud__Addr;

	return (si___Size);
	}



// **************************************************************************
// * ReadMemPakFile ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  - 0 if okay, -1 if cksum or other error                        *	
// **************************************************************************

global	int                 ReadMemPakFile          (
								char *              pub__Addr)

	{
	// Local Variables.

	int                 i;

	int                 si___Size;
	UD *                pud__Addr;
	UB *                pub__Temp;

	TeamRamStat_T       acl__Stat [16];

	// Allocate a buffer for the file.

	pud__Addr = (UD *) pub__Addr;

	// Test the file version and checksum.

	if (   (pud__Addr[2] != LFP_FILE_VERSION) 
		|| (MakeChecksum(pud__Addr, pud__Addr[0]) != 0))
	{
		return (-1);
	}

	//

	pub__Temp = (UB *) &pud__Addr[3];
	si___Size = pud__Addr[0];

	// Read the current options.

	for (i = SOURCE_PRE; i <= SOURCE_PLY; i += 1)
		{
		wordcpy(&acl__gOptions[i], pub__Temp, sizeof(Options_T));

		pub__Temp += (~3 & (3 + sizeof(Options_T)));
		}

	// Read the season's playoff teams.

	wordcpy(asb__gSsnTopTeams, pub__Temp, 16);

	pub__Temp += 16;

	// Read the playoff tree.

	wordcpy(asb__gPlayoffTree, pub__Temp, 64);

	pub__Temp += 64;

	// Read the ssn-rosters.

	wordcpy(apuw_gRoster[SOURCE_SSN], pub__Temp, sl___gInfoRostLen);

	pub__Temp += (~3 & (3 + sl___gInfoRostLen));

	// Read the custom plyrs.

	wordclr(apcl_gCustom[SOURCE_SSN], CUSTOM_PLYRS * sizeof(PlyrRomData_T));

	for (i = 0; i < CUSTOM_PLYRS; i += 1)
	{
		bytecpy(&((apcl_gCustom[SOURCE_SSN])[i]).ub___pTeam, pub__Temp, CREATED_PLAYER_SAVED_BYTES);

		pub__Temp += CREATED_PLAYER_SAVED_BYTES;
	}

	// move SEASON custom player Rom data to GAME custom player Rom data
	wordcpy(pacl_gCustom, apcl_gCustom[SOURCE_SSN], CUSTOM_PLYRS * sizeof(PlyrRomData_T));


	// move the custom players stats (only used for Rookie RolePlayers) to true area.

	wordclr(CustomPlyrStatsData, CUSTOM_PLYRS * sizeof(playerRPstatdata_t));

	for (i = 0; i < CUSTOM_PLYRS; i += 1)
	{		//112 x 20 = 2240 bytes
		bytecpy(&CustomPlyrStatsData[i], pub__Temp, sizeof(playerRPstatdata_t));

		pub__Temp += sizeof(playerRPstatdata_t);
	}



	// Read the team ssn-statistics.

	for (i = 0; i < si___gTeamSsnStatCnt; i += 1)
		{
		wordcpy(&pacl_gTeamSsnStat[i], pub__Temp, sizeof(TeamRamStat_T));

		pub__Temp += (~3 & (3 + sizeof(TeamRamStat_T)));
		}

	// Read the team ply-statistics.

	for (i = 0; i < 16; i += 1)
		{
		wordcpy(&acl__Stat[i], pub__Temp, sizeof(TeamRamStat_T));

		pub__Temp += (~3 & (3 + sizeof(TeamRamStat_T)));
		}

	ExpandTeamPlyStats(acl__Stat, pacl_gTeamPlyStat);

	// Read the plyr ssn-statistics.

	for (i = 0; i < si___gPlyrSsnStatCnt; i += 1)
		{
		wordcpy(&pacl_gPlyrSsnStat[i], pub__Temp, sizeof(PlyrRamStat_T));

		pub__Temp += (~3 & (3 + sizeof(PlyrRamStat_T)));
		}

	// Read the plyr playoff-statistics.

	for (i = 0; i < si___gPlyrPlyStatCnt; i += 1)
	{
		wordcpy(&pacl_gPlyrPlyStat[i], pub__Temp, sizeof(PlyrRamStat_T));

		pub__Temp += (~3 & (3 + sizeof(PlyrRamStat_T)));
	}

/*****	this is now on Flash Rom
	// Read the user team info.
	for (i = 0; i < MAX_CUSTOM_USERS; i += 1)
	{	
		wordcpy(&UserTeams[i], pub__Temp, sizeof(UserTeamInfo_T));
		pub__Temp += (~3 & (3 + sizeof(UserTeamInfo_T)));
	}
****/
	
	// All Done, return OK.

	return (0);
	}



// **************************************************************************
// * MakeChecksum ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	UD                  MakeChecksum            (
								UD *                pud__Addr,
								int                 si___Size)

	{
	// Local Variables.

	UD                  ud___Checksum;

	// Simple checksum.

	ud___Checksum = 0 - si___Size;

	si___Size = (si___Size + 3) >> 2;	// # bytes to # doubles

	while (si___Size--)
	{
		ud___Checksum += *pud__Addr++;
	}

	// All Done.

	return (0 - ud___Checksum);
}



// **************************************************************************
// * ShrinkTeamPlyStats ()                                                  *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ShrinkTeamPlayoffStats      (
								TeamRamStat_T *     pcl__Src,
								TeamRamStat_T *     pcl__Dst)

	{
	// Local Variables.

int                 i;
int                 j;
int		teamnum;

	TeamRamStat_T *     pcl__Tmp;

	// Is the playoff tree blank ?

	if (asb__gPlayoffTree[0] == 0)
		{
		wordclr(pcl__Dst, 16 * sizeof(TeamRamStat_T));
		return;
		}

	// Copy the 16 team stats for the playoff teams.

	for (i = 0; i < 32; i += 16)
	{
		for (j = 0; j < 8; j += 1)
		{
			teamnum = asb__gPlayoffTree[(i+j)] & 0x7fu;
#if DEBUG
			if(teamnum > 29 || teamnum < 1)
			{
				BRK;
			}
#endif
			pcl__Tmp = &pcl__Src[teamnum - 1];

			wordcpy(pcl__Dst, pcl__Tmp, sizeof(TeamRamStat_T));

			pcl__Dst += 1;
			}
		}

	// All Done.

	return;
	}



// **************************************************************************
// * ExpandTeamPlyStats ()                                                  *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ExpandTeamPlyStats      (
								TeamRamStat_T *     pcl__Src,
								TeamRamStat_T *     pcl__Dst)

	{
	// Local Variables.

int                 i;
int                 j;
int		teamnum;

TeamRamStat_T *     pcl__Tmp;

	// Clear the destination.

	wordclr(pcl__Dst, si___gTeamPlyStatCnt * sizeof(TeamRamStat_T));

	// Is the playoff tree blank ?

	if (asb__gPlayoffTree[0] == 0)
		{
		return;
		}

	// Copy the 16 team stats for the playoff teams.

	for (i = 0; i < 32; i += 16)
	{
		for (j = 0; j < 8; j += 1)
		{
			teamnum = asb__gPlayoffTree[(i+j)] & 0x7fu;
#if DEBUG
			if(teamnum > 29 || teamnum < 1)
			{
				BRK;
			}
#endif
			pcl__Tmp = &pcl__Dst[teamnum - 1];
//			pcl__Tmp = &pcl__Dst[(asb__gPlayoffTree[(i+j)] - 1)];

			wordcpy(pcl__Tmp, pcl__Src, sizeof(TeamRamStat_T));

			pcl__Src += 1;
		}
	}

	// All Done.

	return;
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************


#if IN_2_SHELL_C_FILES

char		aub__gError[256];	// used in stats.c


// **************************************************************************
// * ShellLoadPermData ()                                                   *
// **************************************************************************
// * Load permanent data (done only when machine is reset)                  *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ShellLoadPermData       (void)

{
	// Local Variables.

int                 i;
int					size;

DIR_INFO_T *        pcl__DirInfo;
FH                  pcl__File;

UD *                pud__Indx;
UD                  aud__Indx [1];

char *              pub__Temp;

	// Make sure that we only do this once.

	if (ul___gInfoFileRom != 0) return;

	// Initialize the random number generator.

	InitRandomByte();

	// Find out where all my data is located.

	ul___gInfoFileRom = 0;
	ul___gDataFileRom = 0;
	ul___gCommFileRom = 0;

	if ((pcl__DirInfo = FileRomInfo("TEAMINFO.IFF")) != NULL)
	{
		ul___gInfoFileRom = pcl__DirInfo->romOffset;
		ScanAiffFile("TEAMINFO.IFF");
	}

	if ((pcl__DirInfo = FileRomInfo("TEAMDATA.IFF")) != NULL)
	{
		ul___gDataFileRom = pcl__DirInfo->romOffset;
		ScanAiffFile("TEAMDATA.IFF");
	}

	if ((pcl__DirInfo = FileRomInfo("TEAMTALK.IFF")) != NULL)
	{
		ul___gCommFileRom = pcl__DirInfo->romOffset;
		ScanAiffFile("TEAMTALK.IFF");
	}

	if (ul___gInfoFileRom == 0)
	{
		ABORT_PROGRAM;
	}	
	if (ul___gDataFileRom == 0)
	{
		ABORT_PROGRAM;
	}

	// Open the TEAMINFO.IFF file and load the Cstr string table.

	if (ul___gInfoFileRom != 0)
	{
		pcl__File = FileOpen("TEAMINFO.IFF", "rb");

		if (sl___gInfoCstrPos != 0)
		{
			FileSeek(pcl__File, sl___gInfoCstrPos, SEEK_SET);

			pub__gInfoCstr = (UB *) MemMalloc(sl___gInfoCstrLen, MEM_ANY);

			FileRead(pub__gInfoCstr, sl___gInfoCstrLen, 1, pcl__File);
		}

		FileClose(pcl__File);
	}

	// Open the TEAMDATA.IFF file and load the Pimg and Ptex index tables,
	// and the super-palette.

	if (ul___gDataFileRom != 0)
	{
		pcl__File = FileOpen("TEAMDATA.IFF", "rb");

		if (sl___gDataPimgPos != 0)
		{
			FileSeek(pcl__File, sl___gDataPimgPos, SEEK_SET);

			FileRead(aud__Indx, 1, 4, pcl__File);

			i = (aud__Indx[0] + 1) << 2;

			pud__Indx = (UD *) MemMalloc(i+4, MEM_ANY);

			pud__Indx[0] = aud__Indx[0];

			FileRead(&pud__Indx[1], 1, i, pcl__File);

			pud__gIndxPimg = pud__Indx;
		}

		if (sl___gDataPtexPos != 0)
		{
			FileSeek(pcl__File, sl___gDataPtexPos, SEEK_SET);

			FileRead(aud__Indx, 1, 4, pcl__File);

			i = (aud__Indx[0] + 1) << 2;

			pud__Indx = (UD *) MemMalloc(i+4, MEM_ANY);

			pud__Indx[0] = aud__Indx[0];

			FileRead(&pud__Indx[1], 1, i, pcl__File);

			pud__gIndxPtex = pud__Indx;
		}

		if (sl___gDataSpalPos != 0)
		{
			FileSeek(pcl__File, sl___gDataSpalPos, SEEK_SET);

			sl___gSuperPal = sl___gDataSpalLen;
			puw__gSuperPal = (UW *) MemMalloc(sl___gDataSpalLen, MEM_ANY);

			FileRead(puw__gSuperPal, 1, sl___gDataSpalLen, pcl__File);
		}

		FileClose(pcl__File);
	}


	// Open the TEAMTALK.IFF file and load the Pnam and Talk index tables.

	if (ul___gCommFileRom != 0)
	{
		pcl__File = FileOpen("TEAMTALK.IFF", "rb");

		if (sl___gCommPnamPos != 0)
		{
			FileSeek(pcl__File, sl___gCommPnamPos, SEEK_SET);

			FileRead(aud__Indx, 1, 4, pcl__File);

			i = (aud__Indx[0] + 1) << 2;

			pud__Indx = (UD *) MemMalloc(i+4, MEM_ANY);

			pud__Indx[0] = aud__Indx[0];

			FileRead(&pud__Indx[1], 1, i, pcl__File);

			pud__gIndxPnam = pud__Indx;
		}

		if (sl___gCommTalkPos != 0)
		{
			FileSeek(pcl__File, sl___gCommTalkPos, SEEK_SET);

			FileRead(aud__Indx, 1, 4, pcl__File);

			i = (aud__Indx[0] + 1) << 2;

			pud__Indx = (UD *) MemMalloc(i+4, MEM_ANY);

			pud__Indx[0] = aud__Indx[0];

			FileRead(&pud__Indx[1], 1, i, pcl__File);

			pud__gIndxTalk = pud__Indx;
		}

		FileClose(pcl__File);
	}

	// Init rosters.

	si___gRosterLen = (int) sl___gInfoRostLen;

	apuw_gRoster[SOURCE_GAM] = MemCalloc(sl___gInfoRostLen, MEM_ANY);
	apuw_gRoster[SOURCE_SSN] = MemCalloc(sl___gInfoRostLen, MEM_ANY);

	puw__gRoster = apuw_gRoster[SOURCE_GAM];	// usually always points to this (except in demo mode OR during draft)

	// Init custom plyrs.

	apcl_gCustom[SOURCE_GAM] = MemCalloc(CUSTOM_PLYRS * sizeof(PlyrRomData_T), MEM_ANY);
	apcl_gCustom[SOURCE_SSN] = MemCalloc(CUSTOM_PLYRS * sizeof(PlyrRomData_T), MEM_ANY);

	pacl_gCustom = apcl_gCustom[SOURCE_GAM];	// this pointer (pacl_gCustom) never changes


	wordclr(CustomPlyrStatsData, CUSTOM_PLYRS * sizeof(playerRPstatdata_t));

	// Load and free the team info to find out what the size of
	// various structures needs to be.

	ShellLoadTeamInfo();
	ShellFreeTeamInfo();

	// Init statistics.

	si___gTeamSsnStatCnt = TEAM_SSN;
	si___gPlyrSsnStatCnt = si___gPlyrRomSsn + CUSTOM_PLYRS;

	si___gTeamPlyStatCnt = TEAM_SSN;
	si___gPlyrPlyStatCnt = si___gPlyrRomSsn + CUSTOM_PLYRS;

	size = si___gTeamSsnStatCnt * sizeof(TeamRamStat_T);
	pacl_gTeamSsnStat = MemMalloc(size, MEM_ANY);
//	pacl_gTeamSsnStat = MemMalloc(si___gTeamSsnStatCnt * sizeof(TeamRamStat_T), MEM_ANY);
	pacl_gPlyrSsnStat = MemMalloc(si___gPlyrSsnStatCnt * sizeof(PlyrRamStat_T), MEM_ANY);

	pacl_gTeamPlyStat = MemMalloc(si___gTeamPlyStatCnt * sizeof(TeamRamStat_T), MEM_ANY);
	pacl_gPlyrPlyStat = MemMalloc(si___gPlyrPlyStatCnt * sizeof(PlyrRamStat_T), MEM_ANY);


	InitCustomUsers();  // called on power up - zeroes out custom user teams


	// Reset the current season and playoffs.

	ClrSeason();
	ClrPlayoffs();

	// Then we can determine how large the backup file sizes are.

/****	1999 - Flash Rom & MemPack have same file size
	pub__Temp = NULL;

	si___gEepromNeed = MakeEepromFile(&pub__Temp);

	MemFree(pub__Temp);
*******/

	pub__Temp = NULL;

	si___gMemPakNeed = MakeMemPakFile(&pub__Temp);

	MemFree(pub__Temp);
	pub__Temp = NULL;


	// Open resource set.

	ResSetTmpBuffer((unsigned char *)dfb);

	if (ResOpenSet("shell", &pcl__ShellRes) != 0)
	{
		ABORT_PROGRAM;
	}
	// Load up the permanent palette(s).

	ShellLoadPalettes(&acl__gPalGlobal[0]);

	// Load up the fonts.
	i = LoadFonts(pcl__ShellRes,&apul_sFntResShell[0], &acl__sFntPalShell[0]);
	if(i != 0)
	{
		ABORT_PROGRAM;
	}
	
	// Close resource set.

	ResCloseSet(pcl__ShellRes);

	// All Done.

	return;
	}

#endif	//IN_2_SHELL_C_FILES

// **************************************************************************
// * ShellLoadPalettes ()                                                   *
// **************************************************************************
// * Load permanent data (done only when machine is reset)                  *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ShellLoadPalettes       (
								SPR_LIST_T *        pcl__List)

	{
	// Local Variables.

	int                 i;

	SPR_LIST_T *        pcl__Temp;
	UW *                puw__Temp;

	// Allocate space for the palettes.

	i = (sizeof(acl__gPalGlobal) / sizeof(SPR_LIST_T)) - 1;

	puw__gPalette = (UW *) MemMalloc((i * (256*2)), MEM_ANY);

	// Load up the palette resources.

	LoadSprPalettes(pcl__List);

	// Copy the palettes to their permanent home.

	pcl__Temp = pcl__List;
	puw__Temp = puw__gPalette;

	while (i--)
		{
		memcpy(puw__Temp, pcl__Temp->pbf__Res, 256*2);

		pcl__Temp += 1;
		puw__Temp += 256;
		}

	// Free up the palette resources.

	FreeSprPalettes(pcl__List);

	// All Done.

	return;
	}



// **************************************************************************
// * LoadChunk ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  char *   File name                                             *
// *                                                                        *
// * Output  int      Bytes read, or -ve if an error                        *
// **************************************************************************

global	void *              LoadChunk               (
								FH                  pcl__Fil,
								SL                  sl___Pos,
								SL                  sl___Len,
								SL                  sl___Alloc)

	{
	// Local Variables.

	UB *                p;

	// Read the chunk out of the file.

	FileSeek(pcl__Fil, sl___Pos, SEEK_SET);

	p = (UB *) MemMalloc(sl___Alloc, MEM_ANY);

	if (p != NULL)
		{
		FileRead(p, sl___Len, 1, pcl__Fil);
		}

	return (p);
	}



// **************************************************************************
// * ShellLoadTeamInfo ()                                                   *
// **************************************************************************
// * Load the complete team/player/schedule information                     *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ShellLoadTeamInfo       (void)

	{
	// Local Variables.

	int                 i;

	FH                  pcl__File;

	TeamRomData_T *     pcl__Team;
	PlyrRomData_T *     pcl__Plyr;

	// Open the TEAMINFO.IFF file.

	pcl__File = FileOpen("TEAMINFO.IFF", "rb");

	if (pcl__File == NULL) return;

	// Load up the DATE chunk.

	pub__gInfoDate = LoadChunk(pcl__File, sl___gInfoDatePos, sl___gInfoDateLen, sl___gInfoDateLen);

	puw__gSchedule = (UW *) pub__gInfoDate;

	// Load up the TEAM chunk.

	pub__gInfoTeam = LoadChunk(pcl__File, sl___gInfoTeamPos, sl___gInfoTeamLen, sl___gInfoTeamLen);

	si___gTeamRomCnt = ((UD *) pub__gInfoTeam)[0];
	si___gTeamRomSsn = TEAM_SSN;

	pacl_gTeamRom = (TeamRomData_T *) (pub__gInfoTeam + 4);

	// Load up the ROST chunk.

	pub__gInfoRost = LoadChunk(pcl__File, sl___gInfoRostPos, sl___gInfoRostLen, sl___gInfoRostLen);

	// SOURCE_NBA is the default ROM roster which should NEVER be modified 
	//  (apuw_gRoster[SOURCE_SSN] comes from either SOURCE_NBA or is loaded from memcard 
	//  (apuw_gRoster[SOURCE_SSN] is then copied to apuw_gRoster[SOURCE_GAME]
	//  which is modified depending on trades, etc
	// puw__gRoster almost always points to apuw_gRoster[SOURCE_GAME] (unless a demo game)
	// if "saving on memcard" is to take place, [SOURCE_GAME] is copied back to [SOURCE_SSN]
	//  ALL saving is done thru [SOURCE_SSN]
	apuw_gRoster[SOURCE_NBA] = (UW *) pub__gInfoRost;

	InitDefault3PtPlayers();	// uses apuw_gRoster[SOURCE_NBA]

	SetDefaultAllStarRosters();

	// Load up the PLYR chunk.

	pub__gInfoPlyr = LoadChunk(pcl__File, sl___gInfoPlyrPos, sl___gInfoPlyrLen,
						sl___gInfoPlyrLen + (CUSTOM_PLYRS * sizeof(PlyrRomData_T)));

	si___gPlyrRomCnt = ((UD *) pub__gInfoPlyr)[0];
	si___gPlyrRomSsn = ((UD *) pub__gInfoPlyr)[1];
	si___gPlyrRomTot = si___gPlyrRomCnt + CUSTOM_PLYRS;

	pacl_gPlyrRom = (PlyrRomData_T *) (pub__gInfoPlyr + 8);

	// Close the file.

	FileClose(pcl__File);

	// Resolve addresses of strings in the team structures.

	pcl__Team = pacl_gTeamRom;
	i         = si___gTeamRomCnt;

	while (i--)
		{
		pcl__Team->pcz__tCity =
			pub__gInfoCstr + ((UD) pcl__Team->pcz__tCity);
		pcl__Team->pcz__tName =
			pub__gInfoCstr + ((UD) pcl__Team->pcz__tName);
		pcl__Team->pcz__tAbbr =
			pub__gInfoCstr + ((UD) pcl__Team->pcz__tAbbr);
		pcl__Team->pcz__tCityName =
			pub__gInfoCstr + ((UD) pcl__Team->pcz__tCityName);

		pcl__Team += 1;
		}

	// Resolve addresses of strings in the plyr structures.

	pcl__Plyr = pacl_gPlyrRom;
	i         = si___gPlyrRomCnt;

	while (i--)
		{
		pcl__Plyr->pcz__pSName =
			pub__gInfoCstr + ((UD) pcl__Plyr->pcz__pSName);
		pcl__Plyr->pcz__pCName =
			pub__gInfoCstr + ((UD) pcl__Plyr->pcz__pCName);

		pcl__Plyr += 1;
		}

	// All Done.

	SetAllPlayersOriginalTeams();

	SortTeamRoster(FindTeamRosterStart(apuw_gRoster[SOURCE_NBA], TEAM_LFP));

	return;
	}



// **************************************************************************
// * ShellFreeTeamInfo ()                                                   *
// **************************************************************************
// * Free the complete team/player/schedule information                     *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ShellFreeTeamInfo       (void)

	{
	// Free the information chunks that we'd loaded.

	MemFree(pub__gInfoTeam);
	MemFree(pub__gInfoRost);
	MemFree(pub__gInfoPlyr);
	MemFree(pub__gInfoDate);

	pub__gInfoTeam =
	pub__gInfoRost =
	pub__gInfoPlyr =
	pub__gInfoDate = NULL;

	// Snap the links to the information.

	apuw_gRoster[SOURCE_NBA] = NULL;

	puw__gSchedule = NULL;

	pacl_gTeamRom  = NULL;
	pacl_gPlyrRom  = NULL;

	// All done.

	return;
	}



// **************************************************************************
// * LinkCustomPlyrs ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                LinkCustomPlyrs         (void)

{
	// Local Variables.

	int                 i;

	PlyrRomData_T *     pcl__Player;
	PlyrRomData_T *     pcl__Custom;

	// Copy the custom plyr structures to the end of the normal player structures.

	pcl__Player = &pacl_gPlyrRom[si___gPlyrRomCnt];	// where custom players will go (end of normal players)
	pcl__Custom = pacl_gCustom;			// "packed" custom data (NO NBA stats, but union with PlyrCustomInfo_T

	for (i = 0; i < CUSTOM_PLYRS; i += 1)
	{
		wordcpy(pcl__Player, pcl__Custom, sizeof(PlyrRomData_T));

		pcl__Player->uw___pIndx =
			pcl__Custom->uw___pIndx = (si___gPlyrRomCnt + 1) + i;

		pcl__Custom->pcz__pSName =		// this doesn't hurt - could prevent some bugs
		pcl__Player->pcz__pSName =		// point back into custom player original area
			((PlyrCustomInfo_T *) &pcl__Custom->cl___pStatNBA)->asb__pSName;

		pcl__Custom->pcz__pCName =		// this doesn't hurt - could prevent some bugs
		pcl__Player->pcz__pCName =		// point back into custom player area
			((PlyrCustomInfo_T *) &pcl__Custom->cl___pStatNBA)->asb__pCName;

		wordclr(&pcl__Player->cl___pStatNBA, sizeof(PlyrRamStat_T));	// no stats (overwrite PlyrCustomInfo_T)

		pcl__Player += 1;
		pcl__Custom += 1;
	}

	// All Done.

	return;
	}



// **************************************************************************
// * LoadFile ()                                                            *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  char *   File name                                             *
// *         UB **    Ptr to variable holding file address                  *
// *         long *   Ptr to variable holding file size                     *
// *                                                                        *
// * Output  long     Bytes read, or -ve if an error                        *
// **************************************************************************

global	long                LoadFile                (
								char *              pcz__Name,
								unsigned char **    ppbf_Addr,
								long *              psl__Size)

	{
	// Local Variables.

	FH                  file;
	unsigned char *     addr;
	long                size;

	//

	if ((pcz__Name == NULL) || (ppbf_Addr == NULL) || (psl__Size == NULL))
		{
		return (-1);
		}

	if ((file = FileOpen(pcz__Name, "rb")) == NULL)
		{
		return (-1);
		}

	FileSeek(file, 0, SEEK_END);

	size = FileTell(file);

	FileSeek(file, 0, SEEK_SET);

	if ((*psl__Size != 0) && (*psl__Size < size))
		{
		size = *psl__Size;
		}

	addr = *ppbf_Addr;

	if (addr == NULL)
		{
		addr = (unsigned char *) MemMalloc(size, MEM_ANY);
		}

	if (addr == NULL)
		{
		size = -1;
		}
	else
		{
		if (FileRead(addr, 1, size, file) != (size_t) size)
			{
			size = -1;
			}
		}

	FileClose(file);

	*psl__Size = size;

	if (*ppbf_Addr == NULL)
		{
		if (size < 0)
			{
			MemFree(addr);
			}
		else
			{
			*ppbf_Addr = addr;
			}
		}

	// All done, return size or -ve if error.

	return (size);
	}


#if IN_2_SHELL_C_FILES
// **************************************************************************
// * ScanAiffFile ()                                                        *
// **************************************************************************
// * Scan one of our AIFF format data files for chunks                      *
// **************************************************************************
// * Inputs  char *   File name                                             *
// *                                                                        *
// * Output  int      zero if OK, or -ve if an error                        *
// **************************************************************************

global	int                 ScanAiffFile            (
								char *              pcz__Name)

	{
	// Local Variables.

	FH                  pcl__fil;

	SL                  sl___pos;
	SL                  sl___end;

	SD                  asd__chk [3];

	// Open the file.

	if ((pcl__fil = FileOpen(pcz__Name, "rb")) == NULL)
		{
		DIAGNOSE(("Can't open aiff file !"));
		exit(-1);
		}

	// Read the header.

	if (FileRead(asd__chk, 1, 12, pcl__fil) != 12)
		{
		DIAGNOSE(("Can't read aiff file head !"));
		exit(-1);
		}

	if (asd__chk[0] != aiffID('A', 'I', 'F', 'F'))
		{
		DIAGNOSE(("Not an aiff file !"));
		exit(-1);
		}

	// Now scan through the file.

	asd__chk[1] = (asd__chk[1] + 3) & ~ 3;

	sl___pos = 12;
	sl___end = asd__chk[1] + 8;

	while (sl___pos < sl___end)
		{
		// Read chunk header.

		if (FileRead(asd__chk, 1, 8, pcl__fil) != 8)
			{
			DIAGNOSE(("Can't read aiff chunk head !"));
			exit(-1);
			}

		asd__chk[1] = (asd__chk[1] + 3) & ~ 3;

		sl___pos = sl___pos + 8;

		// Process chunk.

		switch (asd__chk[0])
			{
			// INFO file chunks.

			case aiffID('I', 'V', 'E', 'R'):
				{
				sl___gInfoVersPos = sl___pos;
				sl___gInfoVersLen = asd__chk[1];
				break;
				}
			case aiffID('C', 'S', 'T', 'R'):
				{
				sl___gInfoCstrPos = sl___pos;
				sl___gInfoCstrLen = asd__chk[1];
				break;
				}
			case aiffID('T', 'E', 'A', 'M'):
				{
				sl___gInfoTeamPos = sl___pos;
				sl___gInfoTeamLen = asd__chk[1];
				break;
				}
			case aiffID('R', 'O', 'S', 'T'):
				{
				sl___gInfoRostPos = sl___pos;
				sl___gInfoRostLen = asd__chk[1];
				break;
				}
			case aiffID('P', 'L', 'Y', 'R'):
				{
				sl___gInfoPlyrPos = sl___pos;
				sl___gInfoPlyrLen = asd__chk[1];
				break;
				}
			case aiffID('D', 'A', 'T', 'E'):
				{
				sl___gInfoDatePos = sl___pos;
				sl___gInfoDateLen = asd__chk[1];
				break;
				}

			// DATA file chunks.

			case aiffID('D', 'V', 'E', 'R'):
				{
				sl___gDataVersPos = sl___pos;
				sl___gDataVersLen = asd__chk[1];
				break;
				}
			case aiffID('P', 'T', 'E', 'X'):
				{
				sl___gDataPtexPos = sl___pos;
				sl___gDataPtexLen = asd__chk[1];
				break;
				}
			case aiffID('P', 'I', 'M', 'G'):
				{
				sl___gDataPimgPos = sl___pos;
				sl___gDataPimgLen = asd__chk[1];
				break;
				}
			case aiffID('S', 'P', 'A', 'L'):
				{
				sl___gDataSpalPos = sl___pos;
				sl___gDataSpalLen = asd__chk[1];
				break;
				}

			// COMM file chunks.

			case aiffID('C', 'V', 'E', 'R'):
				{
				sl___gCommVersPos = sl___pos;
				sl___gCommVersLen = asd__chk[1];
				break;
				}
			case aiffID('P', 'N', 'A', 'M'):
				{
				sl___gCommPnamPos = sl___pos;
				sl___gCommPnamLen = asd__chk[1];
				break;
				}
			case aiffID('T', 'A', 'L', 'K'):
				{
				sl___gCommTalkPos = sl___pos;
				sl___gCommTalkLen = asd__chk[1];
				break;
				}
			}

		// Move onto the next chunk.

		FileSeek(pcl__fil, asd__chk[1], SEEK_CUR);

		sl___pos = sl___pos + asd__chk[1];
		}

	// Close the file.

	FileClose(pcl__fil);

	// All done, return -ve if error.

	return (0);
	}

#endif	//IN_2_SHELL_C_FILES

// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



#if 0 // ======================= old shell backg


#if 1

// **************************************************************************
// * LoadShellBackgroundSpr ()                                              *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                LoadShellBackgroundSpr  (
								unsigned long int   ui___ID)

	{
	// Local Variables.

	if(ShellBGMode == BG_MODE_NEED_INIT)
	{
		BGCycleIndex = 0;
	}		

	
	if(ui___ID == NO_BACKGROUND)
	{
		FreeShellBackground(0);
		FreeShellBackground(1);
	}
	else 
	{
		if(ui___ID == START_BG_CYCLING)
		{
//			ShellBGMode = BG_MODE_CYCLE;
//			ui___ID = *(apul_gCycleBackground[BGCycleIndex]);
			ShellBGMode = BG_MODE_NO_CYCLE;
			ui___ID =  GetRandomKobeBG();

		}	
		else	// must be forced
		{

			ShellBGMode = BG_MODE_NO_CYCLE;
		}

		LoadShellBackgroundSpr1(ui___ID);

	}

/**********
	// Don't reload the same background.

	if (ui___sBackRes[MainBGSlot] == ui___ID) return;

	// Free old background image.

	FreeShellBackground();

	// Load new background image.

	ui___sBackRes[MainBGSlot] = ui___ID;

	pcl__gBackSpr[NewBGSlot] = LoadSprite(ui___ID);

	if (pcl__gBackSpr[NewBGSlot] == NULL)
	{
		ABORT_PROGRAM;
	}
***********/

	// All done.

	return;
}


#else

/***********
// **************************************************************************
// * LoadShellBackgroundSpr ()                                              *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                LoadShellBackgroundSpr  (
								unsigned long int   ui___ID)

	{
	// Local Variables.

	// Don't reload the same background.

	if (ui___sBackRes[MainBGSlot] == ui___ID) return;

	// Free old background image.

	FreeShellBackground();

	// Load new background image.

	ui___sBackRes[MainBGSlot] = ui___ID;

	pcl__gBackSpr[NewBGSlot] = LoadSprite(ui___ID);

	if (pcl__gBackSpr[NewBGSlot] == NULL)
	{
		ABORT_PROGRAM;
	}

	// All done.
	LoadShellBackgroundSpr1(ui___ID);

	return;
	}

*******/
#endif


// **************************************************************************
// * LoadShellForegroundSpr ()                                              *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************


/***************
global	void                LoadShellForegroundSpr  (
								unsigned long int   ui___ID)

	{
	// Local Variables.

	int                 x;
	int                 y;
	int                 w;
	int                 h;

	UW *                puw__Src;
	UW *                puw__Tmp;
	UW *                puw__Dst;

	// Free old foreground image.

	FreeShellForeground();

	// Load new foreground image.

	pcl__gForeSpr = LoadSprite(ui___ID);

	if (pcl__gForeSpr == NULL)
	{
		ABORT_PROGRAM;
	}

	// All done.

	return;
	}

*******/

// **************************************************************************
// * FreeShellForeground ()                                                 *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

/*************
global	void                FreeShellForeground     (void)

	{
	if (pcl__gForeSpr != NULL)
		{
		if (pcl__gForeSpr->sprPal != NULL)
			{
			ResPurge((unsigned char **) pcl__gForeSpr->sprPal);
			}
		MemFree(pcl__gForeSpr);
		}

	pcl__gForeSpr = NULL;

	return;
	}

************/


// **************************************************************************
// * FreeShellBackground ()                                                 *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void FreeShellBackground     (int slot)

{
	if (pcl__gBackSpr[slot] != NULL)
	{
		if ( (pcl__gBackSpr[slot])->sprPal != NULL)
		{
			ResPurge((unsigned char **) (pcl__gBackSpr[slot])->sprPal);
		}
		MemFree(pcl__gBackSpr[slot]);
	}

	pcl__gBackSpr[slot] = NULL;
	ui___sBackRes[slot] = -1;

	return;
}



// **************************************************************************
// * FreeShellBackground1 ()                                                 *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                FreeShellBackground1     (void)
{

	FreeShellBackground (ShellForeBG);

}


// **************************************************************************
// * LoadShellBackgroundSpr1 ()                                             *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                LoadShellBackgroundSpr1  (
								unsigned long int   ui___ID)

	{
	// Local Variables.

	int                 x;
	int                 y;
	int                 w;
	int                 h;

	UW *                puw__Src;
	UW *                puw__Tmp;
	UW *                puw__Dst;

	// Don't reload the same background.

	//if (ui___sBackRes[ShellForeBG] == ui___ID) return;

	// Free old background image.

	FreeShellBackground1();

	// Load new background image.

	ui___sBackRes[ShellForeBG] = ui___ID;

	pcl__gBackSpr[ShellForeBG] = LoadSprite(ui___ID);

	if (pcl__gBackSpr[ShellForeBG] == NULL)
	{
		ABORT_PROGRAM;
	}

	// All done.

	return;
	}




void ChangeShellBG(void)
{
	if ( fl___gShellOverlay == 0)
	{

		if(++BGCycleIndex >= NUM_CYCLE_BACKGROUNDS)
		{
			BGCycleIndex = 0;
		}			

		LoadShellBackgroundSpr1(*(apul_gCycleBackground[BGCycleIndex]));

/*********
		if(++bgteam >= TEAM_NIN)
		{
			bgteam=1;
		}			
		
		if(bgteam &1)
		{
			LoadShellBackgroundSpr1(*(apul_gTeamBackground[bgteam]));
		}
		else
		{
			LoadShellBackgroundSpr1(ID_SPR_BACK_KB1);

		}
**********/
	}
}



/************
	there are 2 shell backgrounds loaded 
*************/

void ShellBGLogic(void)
{
SprTexture_T *      pcl__Spr;
UB *                pub__Src;
UB *                pub__Des;
UI  i,j,h;


// WARNING - all team based shell backgrounds use different palettes

	BGWidth += (BackgroundScrollMode ? 8:4);	//BGWidthInc;
//	if(BGWidth > 256)	// psuedo-timer value - 6 concurrent wipes
//	if(BGWidth > 1200)	// psuedo-timer value - full screen wipe
	if(BGWidth > 800)	// psuedo-timer value - full screen wipe
	{
//		BGWidth = 0;
		BGWidthInc = BG_WIDTH_INC_VALUE;
		ShellBackBG = ShellForeBG;
//		ShellForeBG ^= 1;	
		if(ShellBGMode == BG_MODE_CYCLE)
		{
//			ShellForeBG ^= 1;	
			ShellForeBG = 0;	// no wipe, only flash
			BGWidth = 0;
			ChangeShellBG();
		}
	}
}	









// **************************************************************************


global	Gfx *   RenderBackground     (
								Gfx *               gp,
								SprTexture_T *      pcl__Spr,
								SI				cellwidth)
{

UB *                pub__Src;
int                 i;
int                 j;
int                 w,cw,start;
	
	if(pcl__Spr == NULL)	// error
	{
		return(gp);		
	}
//	pcl__Spr = pcl__gBackSpr[NewBGSlot];
	pub__Src = (UB *) (pcl__Spr + 1);

	gDPSetTextureLUT(gp++,G_TT_RGBA16);

	gDPLoadTLUT_pal256(gp++,
		(UI) (*pcl__Spr->sprPal));

	if(pcl__Spr->sprW > 384)
	{	// full screen
		start = 0;
	}
	else
	{
		start = MENU_LEFT_KEYX;
	}

	for (i = start,w=0; i < start+pcl__Spr->sprW; i += 64,w += 64)	// right-just the big BG, (most are 384 wide)
//	for (i = 512-pcl__Spr->sprW; i < 512; i += 64)	// right-just the big BG, (most are 384 wide)
//	for (i = 0; i < 512; i += 64)
//	for (i = 128; i < 512; i += 64)
	{
		if(cellwidth < 0)	// doing wipe
		{
			if(BackgroundScrollMode)
			{
				if(w > BGWidth)
					break;
				if(w+64 < BGWidth)
					cw = 64;
				else
					cw = BGWidth-w;
			}
			else
			{
				cw = BGWidth < 64 ? BGWidth :64;
			}
		}
		else	// normal full screen [back]background
		{
			cw=64;
		}
//		cellwidth = BGWidth < 64 ? BGWidth : 512;
//		cellwidth = w+64 >= BGWidth  ? 64 : BGWidth-w;
	
		for (j = 0; j < 224; j += 32)
		{
			gDPLoadTextureBlock(gp++,
				pub__Src,			// ptr (8-byte aligned)
				G_IM_FMT_CI,		// texel fmt
				G_IM_SIZ_8b,		// texel siz
				64,					// w
				32,					// h
				0,					// palette
				G_TX_CLAMP,			// clamp/wrap/mirror s
				G_TX_CLAMP,			// clamp/wrap/mirror t
				6,					// mask s
				5,					// mask t
				G_TX_NOLOD,			// shift s
				G_TX_NOLOD);		// shift t

			gSPTextureRectangle(gp++,
				(i) << 2,			// ulx  (10.2)
				(j) << 2,			// uly  (10.2)
				(i + cw) << 2,		// lrx  (10.2)
				(j + 32) << 2,		// lry  (10.2)
				G_TX_RENDERTILE,	// tile
				0,					// s    (s10.5)
				0,					// t    (s10.5)
				1 << 10,			// dsdx (s5.10)
				1 << 10);			// dtdy (s5.10)

			pub__Src += 64*32;
		}

		// final 1/2 block at bottom - h = 224-240
		gDPLoadTextureBlock(gp++,
			pub__Src,			// ptr (8-byte aligned)
			G_IM_FMT_CI,		// texel fmt
			G_IM_SIZ_8b,		// texel siz
			64,					// w
			16,					// h
			0,					// palette
			G_TX_CLAMP,			// clamp/wrap/mirror s
			G_TX_CLAMP,			// clamp/wrap/mirror t
			6,					// mask s
			5,					// mask t
			G_TX_NOLOD,			// shift s
			G_TX_NOLOD);		// shift t

		gSPTextureRectangle(gp++,
			(i) << 2,			// ulx  (10.2)
			(j) << 2,			// uly  (10.2)
			(i + cw) << 2,		// lrx  (10.2)
			(j + 16) << 2,		// lry  (10.2)
			G_TX_RENDERTILE,	// tile
			0,					// s    (s10.5)
			0,					// t    (s10.5)
			1 << 10,			// dsdx (s5.10)
			1 << 10);			// dtdy (s5.10)

		pub__Src += 64*32;
	}

	return (gp);

}




// **************************************************************************
// * DrawShellBackground ()                                                 *
// **************************************************************************
// * Render the background motif beneath shell screens                      *
// **************************************************************************
// * Inputs  Gfx *   ?                                                      *
// *         dyn *   ?                                                      *
// *                                                                        *
// * Output  Gfx *                                                          *
// **************************************************************************

global	Gfx *               DrawShellBackground     (
								Gfx *               gp,
								dyn *               d)

	{
	// Local Variables.

	int                 i;
	int                 j;

	long                m[9];

	Mtx                 tmp1;
	Mtx                 tmp2;

	long                v1[3];
	long                v2[3];

	SprTexture_T *      pcl__Spr;
	UB *                pub__Src;
	//UW *                puw__Src;

	int cellwidth;
	
	// Render background... first pass

	gSPMatrix(gp++,
		OS_K0_TO_PHYSICAL(&(d->shellFlip)),
		G_MTX_PROJECTION | G_MTX_LOAD | G_MTX_NOPUSH);

	gSPMatrix(gp++,
		OS_K0_TO_PHYSICAL(&(d->identity)),
		G_MTX_MODELVIEW  | G_MTX_LOAD | G_MTX_NOPUSH);

	gDPSetTexturePersp(gp++,
		G_TP_NONE);

	gDPSetRenderMode(gp++,
		G_RM_OPA_SURF,
		G_RM_OPA_SURF2);

	gDPSetAlphaCompare(gp++,
		G_AC_NONE);

	gSPTexture(gp++,
		0x8000,
		0x8000,
		0,
		G_TX_RENDERTILE,
		G_ON);

	gDPSetCombineMode(gp++,
		G_CC_DECALRGB,
		G_CC_DECALRGB);

	gDPSetTextureFilter(gp++,
		G_TF_POINT);

	// Render the RHS part of the background.

#if 1
	if(pcl__gBackSpr[0] != NULL)
	{ // no wiping	
		gp = RenderBackground(gp,pcl__gBackSpr[0],64);
	}	


/************

	if(pcl__gBackSpr[0] == pcl__gBackSpr[1]
	    && pcl__gBackSpr[0] != NULL)
	{ // no wiping	
		gp = RenderBackground(gp,pcl__gBackSpr[ShellBackBG],64);
	}	
	else	//  must be in a wipe process - draw both
	{
		if( ShellBackBG == 0 || ShellBackBG == 1)
		{
			gp = RenderBackground(gp,pcl__gBackSpr[ShellBackBG],64);
		}		

		if(ShellForeBG == 0 || ShellForeBG == 1)
		{
	//		gp = RenderBackground(gp,pcl__gBackSpr[ShellForeBG], BGWidth < 64 ? BGWidth :64);
			gp = RenderBackground(gp,pcl__gBackSpr[ShellForeBG], -1);
		}	
	}

//	if(!displayrainbow)
//	{
//		gp = RenderTBOX(gp, 0,  0, 512, 240, RGB_TRANS_DKGRY);
//	}
***************/

#else //--------------------------------------



	pcl__Spr = pcl__gBackSpr[NewBGSlot];
	pub__Src = (UB *) (pcl__Spr + 1);

	gDPSetTextureLUT(gp++,G_TT_RGBA16);

	gDPLoadTLUT_pal256(gp++,
		(UI) (*pcl__Spr->sprPal));

/**********
	BGWidth += BGWidthInc;
	if(BGWidth > 256)
	{
		BGWidthInc = -BG_WIDTH_INC_VALUE;
//		BGWidth = 512;
	}
	else if (BGWidth < 0)
	{
		BGWidth = 0;
		BGWidthInc = BG_WIDTH_INC_VALUE;
		ChangeShellBG();
		
	}
*********/	

	//	for (i = 512-pcl__Spr->sprW; i < BGWidth; i += 64)	// right-just the big BG, (most are 384 wide)
	for (i = 512-pcl__Spr->sprW; i < 512; i += 64)	// right-just the big BG, (most are 384 wide)
//	for (i = 0; i < 512; i += 64)
//	for (i = 128; i < 512; i += 64)
	{
//		cellwidth = BGWidth < 64 ? BGWidth : 512;
		cellwidth = 64;
		for (j = 0; j < 224; j += 32)
		{
			gDPLoadTextureBlock(gp++,
				pub__Src,			// ptr (8-byte aligned)
				G_IM_FMT_CI,		// texel fmt
				G_IM_SIZ_8b,		// texel siz
				64,					// w
				32,					// h
				0,					// palette
				G_TX_CLAMP,			// clamp/wrap/mirror s
				G_TX_CLAMP,			// clamp/wrap/mirror t
				6,					// mask s
				5,					// mask t
				G_TX_NOLOD,			// shift s
				G_TX_NOLOD);		// shift t

			gSPTextureRectangle(gp++,
				(i) << 2,			// ulx  (10.2)
				(j) << 2,			// uly  (10.2)
				(i + cellwidth) << 2,		// lrx  (10.2)
				(j + 32) << 2,		// lry  (10.2)
				G_TX_RENDERTILE,	// tile
				0,					// s    (s10.5)
				0,					// t    (s10.5)
				1 << 10,			// dsdx (s5.10)
				1 << 10);			// dtdy (s5.10)

			pub__Src += 64*32;
		}

		gDPLoadTextureBlock(gp++,
			pub__Src,			// ptr (8-byte aligned)
			G_IM_FMT_CI,		// texel fmt
			G_IM_SIZ_8b,		// texel siz
			64,					// w
			16,					// h
			0,					// palette
			G_TX_CLAMP,			// clamp/wrap/mirror s
			G_TX_CLAMP,			// clamp/wrap/mirror t
			6,					// mask s
			5,					// mask t
			G_TX_NOLOD,			// shift s
			G_TX_NOLOD);		// shift t

		gSPTextureRectangle(gp++,
			(i) << 2,			// ulx  (10.2)
			(j) << 2,			// uly  (10.2)
			(i + cellwidth) << 2,		// lrx  (10.2)
			(j + 16) << 2,		// lry  (10.2)
			G_TX_RENDERTILE,	// tile
			0,					// s    (s10.5)
			0,					// t    (s10.5)
			1 << 10,			// dsdx (s5.10)
			1 << 10);			// dtdy (s5.10)

		pub__Src += 64*32;
	}

#endif //--------------------------------------



	// Render lines.

//	if(cl___gPreferences.sb___AutoPlyrSwitch)
//	{
//		gp = DrawLines(gp, d);
//	}

	// Render background... second pass

	gDPPipeSync(gp++);

	gDPSetTexturePersp(gp++,
		G_TP_NONE);

	gDPSetRenderMode(gp++,
		G_RM_AA_TEX_EDGE,
		G_RM_AA_TEX_EDGE2);

	gDPSetBlendColor(gp++,
		0x00, 0x00, 0x00, 0x80);

	gDPSetAlphaCompare(gp++,
		G_AC_THRESHOLD);

	gSPTexture(gp++,
		0x8000,
		0x8000,
		0,
		G_TX_RENDERTILE,
		G_ON);

	gDPSetPrimColor(gp++,
		0, 0, 0xFF, 0xFF, 0xFF, 0x80);

	gDPSetCombineMode(gp++,
		G_CC_DECALRGBA,
		G_CC_DECALRGBA);

	gDPSetTextureFilter(gp++,
		G_TF_POINT);

#if 0	//*****************************************************************
	// Render the MID part of the background.

	pcl__Spr = pcl__gForeSpr;
	pub__Src = ((UB *) (pcl__Spr + 1)) + 128*256;

	gDPSetTextureLUT(gp++,G_TT_RGBA16);

	gDPLoadTLUT_pal256(gp++,
		(UI) (*pcl__Spr->sprPal));

	for (i = 128; i < 192; i += 64)
	{
		for (j = 0; j < 224; j += 32)
		{
			gDPLoadTextureBlock(gp++,
				pub__Src,			// ptr (8-byte aligned)
				G_IM_FMT_CI,		// texel fmt
				G_IM_SIZ_8b,		// texel siz
				64,					// w
				32,					// h
				0,					// palette
				G_TX_CLAMP,			// clamp/wrap/mirror s
				G_TX_CLAMP,			// clamp/wrap/mirror t
				6,					// mask s
				5,					// mask t
				G_TX_NOLOD,			// shift s
				G_TX_NOLOD);		// shift t

			gSPTextureRectangle(gp++,
				(i) << 2,			// ulx  (10.2)
				(j) << 2,			// uly  (10.2)
				(i + 64) << 2,		// lrx  (10.2)
				(j + 32) << 2,		// lry  (10.2)
				G_TX_RENDERTILE,	// tile
				0,					// s    (s10.5)
				0,					// t    (s10.5)
				1 << 10,			// dsdx (s5.10)
				1 << 10);			// dtdy (s5.10)

			pub__Src += 64*32;
		}

		gDPLoadTextureBlock(gp++,
			pub__Src,			// ptr (8-byte aligned)
			G_IM_FMT_CI,		// texel fmt
			G_IM_SIZ_8b,		// texel siz
			64,					// w
			16,					// h
			0,					// palette
			G_TX_CLAMP,			// clamp/wrap/mirror s
			G_TX_CLAMP,			// clamp/wrap/mirror t
			6,					// mask s
			5,					// mask t
			G_TX_NOLOD,			// shift s
			G_TX_NOLOD);		// shift t

		gSPTextureRectangle(gp++,
			(i) << 2,			// ulx  (10.2)
			(j) << 2,			// uly  (10.2)
			(i + 64) << 2,		// lrx  (10.2)
			(j + 16) << 2,		// lry  (10.2)
			G_TX_RENDERTILE,	// tile
			0,					// s    (s10.5)
			0,					// t    (s10.5)
			1 << 10,			// dsdx (s5.10)
			1 << 10);			// dtdy (s5.10)

		pub__Src += 64*32;
	}
#endif	//********************************************************************

	// Render background... third pass

	gDPPipeSync(gp++);

	gDPSetTexturePersp(gp++,
		G_TP_NONE);

	gDPSetRenderMode(gp++,
		G_RM_OPA_SURF,
		G_RM_OPA_SURF2);

	gDPSetAlphaCompare(gp++,
		G_AC_NONE);

	gSPTexture(gp++,
		0x8000,
		0x8000,
		0,
		G_TX_RENDERTILE,
		G_ON);

	gDPSetCombineMode(gp++,
		G_CC_DECALRGB,
		G_CC_DECALRGB);

	gDPSetTextureFilter(gp++,
		G_TF_POINT);

#if 0	//*********************************************************
	// Render the LHS part of the background.

	pcl__Spr = pcl__gForeSpr;
	pub__Src = ((UB *) (pcl__Spr + 1)) + 0*256;

//	gDPSetTextureLUT(gp++,
//		G_TT_RGBA16);
//
//	gDPLoadTLUT_pal256(gp++,
//		(UI) (*pcl__Spr->sprPal));

	for (i = 0; i < 128; i += 64)
	{
		for (j = 0; j < 224; j += 32)
		{
			gDPLoadTextureBlock(gp++,
				pub__Src,			// ptr (8-byte aligned)
				G_IM_FMT_CI,		// texel fmt
				G_IM_SIZ_8b,		// texel siz
				64,					// w
				32,					// h
				0,					// palette
				G_TX_CLAMP,			// clamp/wrap/mirror s
				G_TX_CLAMP,			// clamp/wrap/mirror t
				6,					// mask s
				5,					// mask t
				G_TX_NOLOD,			// shift s
				G_TX_NOLOD);		// shift t

			gSPTextureRectangle(gp++,
				(i) << 2,			// ulx  (10.2)
				(j) << 2,			// uly  (10.2)
				(i + 64) << 2,		// lrx  (10.2)
				(j + 32) << 2,		// lry  (10.2)
				G_TX_RENDERTILE,	// tile
				0,					// s    (s10.5)
				0,					// t    (s10.5)
				1 << 10,			// dsdx (s5.10)
				1 << 10);			// dtdy (s5.10)

			pub__Src += 64*32;
		}

		gDPLoadTextureBlock(gp++,
			pub__Src,			// ptr (8-byte aligned)
			G_IM_FMT_CI,		// texel fmt
			G_IM_SIZ_8b,		// texel siz
			64,					// w
			16,					// h
			0,					// palette
			G_TX_CLAMP,			// clamp/wrap/mirror s
			G_TX_CLAMP,			// clamp/wrap/mirror t
			6,					// mask s
			5,					// mask t
			G_TX_NOLOD,			// shift s
			G_TX_NOLOD);		// shift t

		gSPTextureRectangle(gp++,
			(i) << 2,			// ulx  (10.2)
			(j) << 2,			// uly  (10.2)
			(i + 64) << 2,		// lrx  (10.2)
			(j + 16) << 2,		// lry  (10.2)
			G_TX_RENDERTILE,	// tile
			0,					// s    (s10.5)
			0,					// t    (s10.5)
			1 << 10,			// dsdx (s5.10)
			1 << 10);			// dtdy (s5.10)

		pub__Src += 64*32;
	}
#endif	//*******************************************************************

	ShellBGLogic();

	// All done.

	return (gp);
}


#endif // ========================   old shell backg


// **************************************************************************
// * DrawLines ()                                                           *
// **************************************************************************
// * Render the background motif beneath shell screens                      *
// **************************************************************************
// * Inputs  Gfx *   ?                                                      *
// *         dyn *   ?                                                      *
// *                                                                        *
// * Output  Gfx *                                                          *
// **************************************************************************

#if SHOW_BG_COURT_LINES		// killed for Kobe 2

static	Gfx *               DrawLines               (
								Gfx *               gp,
								dyn *               d)

	{
	// Local Variables.

	int                 i;
	int                 j;

	//long                m[9];
	matrix				m;
	
	Mtx                 tmp1;
	Mtx                 tmp2;

//	long                v1[3];
//	long                v2[3];
	vector	v1,v2;

	UW *                puw__Src;

	// Render background... first pass

	gDPPipeSync(gp++);

	gSPMatrix(gp++,
		OS_K0_TO_PHYSICAL(&(d->shellFlip)),
		G_MTX_PROJECTION | G_MTX_LOAD | G_MTX_NOPUSH);

	gSPMatrix(gp++,
		OS_K0_TO_PHYSICAL(&(d->identity)),
		G_MTX_MODELVIEW  | G_MTX_LOAD | G_MTX_NOPUSH);

	// Render lines.

	gDPSetTexturePersp(gp++,
		G_TP_PERSP);

	gDPSetTextureLUT(gp++,
		G_TT_NONE);

	gSPTexture(gp++, 0, 0, 0, 0, G_OFF);

	gDPSetRenderMode(gp++,
		G_RM_AA_XLU_SURF,
		G_RM_AA_XLU_SURF2);

	gDPSetCombineMode(gp++,
		CC_LINEMODE,
		CC_LINEMODE2);

	for (i = 0; i < MAX_ACTIVE_LINES; i++)
	{
		if (!activeLines[i].flags) continue;

#if 1	//MJK_KLUDGE
		N64ResSetAlpha(activeLines[i].alpha);
#endif
//		MakeMatPRH(m,
		MakeMatPRH(&m,
			activeLines[i].camP,
			0,
			activeLines[i].camH);
/**
		v1[0] = 0;
		v1[1] = 0;
		v1[2] = -activeLines[i].camDist;

		VecMulMat(v1, &m, v2);

		v2[0]+=activeLines[i].camX;
		v2[1]+=activeLines[i].camY;
		v2[2]+=activeLines[i].camZ;
***/
		v1.x = v1.y = 0;
		v1.z = -activeLines[i].camDist;

		VecMulMat(&v1, &m, &v2);

		v2.x += activeLines[i].camX;
		v2.y += activeLines[i].camY;
		v2.z += activeLines[i].camZ;

#if	1	//MJK_KLUDGE
		MakeMat64PRH((long *) &tmp2,
			     activeLines[i].camP,
			     0,
			     activeLines[i].camH,
			     0,0,0,
			     256);

		Mat64Invert((short *) &tmp2);

		MakeMat64PRH((long *) &tmp1,
			     0,0,0,
					-v2.x,-v2.y,-v2.z,
//			     -v2[0],
//			     -v2[1],
//			     -v2[2],
			     256);

		guMtxCatL(&tmp1, &tmp2, &d->shellLines[i]);

		gSPMatrix(gp++,
			OS_K0_TO_PHYSICAL(&(d->shellLines[i])),
			G_MTX_MODELVIEW | G_MTX_LOAD | G_MTX_NOPUSH);

		N64ResAppendDL(titleLineScript, &gp, (vtxMap *) 1);
#endif

	}

	// All done.

	return (gp);
}


#endif

/**********************************

// **************************************************************************
// * LoadScreenImg ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	UW *                LoadScreenImg           (
								struct openSet *    pcl__Res,
								unsigned long int   ui___ID)

	{
	// Local Variables.

	int                 x;
	int                 y;
	int                 w;
	int                 h;
	int                 l;

	UW *                puw__Src;
	UW *                puw__Tmp;
	UW *                puw__Dst;

	ImgResource_T *     pcl__Img;

	UW *                puw__Screen;

	// Allocate buffer for screen background.

	puw__Screen = (unsigned short *) MemCalloc(512*256*2, MEM_ANY);

	if (puw__Screen == NULL)
	{
		ABORT_PROGRAM;
	}

	// Load background image.

	pcl__Img = (ImgResource_T *) ResOpenID(pcl__Res, ui___ID);

	// Now convert this into 64 pixel wide chunks.

	puw__Src = (UW *) (pcl__Img + 1);
	puw__Dst = puw__Screen;

	l =
	w = (pcl__Img->imgW);
	h = (pcl__Img->imgH + 31) & ~31;

	if (w > 512) w = 512;

	for (x = 0; x < w; x += 64)
		{
		puw__Tmp = puw__Src;

		for (y = 0; y < h; y += 1)
			{
			memcpy(puw__Dst, puw__Tmp, 64*2);

			puw__Tmp += l;
			puw__Dst += 64;
			}

		puw__Src = puw__Src + 64;
		}

	// Free resource.

	ResPurgeID(pcl__Res, ui___ID);

	// All done.

	return (puw__Screen);
	}



// **************************************************************************
// * DrawScreenImg ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  Gfx *   ?                                                      *
// *         UW *    ?                                                      *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	Gfx *               DrawScreenImg           (
								Gfx *               gp,
								UW *                puw__Src)

	{
	// Local Variables.

	int                 i;
	int                 j;

	// Initialize drawing mode.

	gDPPipeSync(gp++);

	gDPSetTextureLUT(gp++,
		G_TT_NONE);

	gDPSetAlphaCompare(gp++,
		G_AC_NONE);

	gDPSetRenderMode(gp++,
		G_RM_OPA_SURF,
		G_RM_OPA_SURF2);

	gDPSetCombineMode(gp++,
		G_CC_DECALRGB,
		G_CC_DECALRGB);

	gSPTexture(gp++,
		0x8000,				// s scale 0.16
		0x8000,				// t scale 0.16
		0,					// mipmap levels
		G_TX_RENDERTILE,	// tile number
		G_ON);				// enable

	gDPSetTexturePersp(gp++,
		G_TP_NONE);

	gDPSetTextureFilter(gp++,
		G_TF_POINT);

	// Render 512x240 screen in 7.5 rows.

	for (i = 0; i < 512; i += 64)
		{
		for (j = 0; j < 224; j += 32)
			{
			gDPLoadTextureBlock(gp++,
				puw__Src,
				G_IM_FMT_RGBA,
				G_IM_SIZ_16b,
				64,
				32,
				0,
				G_TX_CLAMP,
				G_TX_CLAMP,
				6,
				5,
				G_TX_NOLOD,
				G_TX_NOLOD);

			gSPTextureRectangle(gp++,
				i << 2,
				j << 2,
				(i + 64) << 2,
				(j + 32) << 2,
				G_TX_RENDERTILE,
				0,
				0,
				1 << 10,
				1 << 10);

			puw__Src += 64*32;
			}

		gDPLoadTextureBlock(gp++,
			puw__Src,
			G_IM_FMT_RGBA,
			G_IM_SIZ_16b,
			64,
			16,
			0,
			G_TX_CLAMP,
			G_TX_CLAMP,
			6,
			4,
			G_TX_NOLOD,
			G_TX_NOLOD);

		gSPTextureRectangle(gp++,
			i << 2,
			j << 2,
			(i + 64) << 2,
			(j + 16) << 2,
			G_TX_RENDERTILE,
			0,
			0,
			1 << 10,
			1 << 10);

		puw__Src += 64*32;
		}

	// All Done.

	return (gp);
	}

**********************/

// **************************************************************************
// * LoadFontTest ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

#if SHOW_FONT
static	UB *                pub__sFontTest = NULL;

extern	unsigned long int   ID_SPR_FONTTEST;

static	int                 tbl [16] =
	{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
	  0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };

global	void                LoadFontTest            (void)

	{
	// Local Variables.


	int                 x;
	int                 y;
	int                 w;
	int                 h;
	int                 l;

	UB *                pub__Src;
	UB *                pub__Tmp;
	UB *                pub__Dst;

	ImgResource_T *     pcl__Img;

	// Allocate buffer for screen background.

	if (pub__sFontTest == NULL)
	{
		pub__sFontTest = (unsigned char *) MemCalloc(512*256, MEM_ANY);

		if (pub__sFontTest == NULL)
		{	
			ABORT_PROGRAM;
		}
	}

	// Load background image.

	pcl__Img = (ImgResource_T *) ResOpenID(pcl__ShellRes, ID_SPR_FONTTEST);

	// Remap image.

	pub__Src = (UB *) (pcl__Img + 1);

	w = pcl__Img->imgW;
	h = pcl__Img->imgH;

	l = w*h;

	while (l--)
		{
		w = *pub__Src;

		if (w > 15) w = 0;
		*pub__Src++ = tbl[w];
		}

	// Now convert this into 64 pixel wide chunks.

	pub__Src = (UB *) (pcl__Img + 1);
	pub__Dst = pub__sFontTest;

	l =
	w = (pcl__Img->imgW);
	h = (pcl__Img->imgH + 31) & ~31;

	if (w > 512) w = 512;
	if (h > 256) h = 256;

	for (x = 0; x < w; x += 64)
		{
		pub__Tmp = pub__Src;

		for (y = 0; y < h; y += 1)
			{
			memcpy(pub__Dst, pub__Tmp, 64);

			pub__Tmp += l;
			pub__Dst += 64;
			}

		pub__Src = pub__Src + 64;
		}

	// Free resource.

	ResPurgeID(pcl__ShellRes, ID_SPR_FONTTEST);

	// All done.

	return;
	}
#endif	// show_font



// **************************************************************************
// * DrawFontTest ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  Gfx *   ?                                                      *
// *         UB *    ?                                                      *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

#if SHOW_FONT
global	Gfx *               DrawFontTest            (
								Gfx *               gp)

	{
	// Local Variables.

	int                 i;
	int                 j;

	UB *                pub__Src;

	// Clear screen

	gDPPipeSync(gp++);

	gDPSetCycleType(gp++,
		G_CYC_FILL);

	gDPSetFillColor(gp++,
		GPACK_RGBA5551(0xc0u,0xc0u,0xc0u,1) << 16 |
		GPACK_RGBA5551(0xc0u,0xc0u,0xc0u,1));
//  	GPACK_RGBA5551(0x20u,0x40u,0x20u,1) << 16 |
//  	GPACK_RGBA5551(0x20u,0x40u,0x20u,1));

	gDPFillRectangle(gp++,
		0,
		0,
		512-1,
		(240/2)-1);

	gDPSetFillColor(gp++,
		GPACK_RGBA5551(0x50u,0x50u,0x50u,1) << 16 |
		GPACK_RGBA5551(0x50u,0x50u,0x50u,1));
//  	GPACK_RGBA5551(0x20u,0x40u,0x20u,1) << 16 |
//  	GPACK_RGBA5551(0x20u,0x40u,0x20u,1));

	gDPFillRectangle(gp++,
		0,
		240/2,
		512-1,
		240-1);

	// Initialize drawing mode.

	gDPPipeSync(gp++);

	gDPSetCycleType(gp++,
		G_CYC_1CYCLE);

	gDPSetBlendColor(gp++,
		0x00,		// r
		0x00,		// g
		0x00,		// b
		0x10);		// a

	gDPSetAlphaCompare(gp++,
		G_AC_THRESHOLD);

	gDPSetTexturePersp(gp++,
		G_TP_NONE);

	gDPSetTextureFilter(gp++,
		G_TF_POINT);

	gDPSetTextureLUT(gp++,
		G_TT_NONE);

	gSPTexture(gp++,
		0x8000,				// s scale 0.16
		0x8000,				// t scale 0.16
		0,					// mipmap levels
		G_TX_RENDERTILE,	// tile number
		G_ON);				// enable

	gDPSetRenderMode(gp++,
		RM_XLU_FONT(1),
		RM_XLU_FONT(2));

	gDPSetCombineMode(gp++,
		G_CC_MODULATEIDECALA_PRIM,
		G_CC_MODULATEIDECALA_PRIM);

	// Render font samples.

	pub__Src = pub__sFontTest + 64*0;

	gDPSetPrimColor(gp++,
		0,					// m
		0,					// l
		0x10,				// r
		0x10,				// g
		0x10,				// b
		0x80);				// a

	for (i = 0; i < 448; i += 64)
		{
		for (j = 0; j <  96; j += 32)
			{
			gDPLoadTextureBlock(gp++,
				pub__Src,			// ptr (8-byte aligned)
				G_IM_FMT_IA,		// texel fmt
				G_IM_SIZ_8b,		// texel siz
				64,					// w
				32,					// h
				0,					// palette
				G_TX_CLAMP,			// clamp/wrap/mirror s
				G_TX_CLAMP,			// clamp/wrap/mirror t
				6,					// mask s
				5,					// mask t
				G_TX_NOLOD,			// shift s
				G_TX_NOLOD);		// shift t

			gSPTextureRectangle(gp++,
				(i + 48) << 2,		// ulx  (10.2)
				(j + 24) << 2,		// uly  (10.2)
				(i + 48 + 64) << 2,	// lrx  (10.2)
				(j + 24 + 32) << 2,	// lry  (10.2)
				G_TX_RENDERTILE,	// tile
				0,					// s    (s10.5)
				0,					// t    (s10.5)
				1 << 10,			// dsdx (s5.10)
				1 << 10);			// dtdy (s5.10)

			pub__Src += 64*32;
			}
		pub__Src += 64*160;
		}

	// Render font samples.

	pub__Src = pub__sFontTest + 64*0;

	gDPSetPrimColor(gp++,
		0,					// m
		0,					// l
		0xc0,				// r
		0xc0,				// g
		0xc0,				// b
		0x80);				// a

	for (i = 0; i < 448; i += 64)
		{
		for (j = 96; j < 192; j += 32)
			{
			gDPLoadTextureBlock(gp++,
				pub__Src,			// ptr (8-byte aligned)
				G_IM_FMT_IA,		// texel fmt
				G_IM_SIZ_8b,		// texel siz
				64,					// w
				32,					// h
				0,					// palette
				G_TX_CLAMP,			// clamp/wrap/mirror s
				G_TX_CLAMP,			// clamp/wrap/mirror t
				6,					// mask s
				5,					// mask t
				G_TX_NOLOD,			// shift s
				G_TX_NOLOD);		// shift t

			gSPTextureRectangle(gp++,
				(i + 48) << 2,		// ulx  (10.2)
				(j + 24) << 2,		// uly  (10.2)
				(i + 48 + 64) << 2,	// lrx  (10.2)
				(j + 24 + 32) << 2,	// lry  (10.2)
				G_TX_RENDERTILE,	// tile
				0,					// s    (s10.5)
				0,					// t    (s10.5)
				1 << 10,			// dsdx (s5.10)
				1 << 10);			// dtdy (s5.10)

			pub__Src += 64*32;
			}
		pub__Src += 64*160;
		}

	//

	gDPSetAlphaCompare(gp++,
		G_AC_NONE);

	// All Done.

	return (gp);
	}
#endif	//show_font



// **************************************************************************
// * LoadSprite ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	SprTexture_T *      LoadSprite              (
								unsigned long int   ui___ID)

	{
	// Local Variables.

	int                 x;
	int                 y;
	int                 w;
	int                 h;
	int                 len;
	int size;
	int tlen,clen;

	UB *                pub__Src;
	UB *                pub__Tmp;
	UB *                pub__Dst;

	SprResource_T *     pcl__Img;
	SprTexture_T *      pcl__Spr;

	// Load background image.

	pcl__Img = (SprResource_T *) ResOpenID(pcl__ShellRes, ui___ID);

	clen = len = (pcl__Img->sprW);
	w = (pcl__Img->sprW + 63) & ~63;
	h = (pcl__Img->sprH + 31) & ~31;

	// Allocate texture.

	size = sizeof(SprTexture_T) + (w*h);
	pcl__Spr = (SprTexture_T *) MemMalloc(size, MEM_ANY);

	if (pcl__Spr == NULL)
	{
		ABORT_PROGRAM;
	}

#ifdef DEBUG
	memset(pcl__Spr,0,size);	// fill for easy debugging(know when sprite ends)
#endif
	// Copy header info.

	memcpy(pcl__Spr, pcl__Img, sizeof(SprTexture_T));

	// Convert 12-bit positions into 16-bit positions.

	pcl__Spr->sprX = ((SD) (pcl__Img->sprFlg <<  0)) >> 20;
	pcl__Spr->sprY = ((SD) (pcl__Img->sprFlg << 12)) >> 20;

	// Now convert this into 64 pixel wide chunks.

	pub__Src = (UB *) (pcl__Img + 1);
	pub__Dst = (UB *) (pcl__Spr + 1);

	for (x = 0; x < w; x += 64)
	{
		if((tlen= clen) > 64)		// for easy debugging
			tlen = 64;
		
		pub__Tmp = pub__Src;

		for (y = 0; y < h; y += 1)
		{
//			memcpy(pub__Dst, pub__Tmp, 64);	
			memcpy(pub__Dst, pub__Tmp, tlen);	// for easy debugging

			pub__Tmp += len;
			pub__Dst += 64;
		}

		pub__Src = pub__Src + 64;
		clen -=64;							// for easy debugging
	}

	// Free resource.

	ResPurgeID(pcl__ShellRes, ui___ID);

	// All done.

	return (pcl__Spr);
	}



// **************************************************************************
// * LoadPhoto ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	UW *                LoadPhoto               (
								unsigned long int   ui___ID)

	{
	// Local Variables.

	stuffedImage *      pcl__Stf;
	unsigned short *    puw__Stf;

	// Allocate buffer for the photo.

	puw__Stf = (unsigned short *) MemMalloc(64*64*2, MEM_ANY);

	if (puw__Stf == NULL)
	{
		ABORT_PROGRAM;
	}

	// Load photo (JPEG stuffed).

	pcl__Stf = (stuffedImage *) ResOpenID(pcl__ShellRes, ui___ID);

	SetDither(DITHER_RANDOM);

	UnstuffImage16(pcl__Stf, puw__Stf);

	ResPurgeID(pcl__ShellRes, ui___ID);

	// All done.

	return (puw__Stf);
	}



// **************************************************************************
// * RenderScreenFnt ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  Gfx *   ?                                                      *
// *         UB *    ?                                                      *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	Gfx *               RenderScreenFnt         (
								Gfx *               gp,
								UB *                pub__Src)

	{
	// Local Variables.

	int                 i;
	int                 j;

	//
	// #define G_CC_PRIMITIVEDECALA        0, 0, 0, PRIMITIVE, 0, 0, 0, TEXEL0
	// #define G_CC_MODULATEIDECALA_PRIM   TEXEL0, 0, PRIMITIVE, 0, 0, 0, 0, TEXEL0
	//

	gDPPipeSync(gp++);

	gDPSetTexturePersp(gp++,
		G_TP_NONE);

	gDPSetBlendColor(gp++,
		0x00,				// r
		0x00,				// g
		0x00,				// b
		0x08);				// a

	gDPSetAlphaCompare(gp++,
		G_AC_THRESHOLD);

	gDPSetRenderMode(gp++,
		RM_XLU_FONT(1),
		RM_XLU_FONT(2));

	gSPTexture(gp++,
		0x8000,				// s scale 0.16
		0x8000,				// t scale 0.16
		0,					// mipmap levels
		G_TX_RENDERTILE,	// tile number
		G_ON);				// enable

	gDPSetCombineMode(gp++,
		G_CC_MODULATEIDECALA_PRIM,
		G_CC_MODULATEIDECALA_PRIM);

	gDPSetTextureFilter(gp++,
		G_TF_POINT);

	gDPSetTextureLUT(gp++,
		G_TT_NONE);

	// Render 512x256 screen in 7 rows.

	gDPSetPrimColor(gp++,
		0,					// m
		0,					// l
		0xE0,				// r
		0xE0,				// g
		0x40,				// b
		0x80);				// a

	for (i = 0; i < 512; i += 64)
		{
		for (j = 0; j < 224; j += 32)
			{
			gDPLoadTextureBlock(gp++,
				pub__Src,			// ptr (8-byte aligned)
				G_IM_FMT_IA,		// texel fmt
				G_IM_SIZ_8b,		// texel siz
				64,					// w
				32,					// h
				0,					// palette
				G_TX_CLAMP,			// clamp/wrap/mirror s
				G_TX_CLAMP,			// clamp/wrap/mirror t
				6,					// mask s
				5,					// mask t
				G_TX_NOLOD,			// shift s
				G_TX_NOLOD);		// shift t
/*
			gDPSetPrimColor(gp++,
				0,					// m
				0,					// l
				0x00,				// r
				0x00,				// g
				0x00,				// b
				0x00);				// a

			gSPTextureRectangle(gp++,
				(i + 48 + 2) << 2,		// ulx  (10.2)
				(j + 16 + 2) << 2,		// uly  (10.2)
				(i + 48 + 2 + 64) << 2,	// lrx  (10.2)
				(j + 16 + 2 + 32) << 2,	// lry  (10.2)
				G_TX_RENDERTILE,	// tile
				0,					// s    (s10.5)
				0,					// t    (s10.5)
				1 << 10,			// dsdx (s5.10)
				1 << 10);			// dtdy (s5.10)
*/
			gDPSetPrimColor(gp++,
				0,					// m
				0,					// l
				0xE0,				// r
				0xE0,				// g
				0x40,				// b
				0x00);				// a

			gSPTextureRectangle(gp++,
				(i + 48) << 2,		// ulx  (10.2)
				(j + 16) << 2,		// uly  (10.2)
				(i + 48 + 64) << 2,	// lrx  (10.2)
				(j + 16 + 32) << 2,	// lry  (10.2)
				G_TX_RENDERTILE,	// tile
				0,					// s    (s10.5)
				0,					// t    (s10.5)
				1 << 10,			// dsdx (s5.10)
				1 << 10);			// dtdy (s5.10)

			pub__Src += 64*32;
			}

		pub__Src += 64*32;
		}

	//

	gDPPipeSync(gp++);

	gDPSetAlphaCompare(gp++,
		G_AC_NONE);

	// All Done.

	return (gp);
	}






// **************************************************************************
// **************************************************************************
// **************************************************************************
//	STATIC FUNCTIONS
// **************************************************************************
// **************************************************************************
// **************************************************************************


#if SHOW_BG_COURT_LINES
// **************************************************************************
// * AddLine ()                                                             *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                AddLine                 (void)

	{
	//

	int                 i;

	//

	for (i = 0; i < MAX_ACTIVE_LINES; i++)
		{
		if(!activeLines[i].flags) break;
		}

	if (i == MAX_ACTIVE_LINES) return;

	activeLines[i].flags=1;

	#if 0
		activeLines[i].camX=((int)ReadRnd250()-128)*30000/128;
		activeLines[i].camY=0;
		activeLines[i].camZ=((int)ReadRnd250()-128)*15000/128;
		activeLines[i].camDX=-activeLines[i].camX/128;
		activeLines[i].camDY=-activeLines[i].camY/128;
		activeLines[i].camDZ=-activeLines[i].camZ/128;
	#else
		activeLines[i].camX=((int)ReadRnd250()-128)*3000/128;
		activeLines[i].camY=0;
		activeLines[i].camZ=((int)ReadRnd250()-128)*1500/128;
		activeLines[i].camDX=activeLines[i].camX/12;
		activeLines[i].camDY=activeLines[i].camY/12;
		activeLines[i].camDZ=activeLines[i].camZ/12;
	#endif

	activeLines[i].camP=(((int)ReadRnd250()&31)+5) << 8;
	activeLines[i].camH=0;
	activeLines[i].camDH=((int)ReadRnd250()-128);

	if(!activeLines[i].camDH) activeLines[i].camDH=256;

	activeLines[i].alpha=0x01;
	activeLines[i].camDist=((int)ReadRnd250() * 10000)/256 + 5000;

	// All done.

	return;
	}

#endif 


#if IN_2_SHELL_C_FILES	///---------------------------

// **************************************************************************
// * FindPlyrNam ()                                                         *
// **************************************************************************
// * Locate a GSM compressed player's name                                  *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                FindPlyrNam             (
								UL *                pul__Addr,
								UL *                pul__Size,
								PlyrRomData_T *     pcl__PlyrRom,
								UI                  ui___NameTyp)

	{
	// Local Variables.

	int                 i;

	UD                  ud___pos;
	UD                  ud___len;

	// Initialize for a failure.

	*pul__Addr =
	*pul__Size = 0;

	// Is the index table loaded ?

	if (pud__gIndxPnam != NULL)
		{
		// Is this a custom plyr ?

		if (pcl__PlyrRom->uw___pIndx <= si___gPlyrRomCnt)
			{
			// Locate the data for this plyr.

			i = (pcl__PlyrRom->uw___pIndx * 2) + ui___NameTyp;

			ud___pos = pud__gIndxPnam[i];
			ud___len = pud__gIndxPnam[i+1];

			ud___len = ud___len - ud___pos;

			// Is there any data ?

			if (ud___len != 0)
				{
				ud___pos = ud___pos + ul___gCommFileRom + sl___gCommPnamPos;

				// Save the results.

				*pul__Addr = (UL) ud___pos;
				*pul__Size = (UL) ud___len;

				return;
				}
			}
		}

	// All done, return failure.

	return;
	}



// **************************************************************************
// * FindCommentary ()                                                      *
// **************************************************************************
// * Locate a GSM compressed commentary phrase                              *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                FindCommentary          (
								UL *                pul__Addr,
								UL *                pul__Size,
								UI                  ui___Phrase)

	{
	// Local Variables.

	UD                  ud___pos;
	UD                  ud___len;

	// Is the index table loaded ?

	if (pud__gIndxTalk == NULL)
		{
		*pul__Addr = *pul__Size = 0; return;
		}

	// Locate the data for this plyr.

	ud___pos = pud__gIndxTalk[ui___Phrase];
	ud___len = pud__gIndxTalk[ui___Phrase+1];

	ud___len = ud___len - ud___pos;

	ud___pos = ud___pos + ul___gCommFileRom + sl___gCommTalkPos;

	// Save the results.

	*pul__Addr = (UL) ud___pos;
	*pul__Size = (UL) ud___len;

	// All done.

	return;
	}

#endif	//IN_2_SHELL_C_FILES	///---------------------------

// **************************************************************************
// **************************************************************************
// **************************************************************************
//	END OF SHELL.C
// **************************************************************************
// **************************************************************************
// **************************************************************************


#endif


//#endif




/*****  following included from old files missing from bball2 so at least it complies ****/

void MakeMat64PRH(long *ll,int p,int r,int h,int x,int y,int z,int scale)
{
	long int pc,ps,hc,hs,rc,rs;
	long int rsps,rspc;
	long t;
	short *l;

	l=(short *)ll;
	l[3]=l[7]=l[11]=0;
	l[12]=x/10; l[13]=y/10; l[14]=z/10;
	l[15]=1;
	l[19]=l[23]=l[27]=0;
	l[28]=(x%10)*65536/10;
	l[29]=(y%10)*65536/10;
	l[30]=(z%10)*65536/10;
	l[31]=0;

	pc=FASTCOS(p); ps=FASTSIN(p);
	rc=FASTCOS(r); rs=FASTSIN(r);
	hc=FASTCOS(h); hs=FASTSIN(h);

	rsps=(rs*ps)/16384;
	rspc=(rs*pc)/16384;

	if(scale == 256)
	{
		t=(rc*hc)>>12;
		l[0]=(t>>16);	l[16]=t & 0xFFFF;
		t=rs<<2;
		l[1]=(t>>16);	l[17]=t & 0xFFFF;
		t=(rc*hs)>>12;
		l[2]=(t>>16);	l[18]=t & 0xFFFF;

		t=((-hc*rspc) + (-ps*hs)) >> 12;
		l[4]=(t>>16);	l[20]=t & 0xFFFF;
		t=(pc*rc)>>12;
		l[5]=(t>>16);	l[21]=t & 0xFFFF;
		t=((-hs*rspc) + (ps*hc)) >> 12;
		l[6]=(t>>16);	l[22]=t & 0xFFFF;

		t=((hc*rsps) + (-pc*hs)) >> 12;
		l[8]=(t>>16);	l[24]=t & 0xFFFF;
		t=(-ps*rc) >> 12;
		l[9]=(t>>16);	l[25]=t & 0xFFFF;
		t=((hs*rsps) + (pc*hc)) >> 12;
		l[10]=(t>>16);	l[26]=t & 0xFFFF;
	}
	else
	{
		t=(rc*hc)>>12; t=(t*scale)/256;
		l[0]=(t>>16);	l[16]=t & 0xFFFF;
		t=rs<<2; t=(t*scale)/256;
		l[1]=(t>>16);	l[17]=t & 0xFFFF;
		t=(rc*hs)>>12; t=(t*scale)/256;
		l[2]=(t>>16);	l[18]=t & 0xFFFF;

		t=((-hc*rspc) + (-ps*hs)) >> 12; t=(t*scale)/256;
		l[4]=(t>>16);	l[20]=t & 0xFFFF;
		t=(pc*rc)>>12; t=(t*scale)/256;
		l[5]=(t>>16);	l[21]=t & 0xFFFF;
		t=((-hs*rspc) + (ps*hc)) >> 12; t=(t*scale)/256;
		l[6]=(t>>16);	l[22]=t & 0xFFFF;

		t=((hc*rsps) + (-pc*hs)) >> 12; t=(t*scale)/256;
		l[8]=(t>>16);	l[24]=t & 0xFFFF;
		t=(-ps*rc) >> 12; t=(t*scale)/256;
		l[9]=(t>>16);	l[25]=t & 0xFFFF;
		t=((hs*rsps) + (pc*hc)) >> 12; t=(t*scale)/256;
		l[10]=(t>>16);	l[26]=t & 0xFFFF;
	}
}

/*************************************************************************
 *
 *	
 *
 *************************************************************************
 *
 *
 *
 *************************************************************************/


/*************************************************************************
 *
 *
 *
 *************************************************************************
 *
 *
 *
 *************************************************************************/

void Mat64Invert(short *s)
{
	short t;

	t=s[1]; s[1]=s[4]; s[4]=t;
	t=s[2]; s[2]=s[8]; s[8]=t;
	t=s[6]; s[6]=s[9]; s[9]=t;

	t=s[1+16]; s[1+16]=s[4+16]; s[4+16]=t;
	t=s[2+16]; s[2+16]=s[8+16]; s[8+16]=t;
	t=s[6+16]; s[6+16]=s[9+16]; s[9+16]=t;
}


/******************
int n64ResAlpha=0xFF;

void N64ResSetAlpha(int a)
{
	n64ResAlpha=a;
}
*********************/







/*****

//void InitGenericScreenParameters(char *titletext)
{
	//gLeftSideText = titletext;
}
****/

global char * SourceTypeText[] = {
	"GAME",			// not "player" (could be team)
	"NBA 98-99",
	"SEASON",
	"PLAYOFF",
	"PLAYER"	// as in PLAYER RATINGS		"GOOFY",	//ERROR"
};





/**********

void DisplayVerticalText(char *text, int x, int y)
{
TXTCEL_T *          pcl__Txt;
char OneChar[2] = {0,0};
int ty;

		pcl__Txt = InitTxtCel(NULL,0,FONT_BLKCN50,oncolor,FFLG_XC|FFLG_YT|FFLG_SHADOW);

		ty= 120 - ( strlen(text) *18/2);		// center vertically
		while( (OneChar[0]=*text++) != 0)
		{
		
			ClearTxtCel(pcl__Txt);

			WriteTxtCel(pcl__Txt, OneChar);

			DrawTxtCel (pcl__Txt, x,ty);
			ty += 18;
		}
		
		FreeTxtCel(pcl__Txt);
		MemFree(pcl__Txt);
}

*******/

/*******************************************************************************************************/
extern Gfx *DrawSideText( Gfx *gp, char *text, s32 xpos, s32 ypos );
extern Gfx *ShadFont_Draw( Gfx *gp, char *text, s32 x, s32 y );
extern void ShadFont_Color( const u8 r, const u8 b, const u8 g );
	
int LSBY = 225;	// realtime mod. in debugger

global	Gfx *	DrawLeftSideBar(Gfx * gp, SprTexture_T * pcl__Spr,int x, int y)
{

char *text, *text2;
int ty;
int spw,centerx;

#if 1

//    if(asi__gTeamBtn[EVERYBODY] & CTRL_TRIGGER)
//	{		displayrainbow ^= 1;	}
	
	if( (text = gStateTitleText[si___gCurState]) != NULL
//		&& displayrainbow
	)
	{

		// shadow is 68 pix wide, shad starts at pixel 58
		SetRCColors(10,10,10,10);
		gp = RenderCorner(gp, SPR_PTR(SPR_MBARSHAD), MENU_LEFT_KEYX-58, 240-56);


		pcl__Spr = SPR_PTR(SPR_MENULBAR);
		spw = pcl__Spr->sprW;
		centerx = MENU_LEFT_KEYX - spw/2;

		gp = RenderSprite(gp, pcl__Spr,
						 (UI) MENU_BAR_PAL,
						MENU_LEFT_KEYX - spw,	// right justify on Key X
						//ScreenTitleX+0,
						ScreenTitleY+0); 

//		gp = RenderLSHD(gp, MENU_LEFT_KEYX, 0,  12,  MENU_BOTTOM_LINE_Y-14);
		gp = RenderLSHD(gp, MENU_LEFT_KEYX, 0,  12,   240-56);

		ShadFont_Color( SHADFNT_RGB_WHITE);
		ShadFont_Set(SideShadFont);

		if( (text2= gStateTitleText2[si___gCurState]) != NULL)
		{	
//			gp =DrawSideChar(gp, (u8 *)SideFont, s32 x, s32 y )

		
//			DisplayVerticalText(text2, MENU_LEFT_KEYX - spw/4*3, 0);
//			DisplayVerticalText(text, MENU_LEFT_KEYX - spw/4, 0);
			if(stricmp(text2,"CAREER PLAYER") == ZERO)
			{
				ty = LSBY;
			}
			else
			{
				ty = 200;
			}
			gp =ShadFont_Draw(gp,text2, MENU_LEFT_KEYX - spw + 6+2, ty);
			gp =ShadFont_Draw(gp,text, centerx+2 , 200);

		
		}
		else
		{
//			DisplayVerticalText(text, MENU_LEFT_KEYX - spw/2, 0);
			gp =ShadFont_Draw(gp, text,  centerx+2, 200);

		}

		gp = DrawChrList(gp);

//		if(!cl___gPreferences.sb___Mono)
//		{
//			gp = DisplayKobePhoto(gp, centerx -32,16);	// center in "mainbar"
//		}

	}


#else
	// colorful "courtside" bar
	gp = RenderSprite(gp, SPR_PTR(SPR_MENULBAR), (UI) puw__gMainPalH,ScreenTitleX, ScreenTitleY);
		// "font" graphic on top of it
	gp = RenderSprite(gp, pcl__Spr, (UI) puw__gMainPalH,ScreenTitleX+x, ScreenTitleY+y); 
#endif	
	

	return(gp);
}	
	



global	Gfx *	DrawRCBoxes(Gfx * gp, struct rcbox * boxes)
{
struct rcbox *curbox;
UI	b=0;
UI  c;
SprTexture_T *      pcl__Spr;
UI spW,spH;

	while( (boxes+b)->x >= ZERO)
	{
		curbox = boxes+b;
		gp = RenderTBOX(gp, curbox->x,
							curbox->y,
							curbox->w,
							curbox->h,
							curbox->r,
							curbox->g,
							curbox->b,
							curbox->a
							);
		

		for(c=0; c<4; c++)
		{
			if(curbox->corners[c] > 1)	// anything there?
			{
				SetRCColors(curbox->r,
							curbox->g,
							curbox->b,
							curbox->a);
				pcl__Spr = SPR_PTR(curbox->corners[c]);		
				if(pcl__Spr == NULL)
				{
					continue;
				}

				spW = pcl__Spr->sprW;
				spH = pcl__Spr->sprH;

				switch (c)
				{
					case CN_TL:
						gp = RenderCorner(gp,pcl__Spr,
											curbox->x - spW,
											curbox->y);
						break;
					case CN_TR:
						gp = RenderCorner(gp,pcl__Spr,
											curbox->x + curbox->w,
											curbox->y);
						break;
					case CN_BL:
						gp = RenderCorner(gp,pcl__Spr,
											curbox->x - spW,
											curbox->y + curbox->h - spH);
						break;
					case CN_BR:
						gp = RenderCorner(gp,pcl__Spr,
											curbox->x + curbox->w,
											curbox->y + curbox->h - spH);
						break;
				}

			}
		
		}

		b++;
	}

	return(gp);
}


global	Gfx *	DrawRCBoxesX(Gfx * gp, struct rcbox * boxes,int xoffset)
{
struct rcbox *curbox;
UI	b=0;
UI  c;
SprTexture_T *      pcl__Spr;
UI spW,spH;
int x;

	while( (boxes+b)->x >= ZERO)
	{
		curbox = boxes+b;
		x =  curbox->x+xoffset;
		
		gp = RenderTBOX(gp, x,
							curbox->y,
							curbox->w,
							curbox->h,
							curbox->r,
							curbox->g,
							curbox->b,
							curbox->a
							);
		

		for(c=0; c<4; c++)
		{
			if(curbox->corners[c] > 1)	// anything there?
			{
				SetRCColors(curbox->r,
							curbox->g,
							curbox->b,
							curbox->a);
				pcl__Spr = SPR_PTR(curbox->corners[c]);		
				if(pcl__Spr == NULL)
				{
					continue;
				}

				spW = pcl__Spr->sprW;
				spH = pcl__Spr->sprH;

				switch (c)
				{
					case CN_TL:
						gp = RenderCorner(gp,pcl__Spr,
											x - spW,
											curbox->y);
						break;
					case CN_TR:
						gp = RenderCorner(gp,pcl__Spr,
											x + curbox->w,
											curbox->y);
						break;
					case CN_BL:
						gp = RenderCorner(gp,pcl__Spr,
											x - spW,
											curbox->y + curbox->h - spH);
						break;
					case CN_BR:
						gp = RenderCorner(gp,pcl__Spr,
											x + curbox->w,
											curbox->y + curbox->h - spH);
						break;
				}
			}
		}
		b++;
	}

	return(gp);
}



global int gLineCurveH,gLineCurveW;



global	Gfx *	DrawRCVLineRightTopCurveOnly(Gfx * gp, int x, int y1, int y2,int shadows)
{	// no bottom curve - used on WELCOME/Pregame screen

SprTexture_T *   topcurve;
//SprTexture_T *   botcurve;
int th;
int	yt,yb,x2;

	topcurve = SPR_PTR(SPR_TRLNTL);
//	topcurve = SPR_PTR(SPR_LL_ALNTR);

//	botcurve = SPR_PTR(SPR_LL_ALNBR);
	gLineCurveH = th = topcurve->sprH;
	gLineCurveW = topcurve->sprW;

	yt = y1+th;
	yb = y2;
	gp = RenderSprite(gp, topcurve, (UI) puw__gMainPalH,x,y1);
//	gp = RenderSprite(gp, botcurve, (UI) puw__gMainPalH,x,yb);
	
	gp = RenderTBOX(gp, x,
						yt,
						MENU_LINE_W,
						yb-yt,
						RGB_MENU_LINE_COLOR);
		
/****
	if(shadows)
	{
		gp = RenderLSHD(gp,x+MENU_LINE_W,yt,10,yb-yt);
//		x2 = x + topcurve->sprW;
//		gp = RenderTSHD(gp,x2,y1,512-x2,8);
	}	

	return(gp);
****/
}



global	Gfx *	DrawRCVLineLeftTopCurveOnly(Gfx * gp, int x, int y1, int y2,int shadowx)
{

SprTexture_T *   topcurve;
SprTexture_T *   botcurve;
int th;
int	yt,yb,x2;

	topcurve = SPR_PTR(SPR_TRLNTR);
//	botcurve = SPR_PTR(SPR_LL_ALNBL);
	gLineCurveH = th = topcurve->sprH;
	gLineCurveW = topcurve->sprW;

	yt = y1+th;
	yb = y2;
	x2 = x - topcurve->sprW + MENU_LINE_W;

	gp = RenderSprite(gp, topcurve, (UI) puw__gMainPalH,x2,y1);
//	gp = RenderSprite(gp, botcurve, (UI) puw__gMainPalH,x - botcurve->sprW + MENU_LINE_W,yb);
	
	gp = RenderTBOX(gp, x,
						yt,
						MENU_LINE_W,
						yb-yt,
						RGB_MENU_LINE_COLOR);
		
//	gp = RenderLSHD(gp,x+MENU_LINE_W,yt,10,yb-yt);
/***
	if(shadowx)
	{
		gp = RenderTSHD(gp,shadowx,y1,x2-shadowx,8);
	}
***/			
	return(gp);
}

//---------------------------------------------------------------



global	Gfx *	DrawRCVLineRight(Gfx * gp, int x, int y1, int y2,int shadows)
{

SprTexture_T *   topcurve;
SprTexture_T *   botcurve;
int th,bh;
int	yt,yb,x2;

//	topcurve = SPR_PTR(SPR_LL_ALNTR);
//	botcurve = SPR_PTR(SPR_LL_ALNBR);
	y1 -= 2;
	y2 += 2;
	topcurve = SPR_PTR(SPR_TRLNTL);
	botcurve = SPR_PTR(SPR_TRLNBL);

	gLineCurveH = th = topcurve->sprH;
	gLineCurveW = bh = botcurve->sprH;

	yt = y1+th;
	yb = y2-bh;
	gp = RenderSprite(gp, topcurve, (UI) puw__gMainPalH,x,y1);
	gp = RenderSprite(gp, botcurve, (UI) puw__gMainPalH,x,yb);
	
	gp = RenderTBOX(gp, x,
						yt,
						MENU_LINE_W,
						yb-yt,
						RGB_MENU_LINE_COLOR);
		

	if(shadows)
	{
		gp = RenderLSHD(gp,x+MENU_LINE_W,yt,10,yb-yt);
//		x2 = x + topcurve->sprW;
//		gp = RenderTSHD(gp,x2,y1,512-x2,8);
	}	
	return(gp);
}


global	Gfx *	DrawRCVLineLeft(Gfx * gp, int x, int y1, int y2,int shadowx)
{

SprTexture_T *   topcurve;
SprTexture_T *   botcurve;
int th,bh;
int	yt,yb,x2;

//	topcurve = SPR_PTR(SPR_LL_ALNTL);
//	botcurve = SPR_PTR(SPR_LL_ALNBL);
	y1 -= 2;
	y2 += 2;
	topcurve = SPR_PTR(SPR_TRLNTR);
	botcurve = SPR_PTR(SPR_TRLNBR);
	gLineCurveH = th = topcurve->sprH;
	gLineCurveW = topcurve->sprW;
	bh = botcurve->sprH;

	yt = y1+th;
	yb = y2-bh;
	x2 = x - topcurve->sprW + MENU_LINE_W;

	gp = RenderSprite(gp, topcurve, (UI) puw__gMainPalH,x2,y1);
	gp = RenderSprite(gp, botcurve, (UI) puw__gMainPalH,x - botcurve->sprW + MENU_LINE_W,yb);
	
	gp = RenderTBOX(gp, x,
						yt,
						MENU_LINE_W,
						yb-yt,
						RGB_MENU_LINE_COLOR);
		
//	gp = RenderLSHD(gp,x+MENU_LINE_W,yt,10,yb-yt);
	if(shadowx)
	{
		gp = RenderTSHD(gp,shadowx,y1,x2-shadowx,8);
	}
			
	return(gp);
}






global	Gfx *	DrawBlankBox(Gfx * gp, int x1, int x2, int y1, int y2, int flags)
{		// just the outline and shadows - everything else shows thru to background
		// DOES NOT draw top or bottom lines (just side lines and curves)
		// DOES NOT draw transparent corner sprites

/*
	 /-----\
	/		\
	|		|
L	|		|	R
	|		|
	\		/
	 \-----/


*/
int shadwidth,shadheight;

	
	if(x1 < ZERO)
	{
		x1 = 0;
	}
	
	if(y1 < ZERO)
	{
		y1 = 0;
	}
	if(flags & BB_LINE_L)	// the line on the left
	{		
		gp = DrawRCVLineRight(gp, x1, y1, y2,0);
	}
	
	
	if(x2 > 512)
	{
		x2 = 512;
	}

	if(flags & BB_LINE_R)
	{		
		gp = DrawRCVLineLeft(gp, x2, y1, y2,0);
	}

//	shadwidth = x2-x1-(2*gLineCurveW);

	if(flags & BB_SHAD_T)
	{
		shadwidth = x2-x1-(2*NORMAL_CURVE_W);
//		gp = RenderTSHD(gp,x1+NORMAL_CURVE_W,y1,shadwidth,8);
		gp = RenderTSHD(gp,x1+NORMAL_CURVE_W,y1,shadwidth,8);
	}

	if(flags & BB_SHAD_L)
	{
		shadheight = y2-y1-(2*NORMAL_CURVE_H);
//		gp = RenderLSHD(gp,x1+MENU_LINE_W,y1+NORMAL_CURVE_H-1,12,shadheight+2);
		gp = RenderLSHD(gp,x1+MENU_LINE_W,y1+NORMAL_CURVE_H-2,12,shadheight+2);
	}

	if( (flags & BB_FULL_BOX) == BB_FULL_BOX)
	{	// put in top and bottom lines
		shadwidth = x2-x1-(2*NORMAL_CURVE_W);
		gp = RenderTBOX(gp,x1+NORMAL_CURVE_W,y1-MENU_LINE_H, shadwidth, MENU_LINE_H, RGB_MENU_LINE_COLOR);
		gp = RenderTBOX(gp,x1+NORMAL_CURVE_W,y2, shadwidth, MENU_LINE_H, RGB_MENU_LINE_COLOR);
	
	}
	return(gp);


}





/************************************************************************************************

				Shell  Entry

************************************************************************************************/
int EntryCounter;

int InitScreenShellEntry(int state)
{

	// allow changing of screen resolution to stabilize
	EntryCounter =	2;	//30;

	// Load up the correct background image.
//	LoadShellBackgroundSpr(ID_SPR_BACKNBA);

	// All Done.
	return (state);
}

/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/

int ExitScreenShellEntry(int state)
{
	// Free up the sprites for this screen.
//	FreeSpriteList(sprScreenS);

	LeaveShellMethod = 	LEAVE_SHELL_TO_GAME;	// assume this from now on

	// All Done.
	return (state);
}


void ShellCleanupAfterGame(void)
{

		if (pcl__gMatch != NULL)
		{	// clean up this match
			DoneMatchStruct(pcl__gMatch);
		}

		pcl__gMatch = NULL;

		pcl__gPreferences = &cl___gPreferences;
		pcl__gOptions     = &acl__gOptions[SOURCE_PRE];
		pcl__gMatch       = &cl___gMatch;

		if (pcl__gMatch != NULL)
		{	// clean up any old matches
			DoneMatchStruct(pcl__gMatch);
		}

//		pcl__gMatch = NULL;

}

/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/

int ProcScreenShellEntry(int oldState)	// Select Player (or NEW) to edit
{
	// Local Variables.

int nextstate = oldState;

// NOTE - ShellOpen already set 	puw__gRoster = apuw_gRoster[SOURCE_GAM]


	if(--EntryCounter < ZERO)
	{

	// Are we returning from a demo ?

	if (fl___gDemoFlag)
	{
		fl___gDemoFlag = 0;

		ShellCleanupAfterGame();

		nextstate = STATE_MAIN_MENU;

	}
	else if(C3PT)
	{		// returning from 3 point contest

/*****
		if(gameOver)
		{	//  game was quit 
			nextstate = STATE_MAIN_MENU;
			if (pcl__gMatch != NULL)
			{
				DoneMatchStruct(pcl__gMatch);
			}
		
		}
		else *******/
		{
			nextstate = STATE_3PT_ROUNDS;
			Restart3PtContestAfterMatch();	// sets up scores from last match(if #3pt not quitted)
		}
	}
	else if (pcl__gMatch == NULL ||	LeaveShellMethod ==	LEAVE_SHELL_TO_INTRO)	// Are we returning from a match ?
	{
		// No, we must have just been reset OR coming from intro.

		ShellCleanupAfterGame();

		if(DidSaveOptions)	// if did CONFIG 1st (by pressing START on powerup)
		{ 
			nextstate = STATE_MAIN_MENU;
		}
		else
		{
			nextstate = STATE_MEMPAK_CONFIG;
		}
	}
	else	// Returning from "normal" game
	{
		// Yes, and so we'd better fix up the plyr time-on-court stats.

		XvertPlyrTimeOnCourt();

		// Why have we returned ?

		pcl__gOptions = pcl__gMatch->pcl__mOptions;

		switch (pcl__gMatch->sb___mQuarter)
		{
			case -1: // Practice or 3 pt
			case  0: // Match was quit.
			{

				DoneMatchStruct(pcl__gMatch);

				if (pcl__gOptions->sw___PlayoffIndx != 0)
				{
					// Continue the playoffs.
					nextstate = STATE_H1;
				}
				else if(pcl__gOptions->sw___SeasonMatch != 0)
				{
					// Continue the season.
					nextstate = STATE_H;
				}
				else
				{
					nextstate = STATE_MAIN_MENU;

				}

				break;
			}
/***
			case  0: // End of 1st quarter.
			{
				nextstate = STATE_ENDOFQTR;
				break;
			}
***/
			case  1: // End of 2nd quarter.
			{
				nextstate = STATE_HALFTIME;
				break;
			}
			case  2: // End of 3rd quarter.
			{
				nextstate = STATE_ENDOFQTR;
				break;
			}
		
			default: // End of 4th quarter or overtime.
			{
				if (pcl__gMatch->apcl_mTeamRam[HOME_TEAM]->uw___tPoints ==
					pcl__gMatch->apcl_mTeamRam[AWAY_TEAM]->uw___tPoints)
				{
					// Not over yet ... 

//					nextstate = STATE_ENDOFQTR;

					DoneMatchStruct(pcl__gMatch);	//bug # 559 - tie after 8 overtimes
					GameCodeFlag = 1;			// will show game code
					nextstate = STATE_SPECIAL_MENU1;	
				}
				else
				{
					// Match finished ...

					// in Kobe 1, Post game was a part of game(overlayed), now it's in shell
					//  so ALL games go direct to PostGame,
					// PostGame Exit will then figure where to go
					nextstate = STATE_POST_GAME_MENU;	//STATE_POST_GAME_TEAM_STATS;	

					// following for ANY (season, preseason,playoff, arcade, quick play) game (except sim)
					UpdateCustomUserStats();	

					if ((pcl__gOptions->sw___PlayoffIndx == 0) &&
						(pcl__gOptions->sw___SeasonMatch == 0))
					{
						// It was a pre-season match.  or Arcade mode or quick play

//						nextstate = STATE_B;
//						nextstate = STATE_MAIN_MENU;	// 1999 - could have been quick play
						
					}
					else
					{
						// Was it a season or playoff match.

						if (pcl__gOptions->sw___PlayoffIndx != 0)
						{
							// Continue the playoffs.

							UpdatePlyMatchStats(0);		// just complete true game (not sim)

							NxtPlayoffGame();

//							nextstate = STATE_H1;
						}
						else
						{
							// Continue the season.

							UpdateSsnMatchStats(0);	// just complete true game (0 = not sim)

							pcl__gOptions->sw___SeasonMatch += 1;

//							nextstate = STATE_H;
						}

						// Flag that we need to save data to backup memory.

						sb___gNeedToSave = pcl__gOptions->sb___OptionSrc + 1;
					}

					// Force the fulltime stats screen.
					//
					// nextstate = STATE_FULLTIME;
				}
				break;
			}
		}

	// Free up the match data that we got back from the arcade.
	// NO - no yet for Kobe 2 - still need to do stats screens
	//	DoneMatchStruct(pcl__gMatch);

	
	}


	if(ArcadeMode)
	{	// change options from Arcade options to back to preseason options
		pcl__gOptions = &acl__gOptions[SOURCE_PRE];
	}


	}
	
	return (nextstate);
}




Gfx *DrawScreenShellEntry(int state,Gfx *gp)
{

	// All done.
	return (gp);
}








/*************************  End of file: Shell.c - mjk **********************/
