package xtool;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* RPG ACT 等地图场景 缓存类
* @author 高雷 by 20090616
*/
public class MapBuffer
{
private final int screen_w; //屏幕的宽度
private final int screen_h; //屏幕的高度
private final int tile_w; //地图瓦片的宽
private final int tile_h; //地图瓦片的高
private final int screen_tile_columns; //屏幕上瓦片的列数(一行上的TILE个数)
private final int screen_tile_rows; //屏幕上瓦片的行数(一列上的TILE个数)
private final int buffer_tile_columns; //缓冲区瓦片的列数(一行上的TILE个数)
private final int buffer_tile_rows; //缓冲区瓦片的行数(一列上的TILE个数)
private final int buffer_image_width; //缓冲区图片的宽度
private final int buffer_image_height; //缓冲区图片的高度
private final int map_width_pix; //地图大小pix需要给出
private final int map_height_pix; //地图大小pix需要给出
private final int map_width_tile; //地图大小TILE数量需要给出
private final int map_height_tile; //地图大小TILE数量需要给出
private final int s_t_x = 0; //保留该参数不知道干啥的screen_trans_x
private final int s_t_y = 0; //保留该参数不知道干啥的screen_trans_y
private int tile_x; //瓦片的屏幕坐标x
private int tile_y; //瓦片的屏幕坐标x
private int screen_x = 0; //屏幕左上角 在地图上的左边 偏移(需要给出因为地图初始位置不一定在哪)
private int screen_y = 0; //屏幕左上角 在地图上的左边 偏移
private byte[][] map_data; //地图数据//需要外包传入
private Image map_img; //地图数据//需要外包传入
private Image buffer_img = null; //缓存区图片
private Graphics buffer_g = null; //缓存区绘制设备
private boolean first = true;
public MapBuffer()
{
this( 240, 320, 32, 32, 300, 500,0,0 );
}
/**
* 构造方法
* @param screen_width
* @param screen_height
* @param _tile_w
* @param _tile_h
*/
public MapBuffer( int screen_width, int screen_height, int _tile_w, int _tile_h, int _map_width, int _map_height,int _screen_x, int _screen_y )
{
if( screen_width < 1 || screen_height < 1 || _tile_w < 1 || _tile_h < 1 || _map_width<screen_width || _map_height<screen_height )
throw new IllegalArgumentException();
screen_w = screen_width;
screen_h = screen_height;
tile_w = _tile_w;
tile_h = _tile_h;
map_width_pix = _map_width;
map_height_pix = _map_height;
map_width_tile = getInt4Float(_map_width, _tile_w);
map_height_tile = getInt4Float(_map_height,_tile_h);
setMapOff( _screen_x, _screen_y );
//屏幕上瓦片的列数(一行上的TILE个数)
screen_tile_columns = getInt4Float( screen_w, tile_w );
//屏幕上瓦片的行数(一列上的TILE个数)
screen_tile_rows = getInt4Float( screen_h, tile_h );
//缓冲区瓦片的列数(一行上的TILE个数)
buffer_tile_columns = screen_tile_columns+1;
//缓冲区瓦片的行数(一列上的TILE个数)
buffer_tile_rows = screen_tile_rows+1;
buffer_image_width = buffer_tile_columns * tile_w; // 缓冲区图片的宽度
buffer_image_height= buffer_tile_rows * tile_h; // 缓冲区图片的高度
buffer_img = Image.createImage(buffer_image_width,buffer_image_height);
buffer_g = buffer_img.getGraphics();
first = true;
}
/**
* 得到一个有小数就进位的整数,如(5、3)就是 5/3 返回2
* @param numerator 分子
* @param denominator 分母
* @return 如果有小数 就要进1
*/
private int getInt4Float(int numerator, int denominator)
{
if( numerator < 1 || denominator < 1 )
throw new IllegalArgumentException();
return numerator/denominator+(numerator%denominator==0?0:1);//防止小数
}
/**
* 设置地图的数据和图片
*/
public void setMap(byte[][] mapData, Image mapImg)
{
map_data = mapData; //地图数据//需要外包传入
map_img = mapImg; //地图图片//需要外包传入
}
/**
* 设置屏幕在地图上的偏移位置
* @param _screen_x
* @param _screen_y
*/
private void setMapOff(int _screen_x,int _screen_y)
{
screen_x = _screen_x;
screen_y = _screen_y;
checkScreenOff();
tile_x = screen_x/tile_w;
tile_y = screen_y/tile_h;
}
/**
* 卷动绘制对象,根据需要进行缓冲//用于测试
* @param dx x方向移动距离
* @param dy y方向移动距离
*/
public void scroll( int dx, int dy )
{
screen_x += dx;
screen_y += dy;
checkScreenOff();
}
/**
* 检测屏幕是否完全在地图上
*/
private void checkScreenOff()
{
if( screen_x < 0 )
{
screen_x = 0;
}
else if( screen_x > map_width_pix - screen_w )
{
screen_x = map_width_pix - screen_w;
}
if( screen_y < 0 )
{
screen_y = 0;
}
else if( screen_y > map_height_pix - screen_h )
{
screen_y = map_height_pix - screen_h;
}
}
/**
* 将此缓冲绘制到g
* @param x 屏幕做上角位于地图上的坐标偏移
* @param y 屏幕做上角位于地图上的坐标偏移
*/
public final void paint( Graphics g, int x, int y )
{
screen_x = x;
screen_y = y;
checkScreenOff();
int now_tile_x = screen_x / tile_w;
int now_tile_y = screen_y / tile_h;
int start_x = now_tile_x;
int start_y = now_tile_y;
int end_x = start_x + screen_tile_columns;
int end_y = start_y + screen_tile_rows;
if( first )
{
first = false;
draw_buffer( buffer_g, start_x, start_y, end_x, end_y );
tile_x = now_tile_x;
tile_y = now_tile_y;
}
if( now_tile_x != tile_x )
{
int start_buffer_x;
int end_buffer_x;
if( now_tile_x > tile_x )
{
start_buffer_x = tile_x + buffer_tile_columns;
end_buffer_x = now_tile_x + screen_tile_columns;
}
else
{
start_buffer_x = now_tile_x;
end_buffer_x = tile_x - 1;
}
draw_buffer( buffer_g, start_buffer_x, start_y, end_buffer_x, end_y );
tile_x = now_tile_x;
}
if( now_tile_y != tile_y )
{
int start_buffer_y;
int end_buffer_y;
if( now_tile_y > tile_y )
{
start_buffer_y = tile_y + buffer_tile_rows;
end_buffer_y = now_tile_y + screen_tile_rows;
}
else
{
start_buffer_y = now_tile_y;
end_buffer_y = tile_y - 1;
}
draw_buffer( buffer_g, start_x, start_buffer_y, end_x, end_buffer_y );
tile_y = now_tile_y;
}
int start_draw_x = ( screen_x ) % buffer_image_width;
int end_draw_x = ( screen_x + screen_w ) % buffer_image_width;
int start_draw_y = ( screen_y ) % buffer_image_height;
int end_draw_y = ( screen_y + screen_h ) % buffer_image_height;
if( end_draw_x > start_draw_x )
{
if( end_draw_y > start_draw_y )
{
draw_part_buffer( g, start_draw_x, start_draw_y, screen_w, screen_h, s_t_x, s_t_y );
}
else
{
draw_part_buffer( g, start_draw_x, start_draw_y, screen_w, screen_h - end_draw_y, s_t_x,
s_t_y );
draw_part_buffer( g, start_draw_x, 0, screen_w, end_draw_y, s_t_x, s_t_y
+ screen_h - end_draw_y );
}
}
else if( end_draw_y > start_draw_y )
{
draw_part_buffer( g, start_draw_x, start_draw_y, screen_w - end_draw_x, screen_h, s_t_x,
s_t_y );
draw_part_buffer( g, 0, start_draw_y, end_draw_x, screen_h, s_t_x + screen_w - end_draw_x,
s_t_y );
}
else
{
draw_part_buffer( g, start_draw_x, start_draw_y, screen_w - end_draw_x, screen_h - end_draw_y,
s_t_x, s_t_y );
draw_part_buffer( g, start_draw_x, 0, screen_w - end_draw_x, end_draw_y, s_t_x, s_t_y
+ screen_h - end_draw_y );
draw_part_buffer( g, 0, start_draw_y, end_draw_x, screen_h - end_draw_y, s_t_x + screen_w
- end_draw_x, s_t_y );
draw_part_buffer( g, 0, 0, end_draw_x, end_draw_y, s_t_x + screen_w - end_draw_x,
s_t_y + screen_h - end_draw_y );
}
}
/**
* 绘制 缓存区的一部分
*/
private void draw_part_buffer( Graphics g, int srcX, int srcY, int width, int height, int desX, int desY )
{
g.setClip( desX, desY, width, height );
g.drawImage( buffer_img, desX - srcX, desY - srcY, Graphics.LEFT|Graphics.TOP );
}
/**
* 绘制 缓存区
*/
private void draw_buffer( Graphics g, int start_x, int
J2ME开发RPG,ACT游戏的地图缓存类,卡马克卷轴算法封装类
3星 · 超过75%的资源 需积分: 10 121 浏览量
2009-07-30
16:23:32
上传
评论 2
收藏 3KB RAR 举报
kome2000
- 粉丝: 314
- 资源: 41
最新资源
- [检测统计]北雨访问记数器系统_bycount.rar
- HonoCMS 内容发布系统 v1.0_honocms_cn_CMS程序开发模板(使用说明+源代码+html).zip
- 手机在线点餐模板.zip
- [搜索链接]Page Rank查询_pagerank.rar
- matlab imu和视觉里程计 kalman滤波器 进行融合.rar
- matlab 该模拟器由构成IEEE802.11a协议物理层的多个部分集成
- mmexport1713552623466.jpg
- matlab Physical layer of the IEEE 802.11ac simulator.zip
- 仿药房网手机wap药店网站模板_网站开发模板含源代码(css+html+js+图样).zip
- 超声波测距实验_单片机C语言实例(纯C语言源代码).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈