Plasma
My version of Hello World for each new development platform I encounter is the mystify
screensaver from windows. You know the one -- a whole bunch of lines following each
other as they bounce around the screen, leaving a neat trail behind them reminiscent of
the bad guy in the old Qix arcade game.
With the Yaroze I thought I’d go one better. After all, if you are sitting in front of a
compiler for one of the world’s most powerful video game consoles, it makes no sense to
do something that could be done on any graphics capable PC. The idea of your first
program is to figure ascertain that you know how to write code, that you know how to
compile it and run it on the new platform, and that you have a basic grasp of the facilities
at hand.
So, with that in mind I set forth to write a plasma (swirling graphicness, somewhat
similar to a nebula in appearance) and draw my usual lines over the plasma. Shortly into
the program though I discovered some of the neater features of the Playstation, such
transparencies, and figured "Hey, why not go one better and make the lines see-through,
and how about also making the trail of the lines neatly fade into the plasma background".
A plasma ain't a plasma unless its moving around as well, so some colour cycling is
required.
So, step 1. How do we get a plasma created and on screen. Well, in hindsight I could draw
coloured pixels directly into the backbuffer, but at the time I was less knowledgeable. So
instead I loaded up a pre-drawn image, and then wiped it out with the new plasma, created
on the fly.
Plasma and colour palette were then copied over to the frame buffer, ready to be drawn.
The lines came next -- 255 of them in fact. Initially they are all set to start at the same
location on the screen, though each has a slightly paler colour than the first, until the very
last line is completely black. When the program runs though, these lines don’t have any
colour to speak off. The GsLine structure is used to draw the lines with a transparent
effect. As far as the Yaroze is concerned, a black transparent line is completely see
through, and thus is not drawn.
So, now that the plasma is ready, and the lines are ready, they are drawn. However it’s in
the "game loop" that the really cool stuff happens.
Arrays are used to hold the speed of each end of each line. Code inside the loop is used to
check when the end points of a line would hit an edge of the screen. At that point though,
the speeds are reversed and so the relevant end point of the line that just hit the edge
changes direction. In addition, the colour palette that’s being held in main memory is
cycled. The first colour becomes the last and every other colour is shifted down one
position.
Now, when the original plasma was drawn onto the backbuffer, a sprite was used. That
sprite has pointers to the plasma’s image data in the frame buffer, as well as pointers to
the plasma’s colour palette. After cycling the original palette, all that is required is for us
to copy the cycled palette to the same location in the frame buffer. That way, the next
time the plasma is drawn it is drawn with the new colour.
After it has been drawn, the lines are drawn over the top, each more transparent than the
first. So we end up with this neat pulsing plasma with a wave of see through lines
rippling over the top. Neat huh.
You can download the executable and the source by clicking on this link.
For those who want to know more about plasmas, read on...
So, just how do you make a plasma
Plasmas are just one of a million neat tricks you can pull off if you are willing to dust off
those old math textbooks from school and go exploring. The theory is simple, but as you
will see if you have taken a look at the program, the results can be quite stunning.
To form a plasma, we first need a gradiated colour palette. In the example I just use 64
colours going through various shades of Blue and Red.
Next, take an array of cosine values, obviously going from 0 to 359. Its worth bearing in
mind that while we in the real world deal with cosines of angles in degrees, computers
prefer Radians. So, in order to convert our degree figure into radians we need to multiply
the angle b y Pi (3.14152) and divide the result by 180. No, I can’t explain it, its just
standard math stuff.
Once you have your array of cosines built, set up 4 integers. We’ll use these integers as
pointers into the array.
We are almost ready to go. The next thing we need to create are 4 more integers. These
are speeds, they are used to add onto our other 4 pointers to move those through the
cosine table we built.
Now we are ready to go and draw our plasma. In the example code, we just grab a pointer
to the pixel data, which since we are dealing with an 8-bit image, the pointer is a character
pointer. Now, for each pixel going across the image we add the first two speed values
onto the first two pointer indexes we have into the cosine table. At the end of each row of
pixels, we just add the second two speed integers onto the second two cosine pointers,
and reset the first two back to where they were at the start of the first line.
The good bit comes when we work out the colour to assign to each pixel. We just take the
4 cosine values being pointed to and add them together. The result is then multiplied by
64 and then mod’ed by 64. The mod (%) divides the result by 64 and returns the result.
This makes sure that our result can never be bigger than 64, the total number of colours
that I use in this example.
With a nicely graded colour palette, the result is really neat graphical effect. However
there’s much further that you can take the technique, and the sample code. The program
as is just draws a preset plasma. However, by varying the speeds that are added to each
pointer you can change the plasma. You can also change it by changing the initial values
of the pointers themselves. Do this over and over in the game loop and you end up with
an animated cycling plasma that will make less worthy programmers drop to their knees
and proclaim you a god. I would have done it but I got bored ;)
Hope that helps, and if any of you out there want some more information on what
plasmas are and how they work then drop me a line.
|