We return to OPL's graphic capabilities this week, with an attempt to jazz up our MiLo splash screen with a simple animation. Let's make the pile of money on the left of our picture 'dance' around a little!
Let me give you the code snippet first, so that you can type it in,
translate it and see what I'm talking about. Find the bit at the start of the
program which refers to gCOPYing the picture and amend it as follows:
gCOPY image%,0,0,480,160,0
i%=1
WHILE i%<=50
gAT rnd*10,rnd*10
gCOPY image%,0,0,100,120,3
i%=i%+1
ENDWH
gCLOSE image%
Translate and run and watch the opening graphic carefully.
The money should jiggle about randomly for a second or so. Now that you believe
me(!), let me unpack the extra lines of code and what they mean.
i% is used (as usual) as a general-purpose dogsbody of a variable,
just counting up to 50. Each time through the loop its value is incremented by
1 until the WHILE condition finally fails and the loop stops.
This simple animation is achieved by simply overlaying a portion of the
left-hand side of our image% 'drawable' (i.e. bitmap in memory) fifty times,
each time with a slightly different random offset on the screen. Because the
clipart image has quite a bit of 'white space' around it, there's no need for
us to blank off the previous drawing before overlaying the next - the image is
only 'jiggled' by up to 10 pixels in the horizontal and vertical and in each
case no portion of the previous drawing is left on-screen to look ugly.
The program asks the Psion for a random number between 0 and 1 each time
it gets to the RND command. I've simply multiplied each number by 10 so
that the result is in the right sort of magnitude for us to use for the
jiggling of our graphic. Note, obviously, that the 'jiggling' will be different
every time you run the program as the numbers really are extremely random.
The gCOPY is most important to note. As before, the first 2 parameters
it takes are just the starting point in the image% bitmap, i.e. 0,0 (the
top-left). The second two tell the program how much of the image% bitmap to
copy onto the screen. The numbers 100 and 120 were measured by me in my
painting program to give me the correct bit of the picture - you may find you
need to alter them slightly for your own. Finally, the last parameter is
vital. Let me explain.
Every graphics operation in OPL operates in one of 4 modes, and it's very important to understand the differences between them:
gCOPY command:
gCOPY:
gCOPY command:
gCOPY last parameter back to 3 and check the program now
jiggles the graphic properly again:
You might want to note that mode 2 can be quite useful in
animation, too. By printing (say) a spaceship and then printing it again in
mode 2, it's possible to make it appear and vanish without affecting the
background - very useful in a game. This sort of thing is quite advanced,
though, and requires a lot of fiddling around, so we won't say anymore at this
time.
Mastery of OPL's graphics mode is the first key to getting real work
done when building programs. Make sure you understand the
differences between each one before we move on. The graphics mode for text
operations is set with the gTMODE command and that for general operations
(gCOPY, gFILL etc) with gGMODE. Have a look in Psion's OPL manual for more
details and examples.
Next week we'll look at the differences between normal and 'long' integers and do some integer arithmetic... and in a fortnight, we'll handle some database 'reads'. See you then!
Go to next lesson | Programming index