
/* Generated by Interface Builder */

#import "FruitView.h"
#import <appkit/NXImage.h>
#import <appkit/graphics.h>
#import <dpsclient/psops.h>	// PSxxx functions

int fruits[FRUIT_LEVELS + 1] =
	{ 19, 5, 6, 8, 8, 9, 7, 7, 15, 15, 17, 17, 16, 16, 16, 16, 18 };


@implementation FruitView

- initFrame:(const NXRect *)frm		// initialize instance
{
	[super initFrame:frm];
	lastLevel = 0;
	fruit = [NXImage findImageNamed:"Fruit.tiff"];
	return self;
}

- drawSelf:(NXRect *)rects :(int)rectCount  // standard rendering method
{	// This is a hacked up mess.  Basically, it draw a horizontal line of
	// fruits.  It starts at the left of the view and goes to the right.
	// If you're level is higher than the width of the view would allow
	// then we start on the left with a higher level of fruit so that the
	// fruits will appear to scroll left as the level increases.
	int j, jj, k, f;
	NXRect from;
	NXPoint pos;
	
	[self lockFocus];
	PSsetgray(0.666);
	NXRectFill(&bounds);
	pos.y = 0; f = 0;
	jj = bounds.size.width / FRUIT_SIZE - 1;
	j = lastLevel - jj;
	if (j<1) j = 1;
	for (k=0; k<=jj; k++) {
		f = k + j;
		if (f > lastLevel) f = 0;
		if (f > FRUIT_LEVELS) f = FRUIT_LEVELS;
		f = fruits[f];
		NXSetRect(&from,
			(f % FRUIT_PER_ROW) * FRUIT_SIZE,
			(f / FRUIT_PER_ROW) * FRUIT_SIZE,
			FRUIT_SIZE, FRUIT_SIZE);
		pos.x = k * FRUIT_SIZE;

		[fruit composite:NX_SOVER fromRect:&from toPoint:&pos];
	}
	
	[self unlockFocus];
	NXPing();
	return self;
}

- showLevel:(int)num				// draw all fruits up to level
{
	if (lastLevel == num) return self;
	lastLevel = num;
	[self update];
	return self;
}


@end
