SBSPSS/source/gfx/bubicles.cpp

626 lines
14 KiB
C++
Raw Normal View History

2000-10-18 00:04:57 +02:00
/*=========================================================================
bubicles.cpp
Author: PKG
Created:
Project: Spongebob
Purpose:
Copyright (c) 2000 Climax Development Ltd
===========================================================================*/
/*----------------------------------------------------------------------
Includes
-------- */
#include "bubicles.h"
#ifndef __PRIM_HEADER__
#include "gfx\prim.h"
#endif
#ifndef __MATHTABLE_HEADER__
#include "utils\mathtab.h"
#endif
#ifndef __UTILS_HEADER__
#include "utils\utils.h"
#endif
#ifndef __GFX_SPRBANK_H__
#include "gfx\sprbank.h"
#endif
2000-10-18 20:36:52 +02:00
#ifndef __SYSTEM_GSTATE_H__
#include "system\gstate.h"
#endif
2000-10-18 00:04:57 +02:00
/* Std Lib
------- */
/* Data
---- */
2001-04-20 16:53:35 +02:00
#ifndef __SPR_SPRITES_H__
#include <sprites.h>
2000-10-18 00:04:57 +02:00
#endif
/*----------------------------------------------------------------------
Tyepdefs && Defines
------------------- */
/*----------------------------------------------------------------------
Structure defintions
-------------------- */
/*----------------------------------------------------------------------
Function Prototypes
------------------- */
/*----------------------------------------------------------------------
Vars
---- */
2000-10-18 20:36:52 +02:00
int CBubicleFactory::s_initialised=false;
CBubicleEmitter *CBubicleFactory::s_emitters;
CBubicle *CBubicleFactory::s_bubicles;
2000-11-28 01:06:22 +01:00
2000-10-18 20:36:52 +02:00
SpriteBank *CBubicleFactory::s_sprites;
2000-11-28 01:06:22 +01:00
int CBubicleFactory::s_frameTypeCounter=0;
const int CBubicleFactory::s_frameTabSize=8;
const int CBubicleFactory::s_frameTabSizeMask=0x07;
sFrameHdr *CBubicleFactory::s_frameTab[s_frameTabSize];
const int CBubicleFactory::s_frameTabSrc[s_frameTabSize]=
{
FRM__BUBBLE_1,FRM__BUBBLE_2,FRM__BUBBLE_2,FRM__BUBBLE_1,
FRM__BUBBLE_2,FRM__BUBBLE_1,FRM__BUBBLE_2,FRM__BUBBLE_3,
};
2001-01-23 22:56:51 +01:00
DVECTOR CBubicleFactory::s_mapPositionOffset={0,0};
2000-11-16 18:33:37 +01:00
int s_numLiveBubicleEmitters=0;
int s_numLiveBubicles=0;
2000-10-18 20:36:52 +02:00
2000-10-18 00:04:57 +02:00
2001-01-23 22:56:51 +01:00
2000-10-18 00:04:57 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-10-18 20:36:52 +02:00
void CBubicleEmitter::create()
2000-10-18 00:04:57 +02:00
{
2000-10-18 20:36:52 +02:00
m_active=false;
}
2000-10-18 00:04:57 +02:00
2000-10-18 20:36:52 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicleEmitter::init(BubicleEmitterData *_init)
2000-10-18 00:04:57 +02:00
{
2000-10-18 20:36:52 +02:00
m_data=*_init;
m_frameCount=0;
m_spawnFrameCount=0;
m_active=true;
2000-10-18 00:04:57 +02:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-10-18 20:36:52 +02:00
void CBubicleEmitter::think(int _frames)
2000-10-18 00:04:57 +02:00
{
2000-10-18 20:36:52 +02:00
m_frameCount+=_frames;
m_spawnFrameCount+=_frames;
2000-10-18 00:04:57 +02:00
2000-10-18 20:36:52 +02:00
if(m_spawnFrameCount>=m_data.m_birthRate)
2000-10-18 00:04:57 +02:00
{
2000-10-18 22:00:31 +02:00
m_spawnFrameCount=0;
2000-10-18 20:36:52 +02:00
for(int i=0;i<m_data.m_birthAmount;i++)
2000-10-18 00:04:57 +02:00
{
2000-10-18 20:36:52 +02:00
if(CBubicleFactory::spawnParticle(&m_data)==NULL)
2000-10-18 22:00:31 +02:00
break; // Ran out of bubicles :(
2000-10-18 00:04:57 +02:00
}
}
2000-10-18 20:36:52 +02:00
if(m_data.m_life!=-1&&m_frameCount>m_data.m_life)
2000-11-16 18:33:37 +01:00
{
2000-10-18 20:36:52 +02:00
m_active=false;
2000-11-16 18:33:37 +01:00
s_numLiveBubicleEmitters--;
ASSERT(s_numLiveBubicleEmitters>=0); // Woah, something bad has happened!
}
2000-10-18 00:04:57 +02:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-10-18 20:36:52 +02:00
#ifdef SHOW_BUBICLE_EMITTERS
2000-10-18 00:04:57 +02:00
void CBubicleEmitter::render()
{
2001-01-23 22:56:51 +01:00
DVECTOR mapPos={0,0}
2000-10-18 00:04:57 +02:00
POLY_F4 *f4;
2001-01-23 22:56:51 +01:00
if(m_data.m_applyMapOffset)
{
mapPos=*CBubicleFactory::getMapOffset();
}
2000-10-18 00:04:57 +02:00
f4=GetPrimF4();
setPolyF4(f4);
2001-01-23 22:56:51 +01:00
setXYWH(f4,m_data.m_x-mapPos->vx,m_data.m_y-mapPos->vy,m_data.m_w,m_data.m_h);
2000-10-18 00:04:57 +02:00
setSemiTrans(f4,1);
setShadeTex(f4,0);
setRGB0(f4,30,40,50);
AddPrimToList(f4,m_data.m_bubicleBase.m_ot+1);
}
2000-10-18 20:36:52 +02:00
#endif
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicleEmitter::kill()
{
ASSERT(m_active);
2001-01-12 21:38:11 +01:00
s_numLiveBubicleEmitters--;
2000-10-18 20:36:52 +02:00
m_active=false;
}
2000-10-18 00:04:57 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicleEmitter::setPos(int _x,int _y)
{
2000-10-18 20:36:52 +02:00
ASSERT(m_active);
2000-10-18 00:04:57 +02:00
m_data.m_x=_x;
m_data.m_y=_y;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicleEmitter::setSize(int _w,int _h)
{
2000-10-18 20:36:52 +02:00
ASSERT(m_active);
2000-10-18 00:04:57 +02:00
m_data.m_w=_w;
m_data.m_h=_h;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-10-18 20:36:52 +02:00
void CBubicle::create()
2000-10-18 00:04:57 +02:00
{
2000-10-18 20:36:52 +02:00
m_active=false;
2000-10-18 00:04:57 +02:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2001-01-23 22:56:51 +01:00
void CBubicle::init(BubicleData *_init,int _x,int _y,int _applyMapOffset)
2000-10-18 00:04:57 +02:00
{
m_data=*_init;
m_x=(_x-(m_data.m_w>>1))<<ACCURACY_SHIFT;
m_y=(_y-(m_data.m_h>>1))<<ACCURACY_SHIFT;
m_data.m_vxmax;
m_data.m_vymax;
m_data.m_w<<=ACCURACY_SHIFT;
m_data.m_h<<=ACCURACY_SHIFT;
m_typeSizeChange=getRnd()&1;
m_vSizeChange=0;
m_frameCount=0;
2001-01-23 22:56:51 +01:00
m_applyMapOffset=_applyMapOffset;
2000-10-18 00:04:57 +02:00
2000-11-28 01:06:22 +01:00
m_fhBub=CBubicleFactory::getBubbleFrameHeader();
2000-10-18 20:36:52 +02:00
m_active=true;
2000-10-18 00:04:57 +02:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-10-18 20:36:52 +02:00
void CBubicle::think(int _frames)
2000-10-18 00:04:57 +02:00
{
2000-10-18 20:36:52 +02:00
ASSERT(isActive());
for(int i=0;i<_frames;i++) // Good or bad way to do this? Alternative is a bunch of multiplies..
2000-10-18 00:04:57 +02:00
{
if(m_x!=m_data.m_vxmax)
{
m_x+=m_data.m_vx;
if(m_data.m_vxmax<0)
{
m_data.m_vx+=m_data.m_vdx;
if(m_data.m_vx<m_data.m_vxmax)
m_data.m_vx=m_data.m_vxmax;
}
else if(m_data.m_vxmax>0)
{
m_data.m_vx+=m_data.m_vdx;
if(m_data.m_vx>m_data.m_vxmax)
m_data.m_vx=m_data.m_vxmax;
}
}
if(m_y!=m_data.m_vymax)
{
m_y+=m_data.m_vy;
if(m_data.m_vymax<0)
{
m_data.m_vy+=m_data.m_vdy;
if(m_data.m_vy<m_data.m_vymax)
m_data.m_vy=m_data.m_vymax;
}
else if(m_data.m_vymax>0)
{
m_data.m_vy+=m_data.m_vdy;
if(m_data.m_vy>m_data.m_vymax)
m_data.m_vy=m_data.m_vymax;
}
}
2000-10-18 20:36:52 +02:00
2000-10-18 00:04:57 +02:00
m_data.m_theta=(m_data.m_theta+m_data.m_vtheta)&4095;
2000-10-18 20:36:52 +02:00
2000-10-18 00:04:57 +02:00
m_data.m_wobbleWidth+=m_data.m_vwobbleWidth;
m_data.m_vwobbleWidth+=m_data.m_vdwobbleWidth;
if(m_typeSizeChange)
{
if(m_data.m_w<m_data.m_h)
m_vSizeChange+=m_data.m_dvSizeChange;
else if(m_data.m_w>m_data.m_h)
m_vSizeChange-=m_data.m_dvSizeChange;
m_data.m_w+=m_vSizeChange;
}
else
{
if(m_data.m_h<m_data.m_w)
m_vSizeChange+=m_data.m_dvSizeChange;
else if(m_data.m_h>m_data.m_w)
m_vSizeChange-=m_data.m_dvSizeChange;
m_data.m_h+=m_vSizeChange;
}
}
2000-10-18 20:36:52 +02:00
m_frameCount+=_frames;
2000-10-18 00:04:57 +02:00
2000-10-18 20:36:52 +02:00
if(m_frameCount>m_data.m_life)
2000-11-16 18:33:37 +01:00
{
2000-10-18 20:36:52 +02:00
m_active=false;
2000-11-16 18:33:37 +01:00
s_numLiveBubicles--;
ASSERT(s_numLiveBubicles>=0); // Woah, something bad has happened!
}
2000-10-18 20:36:52 +02:00
}
2000-10-18 00:04:57 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicle::render()
{
2000-10-18 20:36:52 +02:00
ASSERT(isActive());
2001-01-23 22:56:51 +01:00
DVECTOR mapPos={0,0};
2000-10-18 20:36:52 +02:00
POLY_FT4 *ft4;
int x,y,w,h;
2001-01-23 22:56:51 +01:00
if(m_applyMapOffset)
{
mapPos=*CBubicleFactory::getMapOffset();
}
x=(m_x>>ACCURACY_SHIFT)-mapPos.vx;
y=(m_y>>ACCURACY_SHIFT)-mapPos.vy;
2000-10-18 20:36:52 +02:00
w=m_data.m_w>>ACCURACY_SHIFT;
h=m_data.m_h>>ACCURACY_SHIFT;
x+=(msin(m_data.m_theta)*(m_data.m_wobbleWidth>>ACCURACY_SHIFT))>>12;
ft4=CBubicleFactory::getSprites()->printFT4(m_fhBub,0,0,0,0,m_data.m_ot);
setXYWH(ft4,x,y,w,h);
setSemiTrans(ft4,1);
setRGB0(ft4,m_data.m_colour.m_r,m_data.m_colour.m_g,m_data.m_colour.m_b);
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicleFactory::init()
{
int i;
CBubicleEmitter *emt;
CBubicle *bub;
s_sprites=new ("Bubble Sprites") SpriteBank();
2001-04-20 16:53:35 +02:00
s_sprites->load(SPRITES_SPRITES_SPR);
2000-10-18 20:36:52 +02:00
2000-11-28 01:06:22 +01:00
for(i=0;i<s_frameTabSize;i++)
{
s_frameTab[i]=s_sprites->getFrameHeader(s_frameTabSrc[i]);
}
2000-10-18 20:36:52 +02:00
emt=s_emitters=(CBubicleEmitter*)MemAlloc(sizeof(CBubicleEmitter)*NUM_EMITTERS,"BubicleEmitters");
for(i=0;i<NUM_EMITTERS;i++,emt++)
emt->create();
bub=s_bubicles=(CBubicle*)MemAlloc(sizeof(CBubicle)*NUM_BUBICLES,"Bubicles");
for(i=0;i<NUM_BUBICLES;i++,bub++)
bub->create();
SYSTEM_DBGMSG("CBubicleFactory::init ( allocated %d bytes )",(sizeof(CBubicleEmitter)*NUM_EMITTERS)+(sizeof(CBubicle)*NUM_BUBICLES));
s_initialised=true;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicleFactory::shutdown()
{
ASSERT(s_initialised);
MemFree(s_emitters);
MemFree(s_bubicles);
s_sprites->dump(); delete s_sprites;
s_initialised=false;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-11-15 16:37:12 +01:00
void CBubicleFactory::think(int _frames)
2000-10-18 20:36:52 +02:00
{
ASSERT(s_initialised);
int frames;
2000-11-16 18:33:37 +01:00
int i,count;
2000-10-18 20:36:52 +02:00
CBubicleEmitter *emt;
CBubicle *bub;
emt=s_emitters;
2000-11-16 18:33:37 +01:00
count=s_numLiveBubicleEmitters;
for(i=0;i<count;i++)
2000-10-18 20:36:52 +02:00
{
2000-11-16 18:33:37 +01:00
while(!emt->isActive())
{
emt++;
}
emt->think(_frames);
emt++;
2000-10-18 20:36:52 +02:00
}
2000-11-16 18:33:37 +01:00
2000-10-18 20:36:52 +02:00
bub=s_bubicles;
2000-11-16 18:33:37 +01:00
count=s_numLiveBubicles;
for(i=0;i<count;i++)
2000-10-18 00:04:57 +02:00
{
2000-11-16 18:33:37 +01:00
while(!bub->isActive())
{
bub++;
}
bub->think(_frames);
bub++;
2000-10-18 20:36:52 +02:00
}
}
2000-10-18 00:04:57 +02:00
2000-10-18 20:36:52 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicleFactory::render()
{
ASSERT(s_initialised);
2000-10-18 00:04:57 +02:00
2000-10-18 20:36:52 +02:00
int i;
CBubicleEmitter *emt;
CBubicle *bub;
2000-10-18 00:04:57 +02:00
2000-10-18 20:36:52 +02:00
#ifdef SHOW_BUBICLE_EMITTERS
emt=s_emitters;
for(i=0;i<NUM_EMITTERS;i++,emt++)
{
if(emt->isActive())
emt->render();
}
#endif
bub=s_bubicles;
for(i=0;i<NUM_BUBICLES;i++,bub++)
{
if(bub->isActive())
bub->render();
2000-10-18 00:04:57 +02:00
}
}
2000-10-18 20:36:52 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
CBubicleEmitter *CBubicleFactory::spawnEmitter(BubicleEmitterData *_init)
{
ASSERT(s_initialised);
int i;
CBubicleEmitter *emt;
emt=s_emitters;
for(i=0;i<NUM_EMITTERS;i++,emt++)
{
if(!emt->isActive())
{
emt->init(_init);
2000-11-16 18:33:37 +01:00
s_numLiveBubicleEmitters++;
2000-10-18 20:36:52 +02:00
return emt;
}
}
SYSTEM_DBGMSG("Out of CBubicleEmitters!");
return NULL;
}
2000-10-18 00:04:57 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-10-18 20:36:52 +02:00
CBubicle *CBubicleFactory::spawnParticle(BubicleEmitterData *_init)
2000-10-18 00:04:57 +02:00
{
2000-10-18 20:36:52 +02:00
ASSERT(s_initialised);
int i;
CBubicle *bub;
bub=s_bubicles;
for(i=0;i<NUM_BUBICLES;i++,bub++)
{
if(!bub->isActive())
{
BubicleData newBubData;
int x,y;
x=_init->m_x+getRndRange(_init->m_w);
y=_init->m_y+getRndRange(_init->m_h);
newBubData.m_life=_init->m_bubicleBase.m_life+getRndRange(_init->m_bubicleRange.m_life);
newBubData.m_vx=_init->m_bubicleBase.m_vx+getRndRange(_init->m_bubicleRange.m_vx);
newBubData.m_vdx=_init->m_bubicleBase.m_vdx+getRndRange(_init->m_bubicleRange.m_vdx);
newBubData.m_vxmax=_init->m_bubicleBase.m_vxmax+getRndRange(_init->m_bubicleRange.m_vxmax);
newBubData.m_vy=_init->m_bubicleBase.m_vy+getRndRange(_init->m_bubicleRange.m_vy);
newBubData.m_vdy=_init->m_bubicleBase.m_vdy+getRndRange(_init->m_bubicleRange.m_vdy);
newBubData.m_vymax=_init->m_bubicleBase.m_vymax+getRndRange(_init->m_bubicleRange.m_vymax);
newBubData.m_w=_init->m_bubicleBase.m_w+getRndRange(_init->m_bubicleRange.m_w);
newBubData.m_h=_init->m_bubicleBase.m_h+getRndRange(_init->m_bubicleRange.m_h);
newBubData.m_dvSizeChange=_init->m_bubicleBase.m_dvSizeChange+getRndRange(_init->m_bubicleRange.m_dvSizeChange);
newBubData.m_theta=_init->m_bubicleBase.m_theta+getRndRange(_init->m_bubicleRange.m_theta);
newBubData.m_vtheta=_init->m_bubicleBase.m_vtheta+getRndRange(_init->m_bubicleRange.m_vtheta);
newBubData.m_wobbleWidth=_init->m_bubicleBase.m_wobbleWidth+getRndRange(_init->m_bubicleRange.m_wobbleWidth);
newBubData.m_vwobbleWidth=_init->m_bubicleBase.m_vwobbleWidth+getRndRange(_init->m_bubicleRange.m_vwobbleWidth);
newBubData.m_vdwobbleWidth=_init->m_bubicleBase.m_vdwobbleWidth+getRndRange(_init->m_bubicleRange.m_vdwobbleWidth);
newBubData.m_ot=_init->m_bubicleBase.m_ot+getRndRange(_init->m_bubicleRange.m_ot);
newBubData.m_colour.m_r=_init->m_bubicleBase.m_colour.m_r+getRndRange(_init->m_bubicleRange.m_colour.m_r);
newBubData.m_colour.m_g=_init->m_bubicleBase.m_colour.m_r+getRndRange(_init->m_bubicleRange.m_colour.m_g);
newBubData.m_colour.m_b=_init->m_bubicleBase.m_colour.m_r+getRndRange(_init->m_bubicleRange.m_colour.m_b);
2001-01-23 22:56:51 +01:00
bub->init(&newBubData,x,y,_init->m_applyMapOffset);
2000-11-16 18:33:37 +01:00
s_numLiveBubicles++;
2000-10-18 20:36:52 +02:00
return bub;
}
}
2000-12-20 23:46:12 +01:00
// SYSTEM_DBGMSG("Out of CBubicles!");
2000-10-18 20:36:52 +02:00
return NULL;
2000-10-18 00:04:57 +02:00
}
2000-11-28 01:06:22 +01:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
sFrameHdr *CBubicleFactory::getBubbleFrameHeader()
{
s_frameTypeCounter=(s_frameTypeCounter+1)&s_frameTabSizeMask;
return s_frameTab[s_frameTypeCounter];
}
2001-01-23 22:56:51 +01:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void CBubicleFactory::setMapOffset(DVECTOR *_offset)
{
s_mapPositionOffset=*_offset;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
const DVECTOR *CBubicleFactory::getMapOffset()
{
return &s_mapPositionOffset;
}
2000-10-18 00:04:57 +02:00
/*===========================================================================
end */