Joseph Astrahan
Joseph Astrahan

Reputation: 9082

How to get init function of OpenGlView to run?

I'm trying to get the init function to run in a subclassed version of NSOpenGlView that I created.

For example the code below.

- (id)initWithFrame:(NSRect)frameRect{
    NSLog(@"Init function ran");
    return self;
}

It should send to the console that the init function ran but it does not! I have tried creating an init function and initWithFrame:PixelFormat: functions as well to see if they trigged any console messages but nothing happened.

I created the NSOpenGlView by dragging it to my window in interface builder and setting it's class to my subclassed version. My drawrect function works fine and draws an opengl triangle, so I know it's working on that level.

Here is my entire class in case you are curious

#import <Cocoa/Cocoa.h>
@interface openGLView : NSOpenGLView
- (void) drawRect:(NSRect) bounds;
@end

#import "openGLView.h"
#include <OpenGL/gl.h>

@implementation openGLView
- (id)initWithFrame:(NSRect)frameRect{
    NSLog(@"Init function ran");
    return self;
}
- (void) drawRect:(NSRect) bounds{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    drawAnObject();
    glFlush();
    NSLog(@"drawRect");
}

static void drawAnObject ()
{
    glColor3f(1.0f, 0.85f, 0.35f);
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(  0.0,  0.6, 0.0);
        glVertex3f( -0.2, -0.3, 0.0);
        glVertex3f(  0.2, -0.3 ,0.0);
    }
    glEnd();
}

@end

The reason I need to do this is so that I can return back at initialization a new pixelformat with 32bits so that DEPTH_TEST will actually work. Supposedly you can set this in interface builder but for me I don't see any options. This might be because it's inside scroll view but not sure yet.

Upvotes: 0

Views: 194

Answers (1)

iain
iain

Reputation: 5683

If you created the view in Interface Builder then it doesn't call - (id)initWithFrame:(NSRect)frame, but instead calls - (id)initFromCoder:(NSCoder *)coder

Upvotes: 3

Related Questions