tags:

views:

60

answers:

2

Assume I want to program a Star Field animation. Not by using OpenGL but by drawing directly to the screen buffer or to off-screen buffers that can be placed on screen.

That Star Field demo running on the TRS-80 is of course just an example. Think video decoding or full-screen animations as other possibilities where rates of 20+ frames per second are needed.

What are good techniques for doing this? I am interested in both official and private API implementations.

If you can, please show some basic code.

+1  A: 

Here's a brief outline of one common and reasonably fast method.

During initialization, use CGBitmapContextCreate() to create at least 2 bitmap contexts from pointers to off-screen memory buffers, and add a CALayer as a sublayer of your UIView.

In your render loop, alternate your double buffer use, copy into and/or modify the current off-screen memory buffer, use CGBitmapContextCreateImage() to create an image from that buffer, and assign the resulting image to the CALayer contents.

hotpaw2
Have you used this? What kind of frame rate did you get?
St3fan
The frame rate depends on the bitmap size, device and the OS version. Large bitmaps on iPhone 3G's running iOS 4.0 have recently been reported as running well under 10 fps. Small bitmaps on a 3GS running OS 3.1.3 can run at perhaps an order of a magnitude higher frame rates. I've used this method for small bitmaps at 30 fps on my iPhone 4, with room to spare.
hotpaw2
10 frames per second is way too low and certainly not 'reasonable fast' in my book :-(
St3fan
@St3fan: Blame Apple. I read reports that they slowed some 30 fps games down to under 10 fps with their iOS 4.0 "upgrade" to the old 3G model. One hopes that 4.1 will render a bit faster...
hotpaw2
A: 

A common way of doing this type of effect is by making use of bitmap draw functions that allow offsets into the images: for a downward scroll effect, simply do two copies from your source starfield bitmap into the screen buffer: first from source (0, 0) to (width, height-Yoffset) to screen (0, Yoffset) to (width, height). Secondly, take from source (0, height-Yoffset) to (width, height) to screen (0, 0) to (width, Yoffset). By changing Yoffset each frame you can control the scrolling rate, just make sure to reset Yoffset when it reaches (height) back to 0. Reversing the direction is the same, just invert Yoffset changes. The same method can be applied to X as well as Y, even at the same time, to give some very powerful full screen scrolling effects.

We use this method in our iSGPU core for scrolling wallpapers and other background effects. The performance is usually very high, since you're dealing with 2 block transfers of linear memory. Since the method uses bitmaps in memory, you can have a very nice images scrolling rather than just changing pixels. With only a few changes to the parameters above you can also use bitmaps larger than the screen for giving a "larger world" effect.

Good Luck! Stuart, Chief Architect /You.i Labs

Stuart
Thanks for the reply Stuart. Unfortunately you don't mention how to actually get the bits on screen. Which is the core of this question.
St3fan