multiple nsopengl subviews problem

Member
Posts: 194
Joined: 2009.02
Post: #1
I wrote 2 separate classes, each subviews of NSOpenGLView. Then I went into xcode and created 2 windows with custom views and set their respective view's classes to each of the new classes I created. Now when I run the program each view seems to execute both it's own drawing code as well as the other's, meaning drawing happens across both views.

Now I believe the code I'm using(to setup the class)is correct because I've used it in the past(successfully)to do exactly what I'm trying to do, only it's not working this time. Here's my class code.

Code:
#import "structs.h"
#import "constants.h"
#import "externs.h"
#import "prototypes.h"

#import <GLUT/glut.h>
#import <OpenGL/glu.h>
#import <OpenGL/gl.h>
#import "palette_view.h"


@interface palette_view : NSOpenGLView  
{

    NSTimer * palette_loop_timer;
}


- (IBAction)tick:(id)sender;
- (void)start_timer:(void *)userInfo;
- (void)stop_timer:(void *)userInfo;


@end



static NSOpenGLPixelFormatAttribute my_attributes[] =
{
    0
};


@implementation palette_view


- (IBAction)tick:(id)sender
{
    main_loop();
    
    [self setNeedsDisplay:TRUE];
}

- (void)start_timer:(void *)userInfo
{
    if (!palette_loop_timer)
    {
        palette_loop_timer = [NSTimer scheduledTimerWithTimeInterval:1.0/FPS
                         target:self
                         selector:@selector(tick:)
                         userInfo:0
                         repeats:YES];
    }
    
    NSLog(@"start_timer");
}



- (void)stop_timer:(void *)userInfo
{
    if (palette_loop_timer)
    {
        [palette_loop_timer invalidate];
        palette_loop_timer = nil;
    }
    
    NSLog(@"stop_timer");
}



- (void)awakeFromNib
{
    [[self window] makeFirstResponder:self];
    [[self window] setAcceptsMouseMovedEvents:YES];

}


- (void)prepareOpenGL
{

    NSRect view_bounds = [self bounds];

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_ALPHA_TEST);    
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluOrtho2D( 0, view_bounds.size.width, 0, view_bounds.size.height);

}




- (id)initWithFrame:(NSRect)frameRect
{
    NSLog(@"initWithFrame");
        
        
        
    NSOpenGLPixelFormat* pixel_format =
            [[NSOpenGLPixelFormat alloc] initWithAttributes:my_attributes];
    
    if (pixel_format == nil)
    {
        return nil;
    }
    [pixel_format autorelease];
    
    self = [super initWithFrame:frameRect pixelFormat:pixel_format];
    if (self == nil)
    {
        return nil;
    }
    
    
    [[self window] setAcceptsMouseMovedEvents:YES];
    
    [self start_timer:nil];
    
    
    
    return self;

}


- (void)drawRect:(NSRect)r
{
    begin_drawing();    
}


@end


I should add that setting the context before drawing using [[self openGLContext] setView: self] seems to work, except that with or without [[self openGLContext] setView: self] texturing only works in the first view.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  When to create custom OpenGL view instead of subclass NSOpenGL view Coyote 37 22,801 Oct 20, 2009 08:16 PM
Last Post: Coyote
  How do I place all of my UI elements to be subviews of another view/window? xenocide 2 2,801 Feb 1, 2009 08:32 PM
Last Post: xenocide
  bug in OpenGL setup for NSOpenGL subclass (I think) backslash 2 3,791 Jul 18, 2007 01:10 PM
Last Post: backslash
  Help with NSOpenGL: Window resizing Tycho 2 3,372 Aug 13, 2003 09:46 PM
Last Post: Tycho