SBSPSS/source/level/layertile.cpp

179 lines
4.3 KiB
C++
Raw Normal View History

2000-12-07 02:15:53 +01:00
/************************/
/*** Tile Layer Class ***/
/************************/
#include "system\global.h"
#include <DStructs.h>
2000-12-07 16:56:20 +01:00
#include "utils\utils.h"
2000-12-09 18:22:06 +01:00
#include "system\vid.h"
2000-12-07 16:56:20 +01:00
#include "gfx\prim.h"
2000-12-07 02:15:53 +01:00
#include "LayerTile.h"
2001-01-03 23:12:25 +01:00
const u32 XInc=16<<0;
const u32 YInc=16<<16;
/*****************************************************************************/
2001-07-09 22:02:33 +02:00
static const int TILE2D_WIDTH=16;
static const int TILE2D_HEIGHT=12;
static const int SCREEN_TILE2D_WIDTH=((512/TILE2D_WIDTH)+1);
static const int SCREEN_TILE2D_HEIGHT=((256/TILE2D_HEIGHT)+1);
2001-08-13 23:28:35 +02:00
static const int PrimCount=SCREEN_TILE2D_WIDTH*SCREEN_TILE2D_HEIGHT;
static const int PrimMemSize=PrimCount*sizeof(TSPRT);
2000-12-12 21:56:51 +01:00
2000-12-07 16:56:20 +01:00
/*****************************************************************************/
2000-12-07 02:15:53 +01:00
/*****************************************************************************/
/*****************************************************************************/
2001-07-09 22:02:33 +02:00
2001-04-19 17:12:21 +02:00
CLayerTile::CLayerTile(sLevelHdr *LevelHdr,sLayerHdr *Hdr)
2000-12-07 16:56:20 +01:00
{
2001-01-03 23:12:25 +01:00
LayerHdr=Hdr;
2001-02-20 16:56:16 +01:00
MapWidth=LayerHdr->Width;
MapHeight=LayerHdr->Height;
2001-06-28 19:48:30 +02:00
ElemBank2d=LevelHdr->ElemBank2d;
2001-01-03 23:12:25 +01:00
Map=(sTileMapElem*)MakePtr(Hdr,sizeof(sLayerHdr));
2001-07-09 22:02:33 +02:00
2001-08-13 23:28:35 +02:00
PrimBankID=0;
2001-08-17 18:49:26 +02:00
PrimBank[0]=0; PrimBank[1]=0;
2001-07-09 22:02:33 +02:00
2001-08-17 18:49:26 +02:00
if (Hdr->SubType!=LAYER_TILE_TYPE_MID) return;
//-----------------
// anything below here is Mid layer only - how did I miss something like this, 104k WASTED!!
// Create Mid Tile Prim Banks
2001-08-13 23:28:35 +02:00
for (int b=0; b<2; b++)
2001-07-09 22:02:33 +02:00
{
2001-08-13 23:28:35 +02:00
PrimBank[b]=(TSPRT*)MemAlloc(PrimMemSize,"Mid Polyz");
TSPRT *PrimPtr=PrimBank[b];
for (int i=0; i<PrimCount; i++)
{
setTSprt(PrimPtr);
setTSetShadeTex(PrimPtr,1);
PrimPtr->w=TILE2D_WIDTH;
PrimPtr->h=TILE2D_HEIGHT;
PrimPtr++;
}
2001-07-09 22:02:33 +02:00
}
2001-08-17 18:49:26 +02:00
// precalc Mid tile offsets
sTileMapElem *MapPtr=Map;
for (int Y=0; Y<MapHeight; Y++)
{
for (int X=0; X<MapWidth; X++)
{
MapPtr->Tile*=sizeof(sElem2d);
MapPtr++;
}
}
2000-12-07 16:56:20 +01:00
}
/*****************************************************************************/
CLayerTile::~CLayerTile()
{
2001-08-13 23:28:35 +02:00
for (int b=0; b<2; b++)
{
2001-08-17 18:49:26 +02:00
if (PrimBank[b]) MemFree(PrimBank[b]);
2001-08-13 23:28:35 +02:00
}
2000-12-12 21:56:51 +01:00
}
/*****************************************************************************/
/*****************************************************************************/
2000-12-07 02:15:53 +01:00
/*****************************************************************************/
2001-02-22 15:55:13 +01:00
void CLayerTile::init(DVECTOR &MapPos,int Shift)
2000-12-07 02:15:53 +01:00
{
2000-12-14 17:38:34 +01:00
MapXYShift=Shift;
2001-02-22 15:55:13 +01:00
MapXY=MapPos;
2000-12-07 02:15:53 +01:00
}
/*****************************************************************************/
2000-12-12 21:56:51 +01:00
void CLayerTile::shutdown()
{
}
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
2001-01-09 21:38:20 +01:00
void CLayerTile::think(DVECTOR &MapPos)
2000-12-12 21:56:51 +01:00
{
2000-12-14 17:38:34 +01:00
int XPos=MapPos.vx>>MapXYShift;
int YPos=MapPos.vy>>MapXYShift;
2001-02-22 15:55:13 +01:00
MapXY.vx=XPos>>4;
2001-07-09 22:02:33 +02:00
MapXY.vy=YPos/TILE2D_HEIGHT;
2001-04-19 01:04:03 +02:00
2001-01-03 23:12:25 +01:00
ShiftX=XPos & 15;
2001-07-09 22:02:33 +02:00
ShiftY=YPos%TILE2D_HEIGHT;
2001-01-03 23:12:25 +01:00
2001-07-18 18:45:39 +02:00
if (MapXY.vx<0)
{
MapXY.vx=0;
ShiftX=0;
}
if (MapXY.vy<0)
{
MapXY.vy=0;
ShiftY=0;
}
2001-05-09 23:56:48 +02:00
if (MapXY.vx+SCREEN_TILE2D_WIDTH<=MapWidth)
RenderW=SCREEN_TILE2D_WIDTH;
2000-12-12 21:56:51 +01:00
else
2001-02-22 15:55:13 +01:00
RenderW=MapWidth-MapXY.vx;
2000-12-12 21:56:51 +01:00
2001-05-09 23:56:48 +02:00
if (MapXY.vy+SCREEN_TILE2D_HEIGHT<=MapHeight)
RenderH=SCREEN_TILE2D_HEIGHT;
2000-12-12 21:56:51 +01:00
else
2001-02-22 15:55:13 +01:00
RenderH=MapHeight-MapXY.vy;
2000-12-12 21:56:51 +01:00
}
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
2001-04-17 22:09:03 +02:00
void CLayerTile::render()
{
2001-07-28 18:45:46 +02:00
sTileMapElem *MapPtr=Map+GetMapOfs();
2001-04-17 22:09:03 +02:00
s16 TileX,TileY;
sOT *ThisOT=OtPtr+LayerOT;
2001-08-13 23:28:35 +02:00
TSPRT *PrimPtr=PrimBank[PrimBankID];
2001-08-17 18:49:26 +02:00
u8 *TileBank=(u8*)ElemBank2d;
u32 T0,T1;
2001-08-13 23:28:35 +02:00
PrimBankID^=1;
2001-04-17 22:09:03 +02:00
// Setup shift bits of pos
TileY=-ShiftY;
// Render it!!
2001-07-09 22:02:33 +02:00
2001-04-17 22:09:03 +02:00
for (int Y=0; Y<RenderH; Y++)
{
sTileMapElem *MapRow=MapPtr;
TileX=-ShiftX;
for (int X=0; X<RenderW; X++)
{
2001-04-19 01:04:03 +02:00
int ThisTile=MapRow->Tile;
2001-04-17 22:09:03 +02:00
if (ThisTile)
{
2001-08-17 18:49:26 +02:00
sElem2d *Tile=(sElem2d*)(TileBank+ThisTile);
2001-07-09 22:02:33 +02:00
PrimPtr->x0=TileX;
PrimPtr->y0=TileY;
2001-08-17 18:49:26 +02:00
T0=Tile->TPage;
T1=*(u32*)&Tile->u0;
PrimPtr->t_code=T0; // pregen'd setTSprtTPage(PrimPtr,Tile->TPage);
*(u32*)&PrimPtr->u0=T1; // copy uv AND clut
2001-07-09 22:02:33 +02:00
addPrim(ThisOT,PrimPtr);
PrimPtr++;
2001-04-17 22:09:03 +02:00
}
2001-08-13 23:28:35 +02:00
MapRow++;
2001-05-09 23:56:48 +02:00
TileX+=TILE2D_WIDTH;
2001-04-17 22:09:03 +02:00
}
MapPtr+=MapWidth;
2001-05-09 23:56:48 +02:00
TileY+=TILE2D_HEIGHT;
2001-04-17 22:09:03 +02:00
}
}