// Filename : MYLIB.C // Coded by : Scott Evans // Created/Modified : 21/2/98 // Description : This file contains various routines which I find useful for // most Yaroze programs #include // Function : InitOT() // Coded by : Scott Evans // Created/Modified : 16/9/97 // Description : Set up an ordering table // Parameters : length - Z resolution of OT (1-14) // Returns : Pointer to OT of size 2^length // NULL for error (out of memory) // Notes : malloc()/free() do not work correctly but since an // OT lasts the life of a program should not be a problem // // Allocates two OTs (ot and ot+1) GsOT *InitOT(u_byte length) { // Allocate space for tags and ot #ifndef NO_MALLOC GsOT_TAG *tags=(GsOT_TAG *)malloc(((1<length=length,(ot+1)->length=length; ot->org=tags,(ot+1)->org=tags+(1<w,r->h,GPU_OFFSET,1,VRAM_MODE_16BIT); GsDefDispBuff(r->x,r->y+r->h,r->x,r->y); #ifndef NO_MALLOC // Set ordering table for buffer 0 and 1 db->ot=ot; (db+1)->ot=ot+1; // Set packet buffer for buffer 0 and 1 db->pb=pb; (db+1)->pb=pb+(npkts*size); #endif // Set display and drawing environments for buffer 0 and 1 SetDefDrawEnv(&db->drawenv,r->x,r->y+r->h,r->w,r->h); SetDefDrawEnv(&(db+1)->drawenv,r->x,r->y,r->w,r->h); SetDefDispEnv(&db->dispenv,r->x,r->y,r->w,r->h); SetDefDispEnv(&(db+1)->dispenv,r->x,r->y+r->h,r->w,r->h); // Set the drawing area primitives SetDrawEnv(&db->drawenv.dr_env,&db->drawenv); SetDrawEnv(&(db+1)->drawenv.dr_env,&(db+1)->drawenv); return(db); } #ifdef DEBUG_INFO PrintFM("Failed\n"); #endif return(NULL); } // Function : KillDBUFFER() // Coded by : Scott Evans // Created/Modified : 16/9/97 // Description : Free memory used by double buffer // Parameters : db - pointer to DBUFFER structure // Returns : None // Notes : None void KillDBUFFER(DBUFFER *db) { #ifndef NO_MALLOC // Free memory used for packet buffer free(db->pb); // Free memory used by ordering table tags free(db->ot->org); // Free memory used by ordering table free(db->ot); #endif } // Function : SwapDisplay() // Coded by : Scott Evans // Created/Modified : 23/9/97 // Description : Swap display and drawing areas // Parameters : db - pointer to current double buffer // mode - 1 use DRAWENV, 0 use DR_ENV // Returns : None // Notes : None void SwapDisplay(DBUFFER *db,u_long mode) { // Switch the display and drawing areas if(mode) PutDrawEnv(&db->drawenv); else AddPrimToOT_TAG((GsOT_TAG *)&db->drawenv.dr_env,db->ot->org); PutDispEnv(&db->dispenv); // Enable the display SetDispMask(1); } // Function : GsSortBox() // Coded by : Scott Evans // Created/Modified : 27/9/97 // Description : Add a GsBOX to an ordering table // Parameters : box - pointer to GsBOX structure // : ot - pointer to ordering table // : priority - ordering table priority // Returns : None // Notes : None void GsSortBox(GsBOX *box,GsOT *ot,u_word priority) { GsLINE lines[4],*line=lines; u_byte i; // Set up 4 lines to represent a box lines[0].x0=lines[2].x0=lines[3].x0=lines[3].x1=box->x; lines[0].y0=lines[0].y1=lines[1].y0=lines[3].y0=box->y; lines[0].x1=lines[1].x0=lines[1].x1=lines[2].x1=box->x+box->w; lines[1].y1=lines[2].y0=lines[2].y1=lines[3].y1=box->y+box->h; // Set remaining members and put lines in ordering table for(i=0;i<4;i++,line++) { line->attribute=box->attribute; line->r=box->r; line->g=box->g; line->b=box->b; GsSortLine(line,ot,priority); } } // Function : GsSortGBox() // Coded by : Scott Evans // Created/Modified : 21/10/97 // Description : Add a GsGBOX to an ordering table // Parameters : box - pointer to GsGBOX structure // : ot - pointer to ordering table // : priority - ordering table priority // Returns : None // Notes : As box but can spceify colour of each point // Top left point is point 0, goes clockwise void GsSortGBox(GsGBOX *box,GsOT *ot,u_word priority) { GsGLINE lines[4],*line=lines; u_byte i; // Set up 4 lines to represent a box lines[0].x0=lines[2].x0=lines[3].x0=lines[3].x1=box->x0; lines[0].y0=lines[0].y1=lines[1].y0=lines[3].y0=box->y0; lines[0].x1=lines[1].x0=lines[1].x1=lines[2].x1=box->x0+box->w; lines[1].y1=lines[2].y0=lines[2].y1=lines[3].y1=box->y0+box->h; // Set colours of 4 points of box lines[0].r0=lines[3].r0=box->r0; lines[0].g0=lines[3].g0=box->g0; lines[0].b0=lines[3].b0=box->b0; lines[0].r1=lines[1].r0=box->r1; lines[0].g1=lines[1].g0=box->g1; lines[0].b1=lines[1].b0=box->b1; lines[1].r1=lines[2].r1=box->r2; lines[1].g1=lines[2].g1=box->g2; lines[1].b1=lines[2].b1=box->b2; lines[2].r0=lines[3].r1=box->r3; lines[2].g0=lines[3].g1=box->g3; lines[2].b0=lines[3].b1=box->b3; // Set remaining members and put lines in ordering table for(i=0;i<4;i++,line++) { line->attribute=box->attribute; GsSortGLine(line,ot,priority); } } // Function : InitProgram() // Coded by : Scott Evans // Created/Modified : 21/2/98 // Description : Initialise a program // Parameters : p - pointer to the program data structure // Returns : None // Notes : Sets video mode, double buffer, screen and controllers u_byte InitProgram(PROGRAM_DATA *p) { DBUFFER *db=(DBUFFER *)&p->db[0]; RECT *s=&p->screen; u_byte r,g,b; if(p->video_mode==MODE_PAL) SetVideoMode(MODE_PAL); else SetVideoMode(MODE_NTSC); #ifdef PSX ResetCallback(); SsInit(); SsSetTickMode(SS_TICKVSYNC); #endif PadInit(0); ResetGraph(0); #ifdef PSX StartPAD(); SetGraphDebug(0); InitPCFile(); #endif srand(VSync(-1)); if(InitDBUFFER(db,p->ot_length,p->no_packets,p->packet_size,s)) { #ifdef DEBUG_INFO sprintf(fm_string,"Video mode %s\n",(p->video_mode==MODE_PAL ? "PAL" : "NTSC")); PrintFM(fm_string); sprintf(fm_string,"Packet buffer size %d (bytes)\n",p->no_packets*p->packet_size); PrintFM(fm_string); #endif // Automatically clear background db->drawenv.isbg=(db+1)->drawenv.isbg=1; db->drawenv.dtd=(db+1)->drawenv.dtd=1; // Set screen position and size db->dispenv.screen.x=(db+1)->dispenv.screen.x=p->screen_xoff; db->dispenv.screen.y=(db+1)->dispenv.screen.y=p->screen_yoff; db->dispenv.screen.h=(db+1)->dispenv.screen.h=s->h; r=p->back_colour.r; g=p->back_colour.g; b=p->back_colour.b; setRGB0(&db->drawenv,r,g,b); setRGB0(&(db+1)->drawenv,r,g,b); // Clear the screen PutDrawEnv(&(db+1)->drawenv); PutDrawEnv(&db->drawenv); PutDispEnv(&(db+1)->dispenv); PutDispEnv(&db->dispenv); // Initialise program variables p->running=1; p->exit=0; p->pad=0; p->buttons_down=0; return(1); } #ifdef DEBUG_INFO PrintFM("Failed\n"); #endif return(0); } // Function : CloseProgram() // Coded by : Scott Evans // Created/Modified : 21/2/98 // Description : Shutdown a program // Parameters : pd - program data // Returns : None // Notes : None void CloseProgram(PROGRAM_DATA *pd) { KillDBUFFER(pd->db); ResetGraph(3); #ifdef DEBUG_INFO PrintFM("Completed\n"); #endif } // Function : AutoRepeat() // Coded by : Scott Evans // Created/Modified : 23/2/98 // Description : Test controller buttons for auto repeat // Parameters : pad - controller pad mask // repeat - repeat rate // Returns : Mask of buttons held down // Notes : None u_long AutoRepeat(u_long pad,u_word repeat) { u_byte i; u_long mask=0; static u_word repeat_rate[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Test each controller button for(i=0;i<32;i++) { // Set bit if button is held down if(pad&(1<=repeat) { mask&=~(1<