-
Notifications
You must be signed in to change notification settings - Fork 7
/
RSSSkyrocketSaverView.mm
560 lines (403 loc) · 15.8 KB
/
RSSSkyrocketSaverView.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Mac OS X Code
//
// First version: 07/07/02 StŽphane Sudre
#import "RSSSkyrocketSaverView.h"
#include "Skyrocket.h"
#include <sys/time.h>
#import <OpenGL/OpenGL.h>
@implementation RSSSkyrocketSaverView
- (id)initWithFrame:(NSRect)frameRect isPreview:(BOOL) preview
{
NSString *identifier = [[NSBundle bundleForClass:[self class]] bundleIdentifier];
ScreenSaverDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:identifier];
id tObject;
self = [super initWithFrame:frameRect isPreview:preview];
preview_=preview;
isConfiguring_=NO;
if (preview_==YES)
{
mainScreen_=YES;
}
else
{
mainScreen_= (frameRect.origin.x==0 && frameRect.origin.y==0) ? YES : NO;
}
mainScreenOnly_=int([defaults integerForKey:@"MainScreen Only"]);
if (self)
{
soundDisabled_ = NO;
if (mainScreenOnly_!=NSOnState || mainScreen_==YES)
{
NSOpenGLPixelFormatAttribute attribs[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAMinimumPolicy,
NSOpenGLPFAAllowOfflineRenderers,
(NSOpenGLPixelFormatAttribute)0
};
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
if (format!=nil)
{
_view = [[NSOpenGLView alloc] initWithFrame:NSZeroRect pixelFormat:format];
[self addSubview:_view];
_view.wantsBestResolutionOpenGLSurface = YES; // enable Retina support
settings_.frameTime=0;
setDefaults(&settings_);
tObject=[defaults objectForKey:@"WindSpeed"];
if (tObject)
{
[self readDefaults:defaults];
}
self.animationTimeInterval = 1.0/60.0;
}
}
}
return self;
}
- (void) drawRect:(NSRect) inFrame
{
[[NSColor blackColor] set];
NSRectFill(inFrame);
if (_view==nil)
{
if (mainScreenOnly_!=NSOnState || mainScreen_==YES)
{
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];
return;
}
}
}
- (void) setFrameSize:(NSSize)newSize
{
[super setFrameSize:newSize];
if (_view!=nil)
{
[_view setFrameSize:newSize];
}
}
- (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)animateOneFrame
{
if (isConfiguring_==NO && _view!=nil)
{
if (mainScreenOnly_!=NSOnState || mainScreen_==YES)
{
struct timeval tTime;
unsigned long long tCurentTime;
[[_view openGLContext] makeCurrentContext];
gettimeofday(&tTime, NULL);
tCurentTime=(tTime.tv_sec*1000+tTime.tv_usec/1000);
if(tCurentTime >= tLastTime)
times[timeindex] = float(tCurentTime - tLastTime) * 0.001f;
else // else use elapsedTime from last frame
times[timeindex] = settings_.frameTime;
settings_.frameTime = 0.1f * (times[0] + times[1] + times[2] + times[3] + times[4]
+ times[5] + times[6] + times[7] + times[8] + times[9]);
timeindex ++;
if(timeindex >= 10)
timeindex = 0;
tLastTime=tCurentTime;
draw(&settings_);
[[_view openGLContext] flushBuffer];
}
}
}
- (void)startAnimation
{
[super startAnimation];
if (isConfiguring_==NO && _view!=nil)
{
if (mainScreenOnly_!=NSOnState || mainScreen_==YES)
{
NSSize tSize = [_view convertSizeToBacking:_view.frame.size];
struct timeval tTime;
int i;
GLint interval = 1;
[self lockFocus];
[[_view 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
[[_view openGLContext] flushBuffer];
//cleanSettings(&settings_);
initSaver((int) tSize.width,(int) tSize.height,&settings_);
for(i=0;i<10;i++)
{
times[i] = self.animationTimeInterval;
}
timeindex = 0;
settings_.frameTime=0;
[self unlockFocus];
gettimeofday(&tTime, NULL);
tLastTime=(tTime.tv_sec*1000+tTime.tv_usec/1000);
}
}
}
- (void)stopAnimation
{
[super stopAnimation];
if (_view!=nil)
{
if (mainScreenOnly_!=NSOnState || mainScreen_==YES)
{
[[_view openGLContext] makeCurrentContext];
//cleanSettings(&settings_);
cleanup(&settings_);
settings_.frameTime=0;
}
}
}
- (BOOL) hasConfigureSheet
{
return (_view!=nil);
}
- (void) readDefaults:(ScreenSaverDefaults *) inDefaults
{
// jes: looks like I can make these what I want them to be as long as I'm
// consistent throughout file. I think this is the key for the prefs
// file, so... make it user readable
settings_.dMaxrockets=int([inDefaults integerForKey:@"MaxRockets"]);
settings_.dSmoke=int([inDefaults integerForKey:@"SmokeLifespan"]);
settings_.dExplosionsmoke=int([inDefaults integerForKey:@"SmokePerExplosion"]);
settings_.dWind=int([inDefaults integerForKey:@"WindSpeed"]);
settings_.dAmbient=int([inDefaults integerForKey:@"AmbientLight"]);
settings_.dStardensity=int([inDefaults integerForKey:@"StarDensity"]);
settings_.dFlare=int([inDefaults integerForKey:@"LensFlareBrightness"]);
settings_.dMoonglow=int([inDefaults integerForKey:@"MoonGlowBrightness"]);
if (soundDisabled_ == NO)
settings_.dSound = int([inDefaults integerForKey:@"SoundVolume"]);
settings_.kCamera=int([inDefaults integerForKey:@"CameraMovement"]);
settings_.kSlowMotion = [inDefaults boolForKey:@"SlowMotion"];
settings_.dMoon=int([inDefaults integerForKey:@"DrawMoon"]);
settings_.dEarth=int([inDefaults integerForKey:@"DrawEarth"]);
settings_.dClouds=int([inDefaults integerForKey:@"DrawClouds"]);
settings_.dIllumination=int([inDefaults integerForKey:@"Illumination"]);
mainScreenOnly_=int([inDefaults integerForKey:@"MainScreen Only"]);
}
- (void) writeDefaults
{
NSString *identifier = [[NSBundle bundleForClass:[self class]] bundleIdentifier];
ScreenSaverDefaults *inDefaults = [ScreenSaverDefaults defaultsForModuleWithName:identifier];
mainScreenOnly_=int([IBmainScreen_ state]);
settings_.dClouds=int([IBclouds_ state]);
settings_.dEarth=int([IBearth_ state]);
settings_.dMoon=int([IBmoon_ state]);
settings_.dIllumination=int([IBillumination_ state]);
settings_.kCamera=int([IBcamera_ state]);
settings_.kSlowMotion = ([IBslowMotion_ state] == NSOnState ? true : false);
[inDefaults setInteger:settings_.dMaxrockets forKey:@"MaxRockets"];
[inDefaults setInteger:settings_.dSmoke forKey:@"SmokeLifespan"];
[inDefaults setInteger:settings_.dExplosionsmoke forKey:@"SmokePerExplosion"];
[inDefaults setInteger:settings_.dWind forKey:@"WindSpeed"];
[inDefaults setInteger:settings_.dAmbient forKey:@"AmbientLight"];
[inDefaults setInteger:settings_.dStardensity forKey:@"StarDensity"];
[inDefaults setInteger:settings_.dFlare forKey:@"LensFlareBrightness"];
[inDefaults setInteger:settings_.dMoonglow forKey:@"MoonGlowBrightness"];
[inDefaults setInteger:settings_.dSound forKey:@"SoundVolume"];
[inDefaults setInteger:settings_.kCamera forKey:@"CameraMovement"];
[inDefaults setBool:settings_.kSlowMotion forKey:@"SlowMotion"];
[inDefaults setInteger:settings_.dMoon forKey:@"DrawMoon"];
[inDefaults setInteger:settings_.dEarth forKey:@"DrawEarth"];
[inDefaults setInteger:settings_.dClouds forKey:@"DrawClouds"];
[inDefaults setInteger:settings_.dIllumination forKey:@"Illumination"];
[inDefaults setInteger:mainScreenOnly_ forKey:@"MainScreen Only"];
[inDefaults synchronize];
}
- (void) setDialogValue
{
[IBmaxRocketsStepper_ setIntValue:settings_.dMaxrockets];
[IBmaxRocketsTextField_ setIntValue:settings_.dMaxrockets];
[IBambientSlider_ setIntValue:settings_.dAmbient];
[IBambientText_ setIntValue:settings_.dAmbient];
[IBflareSlider_ setIntValue:settings_.dFlare];
[IBflareText_ setIntValue:settings_.dFlare];
[IBmoonGlowSlider_ setIntValue:settings_.dMoonglow];
[IBmoonGlowText_ setIntValue:settings_.dMoonglow];
[IBsmokeExplosionsSlider_ setIntValue:settings_.dExplosionsmoke];
[IBsmokeExplosionsText_ setIntValue:settings_.dExplosionsmoke];
[IBsmokeSlider_ setIntValue:settings_.dSmoke];
[IBsmokeText_ setIntValue:settings_.dSmoke];
[IBstarDensitySlider_ setIntValue:settings_.dStardensity];
[IBstarDensityText_ setIntValue:settings_.dStardensity];
[IBwindSlider_ setIntValue:settings_.dWind];
[IBwindText_ setIntValue:settings_.dWind];
if (soundDisabled_ == NO)
{
[IBsoundSlider_ setIntValue:settings_.dSound];
[IBsoundText_ setIntValue:settings_.dSound];
}
else
{
[IBsoundSlider_ setEnabled:NO];
[IBsoundText_ setIntValue:0];
}
[IBmainScreen_ setState:mainScreenOnly_];
[IBearth_ setState:settings_.dEarth];
[IBmoon_ setState:settings_.dMoon];
[IBillumination_ setState:settings_.dIllumination];
[IBcamera_ setState:settings_.kCamera];
[IBslowMotion_ setState:(settings_.kSlowMotion ? NSOnState : NSOffState)];
[IBclouds_ setState:settings_.dClouds];
}
- (NSWindow*)configureSheet
{
isConfiguring_=YES;
if (IBconfigureSheet_ == nil)
{
[[NSBundle bundleWithIdentifier:@"com.reallyslick.Skyrocket"] loadNibNamed:@"ConfigureSheet" owner:self topLevelObjects:NULL];
[IBversion_ setStringValue:[[[NSBundle bundleForClass:[self class]] infoDictionary] objectForKey:@"CFBundleVersion"]];
[self setDialogValue];
}
return IBconfigureSheet_;
}
- (IBAction)closeSheet:(id)sender
{
[self writeDefaults];
isConfiguring_=NO;
if ([self isAnimating]==YES)
{
[self stopAnimation];
[self startAnimation];
}
[NSApp endSheet:IBconfigureSheet_];
}
- (IBAction)reset:(id)sender
{
setDefaults(&settings_);
[self setDialogValue];
}
- (IBAction)setAmbient:(id)sender
{
settings_.dAmbient=[sender intValue];
[IBambientText_ setIntValue:settings_.dAmbient];
}
- (IBAction)setExplosionsmoke:(id)sender
{
settings_.dExplosionsmoke=[sender intValue];
[IBsmokeExplosionsText_ setIntValue:settings_.dExplosionsmoke];
}
- (IBAction)setMaxrockets:(id)sender
{
settings_.dMaxrockets=[sender intValue];
[IBmaxRocketsTextField_ setIntValue:settings_.dMaxrockets];
}
- (IBAction)setWind:(id)sender
{
settings_.dWind=[sender intValue];
[IBwindText_ setIntValue:settings_.dWind];
}
- (IBAction)setSmoke:(id)sender
{
settings_.dSmoke=[sender intValue];
[IBsmokeText_ setIntValue:settings_.dSmoke];
}
- (IBAction)setStardensity:(id)sender
{
settings_.dStardensity=[sender intValue];
[IBstarDensityText_ setIntValue:settings_.dStardensity];
}
- (IBAction)setFlare:(id)sender
{
settings_.dFlare=[sender intValue];
[IBflareText_ setIntValue:settings_.dFlare];
}
- (IBAction)setMoonglow:(id)sender
{
settings_.dMoonglow=[sender intValue];
[IBmoonGlowText_ setIntValue:settings_.dMoonglow];
}
- (IBAction)setSoundVolume:(id)sender
{
settings_.dSound = [sender intValue];
[IBsoundText_ setIntValue:settings_.dSound];
}
- (void)keyDown:(NSEvent *)anEvent
{
if (ScreenSaverProc(WM_KEYDOWN, [[anEvent characters] characterAtIndex:0], 0, &settings_) != 0)
[super keyDown:anEvent];
[[self window] setAcceptsMouseMovedEvents:(settings_.kCamera == 2)];
}
- (void)mouseDown:(NSEvent *)anEvent
{
NSPoint location = [anEvent locationInWindow];
WORD locationx = WORD(location.x);
WORD locationy = WORD([self frame].size.height-location.y);
if (ScreenSaverProc(WM_LBUTTONDOWN, unsigned(NSEventMaskFromType([anEvent type])), MAKELONG(locationx, locationy), &settings_) != 0)
[super mouseDown:anEvent];
}
- (void)mouseDragged:(NSEvent *)anEvent
{
NSPoint location = [anEvent locationInWindow];
WORD locationx = WORD(location.x);
WORD locationy = WORD([self frame].size.height-location.y);
if (ScreenSaverProc(WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(locationx, locationy), &settings_) != 0)
[super mouseDragged:anEvent];
}
- (void)mouseMoved:(NSEvent *)anEvent
{
NSPoint location = [anEvent locationInWindow];
WORD locationx = WORD(location.x);
WORD locationy = WORD([self frame].size.height-location.y);
if (ScreenSaverProc(WM_MOUSEMOVE, unsigned(NSEventMaskFromType([anEvent type])), MAKELONG(locationx, locationy), &settings_) != 0)
[super mouseMoved:anEvent];
}
- (void)mouseUp:(NSEvent *)anEvent
{
NSPoint location = [anEvent locationInWindow];
WORD locationx = WORD(location.x);
WORD locationy = WORD([self frame].size.height-location.y);
if (ScreenSaverProc(WM_LBUTTONUP, unsigned(NSEventMaskFromType([anEvent type])), MAKELONG(locationx, locationy), &settings_) != 0)
[super mouseUp:anEvent];
}
- (void)rightMouseDown:(NSEvent *)anEvent
{
NSPoint location = [anEvent locationInWindow];
WORD locationx = WORD(location.x);
WORD locationy = WORD([self frame].size.height-location.y);
if (ScreenSaverProc(WM_RBUTTONDOWN, unsigned(NSEventMaskFromType([anEvent type])), MAKELONG(locationx, locationy), &settings_) != 0)
[super rightMouseDown:anEvent];
}
- (void)rightMouseDragged:(NSEvent *)anEvent
{
NSPoint location = [anEvent locationInWindow];
WORD locationx = WORD(location.x);
WORD locationy = WORD([self frame].size.height-location.y);
if (ScreenSaverProc(WM_RBUTTONDOWN, MK_RBUTTON, MAKELONG(locationx, locationy), &settings_) != 0)
[super rightMouseDragged:anEvent];
}
- (void)rightMouseUp:(NSEvent *)anEvent
{
NSPoint location = [anEvent locationInWindow];
WORD locationx = WORD(location.x);
WORD locationy = WORD([self frame].size.height-location.y);
if (ScreenSaverProc(WM_RBUTTONUP, unsigned(NSEventMaskFromType([anEvent type])), MAKELONG(locationx, locationy), &settings_) != 0)
[super rightMouseUp:anEvent];
}
@end
@implementation NSOpenGLView (HacksRUs)
- (void)rightMouseDown:(NSEvent *)anEvent
{
[[self nextResponder] rightMouseDown:anEvent];
}
@end