views:

604

answers:

2

Using UIView and Quartz has proven to be too slow for my 2D game

A: 

In general in OpenGL, there are basically two ways to do "scrolling", i.e. shifting the displayed contents perpendicularly to the viewing direction:

  1. Adjust the viewing, in other words "move the camera", so that the contents shift.
  2. Move the contents directly, keeping the camera fixed.

These are of course totally equivalent, and the inverse of each other.

These both assume that you do complete re-draws of your scene for each frame. Scrolling in the more "classical" sense, by copying the contents of the frame buffer while offsetting by a (dx,dy) amount, is not a technique that is very suitable for OpenGL.

Basically, creating a scrolling image could be as easy as drawing a single textured quad, and then shifting the position of it for each new frame.

unwind
do you have a code sample?
Dimitris
No, I'm sorry ... It's been a while since I actually wrote any GL code. :/
unwind
A: 

How large are your images and how slow is too slow? Using a UIScrollView with a containing UIView whose content is drawn via Quartz, I've been able to smoothly scroll views up to 2000 x 2000 in size. If you aren't redrawing your image content every frame, but instead moving the position of your views and letting Core Animation do the rest, I bet that you'll be able to get the kind of performance you want without dropping down to OpenGL.

Most 2-D performance issues that I've seen result from people trying to do all their animation in drawRect, constantly redrawing their content. You want to draw once, and then just move the origin of the drawn view or layer around. The content of the view is cached as an OpenGL texture, so it becomes very efficient to animate around.

For 2-D work, I regard OpenGL as the last place to turn to after you've exhausted all other alternatives. It will take you much longer to do the same things that you can using simple UIViews and Core Animation.

Brad Larson
Thanks for this. At the moment I have a timer calling an update function 30 times a second. The update function sets the new rectangles for tiles that I have defined as views. With this method, the iPhone drops to below 20fps once the number of tiles passes 20. I would appreciate any advice.
Dimitris
You may wish to see my response to this question: http://stackoverflow.com/questions/595922/how-do-i-prevent-core-animation-blocking-my-main-thread . Don't use timers and update functions, but let Core Animation do the work for you. You'd be surprised at the performance you can get.
Brad Larson
Thanks for the advice Brad. In the end we decided to OpenGL up! It means we get to keep our game logic. I threw something together quickly and got 60 fps. Cheers
Dimitris