BE ENGINEERING INSIGHTS: Kernel Engineer Breaches
  Software's Iron Curtain and Lives to Tell About It
By Ficus Kirkpatrick -- <ficus@be.com>

I don't often poke my head out above the Iron Curtain that
divides user and kernel space, but recently, I decided to
wander over, see the sights, and visit the people.

Benoît had written a series of programs that would display
something cool on the screen. They were all derived from the
same skeleton: constantly iterate over the frame buffer, and
calculate the value of each pixel based on its location. The
only variation between these programs was the expression in
the inner loop.

I experimented with making my own programs like this for a
while, but got tired of constantly recompiling. "It would be
excellent," I mused, "if I could just type the expression in
a window." I came up with a solution that should
simultaneously galvanize and repulse true believers of the
Holy Grail of software engineering: code reuse. Employ what
is already the best expression parser and optimizer in the
system: the C compiler!

<ftp://ftp.be.com/pub/samples/game_kit/Whack.zip>

Whack does lots of interesting things, but the highlight is
that it generates its own drawing code and loads it on the
fly. Whack also makes use of BDirectWindow, BRoster's
running app list update mechanism, and the B_SIMPLE_DATA
Drag-and-Drop protocol. It does lots of multithreading and
everything else a good Be application should do.

The user interface is simple. There's a menu bar at the top
of the window, an area in the middle for viewing the
plotting of your expression, and an area at the bottom to
enter it. You can save add-ons, drop them into the window,
and add the ones you like to a list of favorites.

In the directory Whack runs from, there's a file called
"template" that contains the code for a BDirectWindow
drawing loop, but with __EXPRESSION__ in place of the real
one. All Whack has to do is insert a preprocessor directive
indicating what the expression should actually be and
compile it.

What makes it look cool is that the variables you use in
your expression change depending on the position of the
pixel it is being evaluated for, time, frame count, etc. The
variables available to use are:

x   The screen-relative X coordinate of the current pixel.
y   The screen-relative Y coordinate of the current pixel.
ix  The window-relative X coordinate of the current pixel.
iy  The window-relative Y coordinate of the current pixel.
f   A number incremented once per frame.
t   Time (actually, a number incremented once per pixel).

One of the really interesting effects you can achieve is by
using the screen-based and window-based position variables
together and then moving the window around. Give it a try.

There are a few things missing from Whack. I didn't have
time to implement support for anything other than 32 bits
per pixel, but the space is there for you to fill out, if
you're so inclined. You just need to convert the value
generated by the expression from 32-bit RGBA to your desired
bit depth.

So, what's the alpha channel used for? The extremely simple
drawing loop doesn't do any blending. Somehow it didn't seem
right to be taking that extra byte of memory and not making
any use of it. You may have heard of QuickPaint, the
industry standard for paint programs that is the subject of
George's Newsletter articles. I decided to apply to the
QuickPaint Developer Program and see how I could integrate
that extra unused byte from Whack into QuickPaint's new
alpha-blending feature.

Try running both programs at once, and notice that you can
send the current frame of Whack to QuickPaint as a new
layer. "QuickPaint and Whack have achieved a level of
synergy between two applications unmatched on any platform,"
said George Hoffman, CEO of QuickPaint, Inc.

Unfortunately, it requires a little work on the part of the
expression writer to make the Whack frame show up correctly
in QuickPaint. Take, for example, a boring expression like
"x+y+f", and try sending it to QuickPaint. The problem is
that an alpha value of zero means no opacity. Now try
"(x+y+f)&0xff000000". If you're really clever, you can
probably figure out how to achieve partial opacity.

I feel somehow wiser after this venture into the world of
writing user programs, but I think I'll head back over the
fence again.

See you next time!

