Moving graphical objects without using sprites
I've spent some time working on some different ways of displaying graphics on the screen in OPL, without using sprites. Essentially, what I'm trying to do is draw a number of moving objects to the screen, as quickly and as smoothly as possible. I have a little program which does this for exactly ten seconds, and then displays how many frames per second it managed during that time.
All the programs use a 4-colour grey window the size of the whole screen. The image copied is also 4-colour, and is a 30x30 pixel bitmap which looks a bit like a polo mint. Each loop moves the objects a little, wrapping them around the edge of the screen if required, but does nothing else (to keep the performance as graphic-based (and not cpu-based) as possible).
It might be interesting if some of you with access to other EPOC32 platforms (I'm running all these on a Series 5) such as the GeoFox1, 5mx, MC218, Revo, etc. tried the same programs and let me know of your results...
Technique 1. Straight draw to the screen
The first program just clears the screen and copies the graphics directly to the main window, which is being displayed. I wouldn't expect the performance to be up to much here, as the following programs will explain. This also flickers like anything, because between the point where the old images are cleared and the new ones are drawn, the screen is blank.
Basic performance with 10 objects: 5.4 frames per second.
Interestingly, this really highlights the difference between copying from a window (gCreate) and copying from a bitmap (gCreateBit). If the code is modified to copy from an identical hidden window instead of a bitmap, the performance drops to 3.5 frames per second -- quite a significant difference! Keep this in mind when working on your graphics routines...
Zip file MovingObjects1_StraightCopy.zip contains the source and compiled OPO for this program. The source is in OPLPlus I'm afraid, but the .OPP file is plain-text and should be very easy to convert to standard OPL if required.
Technique 2. Build image in off-screen buffer and copy
This program is slightly different in that it creates an off-screen buffer bitmap, the same size as the screen, and performs all its graphic manipulation to this buffer. So the buffer is cleared, then each of the shapes drawn to it. Once all the drawing is completed, the entire buffer is transferred to the main window in a single gCopy operation.
Basic performance with 10 objects: 13.8 frames per second
The main noticeable visual difference (apart from the speed) between this and method 1 is that this approach does not flicker at all. At no point is the main screen cleared, so the graphics do not flicker.
Interestingly, removing the final gCopy which transfers the back buffer to the main window results in a blank screen, but an impressive 28.6 frames per second! This is very interesting when you consider that in this mode it is essentially exactly the same code as in method 1, except that it's drawing to a hidden bitmap instead of a visible window.
Zip file MovingObjects2_BufferCopy.zip contains the source and compiled OPO for this program.
Technique 3. Double-buffering
Double-buffering refers to a way of manipulating graphics which uses two windows. At any time, one of these windows will be displayed on the screen, whilst the other will be hidden. The displayed window is never manipulated, but the hidden window is updated as required for the next graphic frame.
Once the hidden window is completely drawn, it is made visible and the previously-visible window is hidden. The process then continues, drawing on the now-hidden window (which was displayed a moment ago), swapping the windows back and forth each loop through the code.
I was expecting this to be the fastest method of all, but it was not to be...
Basic performance with 10 objects: 4.1 frames per second
Again, it's nice and smooth, but it isn't as fast as method 2. I can only assume this is because the advantage of not having to copy an entire window is totally outweighed by drawing to a window rather than a bitmap. An interesting and unexpected result!
Zip file MovingObjects3_DoubleBuffer.zip contains the source and compiled OPO for this program.
If you have any comments or suggestions regarding this article, please don't hesitate to contact me.
This article is copyright © Adam Dawes, 2001.