views:

91

answers:

0

Hello to all. I need to attach cc2d Director in different views. After a lot of manipulations I found that I can't use only one instance of EAGLView that is present in [Direcor sharedDirector].

So, I have done some changes, and now I use a separate instances of EAGLView for each view.

The problem I am trying to solve, could be described like that:

  • I have parentVC with it's view, where Director uses it's instance of EAGLView.
  • I call a modalVC where Director obtain another instance of EAGLView and another scene. When parentVC didDisappear, Director stops animation. When modalVC appears, it call Director's startAnimation. All is fine. EAGLView visually responds to touch events.
  • I close modalVC. Director obtains an instance of EAGLView that already exists in parentVC, and the same scene as before. startAnimation. But, unfortunly, Director looks like dead... It still receive and redirects touch events to Layer, but nothing is changed on the screen.

Any ideas?

Here is the code:

//Views of modalVC and parentVC

-(id) initWithFrame:(CGRect) frame {
  if(self = [super initWithFrame:frame]) {
     openGLView_ = [[Director sharedDirector] initGLViewWithView:self withFrame:frame];
     scene_ = [[Scene node] retain];
  }
  return self; 
}

-(void) onViewWillAppear {
         [[Director sharedDirector] setOpenGLView:openGLView_];
         [openGLView_ setUserInteractionEnabled:YES];
         [openGLView_ setMultipleTouchEnabled:YES];
         [Director sharedDirector].animationInterval = 1.0/30.0f;
         if(![[Director sharedDirector] runningScene]) {
            [[Director sharedDirector] runWithScene:scene_];
         } else if([[Director sharedDirector] runningScene] != scene_) {
            [[Director sharedDirector] startAnimation];
            [[Director sharedDirector] pushScene:scene_];
         }
}

-(void) onViewDidDisappear {
   [[Director sharedDirector] stopAnimation];
}


// Director modification

-(id)initGLViewWithView:(UIView *)view withFrame:(CGRect)rect {

        NSString *pFormat = kEAGLColorFormatRGB565;
        GLuint depthFormat = 0;

        if(_pixelFormat==kRGBA8)
               pFormat = kEAGLColorFormatRGBA8;

        if(_depthBufferFormat == kDepthBuffer16)
           depthFormat = GL_DEPTH_COMPONENT16_OES;
        else if(_depthBufferFormat == kDepthBuffer24)
           depthFormat = GL_DEPTH_COMPONENT24_OES;

    // alloc and init the opengl view
        [_openGLView setTouchDelegate:nil];
        _openGLView = [[[EAGLView alloc] initWithFrame:rect pixelFormat:pFormat depthFormat:depthFormat preserveBackbuffer:NO] retain];

    // check if the view was alloced and initialized
    if(!_openGLView)
    {
        // the view was not created
        NSException* myException = [NSException
                                     exceptionWithName:kccException_OpenGLViewCantInit
                                     reason:@"Could not alloc and init the OpenGL View."
                                     userInfo:nil];
        @throw myException;

        return NO;
    }

    // set autoresizing enabled when attaching the glview to another view
    [_openGLView setAutoresizesEAGLSurface:YES];

    // set the touch delegate of the glview to self
    [_openGLView setTouchDelegate:self];


    // check if the superview has touchs enabled and enable it in our view
    if([view isUserInteractionEnabled])
    {
        [_openGLView setUserInteractionEnabled:YES];
        [self setEventsEnabled:YES];
    }
    else
    {
        [_openGLView setUserInteractionEnabled:NO];
        [self setEventsEnabled:NO];
    }

    // check if multi touches are enabled and set them
    if([view isMultipleTouchEnabled])
    {
        [_openGLView setMultipleTouchEnabled:YES];
    }
    else
    {
        [_openGLView setMultipleTouchEnabled:NO];
    }

    // add the glview to his (new) superview
    [view addSubview:_openGLView];

    if([self isOpenGLAttached])
    {
        [self initGLDefaultValues];
    }
    return _openGLView;
}

- (void) setOpenGLView:(EAGLView *) eaGLView {
   [_openGLView setTouchDelegate:nil];
   if(_openGLView) {
      [_openGLView release];
   }
   _openGLView = [eaGLView retain];
   [_openGLView setTouchDelegate:self];
}