2

I wonder if cocos2D is built on top of iOS's frameworks, won't cocos2D be slightly slower than using the Cocoa framework directly? (is cocos2D on top of OpenGL ES, which in turn is on top of Cocoa Touch / iOS frameworks including Core Animation and Quartz?).

However, I heard that OpenGL ES is actually faster than using Core Graphics, Core Animation, and Quartz?

So is OpenGL ES the fastest, cocos2D the second, and Core Animation the slowest? Does someone know why using OpenGL ES is faster than using Cocoa framework directly?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740

1 Answers1

2

cocos2D is built on top of OpenGL. When creating a sprite in cocos2D, you are actually creating a 3D model and applying a texture to it. The 3D model is just a flat square and the camera is always looking straight at it which is why it all appears flat and 2D. But this is why you can do things like scaling and rotating sprites easily - all you are really doing is rotating the 2D square (well, two triangles really) or moving them closer or further away from the camera. But Cocos2D handles all that for you.

OpenGL is designed from the start to pump out 3D graphics very very quickly. So it is designed to handle shoving points and triangles around. This is then enhanced by a 3D rendering hardware which it can use specifically for this. As this is all it does, it can be very optimised for doing all the maths on the points that build up the objects and mapping textures onto those object. It doesn't have to worry about handling touches or other system things that Cocoa does.

Cocoa Touch doesn't use openGl. It may use some hardware acceleration, but it isn't designed for that - it's designed for creating 2D buttons, etc. What it does, it does well, but it has lots of layers to pass through to do what it needs to do which doesn't make it as efficient as something designed just for graphics (openGL).

OpenGL is the fastest cocos2D is slightly slower, but only because there are some wrappers to make your life easier. If you were to do the same thing, then you may get it faster, but with the cost of flexibility. Core Animation is the slowest.

But they all have their uses and are excellent in their individual niche areas.

Nick Bull
  • 4,276
  • 1
  • 18
  • 25
  • 2
    Just wanted to add some things, Cocoa Touch is based on OpenGL and hardware accelerated. However, it is made and optimized specifically for views that are mostly static. This helps immensely with battery life, but most of the power optimizations make it much slower for dynamic views. – Skyler Saleh Apr 19 '12 at 13:03
  • So OpenGL ES isn't on top of Cocoa Touch at all then? Are all frameworks actually on top iOS? If so, then what layer of iOS is OpenGL ES on top of? Or does it actually use some kind of "see through" mechanism so it passes through iOS and work directly with 3D hardware underneath? – nonopolarity Apr 19 '12 at 13:08
  • @RTS I didn't realise that Cocoa Touch is on top of OpenGL. I thought it would have some sort of hardware acceleration though. Got any links to details of Cocoa Touch being on OpenGL? – Nick Bull Apr 19 '12 at 13:21
  • @動靜能量 OpenGL is OS-independent (except for context/window creation) and is a very thin abstraction over the features of the GPU. The only way it interacts with iOS is to create an OpenGL context and get control of the pixels on the screen. It's as close to the hardware as you can get without writing to the GPU registers yourself. – Robert Rouhani Apr 19 '12 at 13:26
  • I see, so OpenGL ES is at the very bottom, and iOS / Cocoa Touch is on top of it. Cocos2D is also on top of OpenGL ES. Cocos2D is very optimized for animation and for gaming, while Cocoa Touch might be more suitable for the general UI and less for animation and gaming then. – nonopolarity Apr 19 '12 at 14:26
  • 1
    @NickBull - I talk about some of this layering on OpenGL / OpenGL ES in my answer here: http://stackoverflow.com/a/7559897/19679 – Brad Larson Apr 19 '12 at 18:47
  • OpenGL ES is one of the frameworks that is used to build iOS. Under the covers, EVERYTHING you do in iOS that draws to the screen boils down to OpenGL ES. – Duncan C Apr 28 '12 at 21:37