home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / DirectX / ddraw / DDrawCanvas.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  4.2 KB  |  187 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.awt.*;
  5. import java.net.URL;
  6. import com.ms.com.*;
  7. import com.ms.directX.*;
  8.     
  9. public class DDrawCanvas extends Canvas implements DirectXConstants
  10. {
  11.     DirectDraw            dd;                    // DirectDraw object
  12.     DirectDrawSurface    ddsPrimary;            // DirectDraw primary surface
  13.     DirectDrawSurface    ddsOffscreen;                // Offscreen surface 1
  14.     DirectDrawPalette    ddPal;                // DirectDraw palette
  15.     DirectDrawClipper    ddclipper;            // clipper for primary
  16.     String                 szBitmap = "Frntback.bmp";
  17.  
  18.  
  19.     boolean    FirstFrame     = true;
  20.     long     lastTickCount;
  21.     long    UpdateDelay = 10;
  22.     Rect    toScreen;
  23.     Rect    dest;
  24.     Rect    rc;
  25.  
  26.     //////////////////////////////////////////////////////////////////////////
  27.     public Dimension preferredSize() 
  28.     {
  29.         return new Dimension(640,480);
  30.     }
  31.  
  32.     //////////////////////////////////////////////////////////////////////////
  33.     DirectDrawSurface LoadBitmap(DirectDraw dd, String file, int dx, int dy)
  34.     {
  35.         DirectDrawSurface    pdds = null;
  36.         DirectDrawBitmap    bm;
  37.         DDSurfaceDesc         ddsd;    
  38.  
  39.         bm = new DirectDrawBitmap();
  40.         bm.filename (file);    
  41.         bm.initWidth(dx);
  42.         bm.initHeight(dy);
  43.  
  44.         if( bm.loaded() != 0 )
  45.         {
  46.             //
  47.             // create a DirectDrawSurface for this bitmap
  48.             //
  49.             ddsd          = new DDSurfaceDesc();
  50.             ddsd.flags    = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
  51.             ddsd.ddsCaps  = DDSCAPS_OFFSCREENPLAIN;
  52.             ddsd.width    = bm.width();
  53.             ddsd.height   = bm.height();
  54.             
  55.             pdds = dd.createSurface( ddsd );
  56.             pdds.copyBitmap(bm, 0, 0, 0, 0);
  57.         }
  58.         
  59.         return pdds;
  60.     }
  61.  
  62.     
  63.     //////////////////////////////////////////////////////////////////////////
  64.     void init(URL url)
  65.     {
  66.         Dimension s = size();
  67.  
  68.         DDSurfaceDesc         ddsd;        
  69.         
  70.         //
  71.         // create the main DirectDraw object
  72.         //
  73.         dd = new DirectDraw();
  74.         dd.setCooperativeLevel(this, DDSCL_NORMAL);
  75.  
  76.         //
  77.         // Create the primary surface
  78.         //
  79.         ddsd         = new DDSurfaceDesc();
  80.         ddsd.flags   = DDSD_CAPS ;
  81.         ddsd.ddsCaps = DDSCAPS_PRIMARYSURFACE;
  82.         ddsPrimary   = dd.createSurface(ddsd); 
  83.  
  84.         //
  85.         // create a clipper for the primary surface
  86.         //
  87.         ddclipper = dd.createClipper(0);
  88.         ddclipper.setComponent(this);            
  89.         ddsPrimary.setClipper( ddclipper );
  90.  
  91.         try
  92.         {
  93.             // Create a default palette
  94.             ddPal = dd.createPalette(DDPCAPS_8BIT);
  95.             if( ddPal != null)
  96.                 ddsPrimary.setPalette(ddPal);
  97.            }
  98.            catch(Exception e)
  99.            {
  100.             // Failed palette probably not in 8-bit mode
  101.            }
  102.  
  103.         // Deternime the path
  104.         String path;
  105.         if ( url != null )    
  106.         {
  107.             path = url.toString();
  108.             path = path.substring(6);
  109.             path = path.replace('/','\\');
  110.  
  111.             // This is a check for when running under appeletviewer 
  112.             // it puts a '.' at the end of path 
  113.             if ( path.endsWith("\\.") )
  114.             {
  115.                 int index = path.lastIndexOf('.');
  116.                 path = path.substring(0,index);
  117.             }
  118.             path = path.concat(szBitmap);
  119.         }
  120.         else
  121.         {
  122.             path = szBitmap;
  123.         }
  124.  
  125.         // load our bitmap
  126.         ddsOffscreen = LoadBitmap(dd, path, 0, 0);
  127.  
  128.         rc              = new Rect();
  129.         toScreen         = new Rect();
  130.  
  131.         toScreen.left    = 0;
  132.         toScreen.top    = 0;
  133.         toScreen.right    = s.width;
  134.         toScreen.bottom = s.height;
  135.     }
  136.  
  137.     //////////////////////////////////////////////////////////////////////////
  138.     void updateFrame()
  139.     {
  140.         long    thisTickCount;
  141.         int        ddrval;
  142.         Point    pt;
  143.         int        rcleft;
  144.         int        rctop;
  145.         int        rcright;
  146.         int        rcbottom;
  147.         int        destleft;
  148.         int        desttop;
  149.         int        destright;
  150.         int        destbottom;
  151.         int        done;
  152.  
  153.         thisTickCount = dd.tickCount();
  154.         if((thisTickCount - lastTickCount) > UpdateDelay)
  155.         {
  156.             //
  157.             // Move to next frame;
  158.             //
  159.             lastTickCount = thisTickCount;
  160.  
  161.             if(FirstFrame == true)
  162.             {
  163.                 // Blit the stuff for the first frame
  164.                 //
  165.                 rc.left   = 0;
  166.                 rc.top    = 0;
  167.                 rc.right  = 640;
  168.                 rc.bottom = 480;
  169.                 FirstFrame = false;
  170.             }
  171.             else
  172.             {
  173.                 // Blit the stuff for the first frame
  174.                 //
  175.                 rc.left   = 0;
  176.                 rc.top    = 480;
  177.                 rc.right  = 640;
  178.                 rc.bottom = 960;
  179.                 FirstFrame = true;
  180.             }
  181.  
  182.             ddsPrimary.blt( toScreen, ddsOffscreen, rc, 0);
  183.         }
  184.     }
  185. }
  186.  
  187.