/* Tile v1.0 */
/* Written in 1995 and 96 and 97... by Ben Matthew */

#include <stdio.h>
#include <stdlib.h>
#include <intuition/intuition.h>
#include <exec/exec.h>
#include <clib/graphics_protos.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>

#include <proto/locale.h>

#include "dtbitmap.h"

extern struct Catalog *catalog;
extern void msg(char *msg);
extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;
extern struct DTImage *piccy;

BOOL ViewPicture(struct Window *win, struct Screen *scr, int x, int y);
BOOL loadpic(char *fname, struct Screen *scr);
void pattern(struct Window *win_p, struct Screen *scr);

//#define DIV(a,b) ((b==0) ? (0):(a/b))

BOOL ViewPicture(struct Window *win, struct Screen *scr, int x, int y) {
	int width=0, height=0;
	
	width=piccy->Width;
	height=piccy->Height;

	if(width+x > win->Width-18) width=(win->Width-18)-x;
	if(height+y > win->Height-scr->WBorBottom) height=win->Height-(scr->WBorBottom)-y;

	if(width < 1 || height < 1) return(0);

	BltBitMapRastPort(piccy->BitMap, 0,0, win->RPort, x/*+win->LeftEdge*/,y/*+win->TopEdge*/, width, height, 0xC0/*, 0xFF, NULL*/);
	WaitBlit();
	return(1);

}

BOOL loadpic(char *fname, struct Screen *scr) {
	USHORT err=0;

	if(!(piccy=LoadDTBitMap(fname,scr,&err))) {
		msg(dterr_msgs[err]);
		return(FALSE);
	}
	
	return(TRUE);

}

void pattern(struct Window *win_p, struct Screen *scr) {
	int i=0, hcount=0, vcount=0 ,p=0;

	if(!win_p) {
			msg("Error loading backdrop (Debug: No win_p)");
			return;
		}

	hcount=win_p->Width-(scr->WBorLeft+18);
	hcount=hcount/piccy->Width;
//	hcount=DIV(hcount,piccy->Width);
	hcount++;
	vcount=win_p->Height-(scr->BarHeight+scr->WBorBottom+1);
	vcount=vcount/piccy->Height;
//	vcount=DIV(vcount,piccy->Height);
	vcount++;
	for(p=0; p<vcount; p++)
		for(i=0; i<hcount; i++) 
				ViewPicture(win_p, scr, scr->WBorLeft+(i*piccy->Width), (scr->BarHeight+1)+(p*piccy->Height));
	
}

/* For testing only */

/*
void main(int argc, char *argv[]) {
	struct Window *win_p;
	struct Screen *scr;
	struct IntuiMessage *msg2;
	ULONG class;
	
	scr=LockPubScreen(NULL);
	if(!scr) {
		msg(GetCatalogStr(catalog,1,"Unable to lock WB!"));
		exit(NULL);
	}
	win_p=(struct Window *)OpenWindowTags(NULL,	WA_Left,50,
												WA_Top,50,
												WA_Width,500,
												WA_Height,50,
												WA_MaxHeight,256,
												WA_MaxWidth,640,
												WA_MinWidth,10,
												WA_MinHeight,10,
												WA_SimpleRefresh,TRUE,
												WA_DragBar,TRUE,
												WA_CloseGadget,TRUE,
												WA_SizeGadget,TRUE,
												WA_IDCMP,IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
												TAG_END);
			
	if(!win_p) {
		msg("Unable to open window!");
		exit(NULL);
	}
							
	printf("Loadpic returned %d\n",loadpic(argv[1],scr));
	pattern(win_p,scr,piccy);

	while(1) {
		Wait(1<<win_p->UserPort->mp_SigBit);
		msg2=(struct IntuiMessage *)GetMsg(win_p->UserPort);
		class=msg2->Class;
		ReplyMsg((struct IntuiMessage *)msg2);

		switch(class) {
			case IDCMP_NEWSIZE:
			case IDCMP_REFRESHWINDOW:
				pattern(win_p,scr,piccy);
			break;
			case IDCMP_CLOSEWINDOW:
      		CloseWindow(win_p);
				unloadpic(piccy);
				exit(NULL);
			break;
		}
	}
}
*/