// Header file for my PSX sprite implementation // Coded by Scott Evans 11/5/97 // Last modified 14/5/97 #ifndef _MYSPR_H #define _MYSPR_H #include #include // Sprite header file structure typedef struct { _word nspr; // Number of sprites in file _word npal; // Number of palettes in file _long paloff; // Offset in bytes from start of file to CLUT data _long pblkoff; // Offset to 1st sprite parameter block }SprHeader; // Sprite parameter block structure typedef struct { _byte w,h; // Width and height of sprite in pixels _word CLUTid; // CLUT number used by this sprite _long imgoff; // Offset (from start of parameter block) to sprite image data }SprPblk; // Sizes of above structures #define _SprHeaderSize (sizeof(SprHeader)) #define _SprPblkSize (sizeof(SprPblk)) #define _4bitCLUTSIZE 32 // Gives offsets into texture page for correct sprite frame typedef struct { _byte u,v; // texture page offsets in pixels _word pad; // padding }SprImgOff; // Structure whilch gives various info. about sprite typedef struct { _byte *animstr; // Animation string, list of frames to cycle through _byte maxfrms; // Maximum frames in animation string _byte currfrm; // Current frame of animation _byte on; // Is sprite active _byte animon; // Activate animation _byte dying; // Sprite is being killed __byte xspd; // Horizontal speed of sprite __byte yspd; // Vertical speed of sprite GsSPRITE *spr; }SprINFO; #define ON 1 #define OFF 0 // Macro to set members of a SprINFO structure #define _SetSprINFO(S,A,M,C,O,N,K,X,Y,P) \ (S)->animstr=(A),(S)->maxfrms=(M),(S)->currfrm=(C),(S)->on=(O),\ (S)->animon=(N),(S)->dying=(K),(S)->xspd=(X),(S)->yspd=(Y),(S)->spr=(P) // Find number of sprites // H is pointer to a SprHeader structure #define _GetNoSpr(H) (H->nspr) // Find address of 1st CLUT #define _GetCLUT(H) ((_byte *)H+H->paloff) // Find address of 1st parameter block #define _GetSprPblks(H) ((SprPblk *)((_byte *)H+H->pblkoff)) // Find address of nth parameter block #define _GetSprPblk(H,N) (_GetSprPblks(H)+(N)) // Find sprite width and height #define _GetSprW(P) (P->w) #define _GetSprh(P) (P->h) // Find address of sprites image data #define _GetSprImgData(P) ((_byte *)P+P->imgoff) #define _GetSprCLUTy(H,N,Y) (_GetSprPblk(H,N)->CLUTid+Y) // Load palettes from a sprite file into frame buffer // Parameters : address of sprite file, location of 1st CLUT in frame buffer void Load4bitCLUT(SprHeader *hdr,_word x,_word y) { _byte i; RECT r; setRECT(&r,x,y,16,1); for(i=0;inpal;i++) { LoadImage(&r,(_long *)((_byte *)_GetCLUT(hdr)+_4bitCLUTSIZE*i)); r.y++; } } // Load sprites into a texture page // Parameters : address of paremeter blocks, number of sprites, texture page coordinates, // buffer to store tpage offsets for each image // Returns : number of sprites actually loaded _word LoadSprites(SprPblk *pblk,_word n,_word *tx,_word *ty,SprImgOff *frames) { static _word x=0,y=0; _word i; _byte sprw,sprh; static _byte maxh=0; RECT r; // Make sure texture page is on correct boundary *tx=(*tx>>5)<<5; *ty=(*ty>>7)<<7; // Try and load n sprites into frame buffer for(i=0;iw; maxh=((sprh=pblk->h)>maxh ? sprh : maxh); // Fit in as many sprites per row as possible if(!(x+sprw>TPWDTH ? (y+maxh>TPHGHT ? 0 : (x=0,y+=maxh)) : 1)) { maxh=x=y=0; break; } // Store offsets frames->u=x; frames->v=y; // Image data to frame buffer setRECT(&r,*tx+(x>>2),*ty+y,sprw>>2,sprh); x+=sprw; LoadImage(&r,(_long *)_GetSprImgData(pblk)); } // Return number of sprites loaded into texture page return(i); } // Animate a sprite // Parameters : sprite information, texture page offsets, animation speed void AnimateSprite(SprINFO *info,SprImgOff *offsets,_byte speed) { if(!(TIMER%speed)) { // Cycle through frames in animation string info->currfrm++; info->currfrm%=info->maxfrms; // Change frame of sprite info->spr->u=offsets[info->animstr[info->currfrm]].u; info->spr->v=offsets[info->animstr[info->currfrm]].v; } } #endif