home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / directX / d3drm / Viewer / FlickStretch.java < prev    next >
Encoding:
Java Source  |  1996-10-10  |  4.2 KB  |  182 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import com.ms.com.*;
  4. import com.ms.com.*;
  5. import com.ms.awt.*;
  6. import com.ms.awt.peer.*;
  7. import com.ms.directX.*;
  8.  
  9. class FlickStretch implements Runnable, ddConstants
  10. {
  11.     private Thread t;
  12.     private boolean running = true;
  13.  
  14.     private long     lastTickCount     = 0;
  15.     private int        currentFrame     = 0;
  16.     private long    UpdateDelay     = 13;
  17.  
  18.     private dDraw        dd;            // DirectDraw object
  19.     private ddSurface    ddsPrimary;    // DirectDraw primary surface
  20.     private ddSurface    ddsOne;        // Offscreen surface 1
  21.     private Rect         toScreen;
  22.     private String        szBitmap;
  23.     private Rect        rc;
  24.     private Rectangle   r;
  25.     
  26.     private ComponentPeerX peer;
  27.     
  28.     //////////////////////////////////////////////////////////////////////////
  29.     
  30.     public FlickStretch(dDraw dx, ddSurface ddsP, ddSurface ddsO, String sz, 
  31.             ComponentPeerX p)
  32.     {
  33.         t = new Thread(this);
  34.         t.setPriority(Thread.NORM_PRIORITY);
  35.         
  36.         dd         = dx;
  37.         ddsPrimary = ddsP;
  38.         ddsOne     = ddsO;
  39.         szBitmap   = sz;
  40.         peer       = p;
  41.         toScreen   = new Rect();
  42.         rc         = new Rect();
  43.         r            = new Rectangle();
  44.     }
  45.  
  46.     //////////////////////////////////////////////////////////////////////////
  47.     
  48.     public void run()
  49.     {
  50.         while(running)
  51.             updateFrame();
  52.     }
  53.  
  54.     //////////////////////////////////////////////////////////////////////////
  55.     
  56.     public void stop()
  57.     {
  58.         running = false;
  59.     }
  60.  
  61.     //////////////////////////////////////////////////////////////////////////
  62.     
  63.     public void start()
  64.     {
  65.         t.start();
  66.     }
  67.  
  68.     //////////////////////////////////////////////////////////////////////////
  69.     
  70.     //
  71.     // load a bitmap from a file or resource into a DirectDraw surface.
  72.     // normaly used to re-load a surface after a restore.
  73.     //
  74.     boolean ReloadBitmap (ddSurface pdds, String szBitmap)
  75.     {
  76.         dBitmap    bm;
  77.         
  78.         bm = new dBitmap();
  79.         bm.filename (szBitmap);
  80.  
  81.         if( bm.loaded() != 0 )
  82.         {
  83.             pdds.CopyBitmap(bm, 0, 0, 0, 0);
  84.             return true;
  85.         }
  86.         else
  87.             return false;
  88.     }
  89.     
  90.     //////////////////////////////////////////////////////////////////////////
  91.     //
  92.     // basically the updateFrame from stretch
  93.     //
  94.     // Decide what needs to be blitted next, wait for flip to complete, then 
  95.     // flip the buffers.
  96.     //
  97.     
  98.     void updateFrame()
  99.     {
  100.         long    thisTickCount;
  101.         int        ddrval;
  102.         Point    pt;
  103.         int        rcleft;
  104.         int        rctop;
  105.         int        rcright;
  106.         int        rcbottom;
  107.         int        done;
  108.         
  109.         thisTickCount = dd.TickCount();
  110.         if((thisTickCount - lastTickCount) > UpdateDelay)
  111.         {
  112.             //
  113.             // Move to next frame;
  114.             //
  115.             lastTickCount = thisTickCount;
  116.             currentFrame++;
  117.             if(currentFrame > 59)
  118.                 currentFrame = 0;
  119.  
  120.             // find out where we are on the screen
  121.             peer.GetPlaceOnScreen(r);
  122.             toScreen.Left   = r.x;
  123.             toScreen.Top    = r.y;
  124.             toScreen.Right  = r.x + r.width;
  125.             toScreen.Bottom = r.y + r.height;
  126.  
  127.             //
  128.             // Blit the stuff for the next frame
  129.             //
  130.             rc.Left   = currentFrame%10*64;
  131.             rc.Top    = currentFrame/10*64;
  132.             rc.Right  = currentFrame%10*64+64;
  133.             rc.Bottom = currentFrame/10*64+64;
  134.  
  135.             done = 0;
  136.             do
  137.             {
  138.                 //
  139.                 // put offset to hwnd in Blt so we dont need ClientToScreen here.
  140.                 //
  141.                 int retval ;
  142.  
  143.                 retval = ddsPrimary.Blt( toScreen, ddsOne, rc, 0);
  144.  
  145.                 if( retval == DD_OK)
  146.                     done = 1;
  147.  
  148.                 else if (retval == DDERR_SURFACELOST)
  149.                 {
  150.                     if( !restoreAll() )
  151.                     {
  152.                         //
  153.                         // cant restore the surfaces, nothing we can do, quit
  154.                         //
  155.                         done = 1;
  156.                     }
  157.                 }
  158.  
  159.                 else if( retval != DDERR_WASSTILLDRAWING)
  160.                     //
  161.                     // undetermined error, quit the loop.
  162.                     //
  163.                     done = 1;
  164.                     
  165.             } while( done == 0);
  166.         }
  167.     }
  168.     
  169.     //////////////////////////////////////////////////////////////////////////
  170.     //
  171.     // restoreAll : restore all lost objects
  172.     //
  173.     boolean restoreAll()
  174.     {
  175.         return ddsPrimary.Restore() == DD_OK &&
  176.                ddsOne.Restore() == DD_OK &&
  177.                ReloadBitmap(ddsOne, szBitmap);
  178.     }
  179.     
  180.     //////////////////////////////////////////////////////////////////////////
  181. }
  182.