iDevGames Forums
OpenGL screensaver. - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: OpenGL screensaver. (/thread-8423.html)



OpenGL screensaver. - StealthyCoin - Dec 14, 2010 01:08 PM

I had a working one years ago. But somehow I cannot get an opengl screensaver to render anything.

Here is the code for my screensaver subclass.
Code:
@implementation ButterflyCurveView

- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
    self = [super initWithFrame:frame isPreview:isPreview];
    if (self) {
        NSOpenGLPixelFormatAttribute attributes[] = {
            NSOpenGLPFAAccelerated,
            NSOpenGLPFADepthSize, 16,
            NSOpenGLPFAMinimumPolicy,
            NSOpenGLPFAClosestPolicy,
            0 };
        
        NSOpenGLPixelFormat *format;
        format = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
        
        glView = [[MyOpenGLView alloc] initWithFrame:NSZeroRect pixelFormat:format];
        
        if (!glView) {
            NSLog(@"Couldn't initialize OpenGL view.");
            [self autorelease];
            [self setupOpenGL];
        }
        
        e = new ButterflyCurve(frame.size.width/2, frame.size.height/2);        
        [self setAnimationTimeInterval:1/30.0];
        
    }
    
    return self;
}

- (void)setupOpenGL
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

-(void)setFrameSize:(NSSize)newSize
{
    [super setFrameSize:newSize];
    [glView setFrameSize:newSize];
    
    [[glView openGLContext] makeCurrentContext];
    
    glViewport(0,0,(GLsizei)newSize.width,(GLsizei)newSize.height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, (GLsizei)newSize.width, (GLsizei)newSize.height, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    [[glView openGLContext] update];
}

- (void)startAnimation
{
    [super startAnimation];
}

- (void)stopAnimation
{
    [super stopAnimation];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];
    
    [[glView openGLContext] makeCurrentContext];
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    e->render();
    
    glColor3f(1.0f, 0.0f, 0.0f);
    
    glBegin(GL_POINTS);
    
    glVertex2i(100, 300);
    glVertex2i(200, 400);
    glVertex2i(300, 500);
    glVertex2i(1, 1);
    glVertex2f(.5f,.5f);
    glVertex2i(0, 0);
    
    glEnd();
    
    NSLog(@"Render");
    
    [[glView openGLContext] flushBuffer];
    
    
}

- (void)animateOneFrame
{
    e->update(1.0f/30.0f);
    [self setNeedsDisplay:TRUE];
    return;
}

- (BOOL)hasConfigureSheet
{
    return NO;
}

- (NSWindow*)configureSheet
{
    return nil;
}

- (void)dealloc
{
    delete e;
    [glView removeFromSuperview];
    [glView release];
    [super dealloc];
}

@end

This prints render over and over again and the screensaver remains black.

MyOpenGLView is a subclass of NSOpenGLView that overrides -(void)isOpaque and returns NO.

particle effect is a fully tested piece of code that does exactly what I want it to do if I run it in a SDL application, the only problem I think is something to do with how I set up the screensaver view, the opengl view or the project.

If anyone could point out what error I made that would be great.


The extra vertexes in the render function were just to see if I had set the coordinates up incorrectly. All that really needs to be there is e->render();


RE: OpenGL screensaver. - OneSadCookie - Dec 14, 2010 01:13 PM

is -setFrameSize: being called?


RE: OpenGL screensaver. - StealthyCoin - Dec 14, 2010 01:31 PM

(Dec 14, 2010 01:13 PM)OneSadCookie Wrote:  is -setFrameSize: being called?

Yessir. Apparently it is called twice in a row every once in awhile. Not sure what that means. Both in the preview and when I hit test.


RE: OpenGL screensaver. - StealthyCoin - Dec 18, 2010 10:00 PM

Found my problem.

Didn't ever add my glView to the screensaver view.
Code:
[self addSubview:glView];

Thanks for looking Rasp