;/* ScaleDemo.c - Execute me to compile me with SAS C 5.10
LC -b1 -cfistq -v -y -j73 ScaleDemo.c
Blink FROM LIB:c.o,ScaleDemo.o,bitmap.o TO ScaleDemo LIBRARY LIB:LC.lib,LIB:Amiga.lib
quit
*/

#define INTUI_V36_NAMES_ONLY

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <Graphics/Scale.h>

#include <Proto/Exec.h>
#include <Proto/graphics.h>
#include <Proto/intuition.h>

UBYTE vers[] = "$VER: ScaleDemo 37.2";

extern struct BitMap SourceBM;  // Pointer to the self contained BitMap module;

struct Library	*GfxBase;
struct Library	*IntuitionBase;

struct Window	*Win;	// window pointer

// Prototypes
VOID doDrawStuff(void);
VOID doMsgLoop(void);
VOID handleWindow(struct Screen *myscreen);

VOID _main(char *line)
{
 register struct Screen *screen;

 if(IntuitionBase=OpenLibrary("intuition.library",37)){
    if(GfxBase=OpenLibrary("graphics.library",33)){
	if(NULL!=(screen=LockPubScreen(NULL))){
		handleWindow(screen);
		UnlockPubScreen(NULL,screen);
	}
	CloseLibrary(GfxBase);
    }
    CloseLibrary(IntuitionBase);
 }
}

VOID handleWindow(struct Screen *screen)
{
 if(!(Win=OpenWindowTags(NULL,
	WA_Width	,320,
	WA_Height	,100,
	WA_MinWidth	,60,
	WA_MinHeight	,40,
	WA_MaxWidth	,-1,
	WA_MaxHeight	,-1,
	WA_IDCMP, IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW,
	WA_Flags, WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET |
		WFLG_CLOSEGADGET | WFLG_SIMPLE_REFRESH,
	WA_Title, vers+6, // take title from version string
	WA_PubScreen, screen,
	TAG_DONE))) return;
 doDrawStuff();
 doMsgLoop();
 CloseWindow(Win);
}

VOID doDrawStuff()
{
 int SrcfX=384;	// Source factors
 int SrcfY=200;
 int DestfX;	// Destination factors
 int DestfY;
 int topborder;
 int InnerWidth;
 int InnerHeight;
 struct BitScaleArgs bsa;

 Forbid(); // Forbid so the user doesn't size the window while we draw.

// Determine window's inner dimensions...
 topborder=Win->WScreen->WBorTop+(Win->WScreen->Font->ta_YSize+1);
 InnerWidth=Win->Width-Win->BorderLeft-Win->BorderRight;
 InnerHeight=Win->Height-topborder-Win->BorderBottom;

 bsa.bsa_SrcX=0;	// Offsets should be 0 to read the whole source
 bsa.bsa_SrcY=0;
 bsa.bsa_SrcWidth=384;	// Full width of source bitmap
 bsa.bsa_SrcHeight=200; // Full height of source bitmap

 bsa.bsa_DestX=Win->LeftEdge+Win->BorderLeft; // Offsets to draw into dest
 bsa.bsa_DestY=Win->TopEdge+Win->BorderTop;
 bsa.bsa_DestWidth=0; // Dest width & height seem to be ignored;
 bsa.bsa_DestHeight=0;

/*
* Examples for determining factors:
*	If dest was 1, and source was 4, it would be 1/4 (exactly one quarter)
*	If dest was 2, and source was 1, it would be 2/1 (exactly doubled)
*
* Here we simplify it by using the source size as the source factor, so...
* ScalerDiv(384,InnerWidth,384) = scale by InnerWidth/384'ths
*/

 DestfX=ScalerDiv(SrcfX,InnerWidth ,384);
 DestfY=ScalerDiv(SrcfY,InnerHeight,200);

 bsa.bsa_XSrcFactor=SrcfX;
 bsa.bsa_YSrcFactor=SrcfY;
 bsa.bsa_XDestFactor=DestfX;
 bsa.bsa_YDestFactor=DestfY;

 bsa.bsa_SrcBitMap=&SourceBM;
// We are drawing directly to the Workbench screen's BitMap.  Not very nice, eh?
 bsa.bsa_DestBitMap=&Win->WScreen->BitMap;

 bsa.bsa_Flags=0;
 BitMapScale(&bsa); // Do it!
 Permit();
}

VOID doMsgLoop()
{
 register struct IntuiMessage *msg;
 register WORD flag=TRUE;

 while(flag){
    WaitPort(Win->UserPort);
    while(msg=(struct IntuiMessage *)GetMsg(Win->UserPort)){
	switch(msg->Class){
	    case IDCMP_CLOSEWINDOW:
		flag=FALSE;
		break;
	    case IDCMP_NEWSIZE:
		doDrawStuff();
		break;
	}
	ReplyMsg((struct Message *)msg);
    }
 }
}
