/* * Animated Sprites Lib * * (C)Copyright 1998 J. Tann * All Rights Reserved */ #include //---------------------------------------------------------------------- // Macros and defined values //---------------------------------------------------------------------- // TRUE and FALSE #define FALSE (0) #define TRUE (!FALSE) // Sprite structure bit flags #define USED_SPR (0x0001) // Indicates that the structure entry is used #define DISPLAY_SPR (0x0002) // Indicates that the sprite is to be displayed #define LOOP_ANI (0x0004) // Indicates the animation is to be looped when it reaches the end #define BOUNCE_ANI (0x0008) // Indicates the loop should play backwards before going forwards again #define ANIMATE_SPR (0x0010) // Indicates the sprite animation state // Animation control values for AsControl() #define ANI_START 1 // Starts animation playback of the sprite #define ANI_STOP 2 // Stops the animation playback and sets the displayed frame as the first frame #define ANI_PAUSE 3 // Stops playback at the current frame // AsMove() values #define MOVE_ABSOLUTE 0 // Moves a sprite directly to a location on the screen #define MOVE_RELATIVE !MOVE_ABSOLUTE // Moves a sprite relative to it's current location // AsShow() values #define HIDE_SPRITE 0 // Hides a sprite #define SHOW_SPRITE !HIDE_SPRITE // Shows a sprite (These are basically FALSE and TRUE respectively!!) // AsAdd() loop values #define ANI_ONCE 1 // Play animation just once #define ANI_LOOP 2 // Loop the animation by going straight to the first frame when the last frame is reached #define ANI_BOUNCE 3 // Loop the animation by playing backwards when last frame reached. Then continue forwards when first reached! //---------------------------------------------------------------------- // Variables //---------------------------------------------------------------------- typedef struct { GsSPRITE spr; // PSX sprite structure int delay; // Timer delay for animating the sprite int delayValue; // Value for reseting the delay timer int aniDir; // Direction of animation int frames; // Number of frames int curFrame; // Current frame being displayed int x1, y1; // Coords of the first frame in the tpage int flags; // Bit flags for the sprite } ANISPRITE; ANISPRITE *sprites; // A pointer to the sprite structures' memory int functionsEnabled = FALSE; // A flag to enable all the main functions if init was successful int maxSpriteNum; // The value for the last sprite so AsAdd wont add too many! //---------------------------------------------------------------------- // Function templates //---------------------------------------------------------------------- int AsInit(int); // Initialise function void AsFree(void); // Free sprite memory int AsCreate(GsIMAGE, u_short, int, int, int, int, int, int, int); // Create a sprite void AsDelete(int); // Delete a sprite void AsMove(int, int, int, int); // Move a sprite void AsShow(int, int); // Shows or hides a sprite int AsControl(int, int); // Controls the animation 'playback' void AsUpdate(GsOT *); // Update all active sprites //---------------------------------------------------------------------- // Main functions //---------------------------------------------------------------------- //---------------------------------------------------------------------- // AsInit() // // Initialises the sprite structures in memory and enables all // ani-sprite functions. // // Parameters: // maxSprites The maximum number of sprites to be used at any one time. // // Return: The function returns a zero for failure (FALSE) // int AsInit(int maxSprites) { if(functionsEnabled) return FALSE; // Only use this function if it has not been used or AsFree() has been used! sprites = (ANISPRITE *)malloc(sizeof(ANISPRITE) * maxSprites); if(!sprites) return FALSE; functionsEnabled = TRUE; maxSpriteNum = maxSprites; return TRUE; } //---------------------------------------------------------------------- // AsFree() // // Frees memory allocated for the sprite structures. // // Parameters: None // // Return: None // void AsFree(void) { if(!functionsEnabled) return; // Only use this function if AsInit was called! free(sprites); functionsEnabled = FALSE; } //---------------------------------------------------------------------- // AsCreate() // // Creates a new sprite. // // Parameters: // tim Sprite's frame images // tpage Sprite's texture page // x X coord of the first frame in the tpage // y Y coord of the first frame // w Width of each frame // h Height of each frame // frames Number of frames // delay VSYNC delay for each frame displayed. 0 means each frame is displayed each VSYNC (or each call to AsUpdate()!) // loop Use ANI_ONCE to play through just once, ANI_LOOP to loop the animation (default) and ANI_BOUNCE to loop back and forth. // // Return: Returns the sprite number or -1 for unsuccessful or if AsInit() was not called. // int AsCreate(GsIMAGE tim, u_short tpage, int x, int y, int w, int h, int frames, int delay, int loop) { int i; if(!functionsEnabled) return -1; // Only use this function if AsInit was called! for(i=0; i