Custom NSView not redrawing

  • I am attempting to write a simple game using
    Objective-C and Cocoa, currently on Tiger.  I have a
    simple game thread using NSThread which looks like
    this:

    - (void) doAnim {
    NSDate *date = nil;
    @try {
      while (animating) {
      [world advanceClock:ANIM_TICK_INTERVAL];

      [[GameView instance] setNeedsDisplay:YES];

      date = [NSDate
    dateWithTimeIntervalSinceNow:ANIM_TICK_INTERVAL];
      [NSThread sleepUntilDate:date];

      }
    }
    @catch(NSException *e) {
      NSLog(@"Caught %@: %@", [e name], [e reason]);
    }
    }

    GameView is an NSView subclass embedded in a window
    created using Interface Builder.

    This was working fine, then it stopped working.  I
    searched the net for answers and eventually got it
    working again (I think by deleting my GameView view
    instance from the NIB, reading the header into IB, and
    adding it back in).  Then I was cheerfully coding
    until it mysteriously stopped working again.

    The behavior is: the game state is correctly being
    updated, but the view does not change.  If I resize my
    window then I can force a redraw and see that my game
    objects have moved to the expected positions.  I can
    watch the game animate by continually resizing the
    window.

    I've simplified the drawRect to this, and it still
    update properly:

    - (void) drawRect:(NSRect)rect {
        NSRect bounds = [self bounds];
    [[Util randomColor] set];
    [NSBezierPath fillRect:bounds];
    }

    I've read that -setNeedsDisplay is not thread-safe,
    and I have tried following the advice as described
    here:

    http://www.cocoabuilder.com/archive/message/cocoa/2005/5/24/136890

    Calling -setNeedsDisplay or -display from the Cocoa
    main thread does not change the behaviour.  I have
    also tried using NSTimer instead of NSThread, to no
    avail.

    And help will be appreciated,

    Steven Barnes
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today