274 lines
12 KiB
C
274 lines
12 KiB
C
#include <exec/types.h>
|
|
#include <intuition/intuition.h>
|
|
#include <graphics/gfxbase.h>
|
|
#include <graphics/view.h>
|
|
#include <devices/timer.h>
|
|
#include <proto/exec.h>
|
|
#include <proto/dos.h>
|
|
#include <proto/intuition.h>
|
|
#include <proto/graphics.h>
|
|
#include <proto/layers.h>
|
|
#include <proto/timer.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#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;y<damageY+damageH;y++)for(x=damageX;x<damageX+damageW;x++)rgbFrame[(ULONG)y*width+x]=rgbPens[pixels[(ULONG)y*width+x]];
|
|
uploadRGB(rp,width,height);
|
|
}else WriteChunkyPixels(rp,0,0,width-1,height-1,pixels,width);
|
|
}
|
|
static UBYTE *background,*sprites,*frame,*scaled;
|
|
static UWORD *xmap;
|
|
static UBYTE sphereX[D*2];
|
|
static int lastSphereSize;
|
|
static LONG pens[21];
|
|
static int load(char *path,UBYTE *p,long size) {
|
|
FILE *f=fopen(path,"rb"); int ok; if(!f)return 0;
|
|
ok=fread(p,1,size,f)==size; fclose(f);return ok;
|
|
}
|
|
/* Intuition completes the native resize first; snap its inner area to 4:3.
|
|
Use the axis moved most, then clamp both axes together in 4x3 pixel steps. */
|
|
static void keepAspect(struct Window *w,struct Screen *sc,int *oldw,int *oldh){
|
|
int bw=w->BorderLeft+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(t<kmax)kmax=t;
|
|
if(kmax<kmin)kmax=kmin;
|
|
if(k<kmin)k=kmin;if(k>kmax)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;i<W*H;i++)background[i]=(UBYTE)pens[background[i]];
|
|
for(i=0;i<N*D*D;i++)if(sprites[i]!=255)sprites[i]=(UBYTE)pens[sprites[i]];
|
|
win=OpenWindowTags(0,WA_PubScreen,(ULONG)screen,WA_Left,740,WA_Top,400,
|
|
WA_InnerWidth,W,WA_InnerHeight,H,WA_Title,(ULONG)"Boing 3D | Space: pausa Esc: esci",
|
|
WA_CloseGadget,TRUE,WA_DragBar,TRUE,WA_DepthGadget,TRUE,WA_Activate,TRUE,
|
|
WA_SizeGadget,TRUE,WA_SizeBBottom,TRUE,
|
|
WA_MinWidth,240,WA_MinHeight,195,WA_MaxWidth,screen->Width,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&&elapsed<interval){timerio->tr_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<<timerport->mp_SigBit)|(1UL<<win->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<<win->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=bx<sx-dx?bx:sx-dx;nr=bx+size>sx+dx?bx+size:sx+dx;
|
|
nt=by<sy-dy?by:sy-dy;nb=by+size>sy+dy?by+size:sy+dy;
|
|
dl=nl<oldL?nl:oldL;dr=nr>oldR?nr:oldR;dtp=nt<oldT?nt:oldT;db=nb>oldB?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<size;i++)sphereX[i]=i*D/size;lastSphereSize=size;}
|
|
for(j=0;j<size;j++){
|
|
int yy=by+j,srcrow=j*D/size*D;
|
|
if(yy<0||yy>=H)continue;
|
|
for(i=0;i<size;i++){
|
|
UBYTE c=sp[srcrow+sphereX[i]];int xx=bx+i;
|
|
if(c!=255&&xx>=0&&xx<W)frame[yy*W+xx]=c;
|
|
}
|
|
}
|
|
iw=win->Width-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;i<rw;i++)xmap[i]=(ULONG)i*W/rw;
|
|
lastw=iw;lasth=ih;
|
|
}
|
|
if(rgbFrame&&force){ULONG k,n=(ULONG)iw*ih;UWORD bg=rgbPens[pens[0]];for(k=0;k<n;k++)rgbFrame[k]=bg;}
|
|
else if(!rgbFrame)memset(scaled,(UBYTE)pens[0],(ULONG)iw*ih);
|
|
prevy=-1;
|
|
for(y=damageY>oy?damageY-oy:0;y<rh&&y+oy<damageY+damageH;y++){
|
|
srcy=(ULONG)y*H/rh;row=scaled+(ULONG)(oy+y)*iw+ox;
|
|
if(rgbFrame){
|
|
UWORD *dst=rgbFrame+(ULONG)(oy+y)*iw+ox;
|
|
for(x=damageX>ox?damageX-ox:0;x<rw&&x+ox<damageX+damageW;x++)dst[x]=rgbPens[frame[srcy*W+xmap[x]]];
|
|
}else{
|
|
if(srcy==prevy)memcpy(row,row-iw,rw);
|
|
else for(x=0;x<rw;x++)row[x]=frame[srcy*W+xmap[x]];
|
|
}
|
|
prevy=srcy;
|
|
}
|
|
if(rgbFrame)uploadRGB(&backRPort,iw,ih);
|
|
else writeBack(&backRPort,scaled,iw,ih);
|
|
}
|
|
/* Finish conversion, then present once per refresh. A resize between
|
|
rendering and presentation invalidates the old geometry: skip that frame. */
|
|
WaitBlit();
|
|
LockLayer(0,win->WLayer);
|
|
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;i<got;i++)ReleasePen(screen->ViewPort.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;
|
|
}
|