⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animations.java

📁 一个3D的保龄球的源代码
💻 JAVA
字号:
/**
 * <p>Project Name: Bowling Mockup</p>
 * <p>Platform: Motorola Triplets V500</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Gameloft Shanghai</p>
 * @author Yu Jian Fei, Yun Shuang
 */


import java.io.*;
import javax.microedition.lcdui.*;

// Animation class is to draw animation.
public class Animations
{
  /** The image buffer is used for storing the animaion needed image. */
  public static Image[] m_aniImages = new Image[def.TOTAL_ANI_IMAGE];

  char m_idImage;

  /** Modules' total number in a frame.  */
  char m_nModules;

  /** Frame's total number in a action. */
  short m_nFrames;

  /** Action's total number in a animation file.*/
  short m_nActions;
  public short[] m_pModule;
  /** Store all frames' information */
  short m_pFrameData[][];

  /** Store all actions' information */
  byte m_pActionData[][];

  // ************************************************************
  /** Construct the class  */
  public Animations()
  {
  }

  // ************************************************************
  /**
   * Load the animation's image from imgFile.
   *
   * @param imgId
   * Image's index.
   * @param imgFile
   * The file path to load.
   * @return
   * If loading successfully return true,otherwise reurn false.
   */
  public boolean loadImage(int imgId)
  {
    try
    {
      m_aniImages[imgId - def.FIRST_ANI_IMAGEID]=Image.createImage("/"+CRender2D.m_text[imgId]);
      return true;
    }
    catch (Exception e)
    {
      e.printStackTrace() ;
      System.out.println("loading image error");
    }
    return false;
  }

  // ************************************************************
  /**
   * Load animation data file.
   *
   * @param is
   * DataInputStream
   *
   * @return
   * Actor type
   */
  // load data of animation class

    public int loadAnimation(  DataInputStream is ) throws Exception
    {
        int num;

        // image id
        char imgid = (char)is.readUnsignedShort() ;
        m_idImage = (char)(imgid - def.FIRST_ANI_IMAGEID) ;
        if ( null == m_aniImages[m_idImage])
        {
          loadImage(imgid);
        }

        // module
        m_nModules = (char)is.readUnsignedByte() ;

        m_pModule = new short[m_nModules * 4];
        for( int i = 0 ; i < m_nModules; i++)
        {
            m_pModule[4*i]  = (short)is.readUnsignedByte() ; //x
            m_pModule[4*i + 1] = (short)is.readUnsignedByte() ; //y
            m_pModule[4*i + 2] = (short)is.readUnsignedByte() ; //w
            m_pModule[4*i + 3] = (short)is.readUnsignedByte() ; //h
        }

        // frame data
        m_nFrames =(short)is.readUnsignedShort() ;

        m_pFrameData = new short[m_nFrames][];
        for (int i = 0; i < m_nFrames; i++)
        {
            //size =  is.readUnsignedByte() & 0xff;    ++byteCount ;
            //bytes = is.readUnsignedByte()& 0xff;   ++byteCount ;
            is.skip(2);
            num =   is.readUnsignedByte()& 0xff;
            m_pFrameData[i] = new short[num*4];

            for (int ii = 0; ii < m_pFrameData[i].length; ii++)
            {
              m_pFrameData[i][ii] = (short) is.readUnsignedShort();
            }
        }


        // action data
        m_nActions = (short)is.readUnsignedShort() ;
        m_pActionData = new byte[m_nActions][];

        for (int i = 0; i < m_nActions; i++)
        {
            //size = is.readUnsignedByte() ;  ++byteCount ;
            is.skip(1);
            num =  is.readUnsignedByte() ; // ++byteCount ;
            m_pActionData[i] = new byte[num*2];
            is.readFully( m_pActionData[i], 0 , num*2 );
        }

        return 0;
    }


  // ************************************************************
  /**
   * Read two bytes from a stream and change into a unsigned short .
   *
   * @param dis
   * DataInputStream
   * @return
   * A short value.
   */
  static short GetUnsignedShort(DataInputStream dis)
  {
    short ret;
    byte b[] = new byte[2];
    try
    {
      dis.read(b);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    ret = (short)((b[0]&0xff) + (b[1]&0xff) * 256);
    return ret;
  }


  // ************************************************************
  /**
   * Input two bytes and change into a unsigned short .
   *
   * @param low
   * low 8bit
   * @param high
   * high 8bit
   * @return
   * A short value.
   */
  short GetShort(byte low, byte high)
  {
    short ret;
    ret = (short)((low&0xff) + (high&0xff) * 256);
    return ret;
  }

  // ************************************************************
  /**
   * Draw module.
   *
   * @param imgId
   * Image's index.
   * @param mId
   * Which module to draw.
   * @param mx
   * The module's x location to draw on the screen.
   * @param my
   * The module's y location to draw on the screen.
   * @param g
   * Graphics object.
   */
  public void drawModule(int mId ,int mx,int my, Graphics g)
  {
    g.setClip(mx , my , m_pModule[mId * 4 + 2] , m_pModule[mId * 4 + 3]);
    g.drawImage(m_aniImages[m_idImage] , mx - m_pModule[mId * 4] , my - m_pModule[mId * 4 + 1] , 0);
  }


  // ************************************************************
  /**
   * Draw frame.
   *
   * @param imgId
   * Image's index.
   * @param fId
   * Which frame to draw.
   * @param fx
   * The frame's location
   * @param fy
   * The frame's location
   * @param g
   * Graphics object.
   */
  public void drawFrame(int fId , int fx , int fy , Graphics g)
  {
    try
    {
      int mCount = m_pFrameData[fId].length / 4; //GetShort(fData[0],fData[1]);
      int mx = 0, my = 0, mId = 0;
      for (int i = 0; i < mCount; i++) {
        mx = m_pFrameData[fId][i * 4 + 2];
        my = m_pFrameData[fId][i * 4 + 3];
        mId = m_pFrameData[fId][i * 4 + 0];
        if (mId < 0)
          mId = 256 + mId;
        drawModule(mId, mx + fx, my + fy, g);
      }
    }
  catch (Exception e)
  {
    e.printStackTrace();
  }
  }


  // ************************************************************
  /**
   * Get the total number of the frame.
   *
   * @param imgId
   * Image's index.
   * @param actionId
   * The action the frame is belong to.
   * @return
   * The number of the frames.
   */
  public int getActionFrames(int actionId)
  {

    return (m_pActionData[actionId].length / 2);

  }

  // ************************************************************
  /**
   * Get the frame's duration.
   *
   * @param imgId
   * Image's index.
   * @param actionId
   * The action the frame is belong to.
   * @param frameId
   * Which frame to get duration.
   *
   * @return
   * The duration value.
   */
  public int getDuration(int actionId , int frameId)
  {

    if (actionId < 0 || actionId >= m_pActionData.length)
        return -1;
    if (frameId < 0 || frameId >= m_pActionData[actionId].length)
        return -1;
    return (m_pActionData[actionId][(frameId<<1)+1]&0xFF) >> 1;

  }

  // ************************************************************
  /**
   * Draw action.
   *
   * @param imgId
   * Image's index.
   * @param actionId
   * Which action to draw.
   * @param dFrameId
   * Which frame to draw.
   * @param ax
   * Loacation to draw on the screen.
   * @param ay
   * Loacation to draw on the screen.
   * @param g
   * Graphics object
   */
  public void drawAction(int actionId , int dFrameId , int ax , int ay , Graphics g)
  {
    int actionNumber = m_pActionData[actionId].length / 2;
    if(actionNumber != 0 && dFrameId < actionNumber)
    {
      int frameid = (m_pActionData[actionId][(dFrameId<<1)]&0xFF) + ((m_pActionData[actionId][(dFrameId<<1) + 1] & 0x01) << 8);
      drawFrame(frameid , ax , ay , g); //yjf
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -