Refreshing NSViews

Member
Posts: 370
Joined: 2002.04
Post: #1
I am trying to create a subclass of NSView (akin to REALbasic's SpriteSurface FYI) and it needs to update itself very very fast- I tried calling display() but it isn't fast enough; it has to wait for the events loop to return again or something like that- is there a better way? Thanks,
Steven
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
I do this in my NSOpenGLView subclass's initWithFrame: method:

Code:
(void)[NSTimer scheduledTimerWithTimeInterval:0.001
                                       target:self
                                     selector:@selector(display)
                                     userInfo:nil
                                      repeats:YES];

... and get 800fps.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #3
I don't know, wouldn't a Thread be a better idea. It will make your game/app run faster since the redrawing procedure is in its own chunck of memory.

"When you dream, there are no rules..."
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
It probably wouldn't provide a significant performance boost. It would almost certainly create a synchronization nightmare.

I've tried to program multithreaded games in the past, and each time I've discovered it's far more work than it's worth. It's something I'm sure I'll revisit in the future, but it's not something I'd personally recommend to anyone!
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #5
Couldn't the timer interval just be 0? I read that it then fires as soon as possible, though I imagine 0.001 is pretty close too Cool
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #6
I'd say setting the timer to 0 would not let the timer repeat at all.

"When you dream, there are no rules..."
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #7
It does repeat, that is what I use. I don't know how many FPS though.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
I'm quite happy capped at 1000FPS Wink
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #9
Steven: shouldn't matter whether you use 0 or 0.001, either way you should get really fast updates.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #10
Quote:public NSTimer( double seconds, Object target, NSSelector aSelector, Object userInfo, boolean repeats)

Creates a new NSTimer that, when registered, will fire after seconds. Upon firing, the timer sends aSelector to target. aSelector must take only one argument, an NSTimer object. The timer passes itself as the argument to aSelector. To pass more information to the target, use userInfo. The target gets userInfo by sending userInfo to the timer.
When it says that it will send aSelector to an object, how does it send it? Also, what do I send and what do I do when I receive it? This is what I have so far:
Code:
NSTimer timer = new NSTimer(.001,this,null,null,true);

(Sorry, I'm pretty new at this)
Thanks,
Steven
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
You should be able simply to use the code snippet I posted in your initWithFrame: or your awakeFromNib. It should work without a hitch.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #12
Ok, but I don't understand how @selector(display) (the aSelector parameter) translates into Java ~ can I leave this null or does it have to be something? I tried it with null, but the display is just as laggy as before.
Steven
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #13
Think of selectors as void functions in C, basically what you are doing is telling NSTimer that you want the method display to be repeated every so often. You have to declare your display function as follows

Code:
- void display:(id)sender {
//display code goes here
}

"When you dream, there are no rules..."
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #14
Sorry, I didn't realize you were using Java.

Quote:
Code:
- void display:(id)sender {
//display code goes here
}

(a) that's wrong (you can't declare display like that, it has to be overridden from NSView)

(b) that's irrelevant, Steven is using Java, not Objective C.

I think you want to do something like this (never having done it in Java before):

Code:
NSTimer timer = new NSTimer(0.001,
                            this,
                            new NSSelector("display", null),
                            null,
                            true);
NSRunLoop.currentRunLoop().addTimerForMode(timer,
                                           NSRunLoop.DefaultRunLoopMode);

NSTimer expects a method with one argument, so while this works in Objective C, it's possible it doesn't in Java.

If that's the case, you'll need to create a "dummy" method like this:

Code:
public void myDisplay(Object anObject) {
    display();
}

and create your selector to call this method instead:

Code:
Class[] parameterTypes = new Class[1];
parameterTypes[0] = Object;

...

new NSSelector("myDisplay", parameterTypes)

Warning! None of this code has even been compiled, let alone tested. It probably contains multi-many errors!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Quickly refreshing NSViews Steven 24 11,168 Nov 13, 2002 07:30 AM
Last Post: Steven