home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / DirectX / dinput / DDrawCanvas.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  5.8 KB  |  267 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.     DirectInput    joy;
  27.     JoyInfo        joypos;
  28.     JoyCaps        joycaps;
  29.     int            joyxrange;
  30.     int            joyyrange;
  31.  
  32.     //////////////////////////////////////////////////////////////////////////
  33.     public Dimension preferredSize() 
  34.     {
  35.         return new Dimension(640,480);
  36.     }
  37.  
  38.     //////////////////////////////////////////////////////////////////////////
  39.     DirectDrawSurface LoadBitmap(DirectDraw dd, String file, int dx, int dy)
  40.     {
  41.         DirectDrawSurface    pdds = null;
  42.         DirectDrawBitmap    bm;
  43.         DDSurfaceDesc         ddsd;    
  44.  
  45.         bm = new DirectDrawBitmap();
  46.         bm.filename (file);    
  47.         bm.initWidth(dx);
  48.         bm.initHeight(dy);
  49.  
  50.         if( bm.loaded() != 0 )
  51.         {
  52.             //
  53.             // create a DirectDrawSurface for this bitmap
  54.             //
  55.             ddsd          = new DDSurfaceDesc();
  56.             ddsd.flags    = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
  57.             ddsd.ddsCaps  = DDSCAPS_OFFSCREENPLAIN;
  58.             ddsd.width    = bm.width();
  59.             ddsd.height   = bm.height();
  60.             
  61.             pdds = dd.createSurface( ddsd );
  62.             pdds.copyBitmap(bm, 0, 0, 0, 0);
  63.         }
  64.         
  65.         return pdds;
  66.     }
  67.  
  68.     
  69.     //////////////////////////////////////////////////////////////////////////
  70.     void init(URL url)
  71.     {
  72.         Dimension s = size();
  73.  
  74.         DDSurfaceDesc         ddsd;        
  75.         
  76.         //
  77.         // create the main DirectDraw object
  78.         //
  79.         dd = new DirectDraw();
  80.         dd.setCooperativeLevel(this, DDSCL_NORMAL);
  81.  
  82.         //
  83.         // Create the primary surface
  84.         //
  85.         ddsd         = new DDSurfaceDesc();
  86.         ddsd.flags   = DDSD_CAPS ;
  87.         ddsd.ddsCaps = DDSCAPS_PRIMARYSURFACE;
  88.         ddsPrimary   = dd.createSurface(ddsd); 
  89.  
  90.         //
  91.         // create a clipper for the primary surface
  92.         //
  93.         ddclipper = dd.createClipper(0);
  94.         ddclipper.setComponent(this);            
  95.         ddsPrimary.setClipper( ddclipper );
  96.  
  97.         try
  98.         {
  99.             // Create a default palette
  100.             ddPal = dd.createPalette(DDPCAPS_8BIT);
  101.             if( ddPal != null)
  102.                 ddsPrimary.setPalette(ddPal);
  103.            }
  104.            catch(Exception e)
  105.            {
  106.             // Failed palette probably not in 8-bit mode
  107.            }
  108.  
  109.         // Deternime the path
  110.         String path;
  111.         if ( url != null )    
  112.         {
  113.             path = url.toString();
  114.             path = path.substring(6);
  115.             path = path.replace('/','\\');
  116.  
  117.             // This is a check for when running under appeletviewer 
  118.             // it puts a '.' at the end of path 
  119.             if ( path.endsWith("\\.") )
  120.             {
  121.                 int index = path.lastIndexOf('.');
  122.                 path = path.substring(0,index);
  123.             }
  124.             path = path.concat(szBitmap);
  125.         }
  126.         else
  127.         {
  128.             path = szBitmap;
  129.         }
  130.  
  131.         // load our bitmap
  132.         ddsOffscreen = LoadBitmap(dd, path, 0, 0);
  133.  
  134.         rc              = new Rect();
  135.         toScreen         = new Rect();
  136.  
  137.         toScreen.left    = 0;
  138.         toScreen.top    = 0;
  139.         toScreen.right    = s.width;
  140.         toScreen.bottom = s.height;
  141.  
  142.         joy           = new DirectInput();
  143.         joypos        = new JoyInfo();
  144.         joycaps       = new JoyCaps();
  145.  
  146.         //
  147.         // Get number of potentially avaible joysticks 
  148.         //
  149.         int numofjoysticks = joy.getNumDevs();
  150.  
  151.         try
  152.         {
  153.             //
  154.             // Get the Capabilities of the joy stick
  155.             //
  156.             joy.getDevCaps(JOYSTICKID1,joycaps);
  157.  
  158.             //
  159.             // Set the axis's I'm interested in
  160.             //
  161.             joypos.flags = JOY_RETURNX | JOY_RETURNY;
  162.  
  163.             //
  164.             // determine the range of useful values 
  165.             //
  166.             joyxrange = (joycaps.xMax - joycaps.xMin)/2;
  167.             joyyrange = (joycaps.yMax - joycaps.yMin)/2;
  168.         }
  169.         catch ( Exception e )
  170.         {
  171.             // DirectInput will throw execptions if a joy stick is not connected
  172.             joypos.flags = 0;
  173.             joyxrange = 0;
  174.             joyyrange = 0;
  175.         }
  176.     }
  177.  
  178.     //////////////////////////////////////////////////////////////////////////
  179.     void updateFrame()
  180.     {
  181.         long    thisTickCount;
  182.         int        ddrval;
  183.         Point    pt;
  184.         int        rcleft;
  185.         int        rctop;
  186.         int        rcright;
  187.         int        rcbottom;
  188.         int        destleft;
  189.         int        desttop;
  190.         int        destright;
  191.         int        destbottom;
  192.         int        done;
  193.  
  194.         thisTickCount = dd.tickCount();
  195.         if((thisTickCount - lastTickCount) > UpdateDelay)
  196.         {
  197.             //
  198.             // Move to next frame;
  199.             //
  200.             lastTickCount = thisTickCount;
  201.  
  202.             if ( joypos.flags != 0 )
  203.             {
  204.                 try
  205.                 {
  206.                     joy.getPos(0,joypos);
  207.                 }
  208.                 catch ( Exception e )
  209.                 {
  210.                     // keep app alive even if joystick is eith missing or offline somehow.
  211.                     joypos.xPos = 0;
  212.                     joypos.yPos = 0;
  213.                 }
  214.             }
  215.  
  216.             if ( joyxrange != 0 && joyyrange != 0 )
  217.             {
  218.                 toScreen.left   += ((joypos.xPos - joyxrange)*10)/joyxrange;
  219.                 toScreen.top    += ((joypos.yPos - joyyrange)*10)/joyyrange;
  220.             }
  221.  
  222.             if ( toScreen.left < 0 || toScreen.left > 640-64 )
  223.             {
  224.                 if ( toScreen.left < 0 )    
  225.                     toScreen.left = 0;
  226.                 else
  227.                     toScreen.left = 640-64;
  228.  
  229.             }
  230.             if ( toScreen.top < 0 || toScreen.top > 480-64 )
  231.             {
  232.                 if ( toScreen.top < 0 )
  233.                     toScreen.top = 0; 
  234.                 else
  235.                     toScreen.top = 480-64;
  236.             }
  237.  
  238.             toScreen.right  = toScreen.left + 64;
  239.             toScreen.bottom = toScreen.top  + 64;
  240.  
  241.             if(FirstFrame == true)
  242.             {
  243.                 // Blit the stuff for the first frame
  244.                 //
  245.                 rc.left   = 0;
  246.                 rc.top    = 0;
  247.                 rc.right  = 640;
  248.                 rc.bottom = 480;
  249.                 FirstFrame = false;
  250.             }
  251.             else
  252.             {
  253.                 // Blit the stuff for the first frame
  254.                 //
  255.                 rc.left   = 0;
  256.                 rc.top    = 480;
  257.                 rc.right  = 640;
  258.                 rc.bottom = 960;
  259.                 FirstFrame = true;
  260.             }
  261.  
  262.             ddsPrimary.blt( toScreen, ddsOffscreen, rc, 0);
  263.         }
  264.     }
  265. }
  266.  
  267.