;/*
sc RESOPT DATA=NEAR UCHAR CONSTLIB STREQ NMINC STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTSIZE IGNORE=73+94 Clock.c
slink from LIB:c.o Clock.o to //Clients/Clock LIB LIB:sc.lib LIB:amiga.lib /lib/client.lib SC SD NOICONS STRIPDEBUG
delete Clock.o
quit

 Clock 1.3  (Client for BServer)

 Copyright © 1994-1995 by Stefano Reksten of 3AM - The Three Amigos!!!
 All rights reserved.
*/

#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/text.h>
#include <graphics/rastport.h>

#include <clib/alib_protos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/utility.h>
#include <proto/diskfont.h>
#include <proto/icon.h>

#include "/include/client.h"

char *ver = "$VER: Clock 1.4 "__AMIGADATE__;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *DiskfontBase;
struct DisplayIDInformation *dinfo;

struct TextAttr tattr = { "diamond.font", 20, 0, FPF_DISKFONT };
struct TextFont *newfont;

char clkstring[5] = { ' ','0',':','0','0' };
struct IntuiText itext = { 1, 0, JAM1, 0, 0, NULL, clkstring, NULL };

extern ULONG RangeSeed;

void DrawClock( void )
{
struct Screen *scr;
UWORD swidth, sheight, xcrd, ycrd;
struct Rectangle *rect;
ULONG secs, mics, ticks, drawn = 0;
struct ClockData clock;
UWORD fontheight;
UBYTE brightness, whichsmp = 0, volume;
Sound *cl1, *cl2;

rect = GETTXTOSCANRECT(dinfo);

swidth = RECTANGLEWIDTH(rect);
sheight = RECTANGLEHEIGHT(rect);
brightness = GETBRIGHTNESS(dinfo);
volume = GETVOLUME(dinfo);

if ( scr = OpenScreenTags( NULL,
	SA_DisplayID, DISPLAYID( dinfo ),
	SA_Width, swidth,
	SA_Height, sheight,
	SA_Left, (RECTANGLEWIDTH(rect) - swidth)/2,
	SA_Top, 0,
	SA_Depth, 1,
	SA_Overscan, OSCAN_TEXT,
	SA_Type, CUSTOMSCREEN,
	SA_Quiet, TRUE,
	TAG_END ) )
	{
	register struct ViewPort *vp = &(scr->ViewPort);
	register struct RastPort *rp = &(scr->RastPort);

	cl1 = Open8SVX( "8SVX/Clock.smp1" );
	cl2 = Open8SVX( "8SVX/Clock.smp2" );

	SpritesOff();

	SetRGB4( vp, 0, 0, 0, 0 );
	SetRGB4( vp, 1, 5*brightness/100, 10*brightness/100, 15*brightness/100 );

	fontheight = ( newfont ? newfont->tf_YSize : rp->Font->tf_YSize );

	ticks = 99;
	while( STILL_BLANKING )
		{
		WaitTOF();

		if ( ticks % 25 == 0 )
			{
			Play8SVX( whichsmp ? cl1 : cl2, volume );
			whichsmp ^= 1;
			}

		if ( ticks++ == 99 )
			{
			ticks = 0;
			if ( drawn++ )
				{
				itext.FrontPen = 0;
				PrintIText( rp, &itext, xcrd, ycrd );
				}
			CurrentTime( &secs, &mics );
			Amiga2Date( secs, &clock );
			clkstring[0] = ( clock.hour >= 10 ? '0' + clock.hour / 10 : ' ' );
			clkstring[1] = '0' + clock.hour % 10;
			clkstring[3] = '0' + clock.min / 10;
			clkstring[4] = '0' + clock.min % 10;
			itext.FrontPen = 1;
			xcrd = RangeRand( swidth - IntuiTextLength( &itext ));
			ycrd = RangeRand( sheight - fontheight );
			PrintIText( rp, &itext, xcrd, ycrd );
			}
		}

	Close8SVX( cl1 );
	Close8SVX( cl2 );

	CloseScreen( scr );
	SpritesOn();
	}
else
	SendClientMsg( ACTION_FAILED );
}


void __main( void )
{
if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
	{
	if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
		{
		if ( dinfo = OpenCommunication() )
			{
			int fh;
			char buff[40];

			if ( GetArgInt( dinfo->di_Args, "FONTHEIGHT", &fh ) )
				tattr.ta_YSize = fh;
			if ( GetArgString( dinfo->di_Args, "FONTNAME", buff ) )
				tattr.ta_Name = buff;

			if ( DiskfontBase = OpenLibrary( "diskfont.library", 37L ) )
				{
				if ( newfont = OpenDiskFont( &tattr ) )
					{
					tattr.ta_Flags = newfont->tf_Flags;
					itext.ITextFont = &tattr;
					}
				}

			CurrentTime( &RangeSeed, &RangeSeed );
			DrawClock();

			if ( DiskfontBase )
				{
				if ( newfont )
					CloseFont( newfont );
				CloseLibrary( DiskfontBase );
				}

			CloseCommunication( dinfo );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
