CoreGraphics sucks? OpenGL for 2d?

dave05
Unregistered
 
Post: #1
hey everyone,

um, I've been working on a game for a while now; it's entirely sprite-based. developer.apple.com has been my primary source of information, so the path I've taken has been as follows.

Cocoa/NSImage/drawInRect/etc... (worked fine before background implementation, except that NSImages seem to have problems with 32 bit PNGs)
--

so I ported it to :
Core Graphics/CGContext/CGContextDraw/etc... (works fine without a background, but the frame rate goes from 60 to about TEN when I add the background)

my background is in such a way that it needs to be drawn every frame, ie constant scrolling, and it has about 4 layers. I suppose the very back layer (sky, sun) need not be redrawn ever, but everything else needs to redraw at 60fps preferably...

I'm changing the screen size to 800x600x32 at the beginning. I figured my relatively slow emac 1.25ghz could completely redraw such a screen at a full frame rate. But there's something wrong with my optimization, or maybe i shouldn't be using CoreGraphics?

Is there a way to implement the CGContext methods such as CGContextSaveGState or RestoreGState in order to speed up the drawing process?

If not...

I've looked at OpenGL as an alternative, but it doesn't seem to be very friendly to 2d-minded people. I know i have to set vertices for the 4 corners and then draw a "texture" in the rectangle... but how is this done? and how do I draw semi-transparent image in the same way that i can with CGContextSetAlpha or [drawInRect .. .. .. fraction:0.5] ?

I've been using several other forums, but you guys actually seem to know what you're talking about, so... help, please! thanks.

Also, my main game loop is still based on an NSTimer and a subclass of NSView... is there a way to strip out the NSView wrapper and just draw directly to the context of the window i create? blah.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
OpenGL is pretty much your best bet. There's a lot of material on the net regarding 2-d sprites via gl -- that said I've never attempted it so I can't give any specific advice.

Also, why abandon NSTimer? It's a perfectly cromulent way to run a game loop.
Quote this message in a reply
Member
Posts: 168
Joined: 2004.10
Post: #3
CoreGraphics doesn't work with my 1.25Ghz emac, and it shouldn't on yours either. The graphics card shouldn't support it. Note the lack of a ripple effect when you drag a new widget into dashboard.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Snakey, you're talking about CoreImage. CoreGraphics, AKA Quartz, works everywhere, of course.

dave05: Yes, OpenGL is the only way to get fast graphics on Mac OS X. It's perfectly friendly to 2D graphics, though it may take a slight shift in thinking Smile Just start with any old OpenGL tutorial (some people like NeHe, some people hate him, but it may be the best bet). Once you've learned about drawing quads, per-vertex colors, texturing and blending (not more than a couple of days' work) you'll be all good to go.

In terms of learning about the Mac side of things, you could (*shameless plug*) try here: http://onesadcookie.com/~keith/XcodeGLUT for a tutorial on getting started with OpenGL via GLUT under Xcode, or here: http://onesadcookie.com/book.html for some more problem-oriented code to help get set up with Cocoa.
Quote this message in a reply
Member
Posts: 168
Joined: 2004.10
Post: #5
Oops... Blush

Sorry.
Quote this message in a reply
dave05
Unregistered
 
Post: #6
alright, your GLUT tutorial looks pretty good, I may have a run through it and a few more tutorials later...

one quick question though, is there a performance hit for writing a carbon program rather than something that is os X native? Obviously it's pretty useless to write an OS 9 compatible app anymore, and I guess the main reason I've been avoiding OpenGL thusfar is that OpenGL and Carbon seem to go hand in hand, and I hate the idea of writing an application that conforms to the rapidly diminishing core of OS 9 users...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
"OpenGL and Carbon go hand in hand"?!
er, no. Absolutely nothing to do with one another.
and, Carbon is Mac OS X native.
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #8
One thing - beware GLUT.... It's pretty limited and may not have everything you need and want.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
GLUT's great for getting started with, though it's certainly not something you'd want to ship an app based on...
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #10
Cocoa + OpenGL is beautiful. Unless you have some particularly good reason to run with Carbon, my personal take would be to prototype your code in GLUT and then write a Cocoa app.
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #11
TomorrowPlusX Wrote:Cocoa + OpenGL is beautiful. Unless you have some particularly good reason to run with Carbon, my personal take would be to prototype your code in GLUT and then write a Cocoa app.

If you're really squeezing for performance, i.e. every last FPS, go with Cocoa/AGL with a (Carbon)WindowRef. Works like a charm Grin

It's not magic, it's Ruby.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
There is no performance difference between AGL and NSGL.
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #13
Also, instead of NSView, you can use NSGraphicsContext

It's not magic, it's Ruby.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #14
... which has absolutely nothing to do with OpenGL at all...
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #15
I, unfortunately, can sympathize with you - my game "Offender" kinda died in the end because it couldn't handle the performance.

There *are* however a few tweaks you can make that make a huge difference. I can't remember them now, but you should definitely search http://cocoa.mamasam.com for their mailing list archives. That's the best Cocoa resource out there, IMHO.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  VBL in CoreGraphics? Oblivion 16 6,719 Nov 17, 2006 02:44 PM
Last Post: Ingemar
  QuickDraw(CopyBits) vs CoreGraphics(CGContextDrawImage) cloke 6 5,846 Aug 7, 2004 03:06 PM
Last Post: arekkusu