/* ** File : sprite.c ** Author: D Smethurst ** Desc. : utilities to manipulate the sprite entries in a list ** Version: 0.1 */ #include #include #include #include #include "sprite.h" int mystrerror = -1; /* Error list: strings pointed to by mystrerror when an error occurs in the lib */ char *errors[] = { "Tried to create sprite with too many frames.", "No memory left." }; /* SprCreate input : name - path to sprite output: pointer to sprite or null on failure */ lpSprite SprCreate( u_long addr ) { /* New sprite */ lpSprite lnew; /* Allocate sprite */ lnew = (lpSprite)malloc( sizeof( Sprite ) ); if( lnew == NULL ) { mystrerror = SPR_NO_MEM; return NULL; } /* Duplicate sprite name */ lnew->frames[0] = addr; lnew->curframe = lnew->frames[0]; /* Number of frames */ lnew->n = 1; InitSprite( lnew ); /* Return the sprite */ return lnew; } /* SprCreateN input : name - array of paths to files n - number of paths in names output: newly created sprite pointer or NULL if failed */ lpSprite SprCreateN( u_long *addrs, int n ) { /* New sprite */ lpSprite lnew; /* counter */ int i; /* Asserts only in debug */ assert( n < MAX_FRAMES ); /* check that we aren't trying to allocate more sprite files than is allowed */ if( n > MAX_FRAMES ) { mystrerror = SPR_TOO_MANY_FRAMES; return NULL; } /* Allocate space for sprite */ lnew = (lpSprite)malloc( sizeof( Sprite ) ); if( lnew == NULL ) { mystrerror = SPR_NO_MEM; return NULL; } /* Count through the files and allocate */ for( i = 0; i < n ; i++ ) { lnew->frames[i] = addrs[i]; } lnew->curframe = lnew->frames[0]; /* setup the number of frames we have */ lnew->n = n; InitSprite( lnew ); /* return the sprite */ return lnew; } /* SprChangeFrame: input : s - sprite to change frame off n - frame to change to output: true if successful, false otherwise */ BOOL SprChangeFrame( lpSprite s, int n ) { /* Only asserts in debug */ assert( (n > s->n || n < 0 ) ); /* check n is in range */ if( n > s->n || n < 0 ) { s->curframe = s->frames[n]; InitSprite( s ); return TRUE; } return FALSE; } /* SpritePrintError: Print out an error that occured no input or output */ void SpritePrintError( void ) { if( mystrerror != -1 ) printf( "Error: %s\n", errors[ mystrerror ] ); mystrerror = -1; } /* ** InitSprite: ** Input : s - sprite to initialise ** Output: none */ void InitSprite( lpSprite s ) { RECT rect; GsIMAGE timData; short tClut; /* get the sprite info */ GsGetTimInfo( (u_long *)( s->curframe+4 ), &timData ); /* Fill in info to load sprite to frame buffer */ rect.x = timData.px; rect.y = timData.py; rect.w = timData.pw; rect.h = timData.ph; LoadImage( &rect, timData.pixel ); /* Fill in info to load clut to frame buffer */ rect.x = timData.cx; rect.y = timData.cy; rect.w = timData.cw; rect.h = timData.ch; LoadImage( &rect, timData.clut ); /* Fill out info in the sprite structure and return */ if( timData.pmode & 0x01 ) { s->spr.attribute = (1<<24); s->spr.w = timData.pw*2; tClut = 1; } else { s->spr.attribute = 0; s->spr.w = timData.pw; tClut = 0; } s->w = s->spr.w; s->h = s->spr.h = timData.ph; s->spr.tpage = GetTPage( tClut, 0, timData.px, timData.py ); s->spr.u = 0; s->spr.v = 0; s->spr.cx = timData.cx; s->spr.cy = timData.cy; s->spr.r = 128; s->spr.g = 128; s->spr.b = 128; s->spr.mx = s->spr.w/2; s->spr.my = s->spr.h/2; s->spr.scalex = ONE; s->spr.scaley = ONE; s->spr.rotate = 0; /* ** Dont do a drawsync - assume it will complete before we draw again ** i.e. do a drawsync before we draw again. I do this as it _should_ ** save time - Dave. ** Uncomment it if you think you will have problems. */ DrawSync( 0 ); } /* ** SprAttachCallBack ** Input - spr - sprite to attach call back to ** cback - call back to attach ** nTicks - number of ticks between updates ** Ouput - none */ void SprAttachCallBack( lpSprite spr, VPSP cback, short nTicks ) { spr->uTicks = nTicks; spr->update = cback; }