Refreshing NSViews
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
Steven
I do this in my NSOpenGLView subclass's initWithFrame: method:
... and get 800fps.
Code:
(void)[NSTimer scheduledTimerWithTimeInterval:0.001
target:self
selector:@selector(display)
userInfo:nil
repeats:YES];... and get 800fps.
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..."
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!
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!
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
I'd say setting the timer to 0 would not let the timer repeat at all.
"When you dream, there are no rules..."
It does repeat, that is what I use. I don't know how many FPS though.
I'm quite happy capped at 1000FPS
Steven: shouldn't matter whether you use 0 or 0.001, either way you should get really fast updates.
Quote:public NSTimer( double seconds, Object target, NSSelector aSelector, Object userInfo, boolean repeats)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:
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.
Code:
NSTimer timer = new NSTimer(.001,this,null,null,true);(Sorry, I'm pretty new at this)
Thanks,
Steven
You should be able simply to use the code snippet I posted in your initWithFrame: or your awakeFromNib. It should work without a hitch.
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
Steven
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..."
Sorry, I didn't realize you were using Java.
(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):
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:
and create your selector to call this method instead:
Warning! None of this code has even been compiled, let alone tested. It probably contains multi-many errors!
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!
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Quickly refreshing NSViews | Steven | 24 | 10,487 |
Nov 13, 2002 07:30 AM Last Post: Steven |
|

