iDevGames Forums
NSOpenGLView's init not being called... why? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: NSOpenGLView's init not being called... why? (/thread-7621.html)



NSOpenGLView's init not being called... why? - codemattic - Aug 11, 2002 11:10 AM

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


Code:
@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



NSOpenGLView's init not being called... why? - kainsin - Aug 12, 2002 08: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.


NSOpenGLView's init not being called... why? - GoodDoug - Aug 12, 2002 01: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
Code:
- (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();
}



NSOpenGLView's init not being called... why? - codemattic - Aug 12, 2002 10:59 PM

Thanks guys. awakeFromNib it is... along with an extra sanity check or two. However:

Quote: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.

Quote: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