/** * Simple Carmark Test */ public class Test { public static void main(String[] args){ int[] data = { 1, 2, 3, 4, 5, 6, 7, 11,12,13,14,15,16,17, 21,22,23,24,25,26, 27, 31,32,33,34,35,36,37, 41,42,43,44,45,46,47, 51,52,53,54,55,56,57, }; int dataColumns = 7, dataRows = 6; CarmarkBuffer CarmarkBuffer = new CarmarkBuffer(4, 4, 1); CarmarkBuffer.setData(data, dataColumns, dataRows, 2, 2); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_RIGHT); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_DOWN); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_DOWN); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_LEFT); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_UP); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_RIGHT); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_RIGHT); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_DOWN); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_DOWN); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_LEFT); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_LEFT); CarmarkBuffer.scroll(CarmarkBuffer.SCROLL_UP); } }
class CarmarkBuffer { final static int SCROLL_LEFT = 1 << 0; final static int SCROLL_UP = 1 << 1; final static int SCROLL_DOWN = 1 << 2; final static int SCROLL_RIGHT = 1 << 3; int _cellSize; int _bufColumns; // Carmark buffer size
int _bufRows; // Carmark buffer size
int _dataColumns;// Map data columns
int _dataRows; // Map data rows
int _rowsToBuffer, _columnsToBuffer; int _bufRow; // Carmark buf row, suppose it to be the cross.
int _bufColumn; // Carmark buf column, suppose it to be the cross.
int _dataRow; // Map data update begin row.
int _dataColumn; // Map data update begin column.
int _column; // Carmark buffer column, offset from src map data.
int _row; // Carmark buffer row, offset from src map data.
int[] _buf; // Carmark buffer
int[] _data; // Src data, suppose it to be the src map data.
/** * Set map data */ public void setData(int[] data, int dataColumns, int dataRows, int startColumn, int startRow){ _column = startColumn; _row = startRow; _data = data; _bufRow = 0; _bufColumn = 0; _rowsToBuffer = _bufRows; _columnsToBuffer = _bufColumns; _dataColumns = dataColumns; _dataRows = dataRows; _dataRow = startColumn; _dataColumn = startRow; updateBuffer(); } public CarmarkBuffer(int columns, int rows, int cellSize){ _cellSize = cellSize; _bufColumns = columns; _bufRows = rows; _buf = new int[_bufRows * _bufColumns]; } boolean checkScroll(int dir, int scrollStep){ switch (dir) { case SCROLL_RIGHT: return _column + _bufColumns <= _dataColumns-scrollStep; case SCROLL_DOWN: return _row + _bufRows <= _dataRows-scrollStep; case SCROLL_UP: return _row >= scrollStep; case SCROLL_LEFT: return _column >= scrollStep; default: return false; } } public boolean scroll(int dir){ return scroll(dir, 1); } /** * Scroll the buffer window. */ public boolean scroll(int dir, int scrollStep){ if (!checkScroll(dir, scrollStep)) return false; int columnInc = 0, rowInc = 0; switch (dir){ case SCROLL_RIGHT: _columnsToBuffer = scrollStep; _rowsToBuffer = _bufRows; _dataColumn = _column + _bufColumns; _dataRow = _row; columnInc = scrollStep; break; case SCROLL_DOWN: _columnsToBuffer = _bufColumns; _rowsToBuffer = scrollStep; _dataColumn = _column; _dataRow = _row + _bufRows; rowInc = scrollStep; break; case SCROLL_UP: _columnsToBuffer = _bufColumns; _rowsToBuffer = scrollStep; _dataColumn = _column; _dataRow = _row - scrollStep; rowInc = -scrollStep; break; case SCROLL_LEFT: _columnsToBuffer = scrollStep; _rowsToBuffer = _bufRows; _dataColumn = _column - scrollStep; _dataRow = _row; columnInc = -scrollStep; break; default: break; } _column += columnInc; _row += rowInc; // On scolling forward, we update buffer before adjusting buf column and row
// Otherwise we adjust buf column and row first
if (dir == SCROLL_LEFT || dir == SCROLL_UP){ adjustBufColumnRow(rowInc, columnInc, scrollStep); updateBuffer(); } else { updateBuffer(); adjustBufColumnRow(rowInc, columnInc, scrollStep); } dump(); dumpWindow(); return true; } void adjustBufColumnRow(int rowInc, int columnInc, int scrollStep){ _bufColumn += columnInc; _bufRow += rowInc;
if (_bufColumn < 0) _bufColumn = _bufColumns-scrollStep; else if (_bufColumn >= _bufColumns) _bufColumn = 0; if (_bufRow < 0) _bufRow = _bufRows-scrollStep; else if (_bufRow >= _bufRows) _bufRow = 0; } /** * Update the carmark buffer, called after buffer window scrolled. */ void updateBuffer(){ int endRow = _row + _bufRows; int endColumn = _column + _bufColumns; int dataLineIndex = _dataRow * _dataColumns; int bufLineIndex = _bufRow * _bufColumns; int bufColumn; for (int row = 0; row < _rowsToBuffer; row++) { if (row + _bufRow >= _bufRows) bufLineIndex = 0; for (int column = 0; column < _columnsToBuffer; column++){ bufColumn = _bufColumn + column; if (bufColumn >= _bufColumns) bufColumn -= _bufColumns; setCell(bufLineIndex + bufColumn, (dataLineIndex + _dataColumn) + column); } dataLineIndex += _dataColumns; bufLineIndex += _bufColumns; } } /** * Copy map data to buffer */ void setCell(int bufIndex, int dataIndex){ //~ System.out.println("setCell: bufIndex=" + bufIndex + ", dataIndex=" + dataIndex);
_buf[bufIndex] = _data[dataIndex]; } /** * Display area. Usually smaller than buffer. */ void dumpWindow(){ System.out.println("=========== dump window ============"); int bufLineIndex = _bufRow * _bufColumns; int bufColumn;
for (int row = 0; row < _bufRows; row++) { if (row + _bufRow >= _bufRows) bufLineIndex = 0;
for (int column = 0; column < _bufColumns; column++) { bufColumn = _bufColumn + column; if (bufColumn >= _bufColumns) bufColumn -= _bufColumns; System.out.print(", " + _buf[bufLineIndex + bufColumn]); } System.out.println(); bufLineIndex += _bufColumns; } } void dump(){ System.out.println("========== dump carmark buf =========="); System.out.println("bufColumn=" + _bufColumn + ", bufRow=" + _bufRow); System.out.println("column=" + _column + ", row=" + _row); System.out.println("******** buf data ********"); int bufLineIndex = 0; for (int row = 0; row < _bufRows; row++) { for (int column = 0; column < _bufColumns; column++) System.out.print(", " + _buf[bufLineIndex + column]); System.out.println(); bufLineIndex += _bufColumns; } } }
|