View Full Version : Refreshing NSViews
Steven
2002.10.25, 01:34 AM
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
OneSadCookie
2002.10.25, 03:03 AM
I do this in my NSOpenGLView subclass's initWithFrame: method:
(void)[NSTimer scheduledTimerWithTimeInterval:0.001
target:self
selector:@selector(display)
userInfo:nil
repeats:YES];
... and get 800fps.
Taxxodium
2002.10.25, 06:57 AM
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.
OneSadCookie
2002.10.25, 07:28 AM
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!
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:
Taxxodium
2002.10.25, 10:11 AM
I'd say setting the timer to 0 would not let the timer repeat at all.
It does repeat, that is what I use. I don't know how many FPS though.
OneSadCookie
2002.10.25, 08:16 PM
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.
Steven
2002.10.26, 12:30 AM
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:
NSTimer timer = new NSTimer(.001,this,null,null,true);
(Sorry, I'm pretty new at this)
Thanks,
Steven
OneSadCookie
2002.10.26, 01:49 AM
You should be able simply to use the code snippet I posted in your initWithFrame: or your awakeFromNib. It should work without a hitch.
Steven
2002.10.26, 11:07 AM
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
Taxxodium
2002.10.26, 02:11 PM
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
- void display:(id)sender {
//display code goes here
}
OneSadCookie
2002.10.26, 08:08 PM
Sorry, I didn't realize you were using Java.
- 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):
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:
public void myDisplay(Object anObject) {
display();
}
and create your selector to call this method instead:
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!
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.