卡马克算法例子(网上转贴的,我没细看哈)

当屏幕在背景中移动时,实际上所涉及的 Tile 根本没有变化,或者只有一小部分发生了改变。所以我们可以创建一个背景图像缓冲(buffer),保存当前屏幕的背景图像,减少每帧得画图次数,可以大大提高速度。
当我们创建的图像缓冲和黄色区域大小相同时,如果背景涉及的Tile没有变化,我们只需将缓冲图像画到屏幕的适当位置上。如果背景涉及的 Tile 发生了变化,如变为绿色区域,我们只需更新变化的部分Tile到背景缓冲,再将缓冲画到屏幕上即可。

0,0                       X
 
 
 
 
 
 
 
 
Y

为了保留缓冲图像中已有部分,我们采用滚动更新的方法。假设,当屏幕向右下移动时,缓冲中最上和最左的Tile已无用(即I、II、III区),我们便将新的最下的Tile 保存在最上(I、II区),将新的最右的Tile保存在最左(I、III区),得到新的缓冲图像。最后,我们将新的背景缓冲分4区,按照实际位置画到屏幕上即可,对应关系如图。
当然,按照屏幕在背景中的位置,我们的缓冲分区会出现四种情况。
l        分1个区,刚好背景没有被缓冲切开
l        分2个区,背景只在X轴方向被缓冲切开
l        分2个区,背景只在Y轴方向被缓冲切开
l        分4个区,背景在X、Y轴方向都被缓冲切开(如例子)

代码要求:

I                    II
 
 
 
 
III                 IV
 
 
 
                     IV                         III
 
 
 
 
 
                     II                            I
背景缓冲
实际背景

1.每帧生成一个随机数,基于随机数让地图随机的上、下、左、右移动。
2.程序一开始应该有菜单选项让用户选择地图随机移动的步长,1 pixel,2pixel,4 pixel,8 pixel
3.程序运行1000帧以后,计算出平均每帧占用的毫秒数(只包括update缓冲区和repaint的时间,不包括调用生成随机数的函数的时间)并显示在屏幕上。
4.可以通过按某个键进行地图移动方式得切换(随机移动和手动移动地图)
5.生成的jad以及jar里面的manifest文件的如下两个字段统一规定如下
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-2.0
6.由于程序要上传到手机上的,而手机的键盘值不同于标准模拟器
键盘值应按如下进行更改
    public static final int DEVKEY_UP    = -1; (方向盘上键)
    public static final int DEVKEY_DOWN = -6; (方向盘下键)
    public static final int DEVKEY_LEFT = -2; (方向盘左键)
    public static final int DEVKEY_RIGTH = -5; (方向盘右键)
    public static final int DEVKEY_LSOFT = -21;(左软键)
    public static final int DEVKEY_RSOFT = -22;(右软键)
public static final int DEVKEY_MSOFT = -20;(中软键)
7. 统一把Canvas设置为全屏模式
 
import  javax.microedition.midlet.MIDlet;
import  javax.microedition.lcdui. * ;
import  java.io. * ;
import  javax.microedition.lcdui.game.Sprite;
import  java.util. * ;

public  class  FastMap  extends  MIDlet  {
    
private TGame m_tGame    = null;
    
private Display m_display    = null;
    
    
public FastMap() {
        
super();
        m_tGame 
= new TGame();
        m_display 
= Display.getDisplay(this);
    }


    
protected void startApp() {
        System.out.println(
"Vendor :" + getAppProperty("MIDlet-Vendor"));
        System.out.println(
"Description :" + getAppProperty("MIDlet-Description"));
        System.out.println(
"JadFile Version :" + getAppProperty("MIDlet-Version"));
        System.out.println(
"MIDlet-Data-Size :" + getAppProperty("MIDlet-Data-Size"));
        m_display.setCurrent(m_tGame);
        
//m_tGame.repaint ();
    }


    
protected void pauseApp() {
        
// TODO Auto-generated method stub
        System.out.println("train pause the app");
    }


    
protected void destroyApp(boolean arg0)  {
        
// TODO Auto-generated method stub
        System.out.println("train destory the app");
    }

}
    

class  TGame  extends  Canvas  implements  Runnable  {
    
public static final int DEVKEY_UP    = -1;     //???????
    public static final int DEVKEY_DOWN  = -6;     //???????
    public static final int DEVKEY_LEFT  = -2;     //???????
    public static final int DEVKEY_RIGTH = -5;     //???????
    public static final int DEVKEY_LSOFT = -21//?????
    public static final int DEVKEY_RSOFT = -22//?????
    public static final int DEVKEY_MSOFT = -20;    //?????

    
private final static long REFRESH_TIME_MS    = 100;
    
private final static int HORZ_FLIP    = 0x4000;
    
//private final static int VERT_FLIP= 0x8000;
    private final static int IND_TILE    = 0x00ff;
    
private boolean m_exitApp    = false;
    
private boolean m_initialize= false;
    
private Thread    m_thread    = null;

    
private class TilePos {//用类表示位置,聪明
        public short x = 0;
        
public short y = 0;
    }

    
    
private Image        m_buffer    = null;
    
private Image        m_buffer2    = null;    // for changed part.
    private Image        m_backImage    = null;
    
private Graphics    m_bufferG    = null
    
private Graphics    m_bufferG2    = null;
    
private TilePos[]    m_imageBuf    = null;
    
    
private short[][] m_mapBuf    = null;
    
    
private short    m_mapRow    = 0;
    
private short    m_mapCol    = 0;
    
private short    m_tileWidth    = 0;
    
private short    m_tileHeight= 0;
    
    
private short    m_scrRow    = 0;
    
private short    m_scrCol    = 0;
    
    
private short    m_oldScrRowInMap    = 0;
    
private short    m_oldScrColInMap    = 0;
    
private short     m_curScrRowInMap    = 0;
    
private short    m_curScrColInMap    = 0;
    
    
// m_xOffset and m_yOffset is between 0 to 8.
    private short    m_oldXOffset    = 0;
    
private short    m_oldYOffset    = 0;
    
private short    m_curXOffset    = 0;
    
private short    m_curYOffset    = 0;
    
    
// In our practise, the move step is 1, 2, 4, 8, we can use index to deal with.
    private short    m_tileIndexs    = 0;
    
private short    m_curXTileIndex    = 0;    // From 1 to m_tileIndexs.
    private short    m_curYTileIndex = 0;    // From 1 to m_tileIndexs.
    
    
private short    m_nSteps    = 0;
    
private short    m_nXSteps    = 0;
    
private short    m_nYSteps    = 0;
    
private boolean m_selectStep= false;

    
private boolean m_ranMode    = false;// random mode.
    private boolean m_keyDown    = false;
    
    
private int m_lastKeyCode    = 0;
    
private long m_preTimeMs    = 0;
    
    TGame() 
{
        m_thread 
= new Thread(this);
        m_thread.start();
        System.out.println(
"train thread started");
    }

    
    
public int getHeight () {
        
return 208;
    }


    
public void run() {
        
if (!m_initialize) {
            
try {
                setFullScreenMode (
true);
                m_backImage 
= Image.createImage("/level1_tile.png");
                                
                InputStream is 
= getClass().getResourceAsStream("/Map.dat");
                DataInputStream dis 
= new DataInputStream(is);
                
                m_mapCol    
= ConvBigEndToLitEnd(dis.readShort());
                m_mapRow    
= ConvBigEndToLitEnd(dis.readShort());
                m_tileWidth    
= ConvBigEndToLitEnd(dis.readShort());
                m_tileHeight
= ConvBigEndToLitEnd(dis.readShort());

//                System.out.println("image width : " + m_backImage.getWidth());
//                System.out.println("image height: " + m_backImage.getHeight());
                System.out.println("map col : " + m_mapCol);
                System.out.println(
"map row: " + m_mapRow);
//                System.out.println("tile width : " + m_tileWidth);
//                System.out.println("tile height: " + m_tileHeight);
                
                
// read map.dat file data.
                m_mapBuf = new short[m_mapRow][m_mapCol];
                
for (int i = 0; i < m_mapRow; i++{
                    
for (int j = 0; j < m_mapCol; j++{
                        m_mapBuf[i][j] 
= ConvBigEndToLitEnd(dis.readShort());
//                        System.out.println("map[" + i + "][" + j + "] = " + m_mapBuf[i][j]);
                    }

                }

                                
                
// 64, 8x8, need the m_tileWidth and m_tileHeight.
                
// computer the number of tiles in the image.
                int col = m_backImage.getWidth () / m_tileWidth;//this is the info of tileset
                int row = m_backImage.getHeight() / m_tileHeight;
                
                
int bufferSize = row * col;//>> 6;
                
                
// allocate memory for control image tiles.先分配内存,才能赋值,切记切记!
                m_imageBuf = new TilePos[bufferSize];
                
for (int i = 0; i < m_imageBuf.length; i++{
                    m_imageBuf[i] 
= new TilePos();
                }

                                    
                
// initialize each tiles' location.
                System.out.println("tile row : " + row);//这是在tileset中的tile的位置,一共七列十七行。
                System.out.println("tile col : " + col);
                
int index = 0;
                
for (int i = 0; i < row; i++{
                    
for (int j = 0; j < col; j++{            
                        m_imageBuf[index].x 
= (short)(j * m_tileWidth);            
                        m_imageBuf[index].y 
= (short)(i * m_tileHeight);
//                        System.out.println("m_imageBuf[" + index + "].xy = {" + m_imageBuf[index].x +
//                            ", " + m_imageBuf[index].y + "}");
                        index++;
                    }

                }

                            
                m_scrCol 
= (short)(getWidth () / m_tileWidth);        // screen.
                m_scrRow = (short)(getHeight () / m_tileHeight);
                
                System.out.println(
"screen row : " + m_scrRow);
                System.out.println(
"screen col: " + m_scrCol);
                                
                m_initialize 
= true;
            }
 catch (IllegalArgumentException e) {
                e.printStackTrace();
                System.out.println(
"Invalid parameter");
            }
 catch (IOException e) {
                e.printStackTrace();
                System.out.println(
"Load resource failed");
            }
 catch (SecurityException e) {
                e.printStackTrace();
                System.out.println(
"Security error");
            }

        }


        System.out.println(
"train enter run()");
        Random random 
= new Random();
        
while (!m_exitApp && m_initialize) {
            
//System.out.println("del once: " + m_keyDelOnce);
                
            
long curTimeMs = System.currentTimeMillis();
                
            
while (curTimeMs - m_preTimeMs < REFRESH_TIME_MS) {
                Thread.yield();
                curTimeMs 
= System.currentTimeMillis();
            }


            m_preTimeMs 
= curTimeMs;
            
            
if (m_ranMode) {
                
switch (random.nextInt(4)) {
                
case 0:
                    keyPressed(Canvas.KEY_NUM4);
                    
break;
                    
                
case 1:
                    keyPressed(Canvas.KEY_NUM6);
                    
break;
                    
                
case 2:
                    keyPressed(Canvas.KEY_NUM2);
                    
break;
                    
                
case 3:
                    keyPressed(Canvas.KEY_NUM8);
                    
break;
                }

            }

            
else {
                
if (m_keyDown)
                    keyPressed(m_lastKeyCode);
            }

            
            repaint();
            serviceRepaints();
        }

    }

    
    
protected void paint(Graphics g) {            
        
// draw background.
        g.setColor(0);
        g.fillRect(
00, getWidth(), getHeight());
            
        
// you must select one step firstly.
        if (!m_selectStep) {
            g.setColor(
0x00ffffff);
            g.drawString(
"Please select move step:"00, Graphics.TOP | Graphics.LEFT);
            g.drawString(
"1 : 1 pixel."012, Graphics.TOP | Graphics.LEFT);
            g.drawString(
"2 : 2 pixel."024, Graphics.TOP | Graphics.LEFT);
            g.drawString(
"3 : 4 pixel."036, Graphics.TOP | Graphics.LEFT);
            g.drawString(
"4 : 8 pixel."048, Graphics.TOP | Graphics.LEFT);
            
            
return;
        }


        
//System.out.println("Begin draw image");
        
        
boolean bufferHasData = true;
        
if (null == m_buffer || null == m_buffer2/* || null == m_tempImage*/{
            m_buffer    
= Image.createImage(getWidth(), getHeight());
            m_bufferG    
= m_buffer.getGraphics();
            m_buffer2    
= Image.createImage(getWidth(), getHeight());
            m_bufferG2    
= m_buffer2.getGraphics();
            bufferHasData 
= false;
        }

                                
        
if (!bufferHasData) {

            m_bufferG.setColor(
0);
            m_bufferG.fillRect(
00, getWidth(), getHeight());
            
            
for (int i = 0; i < m_scrRow; i++{
                
for (int j = 0; j < m_scrCol; j++{
                    
if ((j < m_mapCol) && (i < m_mapRow)) {
                        
short index = m_mapBuf[i + m_curScrRowInMap][j + m_curScrColInMap];
                        
                        
short imageX = (short)(m_imageBuf[index & IND_TILE].x);
                        
short imageY = (short)(m_imageBuf[index & IND_TILE].y);
                                                                    
                        
if ((index & IND_TILE) <  m_imageBuf.length &&
                            (imageX 
>= 0&& (imageX < m_backImage.getWidth()) &&
                            (imageY 
>= 0&& (imageY < m_backImage.getHeight())) {
//                            System.out.println("he1");
                            m_bufferG.drawRegion(m_backImage,
                                (
int)imageX, 
                                (
int)imageY, 
                                m_tileWidth,
                                m_tileHeight,
                                (index 
< 0? Sprite.TRANS_MIRROR_ROT180:    // vertical flip.
                                    ((0 != (index & HORZ_FLIP))    ? Sprite.TRANS_MIRROR :    // horizontally
                                        Sprite.TRANS_NONE),
                                j 
* m_tileWidth,
                                i 
* m_tileHeight,
                                Graphics.TOP 
| Graphics.LEFT);
//                            System.out.println("he2");
                        }

                    }

                }

            }

        }
 else {
            
// move in horizontal.
            if ((m_curXOffset != m_oldXOffset) || (m_curScrColInMap != m_oldScrColInMap)) {
                
int chgCol = m_curScrColInMap - m_oldScrColInMap;//移动的列数

                System.out.println(
"-----------------------------");
                System.out.println(
"m_curScrColInMap = " + m_curScrColInMap);
                System.out.println(
"m_oldScrColInMap = " + m_oldScrColInMap);
                System.out.println(
"m_curXOffset = " + m_curXOffset);
                System.out.println(
"m_oldXOffset = " + m_oldXOffset);
//                    if (1 == m_curScrColInMap && 1 == m_oldScrColInMap && 4 == m_curXOffset && 0 == m_oldXOffset)
//                        m_curXOffset = 8;
                
                
// chgXOffset is  [-8, 8].
                int chgXOffset = chgCol * m_tileWidth +    (m_curXOffset - m_oldXOffset);//移动的像素数
                int chgPartColInMapX = (chgXOffset > 0? 
                        (m_oldScrColInMap 
+ m_scrCol) : m_curScrColInMap;
                
                System.out.println(
"chgXOffset = " + chgXOffset);
                System.out.println(
"chgPartColInMapX = " + chgPartColInMapX);
                System.out.println(
"-----------------------------");
                    
                m_bufferG2.setColor(
0);
                m_bufferG2.fillRect(
00, getWidth(), getHeight());
                            
                
                Random random 
= new Random();
                m_bufferG.setColor(random.nextInt());
                
if (chgXOffset > 0{
                    m_bufferG.copyArea(chgXOffset, 
0, getWidth() - chgXOffset, getHeight(), 
                            
00, Graphics.TOP | Graphics.LEFT);
                                            
                    
// Only for Debug.
                    
//m_bufferG.fillRect(m_scrCol * m_tileWidth - chgXOffset, 0, chgXOffset,
                    
//    m_mapRow * m_tileHeight);
                    
                    
int minRow = Math.min(m_scrRow, m_mapRow);
                    
// compute the horizontal col index.
                    for (int i = 0; i < minRow; i++{
                        
short index = m_mapBuf[i + m_curScrRowInMap][chgPartColInMapX];
                        
short imageX = (short)m_imageBuf[index & IND_TILE].x;
                        
short imageY = (short)m_imageBuf[index & IND_TILE].y;
                        
                        
// draw changed part to buffer2.
                        if ((index & IND_TILE) <  m_imageBuf.length &&
                            (imageX 
>= 0&& (imageX < m_backImage.getWidth()) &&
                            (imageY 
>= 0&& (imageY < m_backImage.getHeight())) {
                            m_bufferG2.drawRegion(m_backImage,
                                (
int)imageX, 
                                (
int)imageY, 
                                m_tileWidth,
                                m_tileHeight,
                                (index 
< 0? Sprite.TRANS_MIRROR_ROT180:    // vertical flip.
                                    ((0 != (index & HORZ_FLIP))    ? Sprite.TRANS_MIRROR :    // horizontally
                                        Sprite.TRANS_NONE),
                                
0,    // put in the first col.
                                i * m_tileHeight,
                                Graphics.TOP 
| Graphics.LEFT);
                        }
                            
                    }

                    
                    
// copy changed part to show buffer.
                    System.out.println("m_curXTileIndex = " + m_curXTileIndex);
                    m_bufferG.drawRegion(m_buffer2, 
                            (m_curXTileIndex 
- 1* chgXOffset,
                            
0,
                            chgXOffset,
                            getHeight(),
                            Sprite.TRANS_NONE,
                            m_scrCol 
* m_tileWidth - chgXOffset,
                            
0,
                            Graphics.TOP 
| Graphics.LEFT);
                }

                
else {
                    
// move to left.
                    m_bufferG.copyArea(00, getWidth() + chgXOffset, getHeight(), 
                            
-chgXOffset, 0, Graphics.TOP | Graphics.LEFT);//为什么是-chgXOffset?
                    
                    
// Only for Debug.
                    
//m_bufferG.fillRect(0, 0, -chgXOffset, m_mapRow * m_tileHeight);

                    System.out.println(
"Left: {row, col} = {" + 
                            m_curScrRowInMap 
+ "" + chgPartColInMapX + "}");
                    
                    
int minRow = Math.min(m_scrRow, m_mapRow);
                    
// compute the horizontal col index.
                    for (int i = 0; i < minRow; i++{
                        
short index = m_mapBuf[i + m_curScrRowInMap][chgPartColInMapX];
                        
short imageX = (short)m_imageBuf[index & IND_TILE].x;
                        
short imageY = (short)m_imageBuf[index & IND_TILE].y;
                        
                        
// draw changed part to buffer2.
                        if ((index & IND_TILE) <  m_imageBuf.length &&
                            (imageX 
>= 0&& (imageX < m_backImage.getWidth()) &&
                            (imageY 
>= 0&& (imageY < m_backImage.getHeight())) {
                            
// move to left.
                            m_bufferG2.drawRegion(m_backImage,
                                (
int)imageX, 
                                (
int)imageY, 
                                m_tileWidth,
                                 m_tileHeight,
                                (index 
< 0? Sprite.TRANS_MIRROR_ROT180:    // vertical flip.
                                    ((0 != (index & HORZ_FLIP))    ? Sprite.TRANS_MIRROR :    // horizontally
                                        Sprite.TRANS_NONE),
                                
0,    // put in the first col.
                                i * m_tileHeight,
                                Graphics.TOP 
| Graphics.LEFT);
                        }
                            
                    }

                    
                    
// move to left.
                    
// copy changed part to show buffer.
                    System.out.println("m_curXTileIndex = " + m_curXTileIndex);
                    m_bufferG.drawRegion(m_buffer2, 
                            m_curXTileIndex 
* -chgXOffset,
                            
0,
                            
-chgXOffset,
                            getHeight(),
                            Sprite.TRANS_NONE,
                            
0,
                            
0,
                            Graphics.TOP 
| Graphics.LEFT);
                }


                System.out.println(
"***** Key have be treated! *****");
                m_oldXOffset 
= m_curXOffset;
                m_oldScrColInMap 
= m_curScrColInMap;
            }
// move in vertical.
            else if ((m_curYOffset != m_oldYOffset) || (m_curScrRowInMap != m_oldScrRowInMap)) {
                
int chgRow = m_curScrRowInMap - m_oldScrRowInMap;

                System.out.println(
"-----------------------------");
//                    System.out.println("m_curScrRowInMap = " + m_curScrRowInMap);
//                    System.out.println("m_oldScrRowInMap = " + m_oldScrRowInMap);
//                    System.out.println("m_curYOffset = " + m_curYOffset);
//                    System.out.println("m_oldYOffset = " + m_oldYOffset);
//                    if (1 == m_curScrColInMap && 1 == m_oldScrColInMap && 4 == m_curXOffset && 0 == m_oldXOffset)
//                        m_curXOffset = 8;
                
                
// chgYOffset is  [-8, 8].
                int chgYOffset = chgRow * m_tileHeight + (m_curYOffset - m_oldYOffset);
                
int chgPartRowInMapX = (chgYOffset > 0? 
                        (m_oldScrRowInMap 
+ m_scrRow) : m_curScrRowInMap;
                
                System.out.println(
"chgYOffset = " + chgYOffset);
                System.out.println(
"chgPartRowInMapX = " + chgPartRowInMapX);
                System.out.println(
"{row, col} = {" + m_curScrRowInMap + "" + m_curScrColInMap + "}");
                System.out.println(
"-----------------------------");
                
                m_bufferG2.setColor(
0);
                m_bufferG2.fillRect(
00, getWidth(), getHeight());                                
                
                Random random 
= new Random();
                m_bufferG.setColor(random.nextInt());
                
if (chgYOffset > 0{
                    m_bufferG.copyArea(
0, chgYOffset, getWidth(), getHeight() - chgYOffset, 
                            
00, Graphics.TOP | Graphics.LEFT);
                    
                    
// Only for Debug.
                    
//m_bufferG.fillRect(0, getHeight() - chgYOffset,    getWidth(), chgYOffset);
                    
                    
int minCol = Math.min(m_scrCol, m_mapCol);
                    
// compute the horizontal col index.
                    for (int i = 0; i < minCol; i++{
                        
short index = m_mapBuf[chgPartRowInMapX][i + m_curScrColInMap];
                        
short imageX = (short)m_imageBuf[index & IND_TILE].x;
                        
short imageY = (short)m_imageBuf[index & IND_TILE].y;
                        
                        
// draw changed part to buffer2.
                        if ((index & IND_TILE) <  m_imageBuf.length &&
                            (imageX 
>= 0&& (imageX < m_backImage.getWidth()) &&
                            (imageY 
>= 0&& (imageY < m_backImage.getHeight())) {
                            m_bufferG2.drawRegion(m_backImage,
                                (
int)imageX, 
                                (
int)imageY, 
                                m_tileWidth,
                                m_tileHeight,
                                (index 
< 0? Sprite.TRANS_MIRROR_ROT180:    // vertical flip.
                                    ((0 != (index & HORZ_FLIP))    ? Sprite.TRANS_MIRROR :    // horizontally
                                        Sprite.TRANS_NONE),
                                i 
* m_tileWidth,
                                
0// put in the first row.
                                Graphics.TOP | Graphics.LEFT);
                        }
                            
                    }

                    
                    
// copy changed part to show buffer.
                    System.out.println("m_curYTileIndex = " + m_curYTileIndex);
                    m_bufferG.drawRegion(m_buffer2, 
                            
0,
                            (m_curYTileIndex 
- 1* chgYOffset,
                            getWidth(),
                            chgYOffset,
                            Sprite.TRANS_NONE,
                            
0,
                            getHeight() 
- chgYOffset,
                            Graphics.TOP 
| Graphics.LEFT);
                }

                
else {
                    
// move to up.
                    m_bufferG.copyArea(00, getWidth(), getHeight() + chgYOffset, 
                             
0-chgYOffset, Graphics.TOP | Graphics.LEFT);
                    
                    
// Only for Debug.
                    
//m_bufferG.fillRect(0, 0, m_mapCol * m_tileWidth, -chgYOffset);
                    
                    
int minCol = Math.min(m_scrCol, m_mapCol);
                    
// compute the horizontal col index.
                    for (int i = 0; i < minCol; i++{
                        
short index = m_mapBuf[chgPartRowInMapX][i + m_curScrColInMap];
                        
short imageX = (short)m_imageBuf[index & IND_TILE].x;
                        
short imageY = (short)m_imageBuf[index & IND_TILE].y;
                        
                        
// draw changed part to buffer2.
                        if ((index & IND_TILE) <  m_imageBuf.length &&
                            (imageX 
>= 0&& (imageX < m_backImage.getWidth()) &&
                            (imageY 
>= 0&& (imageY < m_backImage.getHeight())) {
                            m_bufferG2.drawRegion(m_backImage,
                                (
int)imageX, 
                                (
int)imageY, 
                                m_tileWidth,
                                m_tileHeight,
                                (index 
< 0? Sprite.TRANS_MIRROR_ROT180:    // vertical flip.
                                    ((0 != (index & HORZ_FLIP))    ? Sprite.TRANS_MIRROR :    // horizontally
                                        Sprite.TRANS_NONE),
                                i 
* m_tileWidth,
                                
0,    // put in the first row.
                                Graphics.TOP | Graphics.LEFT);
                        }
                            
                    }

                    
                    
// copy changed part to show buffer.
                    System.out.println("m_curYTileIndex = " + m_curYTileIndex);
                    m_bufferG.drawRegion(m_buffer2, 
                            
0,
                            m_curYTileIndex 
* -chgYOffset,
                            getWidth(),
                            
-chgYOffset,
                            Sprite.TRANS_NONE,
                            
0,
                            
0,
                            Graphics.TOP 
| Graphics.LEFT);
                }


                System.out.println(
"***** Key have be treated! *****");
                m_oldYOffset 
= m_curYOffset;
                m_oldScrRowInMap 
= m_curScrRowInMap;
            }

        }


        
//System.out.println("OK, flush on frame");
        g.drawImage(m_buffer/*m_tempImage*/00, Graphics.TOP | Graphics.LEFT);
    }

    
    
private short ConvBigEndToLitEnd (short value) {
//        short temp;
//        temp = (short)(value<<8);
//        return (short)(value>>>8);
        return (short)((value << 8| (value >> 8));
    }

    
    
protected  void keyReleased(int keyCode) 
    
{
        System.out.println(
"Key release");
        m_keyDown 
= false;
        m_lastKeyCode 
= 0;
    }

    
    
protected void keyPressed (int keyCode)
    
{
        m_keyDown 
= true;
        m_lastKeyCode 
= keyCode;
//        System.out.println("===== Key must be treated! =====");
        
        
if (!m_selectStep) {
            
switch (keyCode) {
            
case Canvas.KEY_NUM1:
                    m_selectStep 
= true;
                    m_nSteps 
= 1;
                
break;
                
            
case Canvas.KEY_NUM2:
                    m_selectStep 
= true;
                    m_nSteps 
= 2;
                
break;
                
            
case Canvas.KEY_NUM3:
                    m_selectStep 
= true;
                    m_nSteps 
= 4;
                
break;
                
            
case Canvas.KEY_NUM4:
                    m_selectStep 
= true;
                    m_nSteps 
= 8;
                
break;
                    
            
default:
            }

            
            
if (m_selectStep) {
                m_tileIndexs 
= (short)(m_tileWidth / m_nSteps);
            }

        }
else {
            
switch (keyCode) {
            
case Canvas.KEY_NUM4:
                
// Sometimes, m_nXSteps < m_nSteps but > 0, should set m_nXSteps = 0;
                if ((m_nXSteps >= m_nSteps) &&
                    (m_oldScrColInMap 
== m_curScrColInMap) &&
                    (m_oldXOffset 
== m_curXOffset)) {
                    m_nXSteps 
-= m_nSteps;
                    m_curScrColInMap 
= (short)(m_nXSteps / m_tileWidth);
                    m_curXOffset 
= (short)(m_nXSteps % m_tileWidth);
                    
                    m_curXTileIndex
--;
                    
if (m_curXTileIndex < 0)
                        m_curXTileIndex 
= (short)(m_tileIndexs - 1);
                    
                    System.out.println(
"m_curScrColInMap = " + m_curScrColInMap);
                    System.out.println(
"m_curXOffset = " + m_curXOffset);
                    System.out.println(
"m_tileIndexs = " + m_tileIndexs);
                    System.out.println(
"m_curXTileIndex = " + m_curXTileIndex);
                    System.out.println (
"press 4");
                }

                
break;
                
            
case Canvas.KEY_NUM6:
                
// can't exceed the map.dat scope.
                
// m_nXSteps will add accumulate.                
                if ((m_nXSteps + m_nSteps + getWidth() < m_mapCol * m_tileHeight) &&
                    (m_oldScrColInMap 
== m_curScrColInMap) &&
                    (m_oldXOffset 
== m_curXOffset)) {
                    m_nXSteps 
+= m_nSteps;
                    m_curScrColInMap 
= (short)(m_nXSteps / m_tileWidth);
                    m_curXOffset 
= (short)(m_nXSteps % m_tileWidth);
                    
                    m_curXTileIndex
++;
                    
if (m_curXTileIndex > m_tileIndexs || m_curXTileIndex < 1)
                        m_curXTileIndex 
= 1;
                                                
                    System.out.println(
"m_curScrColInMap = " + m_curScrColInMap);
                    System.out.println(
"m_curXOffset = " + m_curXOffset);
                    System.out.println(
"m_tileIndexs = " + m_tileIndexs);
                    System.out.println(
"m_curXTileIndex = " + m_curXTileIndex);
                    System.out.println (
"press 6");
                }

                
break;
                
            
case Canvas.KEY_NUM2:
                
// Sometimes, m_nXSteps < m_nSteps but > 0, should set m_nXSteps = 0;
                if ((m_nYSteps >= m_nSteps) &&
                    (m_oldScrRowInMap 
== m_curScrRowInMap) &&
                    (m_oldYOffset 
== m_curYOffset)) {
                    m_nYSteps 
-= m_nSteps;
                    m_curScrRowInMap 
= (short)(m_nYSteps / m_tileWidth);
                    m_curYOffset 
= (short)(m_nYSteps % m_tileWidth);
                    
                    m_curYTileIndex
--;
                    
if (m_curYTileIndex < 0)
                        m_curYTileIndex 
= (short)(m_tileIndexs - 1);

                    System.out.println(
"m_curScrRowInMap = " + m_curScrRowInMap);
                    System.out.println(
"m_curYOffset = " + m_curYOffset);
                    System.out.println(
"m_tileIndexs = " + m_tileIndexs);
                    System.out.println(
"m_curYTileIndex = " + m_curYTileIndex);
                    System.out.println (
"press 2");
                }

                
break;
                
            
case Canvas.KEY_NUM8:
                    
// can't exceed the map.dat scope.
                    if ((m_nYSteps + m_nSteps + getHeight() < m_mapRow * m_tileHeight) &&
                        (m_oldScrRowInMap 
== m_curScrRowInMap) &&
                        (m_oldYOffset 
== m_curYOffset)) {
                        m_nYSteps 
+= m_nSteps;
                        m_curScrRowInMap 
= (short)(m_nYSteps / m_tileHeight);
                        m_curYOffset 
= (short)(m_nYSteps % m_tileHeight);
                        
                        m_curYTileIndex
++;
                        
if (m_curYTileIndex > m_tileIndexs || m_curYTileIndex < 1)
                            m_curYTileIndex 
= 1;
                        
                        System.out.println(
"m_curScrRowInMap = " + m_curScrRowInMap);
                        System.out.println(
"m_curYOffset = " + m_curYOffset);
                        System.out.println(
"m_tileIndexs = " + m_tileIndexs);
                        System.out.println(
"m_curYTileIndex = " + m_curYTileIndex);
                        System.out.println (
"press 8");
                    }

                    
break;
                    
            
case Canvas.KEY_NUM9:
                m_ranMode 
= !m_ranMode;
                m_lastKeyCode 
= 0;
                System.out.println(
"Random mode = " + m_ranMode);
                
break;
                
            
default:                
            }

        }

    }

//    private int ConvBigEndToLitEnd (int value) {
//        return ((value << 24) | ((value & 0xff00)) << 16) | ((value & 0xff0000) << 8) | (value >> 24);
//    }
}

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值