#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 int damageX,damageY,damageW,damageH; 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,damageX,damageY,rp,damageX,damageY,damageW,damageH); } static void writeBack(struct RastPort *rp,UBYTE *pixels,int width,int height){ if(rgbFrame){ int x,y;for(y=damageY;yBorderLeft+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,by,size,den,wx,wy,wz,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=-95*1024,ypos=150*1024,zpos=180*1024; LONG xspeed=95,yspeed=0,zspeed=110; int timeropen=0,force=1,oldL=0,oldT=0,oldR=W-1,oldB=H-1; int dl,dr,dtp,db,nl,nr,nt,nb,renderX,renderY,renderR,renderB; ULONG frameStart=0,interval,elapsed,signals;LONG oldPriority=0;int prioritySet=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; oldPriority=SetTaskPri(FindTask(0),-1);prioritySet=1; 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;force=1;} if(cls==IDCMP_NEWSIZE)force=1; if(cls==IDCMP_NEWSIZE)keepAspect(win,screen,&aspectw,&aspecth); if(cls==IDCMP_REFRESHWINDOW){force=1;BeginRefresh(win);EndRefresh(win,TRUE);} } if(done)break; /* Sleep on timer OR window input; never busy-wait or delay keyboard events. */ ReadEClock(&clock);interval=frequency/50;elapsed=clock.ev_lo-frameStart; if(frameStart&&elapsedtr_node.io_Command=TR_ADDREQUEST;timerio->tr_time.tv_secs=0;timerio->tr_time.tv_micro=(interval-elapsed)*1000UL/(frequency/1000); SendIO((struct IORequest*)timerio);signals=Wait((1UL<mp_SigBit)|(1UL<UserPort->mp_SigBit));if(!CheckIO((struct IORequest*)timerio))AbortIO((struct IORequest*)timerio);WaitIO((struct IORequest*)timerio);continue; } frameStart=clock.ev_lo; if(paused&&!force){Wait(1UL<UserPort->mp_SigBit);continue;} ReadEClock(&clock);delta=clock.ev_lo-last;last=clock.ev_lo; if(delta>frequency/10)delta=frequency/10; dt=delta*1024/frequency; if(!paused){ /* World space in fixed point. Sphere radius 50; room 380x250x400. */ xpos+=xspeed*(LONG)dt; if(xpos<-140*1024){xpos=-140*1024;xspeed=95;} if(xpos>140*1024){xpos=140*1024;xspeed=-95;} zpos+=zspeed*(LONG)dt; if(zpos<50*1024){zpos=50*1024;zspeed=110;} if(zpos>350*1024){zpos=350*1024;zspeed=-110;} ypos+=yspeed*(LONG)dt/1024-625*(LONG)dt*(LONG)dt/2048; yspeed-=625*(LONG)dt; if(ypos<50*1024){ypos=50*1024;yspeed=400*1024;} if(ypos>200*1024){ypos=200*1024;yspeed=-abs(yspeed);} rotation=(rotation+dt*50)%(N*1024); } wx=xpos/1024;wy=ypos/1024;wz=zpos/1024; den=260+wz; size=100*340/den; bx=200+wx*340/den-size/2; by=100+(150-wy)*340/den-size/2; phase=rotation/1024; memcpy(frame,background,W*H); /* Project the shadow separately on the floor at the sphere's world depth. */ {int shadowz=wz+(wy-50)/7,shadowden=260+shadowz; sx=200+(wx+(wy-50)/6)*340/shadowden; sy=100+150*340/shadowden; dx=(40+(wy-50)/10)*340/shadowden; dy=(11+(wy-50)/30)*340/shadowden; if(dy<2)dy=2; } nl=bxsx+dx?bx+size:sx+dx; nt=bysy+dy?by+size:sy+dy; dl=nloldR?nr:oldR;dtp=ntoldB?nb:oldB; oldL=nl;oldR=nr;oldT=nt;oldB=nb; if(dl<0)dl=0;if(dtp<0)dtp=0;if(dr>=W)dr=W-1;if(db>=H)db=H-1; 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; /* The checker sphere uses the original prerendered rotation frames; position and apparent diameter come from a real perspective projection. */ if(size!=lastSphereSize){for(i=0;i=H)continue; for(i=0;i=0&&xxWidth-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){ force=1; 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; } if(force){damageX=damageY=0;damageW=iw;damageH=ih;} else{damageX=dl*iw/W;damageY=dtp*ih/H;damageW=(dr+1)*iw/W+2-damageX;damageH=(db+1)*ih/H+2-damageY;if(damageX+damageW>iw)damageW=iw-damageX;if(damageY+damageH>ih)damageH=ih-damageY;} 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;ioy?damageY-oy:0;yox?damageX-ox:0;xWLayer); if(iw==win->Width-win->BorderLeft-win->BorderRight&& ih==win->Height-win->BorderTop-win->BorderBottom){ BltBitMapRastPort(backBitmap,damageX,damageY,win->RPort, win->BorderLeft+damageX,win->BorderTop+damageY,damageW,damageH,0xc0); force=0; WaitBlit(); } UnlockLayer(win->WLayer); } end: if(prioritySet)SetTaskPri(FindTask(0),oldPriority); 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; }