#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "palette.h" #define W 400 #define H 300 #define D 114 #define N 64 struct IntuitionBase *IntuitionBase; struct GfxBase *GfxBase; struct Library *TimerBase; struct Library *LayersBase; static struct Library *P96Base; /* Picasso96 public ABI: RenderInfo is 12 bytes; RGBFB_R5G5B5 = 11. Only this function is needed, so no complete P96 SDK is required. */ struct BoingRenderInfo {APTR Memory;WORD BytesPerRow,pad;ULONG RGBFormat;}; void __boingP96Write(__reg("a6") void *,__reg("a0") struct BoingRenderInfo *, __reg("d0") UWORD,__reg("d1") UWORD,__reg("a1") struct RastPort *, __reg("d2") UWORD,__reg("d3") UWORD,__reg("d4") UWORD,__reg("d5") UWORD)="\tjsr\t-102(a6)"; static UWORD *rgbFrame,rgbPens[256]; static void uploadRGB(struct RastPort *rp,int width,int height){ struct BoingRenderInfo ri; ri.Memory=rgbFrame;ri.BytesPerRow=width*2;ri.pad=0;ri.RGBFormat=11; __boingP96Write(P96Base,&ri,0,0,rp,0,0,width,height); } static void writeBack(struct RastPort *rp,UBYTE *pixels,int width,int height){ if(rgbFrame){ ULONG n=(ULONG)width*height,k;struct BoingRenderInfo ri; for(k=0;kBorderLeft+w->BorderRight,bh=w->BorderTop+w->BorderBottom; int iw=w->Width-bw,ih=w->Height-bh,k,kmin,kmax,t; if(iw*3==ih*4){*oldw=iw;*oldh=ih;return;} if(abs(iw-*oldw)*3>=abs(ih-*oldh)*4)k=(iw+2)/4; else k=(ih+1)/3; kmin=(w->MinWidth-bw+3)/4; t=(w->MinHeight-bh+2)/3;if(t>kmin)kmin=t; if(kmin<1)kmin=1; kmax=(sc->Width-w->LeftEdge-bw)/4; t=(sc->Height-w->TopEdge-bh)/3;if(tkmax)k=kmax; *oldw=k*4;*oldh=k*3; ChangeWindowBox(w,w->LeftEdge,w->TopEdge,*oldw+bw,*oldh+bh); } int main(void) { struct BitMap *backBitmap=0;struct RastPort backRPort; int backw=0,backh=0,aspectw=W,aspecth=H; struct Screen *screen=0; struct Window *win=0; struct IntuiMessage *msg; int i,j,x,y,bx=45,by=20,vx=3,vy=0,phase=0,done=0,paused=0,got=0; ULONG cls; UWORD code; UBYTE *sp; int sx,sy,dx,dy; int iw,ih,rw,rh,ox,oy,lastw=0,lasth=0,srcy,prevy; UBYTE *row; struct MsgPort *timerport=0; struct timerequest *timerio=0; struct EClockVal clock; ULONG frequency=0,last=0,delta,dt,rotation=0; LONG xpos=45*1024,ypos=20*1024,xspeed=75,yspeed=0; int timeropen=0; IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",39); GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",40); LayersBase=OpenLibrary("layers.library",39); if(!IntuitionBase||!GfxBase||!LayersBase)goto end; timerport=CreateMsgPort();if(!timerport)goto end; timerio=(struct timerequest*)CreateIORequest(timerport,sizeof(struct timerequest));if(!timerio)goto end; if(OpenDevice(TIMERNAME,UNIT_MICROHZ,(struct IORequest*)timerio,0))goto end; timeropen=1;TimerBase=(struct Library*)timerio->tr_node.io_Device; frequency=ReadEClock(&clock);last=clock.ev_lo; screen=LockPubScreen(0); if(!screen)goto end; if(GetBitMapAttr(screen->RastPort.BitMap,BMA_DEPTH)>8){ P96Base=OpenLibrary("Picasso96API.library",2); if(!P96Base)goto end; rgbFrame=malloc((ULONG)screen->Width*screen->Height*sizeof(UWORD)); if(!rgbFrame)goto end; } scaled=malloc((ULONG)screen->Width*screen->Height); xmap=malloc((ULONG)screen->Width*sizeof(UWORD)); background=malloc(W*H); frame=malloc(W*H); sprites=malloc(N*D*D); if(!background||!frame||!sprites||!scaled||!xmap)goto end; if(!load("PROGDIR:background.bin",background,W*H)||!load("PROGDIR:sprites.bin",sprites,N*D*D))goto end; for(i=0;i<21;i++) { pens[i]=ObtainBestPen(screen->ViewPort.ColorMap,colors[i][0]*0x01010101UL,colors[i][1]*0x01010101UL,colors[i][2]*0x01010101UL,TAG_DONE); if(pens[i]<0||pens[i]>255)goto end; got++; rgbPens[pens[i]]=(colors[i][0]>>3)<<10|(colors[i][1]>>3)<<5|(colors[i][2]>>3); } for(i=0;iWidth,WA_MaxHeight,screen->Height, WA_SimpleRefresh,TRUE,WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_RAWKEY|IDCMP_REFRESHWINDOW|IDCMP_NEWSIZE,TAG_DONE); if(!win)goto end; while(!done) { while((msg=(struct IntuiMessage*)GetMsg(win->UserPort))!=0) { cls=msg->Class;code=msg->Code;ReplyMsg((struct Message*)msg); if(cls==IDCMP_CLOSEWINDOW||(cls==IDCMP_RAWKEY&&code==0x45))done=1; if(cls==IDCMP_RAWKEY&&code==0x40)paused=!paused; if(cls==IDCMP_NEWSIZE)keepAspect(win,screen,&aspectw,&aspecth); if(cls==IDCMP_REFRESHWINDOW){BeginRefresh(win);EndRefresh(win,TRUE);} } ReadEClock(&clock);delta=clock.ev_lo-last;last=clock.ev_lo; if(delta>frequency/20)delta=frequency/20; dt=delta*1024/frequency; if(!paused){ xpos+=xspeed*(LONG)dt; if(xpos<24*1024){xpos=24*1024;xspeed=75;} if(xpos>(W-D-24)*1024){xpos=(W-D-24)*1024;xspeed=-75;} ypos+=yspeed*(LONG)dt/1024+625*(LONG)dt*(LONG)dt/2048; yspeed+=625*(LONG)dt; if(ypos>(H-D-37)*1024){ypos=(H-D-37)*1024;yspeed=-400*1024;} rotation=(rotation+dt*50)%(N*1024); } bx=(xpos+512)/1024;by=(ypos+512)/1024;phase=rotation/1024; memcpy(frame,background,W*H); /* Shadow follows the sphere on the grid and contracts with height. */ sx=bx+D/2+23;sy=H-39;dx=34+by/10;dy=6+by/28; for(y=-dy;y<=dy;y++){ int span=dx,left,right,yy=sy+y; LONG limit=(LONG)dx*dx*((LONG)dy*dy-(LONG)y*y); if(yy<0||yy>=H)continue; while(span>0&&(LONG)span*span*dy*dy>limit)span--; left=sx-span;right=sx+span;if(left<0)left=0;if(right>=W)right=W-1; if(left<=right)memset(frame+yy*W+left,(UBYTE)pens[2],right-left+1); } sp=sprites+phase*D*D; for(j=0;j=0&&by+jWidth-win->BorderLeft-win->BorderRight; ih=win->Height-win->BorderTop-win->BorderBottom; if(done)break; if(iw<=0||ih<=0||iw>screen->Width||ih>screen->Height){WaitTOF();continue;} /* Build a complete frame off screen. Never expose the scanline conversion. */ if(iw!=backw||ih!=backh){ struct BitMap *replacement=AllocBitMap(iw,ih, GetBitMapAttr(screen->RastPort.BitMap,BMA_DEPTH),BMF_CLEAR, screen->RastPort.BitMap); if(!replacement)break; WaitBlit();if(backBitmap)FreeBitMap(backBitmap); backBitmap=replacement;backw=iw;backh=ih; InitRastPort(&backRPort);backRPort.BitMap=backBitmap; } WaitBlit(); if(iw==W&&ih==H){ writeBack(&backRPort,frame,W,H); }else if(iw>0&&ih>0&&iw<=screen->Width&&ih<=screen->Height) { /* Fit a 4:3 scene inside any window shape; keep the sphere round. */ rw=iw;rh=iw*H/W; if(rh>ih){rh=ih;rw=ih*W/H;} ox=(iw-rw)/2;oy=(ih-rh)/2; if(iw!=lastw||ih!=lasth){ for(i=0;iWLayer); if(iw==win->Width-win->BorderLeft-win->BorderRight&& ih==win->Height-win->BorderTop-win->BorderBottom){ BltBitMapRastPort(backBitmap,0,0,win->RPort, win->BorderLeft,win->BorderTop,iw,ih,0xc0); WaitBlit(); } UnlockLayer(win->WLayer); } end: if(GfxBase){WaitBlit();if(backBitmap)FreeBitMap(backBitmap);} if(win)CloseWindow(win); if(screen){for(i=0;iViewPort.ColorMap,pens[i]);UnlockPubScreen(0,screen);} free(background);free(frame);free(sprites); free(scaled);free(xmap);free(rgbFrame); if(P96Base)CloseLibrary(P96Base); if(timeropen)CloseDevice((struct IORequest*)timerio); if(timerio)DeleteIORequest((struct IORequest*)timerio); if(timerport)DeleteMsgPort(timerport); if(LayersBase)CloseLibrary(LayersBase); if(GfxBase)CloseLibrary((struct Library*)GfxBase); if(IntuitionBase)CloseLibrary((struct Library*)IntuitionBase); return 0; }