;/*
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.2  (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 <time.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.2 "__AMIGADATE__;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *UtilityBase, *IconBase, *DiskfontBase;
struct DisplayIDInformation *dinfo;

struct TextAttr tattr = { "topaz.font", 8, 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;

rect = GETTXTOSCANRECT(dinfo);

swidth = RECTANGLEWIDTH(rect);
sheight = RECTANGLEHEIGHT(rect);
brightness = GETBRIGHTNESS(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);

	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 = 200;
	while( STILL_BLANKING )
		{
		WaitTOF();

		if ( ticks++ == 200 )
			{
			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 );
			}
		}

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


void __main( char *line )
{
if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
	{
	if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
		{
		if ( UtilityBase = OpenLibrary( "utility.library", 37L ) )
			{
			if ( IconBase = OpenLibrary( "icon.library", 37L ) )
				{
				struct DiskObject *obj;

				if ( obj = GetDiskObject( "Clock" ) )
					{
					char **tooltypes = obj->do_ToolTypes;
					tattr.ta_YSize = ArgInt( tooltypes, "FONTHEIGHT", 8 );
					tattr.ta_Name = ArgString( tooltypes, "FONTNAME", "topaz.font" );

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

						if ( dinfo = OpenCommunication() )
							{
							RangeSeed = time( NULL );
							DrawClock();
							CloseCommunication( dinfo );
							}

						if ( newfont )
							CloseFont( newfont );

						CloseLibrary( DiskfontBase );
						}
					FreeDiskObject( obj );
					}
				CloseLibrary( IconBase );
				}
			CloseLibrary( UtilityBase );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
