-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSSLifeView.m
398 lines (320 loc) · 11.6 KB
/
LSSLifeView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
// Game of Life Screen Saver
// LSSLifeView.m
// By Henry Weiss
//
// Conway's Game of Life...turned into a screen saver! In addition to using a configurable
// grid size, it also implements a "cell history" feature, based off a similar one from Mirek's
// Java Cellebration. And, yes, this has probably been done many times before, and they're
// probably a lot better too, but oh well. Another Game of Life screen saver can't hurt, can it?
//
// The "LSS" prefix stands for "Life Screen Saver."
//
#import "LSSLifeView.h"
@implementation LSSLifeView
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
if (self = [super initWithFrame:frame isPreview:isPreview])
{
// Set the factory default options
NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kDrawGridLinesPref,
[NSNumber numberWithInt:100], kGridWidthPref,
[NSNumber numberWithInt:75], kGridHeightPref,
[NSArchiver archivedDataWithRootObject:[NSColor blackColor]], kGridBGColorPref,
[NSArchiver archivedDataWithRootObject:[NSColor whiteColor]], kGridLineColorPref,
[NSArchiver archivedDataWithRootObject:[NSColor blueColor]], kCellStartColorPref,
[NSArchiver archivedDataWithRootObject:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.2 alpha:1.0]], kCellEndColorPref,
[NSNumber numberWithFloat:12.0f], kGensPerSecPref,
[NSNumber numberWithInt:1], kRandomCellsPref,
nil];
// Register the factory defaults
ScreenSaverDefaults *prefs = [ScreenSaverDefaults defaultsForModuleWithName:[[NSBundle bundleForClass:[self class]] bundleIdentifier]];
[prefs registerDefaults:defaults];
// Init the array to nil
cells = nil;
}
return self;
}
// Initializes the board first and caches some of the preference values before starting the animation
- (void)startAnimation
{
ScreenSaverDefaults *prefs = [ScreenSaverDefaults defaultsForModuleWithName:[[NSBundle bundleForClass:[self class]] bundleIdentifier]];
// Release our cached variables to get the reference count down to zero
[bgColor release];
[gridColor release];
[cellStartColor release];
[cellEndColor release];
// Cache certain variables first
shouldDrawGrid = [prefs boolForKey:kDrawGridLinesPref];
bgColor = [[NSUnarchiver unarchiveObjectWithData:[prefs objectForKey:kGridBGColorPref]] retain];
gridColor = [[NSUnarchiver unarchiveObjectWithData:[prefs objectForKey:kGridLineColorPref]] retain];
cellStartColor = [[NSUnarchiver unarchiveObjectWithData:[prefs objectForKey:kCellStartColorPref]] retain];
cellEndColor = [[NSUnarchiver unarchiveObjectWithData:[prefs objectForKey:kCellEndColorPref]] retain];
randomCells = [prefs integerForKey:kRandomCellsPref];
// Free the memory allocated for the grid if we
// have already allocated it before
if (cells != nil)
{
for (int i = 0; i < gridHeight; i++)
{
free(cells[i]);
}
free(cells);
}
// Now access the grid dimensions (prevents it from
// screwing up the free() calls above).
gridWidth = [prefs integerForKey:kGridWidthPref];
gridHeight = [prefs integerForKey:kGridHeightPref];
// Scale down the grid size if this is a preview
if ([self isPreview])
{
gridWidth *= 0.75;
gridHeight *= 0.75;
randomCells = MAX(randomCells * 0.75, 1); // Make sure we don't stagnate, even if the user specifies 0
}
// Cache certain calculations
rowHeight = ([self frame].size.height / gridHeight);
colWidth = ([self frame].size.width / gridWidth);
// Now allocate the arrays for the game state
cells = (Cell **)calloc(gridHeight, sizeof(Cell *));
for (int i = 0; i < gridHeight; i++)
{
cells[i] = (Cell *)calloc(gridWidth, sizeof(Cell));
}
// Initialize the cells to a random state
[self randomizeGrid];
// And now we can start the animation
[self setAnimationTimeInterval:(1 / [prefs floatForKey:kGensPerSecPref])];
[super startAnimation];
}
- (void)stopAnimation
{
[super stopAnimation];
}
// Randomly regenerates the cell population to a random state (each cell has
// a 1/5 chance of being alive). This is called at the beginning and whenever
// the cell population dies.
- (void)randomizeGrid
{
for (int row = 0; row < gridHeight; row++)
{
for (int col = 0; col < gridWidth; col++)
{
if (SSRandomIntBetween(1, 5) == 1)
{
cells[row][col].alive = YES;
cells[row][col].age = 0;
}
else
{
cells[row][col].alive = NO;
cells[row][col].age = -1;
}
}
}
}
// Pressing G toggles grid lines
- (void)keyDown:(NSEvent *)event
{
if ([[event charactersIgnoringModifiers] isEqualToString:@"g"])
{
shouldDrawGrid = !shouldDrawGrid;
}
else
{
[super keyDown:event];
}
}
// Updates the board state
- (void)animateOneFrame
{
// Insert some random cells to prevent deadlock
for (int i = 0; i < randomCells; i++)
{
int row = SSRandomIntBetween(0, gridHeight - 1);
int col = SSRandomIntBetween(0, gridWidth - 1);
cells[row][col].alive = YES;
cells[row][col].age = MAX(0, cells[row][col].age);
}
// Calculate how many neighbors each cell has
int neighbors[gridHeight][gridWidth];
for (int row = 0; row < gridHeight; row++)
{
for (int col = 0; col < gridWidth; col++)
{
// Set the bounds to search
int minRow = MAX(row - 1, 0);
int maxRow = MIN(row + 1, gridHeight - 1);
int minColumn = MAX(col - 1, 0);
int maxColumn = MIN(col + 1, gridWidth - 1);
// Adjust for current cell
int numberNeighbors = 0;
if (cells[row][col].alive)
numberNeighbors = -1;
// Count the number of live neighbors
for (int i = minRow; i <= maxRow; i++)
{
for (int j = minColumn; j <= maxColumn; j++)
{
if (cells[i][j].alive)
numberNeighbors++;
}
}
neighbors[row][col] = numberNeighbors;
}
}
// Update the generation
BOOL liveCellFound = NO; // Checks if the entire population died out
for (int row = 0; row < gridHeight; row++)
{
for (int col = 0; col < gridWidth; col++)
{
if (cells[row][col].alive)
{
if (neighbors[row][col] == 2 || neighbors[row][col] == 3)
{
cells[row][col].alive = YES;
cells[row][col].age = MIN(cells[row][col].age + 1, kMaxCellAge); // Bounds checking
}
else
{
cells[row][col].alive = NO;
cells[row][col].age = -1;
}
}
else
{
if (neighbors[row][col] == 3)
{
cells[row][col].alive = YES;
cells[row][col].age++; // Cell was age -1 before, so we don't have to do bounds checking
}
else
{
cells[row][col].alive = NO;
cells[row][col].age = 0;
}
}
// Is this cell alive? If so, then we have at least one live cell,
// so we won't have to randomly regenerate the cell grid.
liveCellFound = liveCellFound || cells[row][col].alive;
}
}
if (!liveCellFound)
{
// Randomly respawn the population
[self randomizeGrid];
}
// Draw the current board state
[self setNeedsDisplay:YES];
}
// Board rendering
- (void)drawRect:(NSRect)rect
{
// Draw the background first
[bgColor set];
[[NSBezierPath bezierPathWithRect:rect] fill];
// Draw the grid lines (if allowed to do so)
if (shouldDrawGrid)
{
[gridColor set];
cachedPath = [NSBezierPath bezierPath];
// Draw a big box around the frame
[cachedPath setLineWidth:1.0f];
[cachedPath appendBezierPath:[NSBezierPath bezierPathWithRect:rect]];
[cachedPath stroke];
[cachedPath setLineWidth:0.5f];
// Draw all the horizontal lines first
for (int i = 0; i < gridHeight; i++)
{
[cachedPath moveToPoint:NSMakePoint(0, i * rowHeight)];
[cachedPath lineToPoint:NSMakePoint(rect.size.width, i * rowHeight)];
}
// Then draw all the vertical lines
for (int i = 0; i < gridWidth; i++)
{
[cachedPath moveToPoint:NSMakePoint(i * colWidth, 0)];
[cachedPath lineToPoint:NSMakePoint(i * colWidth, rect.size.height)];
}
[cachedPath stroke];
}
// Draw the board
for (int row = 0; row < gridHeight; row++)
{
for (int col = 0; col < gridWidth; col++)
{
// Only draw if alive
if (cells[row][col].alive)
{
NSBezierPath *path = [NSBezierPath bezierPath];
float age = (float)cells[row][col].age; // Typecasted for later calcuations
// Calculate the current color based on age (i.e. how much it has faded to the end color)
NSColor *currentColor = [cellStartColor blendedColorWithFraction:(age / kMaxCellAge) ofColor:cellEndColor];
if (!currentColor)
currentColor = cellStartColor; // If it can't be blended for some reason
[currentColor set];
// Draw the cell rect
NSRect cellRect = NSMakeRect(col * colWidth, row * rowHeight, colWidth, rowHeight);
[path appendBezierPathWithRect:cellRect];
[path fill];
}
}
}
}
// Configuration stuff
- (BOOL)hasConfigureSheet
{
return YES;
}
- (NSWindow *)configureSheet
{
// Load the sheet if it hasn't been loaded
if (!configSheet)
{
if (![NSBundle loadNibNamed:@"ConfigSheet" owner:self])
{
NSRunAlertPanel(NSLocalizedString(@"Couldn't load the options sheet", @"Bundle load error"),
NSLocalizedString(@"This is probably an internal error. Try reinstalling the screen saver. If the problem persists, contact the developer.",
@"Bundle load error description."),
@"OK", nil, nil);
}
}
// Load the preferences into the sheet
ScreenSaverDefaults *prefs = [ScreenSaverDefaults defaultsForModuleWithName:[[NSBundle bundleForClass:[self class]] bundleIdentifier]];
[drawGridLinesSwitch setState:[prefs boolForKey:kDrawGridLinesPref]];
[widthField setIntValue:[prefs integerForKey:kGridWidthPref]];
[heightField setIntValue:[prefs integerForKey:kGridHeightPref]];
[bgColorWell setColor:[NSUnarchiver unarchiveObjectWithData:[prefs objectForKey:kGridBGColorPref]]];
[gridColorWell setColor:[NSUnarchiver unarchiveObjectWithData:[prefs objectForKey:kGridLineColorPref]]];
[cellStartColorWell setColor:[NSUnarchiver unarchiveObjectWithData:[prefs objectForKey:kCellStartColorPref]]];
[cellEndColorWell setColor:[NSUnarchiver unarchiveObjectWithData:[prefs objectForKey:kCellEndColorPref]]];
[gensField setFloatValue:[prefs floatForKey:kGensPerSecPref]];
[randCellField setIntValue:[prefs integerForKey:kRandomCellsPref]];
return configSheet;
}
- (IBAction)endConfigSheet:(id)sender
{
// Was this an OK?
if ([sender tag] == NSOKButton)
{
ScreenSaverDefaults *prefs = [ScreenSaverDefaults defaultsForModuleWithName:[[NSBundle bundleForClass:[self class]] bundleIdentifier]];
// Save the preferences
[prefs setBool:[drawGridLinesSwitch state] forKey:kDrawGridLinesPref];
[prefs setInteger:MAX(MIN([widthField intValue], kMaxCellWidth), kMinCellWidth) forKey:kGridWidthPref];
[prefs setInteger:MAX(MIN([heightField intValue], kMaxCellHeight), kMinCellHeight) forKey:kGridHeightPref];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[bgColorWell color]] forKey:kGridBGColorPref];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[gridColorWell color]] forKey:kGridLineColorPref];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[cellStartColorWell color]] forKey:kCellStartColorPref];
[prefs setObject:[NSArchiver archivedDataWithRootObject:[cellEndColorWell color]] forKey:kCellEndColorPref];
[prefs setFloat:MAX(MIN([gensField floatValue], kMaxGensPerSec), kMinGensPerSec) forKey:kGensPerSecPref];
// The upper bound for the random cells depends on the current width/height
int upperBound = MIN([prefs integerForKey:kGridHeightPref], [prefs integerForKey:kGridWidthPref]);
[prefs setInteger:MAX(MIN([randCellField intValue], upperBound), 0) forKey:kRandomCellsPref];
// Actually save changes now
[prefs synchronize];
}
// Close out the sheet
[[NSApplication sharedApplication] endSheet:configSheet];
}
@end