/* ** File : Prog2.c ** Author : D Smethurst ** Description: second demo program for the SprLib library. ** Note that the second sprite is jerky I've done ** this to show off the update loop and how it works. ** Version: v1.0 (SprLib v0.2) */ /* ** System headers */ #include #include /* ** Include my headers */ #include "sprite.h" #include "pad.h" #include "screen.h" #include "tims.h" /* ** Local defines */ #define OTABLE_LENGTH (2) #define MAX_PACKETS (2048*24) /* ** Ordering tables */ static GsOT OTHeader[2]; static GsOT_TAG OTArray[2][1<spr.x = SCREEN_W/2; tmpSpr->spr.y = SCREEN_H/2; tmpSpr->x = tmpSpr->spr.x; tmpSpr->y = tmpSpr->spr.y; tmpSpr->dx = 3; tmpSpr->dy = 3; /* Attach an update handle that runs every 2 frames */ SprAttachCallBack( tmpSpr, UpdateSpr, 2 ); /* Add sprite to the list */ head = SprLstAdd( tmpSpr, head ); /* Create a second sprite and add it's screen position */ tmpSpr = SprCreate( PLAYER01 ); tmpSpr->spr.x = SCREEN_W/4; tmpSpr->spr.y = SCREEN_H/2; tmpSpr->x = tmpSpr->spr.x; tmpSpr->y = tmpSpr->spr.y; tmpSpr->dx = 5; /* Made this one sightly faster */ tmpSpr->dy = 5; /* Attach an update handle to work every 4 frames */ SprAttachCallBack( tmpSpr, UpdateSpr, 4 ); /* Add sprite to the list */ head = SprLstAdd( tmpSpr, head ); while( !(rpad & PADselect && rpad & PADstart) ) { /* Read joypad */ rpad = ReadJoyPad(); /* Update */ Update(); /* Get current buffer */ currBuff = GsGetActiveBuff(); /* Set the work base */ GsSetWorkBase( (PACKET *)pktMem[currBuff] ); /* Clear the OT */ GsClearOt( 0, 0, &OTHeader[currBuff] ); /* Draw the sprite list (see sprlst.c) */ SprLstDraw( head, OTHeader[currBuff] ); /* Sync to end of frame */ DrawSync( 0 ); drawTime = VSync( 0 ); /* Swap buffers */ GsSwapDispBuff(); /* Add a clear to the front of the OT */ GsSortClear( 0, 0, 0, &OTHeader[currBuff] ); /* Draw the OT */ GsDrawOt( &OTHeader[currBuff] ); /* Output some text */ FntPrint( FntOut, "Screen Draw Time: %d", drawTime ); FntFlush( FntOut ); } ResetGraph( 3 ); } /* ** This function is attached to each sprite I've created to ** update it's position on the screen. */ void UpdateSpr(void * s) { /* Cast the void * to a sprite ** Note: this should be done as it is a sprite ** structure which is passed through (see sprlst.c ) */ lpSprite spr = (lpSprite)s; /* ** Update x position */ spr->x += spr->dx; if( spr->x+spr->w > SCREEN_W ) { spr->x = SCREEN_W-spr->w; spr->dx = -spr->dx; } if( spr->x < 0 ) { spr->x = 0; spr->dx = -spr->dx; } /* ** Update y position */ spr->y += spr->dy; if( spr->y+spr->h > SCREEN_H ) { spr->y = SCREEN_H - spr->h; spr->dy = -spr->dy; } if( spr->y < 0 ) { spr->y = 0; spr->dy = -spr->dy; } } /* ** InitScreen - Screen initialisation stuff */ void InitScreen() { /* VideMode - Can be changed in screen.h */ SetVideoMode( VID_MODE ); /* Setup graphics subsystem */ GsInitGraph( SCREEN_W, SCREEN_H, 0, 0, 0 ); /* Set the default display buffer */ GsDefDispBuff( 0, 0, 0, SCREEN_H ); /* Load font and set area to print out */ FntLoad( 960, 256 ); FntOut = FntOpen( 0, 0, SCREEN_W, 50, 0, 256 ); /* Setup the OT information */ OTHeader[0].length = OTABLE_LENGTH; OTHeader[1].length = OTABLE_LENGTH; OTHeader[0].org = OTArray[0]; OTHeader[1].org = OTArray[1]; /* Initialise the OT's */ GsClearOt( 0, 0, &OTHeader[0] ); GsClearOt( 0, 0, &OTHeader[1] ); } /* Global track of rotation */ long rot=0; /* ** Update function called once every loop. */ void Update() { SprLstUpdate( head ); }