com.hdcookbook.grin.animator
Class DirectDrawEngine

java.lang.Object
  extended by com.hdcookbook.grin.animator.AnimationEngine
      extended by com.hdcookbook.grin.animator.ClockBasedEngine
          extended by com.hdcookbook.grin.animator.DirectDrawEngine
All Implemented Interfaces:
java.lang.Runnable

public class DirectDrawEngine
extends ClockBasedEngine

A double-buffered animation engine that uses direct draw. The engine maintains a BufferedImage that the animation clients paint into for each animation frame. At the end of each frame, this buffer is blitted to the framebuffer using Component.getGraphics(), and Toolkit.sync() is called.

On most players, direct draw is probably the fastest drawing option. However, direct draw doesn't take advantage of platform double-buffering, that is, platforms where HScene.isDoubleBuffered() returns true. On such platforms, repaint draw will save memory, and on some implementations of platform double buffering, repaint draw may even prove to be faster.

Direct draw also has the drawback (no pun intended) that you can't draw widgets in back of or in front of the area managed by the engine, unless you make sure the right drawing happens yourself. In other words, using direct draw in an area makes it difficult to have widgets (like HButton) occupy overlapping screen real estate.


Field Summary
 
Fields inherited from class com.hdcookbook.grin.animator.AnimationEngine
modelTimeSkipped, renderContext, repaintBounds, targetsCanOverlap, transparent
 
Constructor Summary
DirectDrawEngine()
          Create a new DirectDrawEngine.
 
Method Summary
protected  void callPaintTargets()
          Paint the current frame into the right graphics buffer.
protected  void clearArea(int x, int y, int width, int height)
          Called from showFrame() to cause an area to be cleared in the current frame.
protected  void finishedFrame()
          Called when the engine is finished drawing the current frame.
 java.awt.Component getComponent()
          Get the component that this animation engine renders into.
 int getHeight()
          Get the height of the area we display over
 int getWidth()
          Get the width of the area we display over
 void initContainer(java.awt.Container container, java.awt.Rectangle bounds)
          Initialize this engine with its parent container and the position and size of the engine within the container.
protected  boolean needsFullRedrawInAnimationLoop()
          Tell us whether or not this style of animation requires a full redraw of everything on the screen in each pass through the animation loop.
protected  void terminatingEraseScreen()
          Erase the screen because the AnimationManager is terminating.
 java.lang.String toString()
           
 
Methods inherited from class com.hdcookbook.grin.animator.ClockBasedEngine
getFps, pause, runAnimationLoop, setFps, skipFrames, start
 
Methods inherited from class com.hdcookbook.grin.animator.AnimationEngine
advanceModel, checkDestroy, checkNewClients, destroy, destroyRequested, getAnimationClients, getDrawTargets, getEraseTargets, getModelTimeSkipped, getNumDrawTargets, getNumEraseTargets, initClients, initialize, paintFrame, paintNextFrameFully, paintTargets, repaintFrame, resetAnimationClients, run, setAllowOverlappingTargets, setDrawTargetCollapseThreshold, setThreadPriority, showFrame
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DirectDrawEngine

public DirectDrawEngine()
Create a new DirectDrawEngine. It needs to be initialized with the various initXXX methods (including the inherited ones).

Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

initContainer

public void initContainer(java.awt.Container container,
                          java.awt.Rectangle bounds)
Initialize this engine with its parent container and the position and size of the engine within the container. This should be called exactly once, before start() is called. A good time to call this would be in the animationInitialize() call within the context code passed to initialize().

The Component will be set to the specified bounds, and an internal double buffer may be created to these bounds. The container is expected to have a null layout; if it doesn't, this might change the widget's size to be different than the bounds passed in (and therefore the double buffer). The container must be visible when this is called.

Specified by:
initContainer in class AnimationEngine
See Also:
AnimationEngine.initialize(com.hdcookbook.grin.animator.AnimationContext), AnimationContext.animationInitialize()

getWidth

public int getWidth()
Get the width of the area we display over

Specified by:
getWidth in class AnimationEngine
Returns:
the width

getHeight

public int getHeight()
Get the height of the area we display over

Specified by:
getHeight in class AnimationEngine
Returns:
the height

getComponent

public java.awt.Component getComponent()
Get the component that this animation engine renders into. This is added by the engine to the container.

Specified by:
getComponent in class AnimationEngine
Returns:
the component, or null if initContainer hasn't been called yet.
See Also:
AnimationEngine.initialize(com.hdcookbook.grin.animator.AnimationContext), AnimationContext.animationInitialize()

clearArea

protected void clearArea(int x,
                         int y,
                         int width,
                         int height)
Called from showFrame() to cause an area to be cleared in the current frame.

Specified by:
clearArea in class AnimationEngine
See Also:
AnimationEngine.showFrame()

needsFullRedrawInAnimationLoop

protected boolean needsFullRedrawInAnimationLoop()
Tell us whether or not this style of animation requires a full redraw of everything on the screen in each pass through the animation loop. This is called from the animation loop.

This is always false for direct draw. Because we maintian our own double buffer, nothing external can damage its contents.

Specified by:
needsFullRedrawInAnimationLoop in class AnimationEngine

callPaintTargets

protected void callPaintTargets()
                         throws java.lang.InterruptedException
Paint the current frame into the right graphics buffer. The subclass implementation of this method should call paintTargets(Graphics2D).

Specified by:
callPaintTargets in class AnimationEngine
Throws:
java.lang.InterruptedException
See Also:
AnimationEngine.paintFrame(java.awt.Graphics2D)

finishedFrame

protected void finishedFrame()
Called when the engine is finished drawing the current frame. This should flush the results to the screen, if needed. The framework guarantees that each call to callPaintTargets() will be followed by a call to finishedFrame(), even if the thread is interrupted or there's a RuntimeException that terminates the thread.

Specified by:
finishedFrame in class AnimationEngine

terminatingEraseScreen

protected void terminatingEraseScreen()
Erase the screen because the AnimationManager is terminating. This is called in the animation thread as a result of a call to destroy(), just before the animation thread terminates.

Specified by:
terminatingEraseScreen in class AnimationEngine