-
Notifications
You must be signed in to change notification settings - Fork 13
/
HyperspaceView.mm
359 lines (302 loc) · 10.6 KB
/
HyperspaceView.mm
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
//
// HyperspaceView.m
// Hyperspace
//
// Created by Nick Zitzmann on 2/19/06.
// Copyright (c) 2006, Nick Zitzmann. All rights reserved.
//
/*
* This file is part of Hyperspace.
*
* Hyperspace is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Hyperspace 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#import "HyperspaceView.h"
#import "Hyperspace.h"
#import "flare.h"
#import <sys/time.h>
#import <OpenGL/OpenGL.h>
#import "Hyperspace/extensions.h"
@interface HyperspaceView (Private)
- (void)readDefaults:(ScreenSaverDefaults *)inDefaults;
- (void)setDialogValue;
@end
@implementation HyperspaceView
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
NSString *identifier = [[NSBundle bundleForClass:[self class]] bundleIdentifier];
ScreenSaverDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:identifier];
self = [super initWithFrame:frame isPreview:isPreview];
if (self)
{
lSettings.doingPreview = isPreview;
if (isPreview)
lMainScreen = YES;
else
lMainScreen = (frame.origin.x == 0 && frame.origin.y == 0) ? YES : NO;
lIsConfiguring = NO;
lMainScreenOnly = [defaults boolForKey:@"MainScreenOnly"];
if (lMainScreen || !lMainScreenOnly)
{
NSOpenGLPixelFormatAttribute attribs[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)32,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAMinimumPolicy,
NSOpenGLPFAAllowOfflineRenderers,
(NSOpenGLPixelFormatAttribute)0
};
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
if (format)
{
lView = [[NSOpenGLView alloc] initWithFrame:NSZeroRect pixelFormat:format];
lView.wantsBestResolutionOpenGLSurface = YES;
[self addSubview:lView];
lSettings.frameTime = 0;
[[lView openGLContext] makeCurrentContext]; // must do this before RSSShadersSupported() will work
setDefaults(&lSettings);
if ([defaults objectForKey:@"Speed"])
{
[self readDefaults:defaults];
}
self.animationTimeInterval = 1.0/60.0;
lSettings.first = 1;
}
}
}
return self;
}
- (void)setFrameSize:(NSSize)size
{
[super setFrameSize:size];
if (lView)
[lView setFrameSize:size];
if (lView.wantsBestResolutionOpenGLSurface) // on Lion & later, if we're using a best resolution surface, then call glViewport() with the appropriate width and height for the backing
{
NSRect newBounds = [self convertRectToBacking:self.bounds];
reshape(int(newBounds.size.width), int(newBounds.size.height), &lSettings);
}
else
reshape(int(size.width), int(size.height), &lSettings);
}
- (void)viewDidMoveToWindow
{
[super viewDidMoveToWindow];
if (@available(macOS 12.0, *)) // on Monterey and later, update the time interval for the window's screen's refresh rate
{
self.animationTimeInterval = self.window.screen.maximumRefreshInterval;
}
}
- (void)startAnimation
{
[super startAnimation];
if (lIsConfiguring == NO && lView)
{
if (lMainScreen || !lMainScreenOnly)
{
GLint interval = 1;
[self lockFocus];
[[lView openGLContext] makeCurrentContext];
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &interval); // don't allow screen tearing
[[lView openGLContext] flushBuffer];
initSaver(&lSettings);
gettimeofday(&timer.prev_tv, NULL); // reset the timer
[self unlockFocus];
}
}
}
- (void)stopAnimation
{
[super stopAnimation];
if (lIsConfiguring == NO && lView)
{
if (lMainScreen || !lMainScreenOnly)
{
[[lView openGLContext] makeCurrentContext];
cleanUp(&lSettings);
lSettings.frameTime=0;
}
}
}
- (void)drawRect:(NSRect)rect
{
[[NSColor blackColor] set];
NSRectFill(rect);
if (lView == nil)
{
if (lMainScreen || !lMainScreenOnly)
{
NSRect tFrame=[self frame];
NSRect tStringFrame;
NSDictionary * tAttributes;
NSString * tString;
NSMutableParagraphStyle * tParagraphStyle;
tParagraphStyle=[[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[tParagraphStyle setAlignment:NSCenterTextAlignment];
tAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,[NSColor whiteColor],NSForegroundColorAttributeName,tParagraphStyle,NSParagraphStyleAttributeName,nil];
tString=NSLocalizedStringFromTableInBundle(@"Minimum OpenGL requirements\rfor this Screen Effect\rnot available\ron your graphic card.",@"Localizable",[NSBundle bundleForClass:[self class]],@"No comment");
tStringFrame.origin=NSZeroPoint;
tStringFrame.size=[tString sizeWithAttributes:tAttributes];
tStringFrame=SSCenteredRectInRect(tStringFrame,tFrame);
[tString drawInRect:tStringFrame withAttributes:tAttributes];
}
}
}
- (void)animateOneFrame
{
if (lIsConfiguring == NO && lView)
{
if (lMainScreen || !lMainScreenOnly)
{
lSettings.frameTime = timer.tick();
if (lSettings.readyToDraw)
{
[[lView openGLContext] makeCurrentContext];
draw(&lSettings);
[[lView openGLContext] flushBuffer];
}
}
}
}
- (BOOL)hasConfigureSheet
{
return (lView != nil);
}
- (NSWindow*)configureSheet
{
lIsConfiguring = YES;
if (ibConfigureSheet == nil)
{
if ([[NSBundle bundleForClass:self.class] loadNibNamed:@"ConfigureSheet" owner:self topLevelObjects:NULL])
{
[ibVersionTxt setStringValue:[[[NSBundle bundleForClass:[self class]] infoDictionary] objectForKey:@"CFBundleVersion"]]; // set the version text
[self setDialogValue];
}
else
NSLog(@"Warning: %@ couldn't load ConfigureSheet.nib.", [self className]);
}
return ibConfigureSheet;
}
#pragma mark -
- (IBAction)closeSheet:(id)sender
{
NSString *identifier = [[NSBundle bundleForClass:[self class]] bundleIdentifier];
ScreenSaverDefaults *inDefaults = [ScreenSaverDefaults defaultsForModuleWithName:identifier];
[inDefaults setInteger:lSettings.dSpeed forKey:@"Speed"];
[inDefaults setInteger:lSettings.dStars forKey:@"Stars"];
[inDefaults setInteger:lSettings.dStarSize forKey:@"StarSize"];
[inDefaults setInteger:lSettings.dResolution forKey:@"Resolution"];
[inDefaults setInteger:lSettings.dDepth forKey:@"Depth"];
[inDefaults setInteger:lSettings.dFov forKey:@"Fov"];
[inDefaults setBool:lSettings.dShaders forKey:@"UseShaders"];
[inDefaults setBool:lSettings.dUseGoo forKey:@"UseGoo"];
[inDefaults setBool:lSettings.dUseTunnels forKey:@"UseTunnels"];
[inDefaults setBool:lMainScreenOnly forKey:@"MainScreenOnly"];
[inDefaults synchronize];
lIsConfiguring = NO;
if ([self isAnimating])
{
[self stopAnimation];
[self startAnimation];
}
[NSApp endSheet:ibConfigureSheet];
[ibConfigureSheet orderOut:self];
}
- (IBAction)reset:(id)sender
{
setDefaults(&lSettings);
[self setDialogValue];
}
- (IBAction)setValueForSender:(id)sender
{
switch ([sender tag])
{
case 1: // resolution of geometry
lSettings.dResolution = [sender intValue];
[ibResolutionTxt setIntValue:[sender intValue]];
break;
case 2: // number of stars
lSettings.dStars = [sender intValue];
[ibStarsTxt setIntValue:[sender intValue]];
break;
case 3: // speed
lSettings.dSpeed = [sender intValue];
[ibSpeedTxt setIntValue:[sender intValue]];
break;
case 4: // size of stars
lSettings.dStarSize = [sender intValue];
[ibStarSizeTxt setIntValue:[sender intValue]];
break;
case 5: // view depth
lSettings.dDepth = [sender intValue];
[ibDepthTxt setIntValue:[sender intValue]];
break;
case 6: // field of view
lSettings.dFov = [sender intValue];
[ibFovTxt setIntValue:[sender intValue]];
break;
case 7: // shaders
lSettings.dShaders = ([sender state] == NSOnState ? true : false);
break;
case 8: // main screen
lMainScreenOnly = ([sender state] == NSOnState ? true : false);
break;
case 9: // goo
lSettings.dUseGoo = ([sender state] == NSOnState ? true : false);
break;
case 10: // tunnels
lSettings.dUseTunnels = ([sender state] == NSOnState ? true : false);
break;
}
}
@end
@implementation HyperspaceView (Private)
- (void)readDefaults:(ScreenSaverDefaults *)inDefaults
{
lSettings.dSpeed = int([inDefaults integerForKey:@"Speed"]);
lSettings.dStars = int([inDefaults integerForKey:@"Stars"]);
lSettings.dStarSize = int([inDefaults integerForKey:@"StarSize"]);
lSettings.dResolution = int([inDefaults integerForKey:@"Resolution"]);
lSettings.dDepth = int([inDefaults integerForKey:@"Depth"]);
lSettings.dFov = int([inDefaults integerForKey:@"Fov"]);
lSettings.dShaders = [inDefaults integerForKey:@"UseShaders"];
if ([inDefaults objectForKey:@"UseGoo"])
lSettings.dUseGoo = [inDefaults integerForKey:@"UseGoo"];
if ([inDefaults objectForKey:@"UseTunnels"])
lSettings.dUseTunnels = [inDefaults integerForKey:@"UseTunnels"];
lMainScreenOnly = [inDefaults boolForKey:@"MainScreenOnly"];
}
- (void)setDialogValue
{
[ibResolution setIntValue:lSettings.dResolution];
[ibResolutionTxt setIntValue:lSettings.dResolution];
[ibStars setIntValue:lSettings.dStars];
[ibStarsTxt setIntValue:lSettings.dStars];
[ibSpeed setIntValue:lSettings.dSpeed];
[ibSpeedTxt setIntValue:lSettings.dSpeed];
[ibStarSize setIntValue:lSettings.dStarSize];
[ibStarSizeTxt setIntValue:lSettings.dStarSize];
[ibDepth setIntValue:lSettings.dDepth];
[ibDepthTxt setIntValue:lSettings.dDepth];
[ibFov setIntValue:lSettings.dFov];
[ibFovTxt setIntValue:lSettings.dFov];
[ibShaderCbx setState:(lSettings.dShaders ? NSOnState : NSOffState)];
[ibGooCbx setState:(lSettings.dUseGoo ? NSOnState : NSOffState)];
[ibTunnelCbx setState:(lSettings.dUseTunnels ? NSOnState : NSOffState)];
[ibMainScreenOnlyCbx setState:(lMainScreenOnly ? NSOnState : NSOffState)];
}
@end