/* 2/6/99 - PC file server demonstration, read in and display tim images from the current directory, saved tim file is stored as oursaved.tim in the saves directory */ /* includes */ #include #include "stdlib.h" #include "libcyc.h" #include "pad.h" #include "filedemo.h" /* externs */ /* globals */ /* system stuff */ GsOT WorldOT[2]; GsOT_TAG OTTags[2][1<<1]; PACKET GpuPacketArea[2][1*(20+4)]; volatile u_char *bb0,*bb1; /* ptrs to hold pad buffer addrs */ GsSPRITE sprite; /* structure for tim data */ char *mem_p = NULL; /* pointer to malloc()'ed tim storage */ int fd; /* file descriptor of current file */ int tim_size = 0; int triangle = 0,circle = 0,cross = 0,square = 0; /* button press indicators */ int draw = 0; struct c_find find = {0,0,0,0,NULL}; /* file info structure */ void main() { int i,n,activeBuff; Init_Psx(); /* initialize playstation */ if(!sio_open()) /* open serial port */ exit(1); /* main program loop */ while(Read_Pad() != 3) { /* load tim file and initialize tim for display */ if(cross) { if(Load_Tim()) Init_Tim(); } /* save tim file to new file, oursaved.tim */ if(circle) { Save_Tim(); } FntPrint(" PRESS ~c900X~c999 TO LOAD NEXT TIM FILE"); FntPrint(" PRESS ~c509CIRCLE~c999 TO SAVE TIM FILE\n"); triangle = circle = cross = square = 0; activeBuff = GsGetActiveBuff(); GsSetWorkBase((PACKET *)GpuPacketArea[activeBuff]); /* ordering table clear */ GsClearOt(0, 0, &WorldOT[activeBuff]); FntFlush(-1); if(draw) GsSortFastSprite(&sprite,&WorldOT[activeBuff],0); DrawSync(0); VSync(0); GsSwapDispBuff(); GsSortClear(0, 0, 0, &WorldOT[activeBuff]); GsDrawOt(&WorldOT[activeBuff]); /* draw gfx in buffer */ } cyc_exit(); /* inform PC we are leaving */ /* close serial port */ sio_close(); } /*------------------------------------------------------------------------------------*/ /* General initialization; graphics, pad buffers, debug font pattern */ /* Arguments: */ /* none */ /* Return value: */ /* none */ /*------------------------------------------------------------------------------------*/ void Init_Psx(void) { int i; GetPadBuf(&bb0,&bb1); /* get controller buffers */ VSync(0); /* PAL default video mode, NTSC if user holds down start button at start */ if(Read_Pad() == 1) SetVideoMode(MODE_NTSC); else SetVideoMode(MODE_PAL); /* set up drawing and display enviroment */ GsInitGraph(320, 240, 4, 0, 0); GsDefDispBuff(0, 0, 0, 240); /* set up the ordering table */ for(i = 0; i < 2; i++) { WorldOT[i].length = 1; WorldOT[i].org = OTTags[i]; } FntLoad(960,256); /* loads basic font pattern into frame buffer */ FntOpen(16,216,256,200,0,512); /* display area for text */ } /*------------------------------------------------------------------------------------*/ /* Read controller values */ /* Arguments: */ /* none */ /* Return value: */ /* start/select button combination */ /*------------------------------------------------------------------------------------*/ int Read_Pad(void) { long padd; int end = 0; padd = (~(*(bb0+3) | *(bb0+2) << 8 | *(bb1+3) << 16 | *(bb1+2) << 24)); if(padd & PADRup) triangle = 1; if(padd & PADRright) circle = 1; if(padd & PADRdown) cross = 1; if(padd & PADRleft) square = 1; if(padd & PADselect) end += 2; if(padd & PADstart) end += 1; return end; } /*------------------------------------------------------------------------------------*/ /* Read in tim data from PC file */ /* Arguments: */ /* none */ /* Return value: */ /* 1 - tim data loaded successfully */ /* 0 - error, tim not loaded */ /*------------------------------------------------------------------------------------*/ int Load_Tim(void) { static int first = 1; char fname[24] = {'p','i','c','s','/'}; int rtn,read_size = 0; /* get info on tim files in the PC directory */ if((first) || (rtn = cyc_findnext(&find))) { rtn = cyc_findfirst("pics/*.tim",CA_NORMAL,&find); first = 0; } if(!rtn) /* zero if file found */ { /* first free memory for previous tim file */ if(mem_p) { free(mem_p); mem_p = NULL; } tim_size = find.size; if(mem_p = (char *)malloc((size_t)tim_size)) /* allocate space */ { strcpy(&fname[5],find.name); if((fd = cyc_open(fname, C_RDONLY | C_BINARY)) >= 0) /* open tim file */ { /* read in whole file */ read_size = cyc_read(fd, (char *)mem_p,tim_size); /* close file */ cyc_close(fd); /* check all data was read in */ if(read_size == tim_size) { return OK; } } } } return NG; } /*------------------------------------------------------------------------------------*/ /* Save tim data to PC file */ /* Arguments: */ /* none */ /* Return value: */ /* 1 - tim data saved successfully */ /* 0 - error, tim not saved */ /*------------------------------------------------------------------------------------*/ int Save_Tim(void) { int write_size = 0; if(mem_p) { /* create a new directory in current one */ if(cyc_system("md saves") >= 0) { /* create a new file to save to */ if((fd = cyc_creat("saves/oursaved.tim",C_IRDWR)) >= 0) { cyc_close(fd); /* close a newly created file immediately */ /* open new file for saving */ if((fd = cyc_open("saves/oursaved.tim",C_WRONLY | C_BINARY)) >= 0) { /* write out data to file */ write_size = cyc_write(fd,(char *)mem_p,tim_size); /* close file */ cyc_close(fd); } } } } /* test data was written to file */ if(write_size == tim_size) return OK; else return NG; } /*------------------------------------------------------------------------------------*/ /* Initialize tim for display */ /* Arguments: */ /* none */ /* Return value: */ /* none */ /*------------------------------------------------------------------------------------*/ void Init_Tim(void) { RECT rect; GsIMAGE tim_data; int width[4] = {4,2,1,0}; /* adjust width for 4/8/16bit tims */ short mode; GsGetTimInfo((u_long *)(mem_p+4),&tim_data); /* get TIM info */ mode = (tim_data.pmode & 0x03); /* transfer image to frame buffer */ rect.x = tim_data.px; /* x,y storage position in buffer */ rect.y = tim_data.py; rect.w = tim_data.pw; /* width/height of actual sprite image */ rect.h = tim_data.ph; LoadImage(&rect,tim_data.pixel); /* pixel=sprite image address; tranferred to vram */ /* load the CLUT into the frame buffer */ rect.x = tim_data.cx; /* x,y storage position in buffer */ rect.y = tim_data.cy; rect.w = tim_data.cw; /* width/height of CLUT data */ rect.h = tim_data.ch; LoadImage(&rect,tim_data.clut); /* and in it goes */ /* initialize the sprite GsSprite structure */ sprite.attribute = (u_long)(mode<<24); sprite.x = 32; /* actual screen display x,y position */ sprite.y = 16; sprite.w = (tim_data.pw*width[mode]); /* width/height of image */ sprite.h = (tim_data.ph); sprite.tpage = GetTPage(1,0,tim_data.px,tim_data.py); sprite.u = 0; sprite.v = 0; sprite.cx = tim_data.cx; /* coords of the CLUT */ sprite.cy = tim_data.cy; sprite.r = 128; sprite.g = 128; sprite.b = 128; sprite.mx = 16; sprite.my = 16; sprite.scalex = ONE; sprite.scaley = ONE; sprite.rotate = 0; DrawSync(0); draw = 1; }