Animation in cocoa

AndyWanKenobi
Unregistered
 
Post: #1
I'm pretty new to c-based games, having done all my development in java, so i'm not even really sure if i'm doing any of this right, especially for a game using obj-c cocoa

Right now my sprite classes are subclasses of NSView and are set as subviews of a container NSView. When I want to move them, I change their frame with setFrame, and then call setNeedsDisplay. However, I'm encountering that this isn't causing the previous position of the sprite to be painted over, so i'm left with artifacts of my sprites left on the screen.

I'm sure I need to do something to calculate that area and repaint it, but I haven't been able to figure out a good way to do it. Does anyone have any tips, or some reference that will tell me what I need to know?
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #2
you... have sprites... made from... NSView... WHY?!
Quote this message in a reply
w_reade
Unregistered
 
Post: #3
skyhawk:
Öbecause, like he said, he'sÖ pretty newÖ andÖ well, you have graphics in one, so it's fair enough to assume that it might be a useful thing to store sprites in, at least to begin with, isn't it? He's well aware he may be doing the wrong thing somewhere. It seems you know better than himÖ if so, please offer advice, not just sarky criticism.

AWK:
Um, I don't know the answer myself, but I felt like I ought to say thatÖ

I'm sure someone will offer you some help soon :-).
Quote this message in a reply
AndyWanKenobi
Unregistered
 
Post: #4
Yeah, I just figured that using NSView was a logical progression from using JComponent subclasses in Swing with Java. I don't have any problem with changing that, it just seemed like a logical step to make
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #5
Views are fairly cpu intensive. I dont recommend using them. What I recommend is putting each sprite in its own *window* and moving those around with setFrame:display: messages instead. Wink

ok - now back to reality.

check out http://www.spriteworld.org The beta for 3.0 which will be osx native and can be built from CW or PB is very close to release. It has many example apps, particle effects, special effects, level editors, good documentation - and an active email list and pretty constant development.

But spriteworld is a strictly c affair. If its Objective-c you are after check out Cocoa Sprite Kit: http://www.sugarcubesoftware.com/csk/

Download both of these worthy opensource projects and see how they do it. Then either - rewrite a sprite engine yourself using what youve gleened for the learning experience (or b/c you think you can do it better) - or choose the api you like better and use that library to handle all the nasty low-level work of sprite animation/collision/input etc and just concentrate on the things that will make your game unique. Not everyone should re-invent the wheel.

good luck.

hth,
codemattic
Quote this message in a reply
anarchie
Unregistered
 
Post: #6
I've began creating replacements for various Cocoa imaging classes which render using OpenGL in place of Quartz. They're poorly documented, and a bit sloppy: http://sourceforge.net/projects/cogltoolkit
One day I might be able to work them into CSK relatively easily.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #7
I'm not at my Mac OS X computer, but I believe there is a function called NSEraseRect. So... just call
Code:
NSEraseRect([[yourView window] rect]);
Note: all of that code is theory and off the top of my head.

What setNeedsDisplay are you calling? You should probably call -update on both the main view and the subview.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #8
Thinking again... in your drawRect, you should just fill the view with a color.
Quote this message in a reply
anarchie
Unregistered
 
Post: #9
All of your little sprite views have one superview in common, right? Just set that one as needing display and it will calculate and redraw all the dirty rects for you.
Quote this message in a reply
AndyWanKenobi
Unregistered
 
Post: #10
how will the superview know which rects are dirty? by having changed the frames of its subviews? do I need to do anything to let the superview know which of its subviews are dirty?
Quote this message in a reply
anarchie
Unregistered
 
Post: #11
To mark a view as dirty, send it setNeedsDisplay:YES. If you change a view's frame, do something like this:

Code:
[[myView superview] setNeedsDisplayInRect:[myView frame]];
[myView setFrame:myRect];
[myView setNeedsDisplay:YES];
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  variable frame rate animation in Cocoa Damian 3 3,576 Jul 16, 2003 06:00 PM
Last Post: macboy