DirectX Media for Animation Programmer's Guide |
![]() Previous |
![]() TOC |
![]() Index |
![]() Next |
Before beginning our explanation of behaviors, which is central to Direct Animation, we will present a few simple examples that communicate the flavor of developing in Direct Animation.
The following example uses Direct Animation for Java to construct an applet that, when displayed, results in a rendered text string that says "Hello, World." There is no animation and no interaction.
use com.ms.dxmedia.* public class MyModel extends Model { public void createModel(double startTime, BehaviorCtx ctx) { TextBvr tx = simpleText(toBvr("Hello, World")); ImageBvr im = tx.render(); setImage(im); } } public class MyApplet extends DXMApplet { public void init(){ // Always call the superclass's init() first to ensure codeBase is set super.init(); // Now set the model setModel(new MyModel()); } }
This is probably the simplest Direct Animation applet a developer can construct. It simply creates a rendering of a piece of static text. In a moment, we will see that, by adding just a few more lines of code, this example becomes far more interesting.
First, however, we will discuss how our initial example works. The Model class is introduced by Direct Animation and includes the abstract method createModel. MyModel subclasses Model and implements createModel to build some behaviors. In this case, there is a text and an image behavior. It then calls the setImage(im) method to set the model's image behavior.
In this case, we construct a Direct Animation TextBvr type by converting a character string ("Hello, World") into a TextBvr with the simpleTextfunction. (For now, we will ignore the toBvr method.) We then "render" the Text into a Direct Animation ImageBvr by calling the render method of the TextBvr. This is an abstract rendering and doesn't actually invoke the font rasterizer.
Next, MyApplet subclasses the Direct Animation-provided DXMApplet subclass of the Abstract Windows Toolkit (AWT) Applet class. This initializes its superclass (DXMApplet) to take an instance of the MyModel class. When the applet is invoked, Direct Animation builds the model (by invoking createModel) and displays it in the applet.
We will now make a small addition to our code that causes the text to continuously change color as it is rendered. We don't need to change MyApplet at all, and our MyModel now looks like this:
public class MyModel extends Model { public void createModel(double startTime, BehaviorCtx ctx) { TextBvr tx = simpleText(toBvr("Hello, World")); ColorBvr col = colorHsl(localTime, toBvr(0.5), toBvr(0.5)); TextBvr colText = tx.color(col); ImageBvr im = colText.render(); setImage(im); } }
We have added two lines to createModel. The first defines a color, using the colorHsl method. This method allows you to define a color using the Hue, Saturation, Lightness (HSL) model. For its arguments, we use 0.5 for both saturation and lightness, but provide the built-in behavior called localTime for the hue. The localTime behavior is a time-varying value of type NumberBfr that increases at the rate of one unit per second. Values of all the defined Direct Animation types are potentially time-varying and interactive.
Using localTime in a color-producing method such as colorHsl yields a time-varying color. Using this time-varying color as an argument in the tx.color(col) method yields a time-varying text value. Calling the render method on a time-varying text value produces a time-varying image. Because setImage now sets a time-varying image, the result of this code is an image that is animated.
Note that the entire applet is as you see it. No other methods are required and, in particular, there is no need for a frame loop, even though the applet is displaying an animation. For programmers who have been using the awt.Graphics package to do animation, this means you do not need to worry about threads, while() loops, or repainting the screen.
Our next step is to add some very simple interactivity. The new version of MyModel, listed below, uses the time-varying color until the left mouse button is pressed. It then changes the color to red.
public class MyModel extends Model { public void createModel(double startTime, BehaviorCtx ctx) { TextBvr tx = simpleText(toBvr("Hello, World")); ColorBvr animCol = colorHsl(localTime, toBvr(0.5), toBvr(0.5)); ColorBvr col = (ColorBvr)until(animCol, leftButtonDown, red); TextBvr colText = tx.color(col); ImageBvr im = colText.render(); setImage(im); } }
We've added a single line of code to affect this change. The expression:
until(timeVaryingCol, leftButtonDown, red);
produces a behavior that is initially animCol and remains so until the leftButtonDown event occurs. When this happens, col changes to red. There is still no need to provide a frameloop. In addition, there is no need to provide an event detection/response loop (to wait for the leftButtonDown), because this is dealt with explicitly in the implementation of the until method.
The above examples suggest the flavor of developing applications in Direct Animation. Time-varying, interactive, and reactive behaviors are constructed out of media data types and operations. The Direct Animation runtime system then takes on the task of animation, event detection, and media presentation.
The remainder of this document discusses the details of behaviors, the media data types, how behaviors are viewed, and how applications can optionally provide explicit control over their execution.
© 1997 Microsoft Corporation. All rights reserved. Legal Notices.