Files
amiga/AmberChrono/chrono.c
T

95 lines
6.7 KiB
C

#include <exec/types.h>
#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <dos/datetime.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "colors.h"
#include "trig.h"
#define W 320
#define H 430
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
static UBYTE *face,*frame,*out,*font;
static LONG pen[64];
static UWORD *xmap;
static int mapWidth;
static LONG lastDate=-1;
static int mode=2,borderless=0,px=976,py=28,pw=280,ph=394;
static void pixel(int x,int y,int c){if(x>=0&&x<W&&y>=0&&y<H)frame[y*W+x]=(UBYTE)pen[c];}
static void dot(int x,int y,int r,int c){int a,b;for(b=-r;b<=r;b++)for(a=-r;a<=r;a++)if(a*a+b*b<=r*r)pixel(x+a,y+b,c);}
static void line(int x0,int y0,int x1,int y1,int r,int c){int dx=abs(x1-x0),sx=x0<x1?1:-1,dy=-abs(y1-y0),sy=y0<y1?1:-1,e=dx+dy,e2;for(;;){dot(x0,y0,r,c);if(x0==x1&&y0==y1)break;e2=2*e;if(e2>=dy){e+=dy;x0+=sx;}if(e2<=dx){e+=dx;y0+=sy;}}}
static void hand(int angle,int length,int tail,int radius,int color){int a=(angle%3600)*2;line(160-trig[a]*tail/10000,158-trig[a+1]*tail/10000,160+trig[a]*length/10000,158+trig[a+1]*length/10000,radius,color);}
static void label(int x,int y,char *s,int color){int i,j,k;for(k=0;s[k];k++){i=(unsigned char)s[k]-32;if(i<0||i>=96)continue;for(j=0;j<12;j++){int z;for(z=0;z<8;z++)if(font[j*768+i*8+z])pixel(x+k*8+z,y+j,color);}}}
static void digit(int x,int y,int n){static int bits[]={63,6,91,79,102,109,125,7,127,111};int b=bits[n],i;static int seg[7][4]={{5,0,24,0},{28,4,28,19},{28,25,28,40},{5,44,24,44},{1,25,1,40},{1,4,1,19},{5,22,24,22}};for(i=0;i<7;i++)line(x+seg[i][0],y+seg[i][1],x+seg[i][2],y+seg[i][3],2,(b&(1<<i))?2:4);}
static int load(char *name,UBYTE *buf,long size){FILE*f=fopen(name,"rb");int ok;if(!f)return 0;ok=fread(buf,1,size,f)==size;fclose(f);return ok;}
static void save(struct Window*w){FILE*f;px=w->LeftEdge;py=w->TopEdge;pw=w->Width;ph=w->Height;f=fopen("PROGDIR:Chrono.prefs","w");if(f){fprintf(f,"%d %d %d %d %d %d\n",px,py,pw,ph,mode,borderless);fclose(f);}}
static void fitmode(struct Window*w,struct Screen*s){int h=(w->Width-w->BorderLeft-w->BorderRight)*(mode==0?320:mode==1?110:430)/320+w->BorderTop+w->BorderBottom;int top=w->TopEdge;if(h<100)h=100;if(top+h>s->Height)top=s->Height-h;ChangeWindowBox(w,w->LeftEdge,top,w->Width,h);}
int main(void){struct Screen*sc=0;struct Window*w=0;struct IntuiMessage*m;struct DateTime dt;struct DateStamp ds;char date[64],time[32];FILE*f;int got=0,i,x,y,done=0,reopen=0,iw,ih,rw,rh,ox,oy,srcH,srcOff,sec,min,hour;ULONG cls,ts=0,tm=0,ms,mm;UWORD code;int ticks;
IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",39);GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",40);if(!IntuitionBase||!GfxBase)goto end;
sc=LockPubScreen(0);if(!sc)goto end;
face=malloc(W*H);frame=malloc(W*H);font=malloc(768*12);out=malloc((ULONG)sc->Width*sc->Height);xmap=malloc((ULONG)sc->Width*sizeof(UWORD));if(!face||!frame||!font||!out||!xmap)goto end;
if(!load("PROGDIR:face.bin",face,W*H)||!load("PROGDIR:font.bin",font,768*12))goto end;
for(i=0;i<64;i++){pen[i]=ObtainBestPen(sc->ViewPort.ColorMap,colors[i][0]*0x01010101UL,colors[i][1]*0x01010101UL,colors[i][2]*0x01010101UL,TAG_DONE);if(pen[i]<0)goto end;got++;}
for(i=0;i<W*H;i++)face[i]=(UBYTE)pen[face[i]];
f=fopen("PROGDIR:Chrono.prefs","r");if(f){fscanf(f,"%d %d %d %d %d %d",&px,&py,&pw,&ph,&mode,&borderless);fclose(f);}
if(mode<0||mode>2)mode=2;if(pw<180||pw>sc->Width)pw=280;if(ph<100||ph>sc->Height)ph=394;
if(px<0)px=0;if(py<12)py=12;if(px+pw>sc->Width)px=sc->Width-pw;if(py+ph>sc->Height)py=sc->Height-ph;
open:
w=OpenWindowTags(0,WA_PubScreen,(ULONG)sc,WA_Left,px,WA_Top,py,WA_Width,pw,WA_Height,ph,
WA_Title,(ULONG)"Amber Chrono",WA_CloseGadget,!borderless,WA_DragBar,!borderless,WA_DepthGadget,!borderless,WA_SizeGadget,!borderless,WA_SizeBBottom,TRUE,WA_Borderless,borderless,
WA_MinWidth,180,WA_MinHeight,100,WA_MaxWidth,sc->Width,WA_MaxHeight,sc->Height,
WA_SimpleRefresh,TRUE,WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_RAWKEY|IDCMP_MOUSEBUTTONS|IDCMP_REFRESHWINDOW|IDCMP_NEWSIZE|IDCMP_CHANGEWINDOW,TAG_DONE);
if(!w)goto end;
while(!done&&!reopen){
while((m=(struct IntuiMessage*)GetMsg(w->UserPort))!=0){cls=m->Class;code=m->Code;ms=m->Seconds;mm=m->Micros;ReplyMsg((struct Message*)m);
if(cls==IDCMP_CLOSEWINDOW||(cls==IDCMP_RAWKEY&&code==0x45))done=1;
if(cls==IDCMP_REFRESHWINDOW){BeginRefresh(w);EndRefresh(w,TRUE);}
if(cls==IDCMP_MOUSEBUTTONS&&code==SELECTDOWN){if(DoubleClick(ts,tm,ms,mm)){mode=(mode+1)%3;ts=tm=0;fitmode(w,sc);}else{ts=ms;tm=mm;}}
if(cls==IDCMP_RAWKEY){if(code>=1&&code<=3){mode=code-1;fitmode(w,sc);}if(code==0x35){save(w);borderless=!borderless;reopen=1;}}
if(cls==IDCMP_NEWSIZE||cls==IDCMP_CHANGEWINDOW)save(w);
}
DateStamp(&ds);ticks=ds.ds_Tick;sec=ticks/50;min=ds.ds_Minute%60;hour=ds.ds_Minute/60;
memcpy(frame,face,W*H);
if(mode!=1){
hand((hour%12)*300+min*5+ticks/600,63,7,3,1);
hand(min*60+ticks/50,91,10,2,5);
/* Keep a solid three-pixel stroke so downscaling cannot drop the hand. */
hand(ticks*6/5,111,23,1,2);dot(160,158,6,3);dot(160,158,3,2);
}
if(mode!=0){
digit(34,351,hour/10);digit(70,351,hour%10);digit(132,351,min/10);digit(168,351,min%10);digit(230,351,sec/10);digit(266,351,sec%10);
dot(116,365,2,2);dot(116,383,2,2);dot(214,365,2,2);dot(214,383,2,2);
if(ds.ds_Days!=lastDate){lastDate=ds.ds_Days;
memset(&dt,0,sizeof(dt));dt.dat_Stamp=ds;dt.dat_Format=FORMAT_DOS;dt.dat_StrDate=date;dt.dat_StrTime=time;DateToStr(&dt);}
label(116,403,date,5);
/* A tiny pendulum swings below the seconds display. */
x=160+trig[(ticks%100)*72]*11/10000;line(160,398,x,401,0,3);dot(x,401,1,2);
}
iw=w->Width-w->BorderLeft-w->BorderRight;ih=w->Height-w->BorderTop-w->BorderBottom;
srcH=mode==0?320:mode==1?110:430;srcOff=mode==1?320:0;
if(iw>0&&ih>0&&iw<=sc->Width&&ih<=sc->Height){rw=iw;rh=iw*srcH/W;if(rh>ih){rh=ih;rw=ih*W/srcH;}ox=(iw-rw)/2;oy=(ih-rh)/2;memset(out,(UBYTE)pen[0],(ULONG)iw*ih);
if(mapWidth!=rw){for(x=0;x<rw;x++)xmap[x]=(ULONG)x*W/rw;mapWidth=rw;}
{int previous=-1;
for(y=0;y<rh;y++){
int sy=srcOff+(ULONG)y*srcH/rh;UBYTE *dst=out+(ULONG)(oy+y)*iw+ox;
if(sy==previous)memcpy(dst,dst-iw,rw);
else{UBYTE *src=frame+sy*W;for(x=0;x<rw;x++)dst[x]=src[xmap[x]];}
previous=sy;
}
}
WriteChunkyPixels(w->RPort,w->BorderLeft,w->BorderTop,w->BorderLeft+iw-1,w->BorderTop+ih-1,out,iw);
}
Delay(2);
}
save(w);CloseWindow(w);w=0;if(reopen){reopen=0;goto open;}
end:
if(w)CloseWindow(w);if(sc){for(i=0;i<got;i++)ReleasePen(sc->ViewPort.ColorMap,pen[i]);UnlockPubScreen(0,sc);}free(face);free(frame);free(font);free(out);free(xmap);
if(GfxBase)CloseLibrary((struct Library*)GfxBase);if(IntuitionBase)CloseLibrary((struct Library*)IntuitionBase);return 0;
}