NSOpenGLView's init not being called... why?

Member
Posts: 304
Joined: 2002.04
Post: #1
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
Quote this message in a reply
kainsin
Unregistered
 
Post: #2
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 this message in a reply
Member
Posts: 104
Joined: 2002.04
Post: #3
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();
}
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #4
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  drawRect not being called IckyThump 4 7,343 Aug 13, 2009 06:48 AM
Last Post: IckyThump
  Taking advantage of 3DNow, SSE, MMX or whatever the PPC equivalent is called... Jones 3 3,588 Oct 24, 2006 07:06 AM
Last Post: Jones
  Newbie Cocoa query - setTitle: at window init sealfin 3 5,098 Nov 14, 2005 10:32 AM
Last Post: akb825
  how to init and get a dictionary working kensuguro 4 4,086 Oct 2, 2005 02:33 PM
Last Post: blobbo