PDA

View Full Version : NSOpenGLView's init not being called... why?


codemattic
2002.08.11, 02:10 PM
I have a subclass of an NSOpenGLView - which I placed in a window in IB. Now there is some member variables I need to initialize when the view is created. Ive tried alternately the methods listed below. None seems to get called. My workaround is to have a method called -setup, and every other method (-drawRect, -reshape, etc) first checks if the view has been initialized, and if not calls setup first before doing anything. It works - but its inelegant - and Id like to know why I cant initialize properly. help.

thanks,
Codemattic



@interface MyOpenGLView : NSOpenGLView {
// some member variables go here
}

- (id)init;
- (id)initWithFrame:(NSRect)frame;
- (id)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat*)format;

@end



@implementation MyOpenGLView

- (id)init {
NSLog(@"init");
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}

- (id)initWithFrame:(NSRect)frame {
NSLog(@"initWithFrame:");
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}

- (id)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat*)format {
NSLog(@"initWithFrame:pixelformat:");
self = [super initWithFrame:frame pixelFormat:format];
if (self) {
// Initialization code here.
}
return self;
}

@end

kainsin
2002.08.12, 11:05 AM
Have you tried awakeFromNib? I don't believe that objects will have their init* methods called if they are stored in nib files since they are "freeze-dried" objects, not just placeholders.

GoodDoug
2002.08.12, 04:42 PM
Kainsin is exactly right. objects created from Interface Builder nibs are intited with - (id) initWithCoder: and so bypasess all of your init routines. That's what awakeFromNib does. Now, be aware that in most cases, the OpenGL context is not fully available until after awakeFromNib. The context isn't really created until it is first drawn, so any OpenGL initialization calls need to be done in the views, - (void) drawRect: method

An example

- (void) drawRect:(NSRect) inRect
{
if (!initedGL)
{
gluOrtho2D(up, down, left, right);
// etc...
}

glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glBegin{GL_POINTS);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(1.0, 1.0);

// etc...
glEnd();
glFlush();
}

codemattic
2002.08.13, 01:59 AM
Thanks guys. awakeFromNib it is... along with an extra sanity check or two. However:

Originally posted by kainsin
Have you tried awakeFromNib? I don't believe that objects will have their init* methods called if they are stored in nib files since they are "freeze-dried" objects, not just placeholders.

Originally posted by GoodDoug
Kainsin is exactly right. objects created from Interface Builder nibs are intited with - (id) initWithCoder: and so bypasess all of your init routines. That's what awakeFromNib does.

okay smarty-guys, why when I create a subclass of NSView and attach it up in IB does its -(id)initWithFrame:(NSRect)frame method get called? Everything is the same as before - except its a nsview subclass instead of an nsopenglview.

????,
Codemattic