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

 ShutScreen 1.2  (Client for BServer)

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

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <graphics/scale.h>

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>

#include "/include/client.h"
#include "/include/bitmap/bitmap.h"
#include "/include/bitmap/bitmap_pragmas.h"

char *ver = "$VER: ShutScreen 1.2 "__AMIGADATE__;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *BitMapBase;

struct Screen *clonescr, *scr;
UWORD scrwidth, scrheight, scrdepth;
ULONG command;

struct BitMap *bmap, *scalebmap;
struct DisplayIDInformation *dinfo;

struct BitMapScaleInfo myscaleinfo = {
	NULL, NULL, NULL, 1, 1, 0, 0,
	0, 0, 0, 0, 0, 0, NULL };

void Shut( void )
{
BOOL still_blitting;
BOOL blanking, success = FALSE;
UWORD n, frame, top;
ULONG color, darkest_color = 0xF, darkest_index = 0;

clonescr = IntuitionBase->FirstScreen;
scrwidth = clonescr->Width;
scrheight = clonescr->Height;
scrdepth = clonescr->RastPort.BitMap->Depth;

if ( scalebmap = CreateBitMap( scrwidth, scrheight, scrdepth ) )
	{
	if ( bmap = CreateBitMap( scrwidth, scrheight, scrdepth ) )
		{
		BltBitMap( clonescr->RastPort.BitMap, 0, 0,
			bmap, 0, 0, scrwidth, scrheight, 0xC0, 0xFF, NULL );

		myscaleinfo.bsi_SrcBitMap = clonescr->RastPort.BitMap;
		myscaleinfo.bsi_TempBitMap = scalebmap;
		myscaleinfo.bsi_DestBitMap = bmap;
		myscaleinfo.bsi_Width = scrwidth;
		myscaleinfo.bsi_Height = scrheight;
		myscaleinfo.bsi_VertDen = scrheight;

		if ( scr = OpenScreenTags( NULL,
			SA_Width, scrwidth,
			SA_Height, scrheight,
			SA_Depth, scrdepth,
			SA_DisplayID, GetVPModeID( &clonescr->ViewPort ),
			SA_BitMap, bmap,
			SA_Overscan, OSCAN_TEXT,
			TAG_END ) )
			{
			if ( scr->ViewPort.ColorMap = GetColorMap( 1<<scrdepth ) )
				{
				for ( n = 0; n < 1<<scrdepth; n++ )
					{
					color = GetRGB4( clonescr->ViewPort.ColorMap, n );
					if ( (color & 0xF0)>>4 < darkest_color )
						{
						darkest_color = (color & 0xF0)>>4;
						darkest_index = n;
						}
					SetRGB4( &scr->ViewPort, n, color >> 8, (color & 0xF0) >> 4, color & 0xF );
					}

				ScreenToFront( scr );

				success = TRUE;
				SpritesOff();

				blanking = TRUE;
				still_blitting = TRUE;
				frame = 0;
				top = 0;

				SetAPen( &scr->RastPort, darkest_index );

				while( blanking )
					{
					if ( still_blitting )
						command = GetServerCommand();
					else
						command = WaitServerCommand();

					if ( command != COMMAND_IDLE )
						blanking = FALSE;
					else
					if ( still_blitting )
						{
						frame += 2;
						if ( frame > scrheight )
							{
							SetAPen( &scr->RastPort, 0 );
							SetRGB4( &scr->ViewPort, 0, 0, 0, 0 );
							SetRast( &scr->RastPort, 0 );
							still_blitting = FALSE;
							}
						else
							{
							myscaleinfo.bsi_VertNum = scrheight - frame;
							myscaleinfo.bsi_DestTopEdge = 1+top>>1;
							ScaleBitMap( &myscaleinfo );
							}

						Move( &scr->RastPort, 0, top );
						Draw( &scr->RastPort, scrwidth - 1, top );
						Move( &scr->RastPort, 0, scrheight - top - 2);
						Draw( &scr->RastPort, scrwidth - 1, scrheight - top - 2 );
						Move( &scr->RastPort, 0, scrheight - top - 1);
						Draw( &scr->RastPort, scrwidth - 1, scrheight - top - 1 );
						top++;

						}
					}

				SpritesOn();
				FreeColorMap( scr->ViewPort.ColorMap );
				}
			CloseScreen( scr );
			}
		DisposeBitMap( bmap );
		}
	DisposeBitMap( scalebmap );
	}

if ( !success )
	SendClientMsg( ACTION_FAILED );
}


void __main( char *line )
{
if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 36L ) )
	{
	if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library",0L ) )
		{
		if ( BitMapBase = OpenLibrary( "bitmap.library", 0L ) )
			{
			if ( dinfo = OpenCommunication() )
				{
				Shut();
				CloseCommunication( dinfo );
				}
			CloseLibrary( BitMapBase );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
