📄 sprite.java
字号:
// ---
// setTransform sets up all transformation related data structures
// except transforming the current frame's bitmap.
// x, y, width, height, dRefX, dRefY,
// collisionRectX, collisionRectY, collisionRectWidth,
// collisionRectHeight, t_currentTransformation,
// t_bufferImage
// The actual tranformed frame is drawn at paint time.
// ---
// update top-left corner position
this.x = this.x
+ getTransformedPtX(dRefX, dRefY, this.t_currentTransformation)
- getTransformedPtX(dRefX, dRefY, transform);
this.y = this.y
+ getTransformedPtY(dRefX, dRefY, this.t_currentTransformation)
- getTransformedPtY(dRefX, dRefY, transform);
// Calculate transformed sprites collision rectangle
// and transformed width and height
computeTransformedBounds(transform);
// set the current transform to be the one requested
t_currentTransformation = transform;
}
private void computeTransformedBounds(int transform) {
switch (transform) {
case TRANS_NONE:
t_collisionRectX = collisionRectX;
t_collisionRectY = collisionRectY;
t_collisionRectWidth = collisionRectWidth;
t_collisionRectHeight = collisionRectHeight;
this.width = srcFrameWidth;
this.height = srcFrameHeight;
break;
case TRANS_MIRROR:
// flip across vertical
// NOTE: top left x and y coordinate must reflect the transformation
// performed around the reference point
// the X-offset of the reference point from the top left corner
// changes.
t_collisionRectX = srcFrameWidth
- (collisionRectX + collisionRectWidth);
t_collisionRectY = collisionRectY;
t_collisionRectWidth = collisionRectWidth;
t_collisionRectHeight = collisionRectHeight;
// the Y-offset of the reference point from the top left corner
// remains the same,
// top left X-co-ordinate changes
this.width = srcFrameWidth;
this.height = srcFrameHeight;
break;
case TRANS_MIRROR_ROT180:
// flip across horizontal
// NOTE: top left x and y coordinate must reflect the transformation
// performed around the reference point
// the Y-offset of the reference point from the top left corner
// changes
t_collisionRectY = srcFrameHeight
- (collisionRectY + collisionRectHeight);
t_collisionRectX = collisionRectX;
t_collisionRectWidth = collisionRectWidth;
t_collisionRectHeight = collisionRectHeight;
// width and height are as before
this.width = srcFrameWidth;
this.height = srcFrameHeight;
// the X-offset of the reference point from the top left corner
// remains the same.
// top left Y-co-ordinate changes
break;
case TRANS_ROT90:
// NOTE: top left x and y coordinate must reflect the transformation
// performed around the reference point
// the bottom-left corner of the rectangle becomes the
// top-left when rotated 90.
// both X- and Y-offset to the top left corner may change
// update the position information for the collision rectangle
t_collisionRectX = srcFrameHeight
- (collisionRectHeight + collisionRectY);
t_collisionRectY = collisionRectX;
t_collisionRectHeight = collisionRectWidth;
t_collisionRectWidth = collisionRectHeight;
// set width and height
this.width = srcFrameHeight;
this.height = srcFrameWidth;
break;
case TRANS_ROT180:
// NOTE: top left x and y coordinate must reflect the transformation
// performed around the reference point
// width and height are as before
// both X- and Y- offsets from the top left corner may change
t_collisionRectX = srcFrameWidth
- (collisionRectWidth + collisionRectX);
t_collisionRectY = srcFrameHeight
- (collisionRectHeight + collisionRectY);
t_collisionRectWidth = collisionRectWidth;
t_collisionRectHeight = collisionRectHeight;
// set width and height
this.width = srcFrameWidth;
this.height = srcFrameHeight;
break;
case TRANS_ROT270:
// the top-right corner of the rectangle becomes the
// top-left when rotated 270.
// both X- and Y-offset to the top left corner may change
// update the position information for the collision rectangle
t_collisionRectX = collisionRectY;
t_collisionRectY = srcFrameWidth
- (collisionRectWidth + collisionRectX);
t_collisionRectHeight = collisionRectWidth;
t_collisionRectWidth = collisionRectHeight;
// set width and height
this.width = srcFrameHeight;
this.height = srcFrameWidth;
break;
case TRANS_MIRROR_ROT90:
// both X- and Y- offset from the top left corner may change
// update the position information for the collision rectangle
t_collisionRectX = srcFrameHeight
- (collisionRectHeight + collisionRectY);
t_collisionRectY = srcFrameWidth
- (collisionRectWidth + collisionRectX);
t_collisionRectHeight = collisionRectWidth;
t_collisionRectWidth = collisionRectHeight;
// set width and height
this.width = srcFrameHeight;
this.height = srcFrameWidth;
break;
case TRANS_MIRROR_ROT270:
// both X- and Y- offset from the top left corner may change
// update the position information for the collision rectangle
t_collisionRectY = collisionRectX;
t_collisionRectX = collisionRectY;
t_collisionRectHeight = collisionRectWidth;
t_collisionRectWidth = collisionRectHeight;
// set width and height
this.width = srcFrameHeight;
this.height = srcFrameWidth;
break;
default:
// INVALID TRANSFORMATION!
throw new IllegalArgumentException();
}
}
int getTransformedPtX(int x, int y, int transform) {
int t_x = 0;
switch (transform) {
case TRANS_NONE:
t_x = x;
break;
case TRANS_MIRROR:
t_x = srcFrameWidth - x - 1;
break;
case TRANS_MIRROR_ROT180:
t_x = x;
break;
case TRANS_ROT90:
t_x = srcFrameHeight - y - 1;
break;
case TRANS_ROT180:
t_x = srcFrameWidth - x - 1;
break;
case TRANS_ROT270:
t_x = y;
break;
case TRANS_MIRROR_ROT90:
t_x = srcFrameHeight - y - 1;
break;
case TRANS_MIRROR_ROT270:
t_x = y;
break;
default:
// INVALID TRANSFORMATION!
throw new IllegalArgumentException();
}
return t_x;
}
int getTransformedPtY(int x, int y, int transform) {
int t_y = 0;
switch (transform) {
case TRANS_NONE:
t_y = y;
break;
case TRANS_MIRROR:
t_y = y;
break;
case TRANS_MIRROR_ROT180:
t_y = srcFrameHeight - y - 1;
break;
case TRANS_ROT90:
t_y = x;
break;
case TRANS_ROT180:
t_y = srcFrameHeight - y - 1;
break;
case TRANS_ROT270:
t_y = srcFrameWidth - x - 1;
break;
case TRANS_MIRROR_ROT90:
t_y = srcFrameWidth - x - 1;
break;
case TRANS_MIRROR_ROT270:
t_y = x;
break;
default:
// INVALID TRANSFORMATION!
throw new IllegalArgumentException();
}
return t_y;
}
/*
private int getNokiaTransform(int trans){
int retT=0;
switch (trans) {
case TRANS_NONE:
retT=0;
break;
case TRANS_MIRROR_ROT180:
retT=DirectGraphics.FLIP_VERTICAL;
break;
case TRANS_MIRROR:
retT=DirectGraphics.FLIP_HORIZONTAL;
break;
case TRANS_ROT180:
retT=DirectGraphics.ROTATE_180;
break;
case TRANS_ROT90:
retT=DirectGraphics.ROTATE_270;
break;
case TRANS_MIRROR_ROT270:
retT=DirectGraphics.FLIP_HORIZONTAL|DirectGraphics.ROTATE_90;
break;
case TRANS_ROT270:
retT=DirectGraphics.ROTATE_90;
break;
case TRANS_MIRROR_ROT90:
retT=DirectGraphics.FLIP_HORIZONTAL|DirectGraphics.ROTATE_270;
break;
}
return retT;
}
*/
private void drawRegion(Graphics g, Image img, int sx, int sy, int width, int height,
int trans, int dx,int dy, int anchor){
/*int cx=g.getClipX();
int cy=g.getClipY();
int cw=g.getClipWidth();
int ch=g.getClipHeight();
int imgW=img.getWidth();
int imgH=img.getHeight();
switch (trans) {
case TRANS_NONE:
case TRANS_MIRROR_ROT180:
case TRANS_MIRROR:
case TRANS_ROT180:
if(width!=imgW||height!=imgH){
g.clipRect(dx,dy,width,height);
}
break;
case TRANS_ROT90:
case TRANS_MIRROR_ROT270:
case TRANS_ROT270:
case TRANS_MIRROR_ROT90:
if(width!=imgW||height!=imgH){
g.clipRect(dx,dy,height,width);
}
int tp=sx;
sx=sy;
sy=tp;
break;
}
int transform=getNokiaTransform(trans);
DirectGraphics dg=DirectUtils.getDirectGraphics(g);
dg.drawImage(img,dx-sx,dy-sy,Graphics.LEFT|Graphics.TOP,transform);
if(width!=imgW||height!=imgH){
g.setClip(cx,cy,cw,ch);
}*/
g.drawRegion(img,sx,sy,width,height,trans,dx,dy,anchor);
}
private static void getRGBNokia(Image img, int[] rgbData, int offset, int scanlength,int x, int y, int width, int height){
Graphics g=img.getGraphics();
DirectGraphics dg=DirectUtils.getDirectGraphics(g);
int format=dg.getNativePixelFormat();
switch(format){
case DirectGraphics.TYPE_BYTE_1_GRAY:
case DirectGraphics.TYPE_BYTE_1_GRAY_VERTICAL:
case DirectGraphics.TYPE_BYTE_2_GRAY:
case DirectGraphics.TYPE_BYTE_332_RGB:
case DirectGraphics.TYPE_BYTE_4_GRAY:
case DirectGraphics.TYPE_BYTE_8_GRAY:
throw new IllegalArgumentException("Shall we support Gray-Color screen phone?");
case DirectGraphics.TYPE_INT_8888_ARGB:
case DirectGraphics.TYPE_INT_888_RGB:
dg.getPixels(rgbData,offset,scanlength,x,y,width,height,format);
break;
case DirectGraphics.TYPE_USHORT_1555_ARGB:
case DirectGraphics.TYPE_USHORT_4444_ARGB:
case DirectGraphics.TYPE_USHORT_444_RGB:
case DirectGraphics.TYPE_USHORT_555_RGB:
case DirectGraphics.TYPE_USHORT_565_RGB:
int len=rgbData.length;
short[] rgb=new short[len];
dg.getPixels(rgb,offset,scanlength,x,y,width,height,format);
for(int i=0;i<len;++i){
rgbData[i]=rgb[i];
}
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -