Import Amiga applications and desktop tools
This commit is contained in:
commit
1c144c9a3c
347 files changed
+423483
No files matched your search
@@ -0,0 +1,224 @@
|
||||
#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 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;k<n;k++)rgbFrame[k]=rgbPens[pixels[k]];
|
||||
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 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=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;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,420,WA_Top,160,
|
||||
WA_InnerWidth,W,WA_InnerHeight,H,WA_Title,(ULONG)"Boing! | 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;
|
||||
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<D;j++)for(i=0;i<D;i++)if(sp[j*D+i]!=255 && by+j>=0&&by+j<H)
|
||||
frame[(by+j)*W+bx+i]=sp[j*D+i];
|
||||
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){
|
||||
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;i<rw;i++)xmap[i]=(ULONG)i*W/rw;
|
||||
lastw=iw;lasth=ih;
|
||||
}
|
||||
if(rgbFrame){ULONG k,n=(ULONG)iw*ih;UWORD bg=rgbPens[pens[0]];for(k=0;k<n;k++)rgbFrame[k]=bg;}
|
||||
else memset(scaled,(UBYTE)pens[0],(ULONG)iw*ih);
|
||||
prevy=-1;
|
||||
for(y=0;y<rh;y++){
|
||||
srcy=(ULONG)y*H/rh;row=scaled+(ULONG)(oy+y)*iw+ox;
|
||||
if(rgbFrame){
|
||||
UWORD *dst=rgbFrame+(ULONG)(oy+y)*iw+ox;
|
||||
if(srcy==prevy)memcpy(dst,dst-iw,rw*2);
|
||||
else for(x=0;x<rw;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();WaitTOF();
|
||||
LockLayer(0,win->WLayer);
|
||||
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;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;
|
||||
}
|
||||
Reference in new issue
Block a user