Drawing a subview loaded from a bundle

Nibbie
Posts: 1
Joined: 2010.11
Post: #1
Hi,
I'm still trying to draw a ScreenSaverView. I now have an NSView subclass that is drawing properly, but I still can't get it to draw the ScreenSaverView as a subclass.
Here's my NSView subclass:

Code:
//
//  Preview.m
//  DesktopSaver
//
//  Created by Joseph Duchesne on 12/02/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "Preview.h"


@implementation Preview

void timerCallBack(CFRunLoopTimerRef timer, void *info) {
    Preview *self = (Preview *)info;
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();

    [self display];
    CFRunLoopTimerSetNextFireDate(timer, startTime + CFRunLoopTimerGetInterval(timer));
}

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {        
        CFRunLoopTimerContext context = {0, self, NULL, NULL, NULL};
        theTimer =  CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent(), 1.0/60.0, 0, 0, timerCallBack, &context);
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), theTimer, kCFRunLoopCommonModes);
    }
    return self;
}

- (void)drawRect:(NSRect)aRect {

    if(!hasLoaded)
    {
        hasLoaded=TRUE;
        SSBundle = [NSBundle bundleWithPath:@"/System/Library/Screen Savers/Flurry.saver"];

        Class SSBundleClass;

        NSLog(@"Bundle Info\n%@",[ [ SSBundle infoDictionary ] description ] );

        NSLog(@"Found and loaded principalClass");
        theStupidView = [[SSBundleClass alloc] initWithFrame:NSMakeRect(0,0,320,240) isPreview:YES];
        [theStupidView startAnimation];
        [self addSubview:theStupidView];
    }

    [[NSColor blackColor] set];
    NSRectFill(aRect);
    [theStupidView lockFocus];
    [theStupidView drawRect:aRect];
    [theStupidView unlockFocus];
    return;
}

@end

I am getting the following errors:
*** -[Preview alloc]: selector not recognized [self = 0x3d61b0]

*** NSRunLoop ignoring exception '*** -[Preview alloc]: selector not recognized [self = 0x3d61b0]' that raised during posting of delayed perform with target 34e8a0 and selector 'openPrefPaneWithParameters:'
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #2
I'm pretty certain that you don't need to implement your own NSView subclass, it should be possible to just use an NSView and call addSubview: with your screen saver view you created. In your code, where do you actually assign SSBundleClass? I cannot find that part.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #3
I changed Class SSBundleClass; to Class SSBundleClass=[ScreenSaverView class];

But it still isn't drawing anything.

I'm getting the following console errors:

2006-02-12 12:45:22.095 System Preferences[14992] *** Assertion failure in -[ScreenSaverView lockFocus], AppKit.subproj/NSView.m:3248
2006-02-12 12:45:22.096 System Preferences[14992] lockFocus sent to a view whose window is deferred and does not yet have a corresponding platform window
2006-02-12 12:45:22.259 System Preferences[14992] *** Assertion failure in -[ScreenSaverView lockFocus], AppKit.subproj/NSView.m:3248
2006-02-12 12:45:22.260 System Preferences[14992] lockFocus sent to a view whose window is deferred and does not yet have a corresponding platform window
2006-02-12 12:45:22.359 System Preferences[14992] *** Assertion failure in -[ScreenSaverView lockFocus], AppKit.subproj/NSView.m:3248
2006-02-12 12:45:22.359 System Preferences[14992] lockFocus sent to a view whose window is deferred and does not yet have a corresponding platform window
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #4
Ok, I just changed back to how it was before I made the view its own file.

here's the code:

Code:
SSBundle = [NSBundle bundleWithPath:@"/Users/josephduchesne/Library/Screen Savers/uDG Screensaver.saver"];
Class SSBundleClass=[ SSBundle principalClass ];
newView = [[SSBundleClass alloc] initWithFrame:NSMakeRect(0,0,320,240) isPreview:YES];
[theStupidView addSubview:newView];
[newView startAnimation];
The bundle is loading and init'ing properly, it's printing out the normal init stuff I made it print out but...

When it is drawn, I get a huge number of the following error:
Assertion failure in -[uDGS lockFocus], AppKit.subproj/NSView.m:3248
lockFocus sent to a view whose window is deferred and does not yet have a corresponding platform window

And that error just keeps repeating in the console until I quit.
Any idea what I'm doing wrong?
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #5
From where I'm standing, it looks like you're not adding the view to a window? You're adding it to a view as a subview, but does the superview belong to a window? And is that window shown?
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #6
The view is part of the window in IB. The window is shown and this code is not run until after that point.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2004.10
Post: #7
Joseph Duchesne Wrote:The view is part of the window in IB. The window is shown and this code is not run until after that point.

Uhh, if you're making a screen saver, you can just use the Screen Saver XCode template project... I believe these were introduced around XCode 2.1? (Someone correct me if I'm wrong.)

The template is really straightforward, and includes most of the hooks you need. (Additionally, you can forego the timer loop.) There are also a couple of excellent examples over at cocoadevcentral that get you through this step by step (for OpenGL and Quartz SSViews).

Ignore this comment if I completely missed the point or you're doing something else.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #8
I'm not creating a screensaver, I'm displaying a screensaver, like the screensaver preferencePane in System Preferences.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #9
If this isn't for a commercial product, take a look at SaverLab's source code:
http://www.dozingcatsoftware.com/

Actually, I think it is released under the MIT License, although it is not included in the source as far as I can tell (it is at the end of the online Readme)

Edit: Found it! The readme is inside the English.lproj folder 0.o
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  grayscale loaded as gl_alpha: all texture is white sefiroths 2 5,345 Jan 18, 2011 12:34 AM
Last Post: sefiroths
  grayscale loaded as gl_alpha: the opaque image can be only white? sefiroths 2 5,759 Jan 14, 2011 08:06 AM
Last Post: sefiroths
  NSView loaded from bundle won't draw... Joseph Duchesne 1 3,468 Feb 12, 2006 03:33 AM
Last Post: Cochrane