SBSPSS/source/gfx/actor.cpp

426 lines
10 KiB
C++
Raw Normal View History

2001-04-01 09:22:30 +02:00
/******************/
/*** Actor Bank ***/
/******************/
#include "system\global.h"
#include "mem\memory.h"
#include "fileio\fileio.h"
#include "utils\utils.h"
2001-04-01 22:22:49 +02:00
#include "utils\pak.h"
#include "gfx\prim.h"
2001-04-01 23:38:59 +02:00
#include "gfx\actor.h"
2001-04-01 09:22:30 +02:00
#include <dstructs.h>
2001-04-09 23:27:21 +02:00
/*****************************************************************************/
int ShadowXOfs=32;
int ShadowYOfs=32;
2001-04-04 18:51:04 +02:00
/*****************************************************************************/
2001-04-19 01:12:24 +02:00
CActorPool::ACTOR_TYPE CActorPool::actorType[39] =
2001-04-04 18:51:04 +02:00
{
2001-04-19 01:12:24 +02:00
ACTOR_PLAYER, //SpongeBob=0
ACTOR_FRIEND_NPC, //BarnacleBoy=1
ACTOR_FRIEND_NPC, //Gary=2
ACTOR_FRIEND_NPC, //Krusty=3
ACTOR_FRIEND_NPC, //MermaidMan=4
ACTOR_FRIEND_NPC, //Patrick=5
ACTOR_FRIEND_NPC, //Sandy=6
ACTOR_FRIEND_NPC, //Squidward=7
ACTOR_FRIEND_NPC, //Plankton=8
2001-04-04 18:51:04 +02:00
ACTOR_UNKNOWN,
2001-04-19 01:12:24 +02:00
ACTOR_ENEMY_NPC, //SmallJellyfish-Level1=10
ACTOR_ENEMY_NPC, //SmallJellyfish-Level2=11
ACTOR_ENEMY_NPC, //Motherjellyfish=12
ACTOR_ENEMY_NPC, //Anenome-Level1=13
ACTOR_ENEMY_NPC, //SpikeyAnenome=14
ACTOR_ENEMY_NPC, //Anenome-Level3=15
ACTOR_ENEMY_NPC, //BabyOctopus=16
ACTOR_ENEMY_NPC, //Ballblob=17
ACTOR_ENEMY_NPC, //Boogermonster=18
ACTOR_ENEMY_NPC, //Caterpillar=19
ACTOR_ENEMY_NPC, //Clam-Level1=20
ACTOR_ENEMY_NPC, //Clam-Level2=21
ACTOR_ENEMY_NPC, //Eyeball=22
ACTOR_ENEMY_NPC, //Flamingskull=23
ACTOR_ENEMY_NPC, //FlyingDutchman=24
ACTOR_ENEMY_NPC, //Ghost=25
ACTOR_ENEMY_NPC, //GiantWorm=26
ACTOR_ENEMY_NPC, //HermitCrab=27
ACTOR_ENEMY_NPC, //IronDogFish=28
ACTOR_ENEMY_NPC, //PuffaFish=29
ACTOR_ENEMY_NPC, //SeaSnake=30
ACTOR_ENEMY_NPC, //Sharkman=31
ACTOR_ENEMY_NPC, //SharkSub=32
ACTOR_ENEMY_NPC, //Skeletalfish=33
ACTOR_ENEMY_NPC, //SpiderCrab=34
ACTOR_ENEMY_NPC, //Squiddart=35
ACTOR_ENEMY_NPC, //Stomper=36
ACTOR_ENEMY_NPC, //DustDevil=37
ACTOR_ENEMY_GENERATOR_NPC, //SiderCrabSpawner=38
2001-04-04 18:51:04 +02:00
};
2001-04-01 09:22:30 +02:00
/*****************************************************************************/
2001-04-09 23:27:21 +02:00
sActorPool CActorPool::ActorPool[MAX_ACTORS];
CSlotCache CActorGfx::SlotCache;
u8 CActorGfx::UnpackBuffer[CActorPool::MAX_ACTOR_SIZE];
2001-04-01 09:22:30 +02:00
/*****************************************************************************/
2001-04-09 23:27:21 +02:00
/*** Slot Cache **************************************************************/
2001-04-01 09:22:30 +02:00
/*****************************************************************************/
2001-04-09 23:27:21 +02:00
void CSlotCache::Init()
{
for (int Y=0; Y<DYN_SLOTH; Y++)
{
for (int X=0; X<DYN_SLOTW; X++)
{
Cache[X][Y].W=0;
}
}
}
2001-04-01 22:22:49 +02:00
/*****************************************************************************/
2001-04-09 23:27:21 +02:00
bool CSlotCache::FindSlot(int SprW,int SprH,u16 &TexX,u16 &TexY,u8 &u,u8 &v)
2001-04-01 22:22:49 +02:00
{
2001-04-09 23:27:21 +02:00
int W=(SprW+(DYN_W-1))/DYN_W;
int H=(SprH+(DYN_H-1))/DYN_H;
for (int Y=0; Y<DYN_SLOTH; Y++)
{
for (int X=0; X<DYN_SLOTW; X++)
{
if (TakeSlot(X,Y,W,H))
{
printf("TakeSlot %i %i\n",X,Y);
TexX=DYN_SLOTX+(X*DYN_W/4);
TexY=DYN_SLOTY+(Y*DYN_H);
u=(X&3)*64;
v=(Y&3)*64;
return(true);
}
}
}
return(false);
2001-04-01 22:22:49 +02:00
}
2001-04-09 23:27:21 +02:00
/*****************************************************************************/
bool CSlotCache::TakeSlot(int SX,int SY,int SW,int SH)
{
int W,H;
if (SX+SW>DYN_SLOTW) return(false);
if (SY+SH>DYN_SLOTH) return(false);
if ((SX&3)==3 && SW>1) return(false);
for (H=0; H<SH; H++)
{
for (W=0; W<SW; W++)
{
if (Cache[SX+W][SY+H].W) return(false);
}
}
// Slot good
for (H=0; H<SH; H++)
{
for (W=0; W<SW; W++)
{
Cache[SX+W][SY+H].W=SW;
Cache[SX+W][SY+H].H=SH;
}
}
return(true);
}
2001-04-02 00:55:15 +02:00
2001-04-01 09:22:30 +02:00
/*****************************************************************************/
2001-04-09 23:27:21 +02:00
/*****************************************************************************/
/*****************************************************************************/
void CActorPool::Init()
2001-04-01 09:22:30 +02:00
{
2001-04-01 22:22:49 +02:00
int i;
2001-04-09 23:27:21 +02:00
for (i=0; i<MAX_ACTORS; i++)
{
ActorPool[i].SpriteBank=0;
}
CActorGfx::ResetCache();
}
2001-04-02 00:55:15 +02:00
2001-04-09 23:27:21 +02:00
/*****************************************************************************/
int CActorPool::FindActorInPool(FileEquate Filename)
{
for (int i=0; i<MAX_ACTORS; i++)
{
if (ActorPool[i].SpriteBank && ActorPool[i].Filename==Filename) return(i);
}
return(-1);
}
2001-04-01 09:22:30 +02:00
2001-04-09 23:27:21 +02:00
/*****************************************************************************/
int CActorPool::FindFreeActor()
{
for (int i=0; i<MAX_ACTORS; i++)
{
if (ActorPool[i].SpriteBank==0) return(i);
}
return(-1);
}
/*****************************************************************************/
int CActorPool::LoadActor(FileEquate Filename)
{
int i;
sSpriteAnimBank *Spr=(sSpriteAnimBank*)CFileIO::loadFile(Filename);
Spr->AnimList=(sSpriteAnim*) MakePtr(Spr,(int)Spr->AnimList);
Spr->FrameList=(sSpriteFrame*) MakePtr(Spr,(int)Spr->FrameList);
Spr->Palette=(u8*) MakePtr(Spr,(int)Spr->Palette);
2001-04-01 22:22:49 +02:00
// FixUp AnimList
2001-04-09 23:27:21 +02:00
DAVE_DBGMSG("Anims %i\n",Spr->AnimCount);
for (i=0; i<Spr->AnimCount; i++)
2001-04-01 22:22:49 +02:00
{
2001-04-09 23:27:21 +02:00
sSpriteAnim *ThisAnim=&Spr->AnimList[i];
ThisAnim->Anim=(u16*) MakePtr(Spr,(int)ThisAnim->Anim);
2001-04-01 22:22:49 +02:00
}
// FixUp FrameList
2001-04-09 23:27:21 +02:00
DAVE_DBGMSG("Anims %i\n",Spr->FrameCount);
for (i=0; i<Spr->FrameCount; i++)
2001-04-01 09:22:30 +02:00
{
2001-04-09 23:27:21 +02:00
sSpriteFrame *ThisFrame=&Spr->FrameList[i];
ThisFrame->PAKSpr=(u8*) MakePtr(Spr,(int)ThisFrame->PAKSpr);
2001-04-01 09:22:30 +02:00
}
2001-04-09 23:27:21 +02:00
// Store it
int Idx=FindFreeActor();
ASSERT(Idx!=-1);
sActorPool &ThisActor=ActorPool[Idx];
2001-04-02 00:55:15 +02:00
2001-04-09 23:27:21 +02:00
ThisActor.Filename=Filename;
ThisActor.SpriteBank=Spr;
ThisActor.RefCount=1;
ThisActor.Clut=LoadPalette(ThisActor,Idx);
return(Idx);
}
/*****************************************************************************/
#define DYN_PALW 64
#define DYN_PALH 1
2001-04-10 01:29:26 +02:00
#define DYN_PALX 512
2001-04-09 23:27:21 +02:00
#define DYN_PALY 511
u16 CActorPool::LoadPalette(sActorPool &ThisActor,int Idx)
{
RECT R;
2001-04-10 01:29:26 +02:00
int X=Idx&31;
int Y=Idx%31;
2001-04-09 23:27:21 +02:00
R.x=DYN_PALX+(Idx*DYN_PALW);
2001-04-10 01:29:26 +02:00
R.y=DYN_PALY-Y;
2001-04-09 23:27:21 +02:00
R.w=DYN_PALW;
R.h=DYN_PALH;
2001-04-10 01:29:26 +02:00
// DrawSync(0);
2001-04-09 23:27:21 +02:00
LoadImage( &R, (u32*)ThisActor.SpriteBank->Palette);
int Clut=getClut(R.x,R.y);
return(Clut);
}
/*****************************************************************************/
void CActorPool::AddActor(FileEquate Filename)
{
sActorPool *Actor;
int Idx=FindActorInPool(Filename);
if (Idx!=-1) return;
// Load it
LoadActor(Filename);
}
/*****************************************************************************/
CActorGfx *CActorPool::GetActor(FileEquate Filename)
{
CActorGfx *Actor;
int Idx;
// Find Actor in Pool
Idx=FindActorInPool(Filename);
if (Idx==-1) ASSERT(!"Actor Not Loaded");
sActorPool &ThisActor=ActorPool[Idx];
Actor=new ("CActorGfx") CActorGfx;
Actor->SetData(Filename,ThisActor.SpriteBank,ThisActor.Clut);
ThisActor.RefCount++;
return(Actor);
}
/*****************************************************************************/
/*** Dump ********************************************************************/
/*****************************************************************************/
// Dumps all apart from spongeybob
void CActorPool::DumpActors()
{
2001-04-10 01:29:26 +02:00
printf("DumpActors\n");
2001-04-09 23:27:21 +02:00
for (int i=0; i<MAX_ACTORS; i++)
{
if (ActorPool[i].SpriteBank && ActorPool[i].Filename!=ACTORS_SPONGEBOB_SBK)
{
if (ActorPool[i].SpriteBank) MemFree(ActorPool[i].SpriteBank);
ActorPool[i].SpriteBank=0;
}
}
2001-04-10 01:29:26 +02:00
printf("DumpActorsDone\n");
2001-04-09 23:27:21 +02:00
}
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
CActorGfx::CActorGfx()
{
2001-04-01 09:22:30 +02:00
}
/*****************************************************************************/
CActorGfx::~CActorGfx()
{
2001-04-09 23:27:21 +02:00
}
/*****************************************************************************/
void CActorGfx::SetData(FileEquate Filename,sSpriteAnimBank *_SpriteBank,u16 _Clut)
{
SpriteBank=_SpriteBank;
Clut=_Clut;
if (Filename!=ACTORS_SPONGEBOB_SBK)
{
bool SlotStatus=SlotCache.FindSlot(_SpriteBank->MaxW,_SpriteBank->MaxH,TexX,TexY,U,V);
ASSERT(SlotStatus);
}
else
{
TexX=512;
TexY=256;
U=0;
V=0;
}
TPage=getTPage(0,0,TexX,TexY);
}
/*****************************************************************************/
void CActorGfx::ResetCache()
{
RECT R;
SlotCache.Init();
R.x=512;
R.y=256;
R.w=512;
R.h=250;
ClearImage(&R,0,255,0);
2001-04-01 09:22:30 +02:00
}
2001-04-02 20:25:09 +02:00
/*****************************************************************************/
sSpriteFrame *CActorGfx::GetFrame(int Anim,int Frame)
{
sSpriteAnim *ThisAnim=SpriteBank->AnimList+Anim;
u16 ThisFrame=ThisAnim->Anim[Frame];
return(SpriteBank->FrameList+ThisFrame);
}
2001-04-01 09:22:30 +02:00
/*****************************************************************************/
2001-04-09 23:27:21 +02:00
2001-04-17 22:57:00 +02:00
int ActorOT=10;
2001-04-01 23:38:59 +02:00
POLY_FT4 *CActorGfx::Render(DVECTOR &Pos,int Anim,int Frame,bool XFlip,bool YFlip,bool Shadow)
2001-04-01 09:22:30 +02:00
{
2001-04-02 20:25:09 +02:00
sSpriteFrame *FrameGfx=GetFrame(Anim,Frame);
2001-04-09 23:27:21 +02:00
PAK_doUnpak(UnpackBuffer,FrameGfx->PAKSpr);
2001-04-01 22:22:49 +02:00
// Gfx
2001-04-09 23:27:21 +02:00
2001-04-02 00:55:15 +02:00
RECT Rect;
Rect.x=TexX;
Rect.y=TexY;
2001-04-02 20:25:09 +02:00
Rect.w=FrameGfx->W/4;
Rect.h=FrameGfx->H;
2001-04-10 01:29:26 +02:00
2001-04-09 23:27:21 +02:00
LoadImage( &Rect, (u32*)UnpackBuffer);
2001-04-01 22:22:49 +02:00
2001-04-01 23:38:59 +02:00
POLY_FT4 *Ft4=GetPrimFT4();
2001-04-02 20:25:09 +02:00
SetUpFT4(Ft4,FrameGfx,Pos.vx,Pos.vy,XFlip,YFlip);
2001-04-01 23:38:59 +02:00
setRGB0(Ft4,128,128,128);
2001-04-09 23:27:21 +02:00
Ft4->tpage=TPage;
Ft4->clut=Clut;
2001-04-17 22:57:00 +02:00
AddPrimToList(Ft4,ActorOT);
2001-04-01 22:22:49 +02:00
2001-04-01 23:38:59 +02:00
if (Shadow)
{
POLY_FT4 *sFt4=GetPrimFT4();
*sFt4=*Ft4;
sFt4->x0-=ShadowXOfs;
sFt4->x1-=ShadowXOfs;
sFt4->y0+=ShadowYOfs;
sFt4->y1+=ShadowYOfs;
setSemiTrans(sFt4,1);
setRGB0(sFt4,0,0,0);
2001-04-17 22:57:00 +02:00
AddPrimToList(sFt4,ActorOT);
2001-04-01 23:38:59 +02:00
}
return(Ft4);
}
/*****************************************************************************/
2001-04-04 17:54:27 +02:00
void CActorGfx::SetUpFT4(POLY_FT4 *Ft4,sSpriteFrame *Frame,int X,int Y,bool XFlip,bool YFlip)
2001-04-01 23:38:59 +02:00
{
2001-04-09 23:27:21 +02:00
u8 W=Frame->W;
u8 H=Frame->H;
2001-04-01 09:22:30 +02:00
2001-04-01 23:38:59 +02:00
if (XFlip)
2001-04-04 17:54:27 +02:00
{
X-=Frame->XOfs;
X-=W;
2001-04-01 23:38:59 +02:00
Ft4->u0=U+W-1;
2001-04-04 17:54:27 +02:00
Ft4->u1=U;//-1;
2001-04-01 23:38:59 +02:00
Ft4->u2=U+W-1;
2001-04-04 17:54:27 +02:00
Ft4->u3=U;//-1;
}
2001-04-01 23:38:59 +02:00
else
{
2001-04-04 17:54:27 +02:00
X+=Frame->XOfs;
2001-04-01 23:38:59 +02:00
Ft4->u0=U;
Ft4->u1=U+W;
Ft4->u2=U;
Ft4->u3=U+W;
2001-04-04 17:54:27 +02:00
2001-04-01 23:38:59 +02:00
}
if (YFlip)
2001-04-04 17:54:27 +02:00
{
Y-=Frame->YOfs;
Y-=H;
2001-04-01 23:38:59 +02:00
Ft4->v0=V+H-1;
Ft4->v1=V+H-1;
2001-04-04 17:54:27 +02:00
Ft4->v2=V;//-1;
Ft4->v3=V;//-1;
}
2001-04-01 23:38:59 +02:00
else
{
2001-04-04 17:54:27 +02:00
Y+=Frame->YOfs;
2001-04-01 23:38:59 +02:00
Ft4->v0=V;
Ft4->v1=V;
Ft4->v2=V+H;
Ft4->v3=V+H;
}
setXYWH(Ft4,X,Y,W,H);
2001-04-01 09:22:30 +02:00
}