📄 sprite.java
字号:
// Copyright (c) 2004 Venan Entertainment, Inc. All rights reserved.
//
// Venan Entertainment, Inc., Middletown, Connecticut 06457
// http://www.venan.com
//
//#if defined(_IS_NOKIA) && defined(_DRAW_PIXELS_SUPPORT)
//import com.nokia.mid.ui.DirectGraphics;
//import com.nokia.mid.ui.DirectUtils;
//#endif
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.io.*;
//#ifdef _CLOSE_STREAMS
////#define CLOSE_IS(s) TorinoCanvas.closeInputStream(s);
////#define CLOSE_OS(s) TorinoCanvas.closeOutputStream(s);
//#else
//#endif
public class Sprite extends Drawable
{
//#ifdef _DEBUG
////#define DUMP_EXCEPTION(e) { //System.out.println( e.getMessage()); e.printStackTrace(); }
////#define DUMP_EXCEPTION_MSG(e,msg) { //System.out.println( msg + "\n" + e.getMessage()); e.printStackTrace(); }
////#define DEBUG_MSG(msg) //System.out.println(msg)
//#else
//#endif
//#ifdef _PROFILE
////#define START_TIMER(a) a = System.currentTimeMillis();
////#define STOP_TIMER(a) a = System.currentTimeMillis() - a;
//#else
//#endif
public Image m_image;
//#ifdef _DRAW_PIXELS_SUPPORT
// public short[] m_pixels;
//#endif
private int m_iImageWidth;
private int m_iImageHeight;
public int m_iFrameCountX;
public int m_iFrameCountY;
public int m_iNumFrames;
public int m_iSequenceLength;
public int m_iCurrentFrame;
private int m_iPaintFrame;
public int m_iFrameTime = 67;
public int m_iRefX, m_iRefY;
public int[] m_sequence;
public int[] m_aiFrameOffsetsX;
public int[] m_aiFrameOffsetsY;
public int[] m_aiFrameSrcY;
public int[] m_aiFrameWidths;
public boolean m_bLooping = false;
public Sprite( Image image )
{
m_image = image;
m_iWidth = m_iImageWidth = image.getWidth();
m_iHeight = m_iImageHeight = image.getHeight();
m_iFrameCountX = m_iFrameCountY = 1;
m_iSequenceLength = m_iNumFrames = 1;
}
public Sprite( Image image, int iWidth, int iHeight )
{
m_image = image;
m_iWidth = iWidth;
m_iHeight = iHeight;
m_iImageWidth = image.getWidth();
m_iImageHeight = image.getHeight();
m_iFrameCountX = m_iImageWidth / iWidth;
m_iFrameCountY = m_iImageHeight / iHeight;
m_iSequenceLength = m_iNumFrames = m_iFrameCountX * m_iFrameCountY;
}
public Sprite( Image image, byte[] abyData )
{
m_image = image;
m_iImageWidth = image.getWidth();
m_iImageHeight = image.getHeight();
m_iFrameCountX = 1;
ByteArrayInputStream bs = null;
DataInputStream ds = null;
try
{
bs = new ByteArrayInputStream( abyData );
ds = new DataInputStream( bs );
m_iWidth = ds.readShort();
m_iHeight = ds.readShort();
m_iSequenceLength = m_iNumFrames = m_iFrameCountY = ds.readShort();
m_aiFrameOffsetsX = new int[m_iNumFrames];
m_aiFrameOffsetsY = new int[m_iNumFrames];
m_aiFrameSrcY = new int[m_iNumFrames];
m_aiFrameWidths = new int[m_iNumFrames];
for (int i=0; i<m_iNumFrames; i++)
{
m_aiFrameOffsetsX[i] = ds.readShort();
m_aiFrameOffsetsY[i] = ds.readShort();
m_aiFrameSrcY[i] = ds.readShort();
m_aiFrameWidths[i] = ds.readShort();
}
}
catch ( IOException e )
{
}
//#ifdef _CLOSE_STREAMS
// finally
// {
// try
// {
//// CLOSE_IS(ds);
//// CLOSE_IS(bs);
// }
// catch (Exception e)
// {
// }
// }
//#endif
}
//#ifdef _DRAW_PIXELS_SUPPORT
// public Sprite( short[] pixels )
// {
// m_pixels = pixels;
// m_iWidth = m_iImageWidth = pixels[0];
// m_iHeight = m_iImageHeight = pixels[1];
// m_iFrameCountX = m_iFrameCountY = 1;
// m_iSequenceLength = m_iNumFrames = 1;
// }
//
// public Sprite( short[] pixels, int iWidth, int iHeight )
// {
// m_pixels = pixels;
// m_iWidth = iWidth;
// m_iHeight = iHeight;
// m_iImageWidth = pixels[0];
// m_iImageHeight = pixels[1];
// m_iFrameCountX = m_iImageWidth / iWidth;
// m_iFrameCountY = m_iImageHeight / iHeight;
// m_iSequenceLength = m_iNumFrames = m_iFrameCountX * m_iFrameCountY;
// }
//
// public Sprite( short[] pixels, byte[] abyData )
// {
// m_pixels = pixels;
// m_iImageWidth = pixels[0];
// m_iImageHeight = pixels[1];
// m_iFrameCountX = 1;
// ByteArrayInputStream bs = null;
// DataInputStream ds = null;
// try
// {
// bs = new ByteArrayInputStream( abyData );
// ds = new DataInputStream( bs );
// m_iWidth = ds.readShort();
// m_iHeight = ds.readShort();
// m_iSequenceLength = m_iNumFrames = m_iFrameCountY = ds.readShort();
// m_aiFrameOffsetsX = new int[m_iNumFrames];
// m_aiFrameOffsetsY = new int[m_iNumFrames];
// m_aiFrameSrcY = new int[m_iNumFrames];
// m_aiFrameWidths = new int[m_iNumFrames];
// for (int i=0; i<m_iNumFrames; i++)
// {
// m_aiFrameOffsetsX[i] = ds.readShort();
// m_aiFrameOffsetsY[i] = ds.readShort();
// m_aiFrameSrcY[i] = ds.readShort();
// m_aiFrameWidths[i] = ds.readShort();
// }
// }
// catch ( IOException e )
// {
// }
//#ifdef _CLOSE_STREAMS
// finally
// {
// try
// {
//// CLOSE_IS(ds);
//// CLOSE_IS(bs);
// }
// catch (Exception e)
// {
// }
// }
//#endif
// }
//#endif
public final int getCenterX()
{
return m_iX + (m_iWidth>>1);
}
public final int getCenterY()
{
return m_iY + (m_iHeight>>1);
}
public void setFPS( int iFPS )
{
m_iFrameTime = 1000/iFPS;
}
public void setTime( int iTime )
{
m_iCurrentFrame = iTime / m_iFrameTime;
if (m_bLooping)
m_iCurrentFrame %= m_iSequenceLength;
else
m_iCurrentFrame = ( ( (m_iCurrentFrame) <= (0) ) ? (0) : ( ( (m_iCurrentFrame) >= (m_iSequenceLength-1) ) ? (m_iSequenceLength-1) : (m_iCurrentFrame) ) ) ;
calcPaintFrame();
}
public void nextFrame()
{
if( m_iCurrentFrame == m_iSequenceLength - 1 )
{
m_iCurrentFrame = 0;
}
else
{
m_iCurrentFrame++;
}
calcPaintFrame();
}
public void prevFrame()
{
if( m_iCurrentFrame == 0 )
{
m_iCurrentFrame = m_iSequenceLength - 1;
}
else
{
m_iCurrentFrame--;
}
calcPaintFrame();
}
public void setFrame( int iFrame )
{
m_iCurrentFrame = ( ( (iFrame) <= (0) ) ? (0) : ( ( (iFrame) >= (m_iSequenceLength-1) ) ? (m_iSequenceLength-1) : (iFrame) ) ) ;
calcPaintFrame();
}
public void setFrameSequence( int[] sequence )
{
m_sequence = sequence;
m_iCurrentFrame = 0;
m_iPaintFrame = 0;
if (m_sequence != null)
m_iSequenceLength = m_sequence.length;
else
m_iSequenceLength = m_iNumFrames;
}
private void calcPaintFrame()
{
if (m_sequence != null)
m_iPaintFrame = m_sequence[m_iCurrentFrame];
else
m_iPaintFrame = m_iCurrentFrame;
}
int iClipX;
int iClipY;
int iClipWidth;
int iClipHeight;
int iPaintX;
int iPaintY;
int iSrcX;
int iSrcY;
int iWidth;
int iHeight;
public void paint( Graphics g )
{
if (m_aiFrameSrcY != null)
{
iPaintX = m_iX-m_iRefX+m_aiFrameOffsetsX[m_iPaintFrame];
iPaintY = m_iY-m_iRefY+m_aiFrameOffsetsY[m_iPaintFrame];
if (m_iPaintFrame == m_iFrameCountY-1)
iHeight = m_iImageHeight-m_aiFrameSrcY[m_iPaintFrame];
else
iHeight = m_aiFrameSrcY[m_iPaintFrame+1]-m_aiFrameSrcY[m_iPaintFrame];
iWidth = m_aiFrameWidths[m_iPaintFrame];
}
else
{
iPaintX = m_iX-m_iRefX;
iPaintY = m_iY-m_iRefY;
iWidth = m_iWidth;
iHeight = m_iHeight;
}
//#ifdef _FAST_SPRITE_REJECT
// better clipping code to reduce graphics calls
if (iPaintX >= TorinoCanvas.m_iScreenWidth ||
iPaintY >= TorinoCanvas.m_iScreenHeight ||
iPaintX+iWidth <= 0 ||
iPaintY+iHeight <= 0)
return;
//#endif
if (m_aiFrameSrcY != null)
{
iSrcX = 0;
iSrcY = m_aiFrameSrcY[m_iPaintFrame];
}
else
{
iSrcX = m_iWidth*(m_iPaintFrame % m_iFrameCountX);
iSrcY = m_iHeight*(m_iPaintFrame / m_iFrameCountX);
}
//#ifdef _IS_NokiaS60
// int iOldClipX = TorinoCanvas.getClipX(g);
// int iOldClipY = TorinoCanvas.getClipY(g);
// int iOldClipWidth = TorinoCanvas.getClipWidth(g);
// int iOldClipHeight = TorinoCanvas.getClipHeight(g);
//#else
int iOldClipX = g.getClipX();
int iOldClipY = g.getClipY();
int iOldClipWidth = g.getClipWidth();
int iOldClipHeight = g.getClipHeight();
//#endif
g.clipRect( iPaintX, iPaintY, iWidth, iHeight );
//#ifdef _DRAW_PIXELS_SUPPORT
// if (m_image != null)
// {
// g.drawImage( m_image, iPaintX - iSrcX, iPaintY - iSrcY,
// Graphics.TOP|Graphics.LEFT );
// }
// else
// {
// DirectGraphics dg = DirectUtils.getDirectGraphics(g);
// dg.drawPixels( m_pixels, true, 2 + iSrcX + (iSrcY*m_iImageWidth),
// m_iImageWidth, iPaintX, iPaintY, iWidth, iHeight, 0,
// DirectGraphics.TYPE_USHORT_4444_ARGB );
// }
//#else
g.drawImage( m_image, iPaintX - iSrcX, iPaintY - iSrcY,
Graphics.TOP|Graphics.LEFT );
//#endif
//#ifdef _IS_NokiaS60
// TorinoCanvas.setClip( g, iOldClipX, iOldClipY, iOldClipWidth, iOldClipHeight );
//#else
g.setClip( iOldClipX, iOldClipY, iOldClipWidth, iOldClipHeight );
//#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -