Search code examples
xcodemacoscocoaquartz-graphicsscreensaver

Cocoa Screensaver Embed Quartz


I am trying to create a screensaver in Xcode to deploy as a .saver file.

However, I want to embed a Quartz Composition file (QTZ) into it.

As there is no XIB or NIB in a screensaver template, how does one embed a qtz with code?

Here's what's in the ScreenSaverView.h:

#import <ScreenSaver/ScreenSaver.h>
@interface XmasView : ScreenSaverView
@end

And in ScreenSaverView.m,

#import "ScreenSaverView.h"

@implementation XmasView

- (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
{
    return;
}

- (BOOL)hasConfigureSheet
{
    return NO;
}

- (NSWindow*)configureSheet
{
    return nil;
}

@end

I presume I have to put some code in initWithFrame: to embed the quartz composition? If so, what would I have to put in?

Thanks in advance


Solution

  • You just need to create a QCView instance and place it as a subview of your screensaver view:

    .h:

    #import <ScreenSaver/ScreenSaver.h>
    #import <Quartz/Quartz.h>
    
    @interface XmasView : ScreenSaverView
    @property (strong) QCView* qtzView;
    @end
    

    .m:

    #import "ScreenSaverView.h"
    
    @implementation XmasView
    @synthesize qtzView;
    
    - (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
    {
        self = [super initWithFrame:frame isPreview:isPreview];
        if (self) 
        {
            [self setAnimationTimeInterval:1/30.0];
    
            //create the quartz composition view
            qtzView = [[QCView alloc] initWithFrame: NSMakeRect(0, 0, NSWidth(frame), NSHeight(frame))];
            //make sure it resizes with the screensaver view
            [qtzView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
    
            //match its frame rate to your screensaver
            [qtzView setMaxRenderingFrameRate:30.0f];
    
            //get the location of the quartz composition from the bundle
            NSString* compositionPath = [[NSBundle mainBundle] pathForResource:@"YourComposition" ofType:@"qtz"];
            //load the composition
            [qtzView loadCompositionFromFile:compositionPath];
    
            //add the quartz composition view
            [self addSubview:qtzView];
        }
        return self;
    }
    
    //...implementation continues