// MPMorph - Amiga Morphing program
// Copyright (C) © 1993  Topicsave Limited

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

// mark@topic.demon.co.uk

// include precompiled header if not already
#ifndef MPMORPH_AMIGA_H
#include "MPMorph-amiga.h"
#endif
#ifndef MPMOPRH_H
#include "MPMorph.h"
#endif

static struct MPImage *MPi=NULL;

static struct BitMap Add_bm,Del_bm,L1_bm,L2_bm,Mov_bm,One_bm,Rel_bm,
							Two_bm,U1_bm,U2_bm;

extern APTR Add_o=NULL,Del_o=NULL,L1_o=NULL,L2_o=NULL,Mov_o=NULL,One_o=NULL,Rel_o=NULL,
				Two_o=NULL,U1_o=NULL,U2_o=NULL;

/* version of loadbrush which first check file exists
 * to prevent any error message being displayed if not present
 */
int
myloadbrush(UBYTE *filename) {
	BPTR fh;
	if (fh = Open(filename,MODE_OLDFILE)) {
		Close(fh);
		return (int)(MPi = LoadMPImage(filename,NULL,MPIF_NOREMAP));
	}
	return 0;
}

/* load a pointer from a 4 colour ILBM file.
 *
 * ilbm     - ILBM stuff
 * address  - address to store sprite
 * filename - name of iff file to load
 */
void
mycopysprite(UWORD *address,UBYTE *filename) {
	int i;
	UWORD *p0,*p1,*o0;
	// try and load brush first
	if (myloadbrush(filename)) {
		// if ok then convert to pointer format
		p0 = (UWORD *)MPi->BitMap->Planes[0];
		p1 = (UWORD *)MPi->BitMap->Planes[1];
		o0 = address + 2;
		for (i = 0;
			  i < 32;
			  ++i) {
			*o0++ = *p0++;
			*o0++ = *p1++;
		}
	}
	// close and unload brush
	if (MPi) {
		FreeMPImage(MPi);
		MPi = NULL;
	}
}

/* load an image from a 4 colour ILBM file
 *
 * im       - pointer to image to store data
 * filename - name of iff file to load
 * size     - size of image bit plane
 */
void
mycopybrush(struct Image *im,UBYTE *filename,UWORD size) {
	// try and load brush first
	if (!myloadbrush(filename)) {
		// if ok then copy to image data
		CopyMem(im->ImageData,MPi->BitMap->Planes[0],size);
		CopyMem(((UBYTE *)im->ImageData)+size,MPi->BitMap->Planes[1],size);
	}
	// close and unload brush
	if (MPi) {
		FreeMPImage(MPi);
		MPi = NULL;
	}
}

/* Invert an image for a highlighted gadget
 * this is used rather than complement to look good on >4 colour screen
 * (There must be a better way)
 *
 * im    - Image pointer, note that the memory for the image data is twice the required size
 * size  - size of image data
 */
void
invertbrush(struct Image *im,UWORD size) {
	UWORD *a,*b;
	UWORD i;
	a = im->ImageData,
	b = im->ImageData+size;
	for (i = 0;
		  i < size;
		  i++) {
		*b++ = ~*a++;
	}
}

/* Create a V39 pointer from a sprite
 */
APTR
CreatePointer(UBYTE *data,struct BitMap *bm,UBYTE *d) {
	APTR o;
	int i;
	InitBitMap(bm,2,16,10);
	bm->Planes[0] = d;
	bm->Planes[1] = &(d[20]);
	for (i = 0;
		  i < 10;
		  ++i) {
		d[i*2] = data[i*4+4];
		d[i*2+1] = data[i*4+5];
		d[i*2+20] = data[i*4+6];
		d[i*2+21] = data[i*4+7];
	}
	o = NewObject(NULL,"pointerclass",
					POINTERA_BitMap, bm,
					POINTERA_XOffset, 0,
					POINTERA_YOffset, 0,
					POINTERA_WordWidth,	1,
					POINTERA_XResolution,	POINTERXRESN_DEFAULT,
					POINTERA_YResolution,	POINTERYRESN_DEFAULT,
					TAG_END);
	return o;
}

static UBYTE Add_d[40],Del_d[40],L1_d[40],L2_d[40],Mov_d[40],
				Rel_d[40],One_d[40],Two_d[40],U1_d[40],U2_d[40];

/* Load pointer and
 * gadget images
 */
void
LoadBrushes(void) {
	BPTR 					lock, cd;		// lock on new directory and old directory
	if (lock = Lock("MPMorph:brush",ACCESS_READ)) {	// try and change to this directory
		cd = CurrentDir(lock);
		// Load all image files
		mycopybrush(&Add_im,"add",204);
		mycopybrush(&Del_im,"del",204);
		mycopybrush(&Link_im,"link",204);
		mycopybrush(&None_im,"none",204);
		mycopybrush(&One_im,"one",204);
		mycopybrush(&Rel_im,"rel",204);
		mycopybrush(&Two_im,"two",204);
		mycopybrush(&Unlink_im,"unlink",204);
		mycopybrush(&st_im,"1st",30);
		mycopybrush(&prev_im,"prev",30);
		mycopybrush(&goto_im,"goto",30);
		mycopybrush(&next_im,"next",30);
		mycopybrush(&last_im,"last",30);
		// switch back to old dir
		CurrentDir(cd);
		UnLock(lock);
	}
	// invert all gadgets for hilite
	invertbrush(&Add_im,204);
	invertbrush(&Del_im,204);
	invertbrush(&Link_im,204);
	invertbrush(&None_im,204);
	invertbrush(&One_im,204);
	invertbrush(&Rel_im,204);
	invertbrush(&Two_im,204);
	invertbrush(&Unlink_im,204);
	invertbrush(&st_im,30);
	invertbrush(&prev_im,30);
	invertbrush(&goto_im,30);
	invertbrush(&next_im,30);
	invertbrush(&last_im,30);
	if (lock = Lock("MPMorph:cursor",ACCESS_READ)) {	// try and change to this directory
		cd = CurrentDir(lock);
		// Load all pointer files
		mycopysprite(Add,"XAdd");
		mycopysprite(Del,"XDel");
		mycopysprite(L1,"XL1");
		mycopysprite(L2,"XL2");
		mycopysprite(Mov,"XMov");
		mycopysprite(One,"XOne");
		mycopysprite(Rel,"XRel");
		mycopysprite(Two,"XTwo");
		mycopysprite(U1,"XU1");
		mycopysprite(U2,"XU2");
		// switch back to old dir
		CurrentDir(cd);
		UnLock(lock);
	}
	Add_o = CreatePointer((UBYTE *)Add,&Add_bm,(UBYTE *)Add_d);
	Del_o = CreatePointer((UBYTE *)Del,&Del_bm,(UBYTE *)Del_d);
	L1_o = CreatePointer((UBYTE *)L1,&L1_bm,(UBYTE *)L1_d);
	L2_o = CreatePointer((UBYTE *)L2,&L2_bm,(UBYTE *)L2_d);
	Mov_o = CreatePointer((UBYTE *)Mov,&Mov_bm,(UBYTE *)Mov_d);
	One_o = CreatePointer((UBYTE *)One,&One_bm,(UBYTE *)One_d);
	Rel_o = CreatePointer((UBYTE *)Rel,&Rel_bm,(UBYTE *)Rel_d);
	Two_o = CreatePointer((UBYTE *)Two,&Two_bm,(UBYTE *)Two_d);
	U1_o = CreatePointer((UBYTE *)U1,&U1_bm,(UBYTE *)U1_d);
	U2_o = CreatePointer((UBYTE *)U2,&U2_bm,(UBYTE *)U2_d);
}	
