File:  [NeXTSTEP 3.3 examples] / Examples / DistributedObjects / remoteSpot / Spot.m
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 17:49:03 2018 UTC (8 years, 1 month ago) by root
Branches: NeXT, MAIN
CVS tags: NeXTSTEP33, HEAD
Sample Programs from NeXSTEP 3.3


#import "Spot.h"
#import "Thinker.h"
#import <appkit/color.h>

@implementation Spot

// This should return a float between 0 and 1
float frandom()
{
	float val = (random() & 0x7fffffff);
	val /= 0x7fffffff;
	return val;
}

float randBetween(float a, float b)
{
	float val, scale, t;

	if (a > b)
	{	t = a; a = b; b = t;
	}
	
	scale = (b-a);
	val = scale * frandom();
	return (a + val);
}

- init
{
	float r,g,b;
	srandom(time(0));
	r = frandom();
	g = frandom();
	b = frandom();
	color = NXConvertRGBToColor(r,g,b);
	location.x = randBetween(0,320);
	location.y = randBetween(0,220);
	isValid = YES;
	return self;
}

- (NXColor) color
{	return color;
}

- (NXPoint) location
{	return location;
}

- (BOOL) setLocation:(NXPoint)pnt
{
	if (!isValid)
	{
		[[NXApp delegate] spotDidChange];
		return NO;
	}
	if (pnt.x < -15) pnt.x = -15;
	else if (pnt.x > 335) pnt.x = 335;
	if (pnt.y < -15) pnt.y = -15;
	else if (pnt.y > 235) pnt.y = 235;
	location = pnt;

	[[NXApp delegate] spotDidChange];
	return YES;
}

- (BOOL) doLock
{
	if (locked) return NO;
	locked = YES;
	return YES;
}
	
- unlock
{
	locked = NO;
	return self;
}

- invalidate
{	isValid = NO;
	return nil;
}

- (BOOL) isValid
{	return isValid;
}

- encodeRemotelyFor:(NXConnection *)connection
	freeAfterEncoding:(BOOL *)flagp
	isBycopy:(BOOL)isBycopy
{
	if (isBycopy) return self;
	return [super encodeRemotelyFor:connection
				freeAfterEncoding:flagp
				isBycopy:isBycopy];
}

- encodeUsing:(id <NXEncoding>)portal
{
	//xxx should encode valid flag

	float r,g,b;
	NXConvertColorToRGB(color, &r, &g, &b);
	[portal encodeData:&location.x ofType:"f"];
	[portal encodeData:&location.y ofType:"f"];

	[portal encodeData:&r ofType:"f"];
	[portal encodeData:&g ofType:"f"];
	[portal encodeData:&b ofType:"f"];

	return self;
}

- decodeUsing:(id <NXDecoding>)portal
{
	float r,g,b;
	NXPoint p;
	NXColor c;

	[portal decodeData:&p.x ofType:"f"];
	[portal decodeData:&p.y ofType:"f"];

	[portal decodeData:&r ofType:"f"];
	[portal decodeData:&g ofType:"f"];
	[portal decodeData:&b ofType:"f"];

	c = NXConvertRGBToColor(r,g,b);

	// [self init];	//not really necessary in this case...
	color = c;
	location = p;

	return self;
}

- (unsigned) references
{
	return refs;
}

- addReference
{
	refs++;
//	printf("Spot: adding reference, refs = %d\n",(int)refs);
	return self;
}

- free
{
	refs--;
//	printf("Spot: decrementing reference, refs = %d\n", (int)refs);
	if (refs > 0) return self;

//	printf("Spot: ...and freeing\n");

	// this cleans up remote connections that still have refs to theSpot
	[NXConnection removeObject:self];

	return [super free];
}

@end



unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.