Tag Archives: osx

Apologize…

I know its been awhile since I’ve posted on here.  About a month ago, I got a new job where I’m working overnights so its been tough getting programming as a hobby in and especially writing as it while I adjust to the new work shift.  Some projects I’ve been working on:

1. I’ve gotten real heavy into using microprocessors.  Recently, I’ve gone to programming on the Arduino to using the ATMega in its own circuit (programmed while on the Arduino board for testing and then once ready to complete, finishing it on its own circuit.)

2. As usual, working on an XFDL for Linux project.  The GUI in WXWidgets is coming along, working out using libxml2 to parse the file, which is taking awhile but its beginning to flow.  Keep checking back for updates as they happen.

3. Finally got a Mac OSX running with xCode in Virtual Box.  I’m going to get back into Objective C and iPhone apps as I get it working more smoothly.

Thank you for checking in, I’ll be posting more regularly from here on out.

Fern Screensaver : Part 4

I’ve now completed the Fern Screensaver in Objective-C for the Mac!  Surprisingly, using Objective-C was not that far from C/C++.  In fact, much of my code could be pasted directly in.  That includes the variables, math functions, etc.  Also, Mac has done a good job of documenting it’s libraries so using the NSObjects was not that difficult between Google and a quick reference search along with the example screensaver code.  I’m going to hit a few key differences and then post the entire file I edited.  XCode does the templating pretty well.

First, all the variables are at the top of the code so they don’t change as the program advances frames.

// VARIABLES
float mat[4][7] = {
//...

@implementation ZackScreenSaverView

Secondly, I had to use implement my NSObjects at the start of the -(void)animateOneFrame method.  The size = [self bounds].size; calls the screensize.  It’ll take some time to get used to the method calls in Objective-C!

- (void)animateOneFrame
{
	NSRect dot;
	NSSize size;
	NSColor *color;
	NSBezierPath *path;
	float red,green,blue,alpha;

	size = [self bounds].size;

The framework has some good methods for getting random floats, or any integer type!

	// random number
	p = SSRandomFloatBetween(0.0,1.0);

Finally, it took some new methods to draw my dots to the screen.

	// set dot
	dot = NSMakeRect(jx,jy,1.0,1.0);

	// make dot
	path = [NSBezierPath bezierPathWithRect:dot];

	// draw dot
	[path stroke];

Other than that, the rest is pretty much the same.  I encourage those more used to Objective-C and programming for Mac products to comment about the use of this code and correct any bad assumptions!  Here is the code in it’s entirety (note that I’ve taken out the redraw at this time)…

//
//  ZackScreenSaverView.m
//  ZackScreenSaver
//
//  Created by Zachary D. Skelton on 5/30/10.
//  Copyright (c) 2010, Skelton Networks. All rights reserved.
//

#import "ZackScreenSaverView.h"

// VARIABLES
float mat[4][7] = {
	{0.0,0.0,0.0,0.16,0.0,0.0,0.01},
	{0.85,0.04,-0.04,0.85,0.0,1.6,0.85},
	{0.2,-0.25,0.23,0.22,0.0,1.6,0.07},
	{-0.15,0.28,0.26,0.24,0.0,0.44,0.07}
};
// constant sets
float xa = -5.5;
float xb = 6.5;
float ya = -0.5;
float yb = 10.5;
// x and y to be altered
float x = 0.0;
float y = 0.0;
// screen/image size
int ix = 512;
int iy = 512;
// floats for math fulction
float p = 0.0;
float x0 = 0;
float jx = 0;
float jy = 0;
// variables to control function
int i = 0;
int k = 1;
int end = 10000;

@implementation ZackScreenSaverView

- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
    self = [super initWithFrame:frame isPreview:isPreview];
    if (self) {
        [self setAnimationTimeInterval:1/30.0];
    }
    return self;
}

- (void)startAnimation
{
    [super startAnimation];
}

- (void)stopAnimation
{
    [super stopAnimation];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];
}

- (void)animateOneFrame
{
	NSRect dot;
	NSSize size;
	NSColor *color;
	NSBezierPath *path;
	float red,green,blue,alpha;

	size = [self bounds].size;

	ix = size.width;
	iy = size.height;
	dot = NSMakeRect(ix/2,iy/2,1.0,1.0);

	red = 255.0;
	green = 255.0;
	blue = 255.0;
	alpha = 255.0;
	color = [NSColor colorWithCalibratedRed: red
									  green: green
									   blue: blue
									  alpha: alpha];

	[color set];

	// random number
	p = SSRandomFloatBetween(0.0,1.0);

	// find random i
	if(p <= mat[0][6])
		i = 0;
	else if(p <= (mat[0][6] + mat[1][6]))
		i = 1;
	else if(p <= (mat[0][6] + mat[1][6] + mat[2][6]))
		i = 2;
	else
		i = 3;

	// super math function
	x0 = ((x*mat[i][0])+(y*mat[i][1])+mat[i][4]);
	y = (x*mat[i][2]+y*mat[i][3]+mat[i][5]);
	x = x0;
	jx = ((x-xa)/(xb-xa)*(ix-1));
	jy = (iy-1)-(y-ya)/(yb-ya)*iy-1;

	// set dot
	dot = NSMakeRect(jx,jy,1.0,1.0);

	// make dot
	path = [NSBezierPath bezierPathWithRect:dot];

	// draw dot
	[path stroke];

	// CLEANUP
	/*
	if(k == end) {
		k = 0;
		//sleep(5);
	} else
		k += 1;
	*/
	return;
}

- (BOOL)hasConfigureSheet
{
    return NO;
}

- (NSWindow*)configureSheet
{
    return nil;
}

@end