CoreGraphics sucks? OpenGL for 2d?
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.
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.
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.
Also, why abandon NSTimer? It's a perfectly cromulent way to run a game loop.
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.
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
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.
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
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.
Oops...
Sorry.
Sorry.
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...
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...
"OpenGL and Carbon go hand in hand"?!
er, no. Absolutely nothing to do with one another.
and, Carbon is Mac OS X native.
er, no. Absolutely nothing to do with one another.
and, Carbon is Mac OS X native.
One thing - beware GLUT.... It's pretty limited and may not have everything you need and want.
GLUT's great for getting started with, though it's certainly not something you'd want to ship an app based on...
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.
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
It's not magic, it's Ruby.
There is no performance difference between AGL and NSGL.
... which has absolutely nothing to do with OpenGL at all...
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.
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| VBL in CoreGraphics? | Oblivion | 16 | 6,153 |
Nov 17, 2006 02:44 PM Last Post: Ingemar |
|
| QuickDraw(CopyBits) vs CoreGraphics(CGContextDrawImage) | cloke | 6 | 5,272 |
Aug 7, 2004 03:06 PM Last Post: arekkusu |
|

