卡马克算法-代码研究
2007-04-27 15:06
429 查看
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@ CIRCULAR DIRTY BUFFER CIRCULAR DIRTY BUFFER CIRCULAR DIRTY BUFFER
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
static int _oldLevelX;
static int _oldLevelY;
static int _lastTileX;
static int _lastTileY;
static int _vp_x;
static int _vp_y;
static int _vp_w;
static int _vp_h;
static Image _imgCDB;
static Graphics _gCDB;
static void CDB_Init() ...{
_imgCDB = Image.createImage(DEF.CDB_W, DEF.CDB_H);
_gCDB = _imgCDB.getGraphics();
_lastTileX = -1;
_lastTileY = -1;
_oldLevelX = -1;
_oldLevelY = -1;
_vp_x = DEF.SV_X;
_vp_y = DEF.SV_Y;
_vp_w = DEF.SV_W;
_vp_h = DEF.SV_H;
}
static void CDB_Free() ...{
_gCDB = null;
_imgCDB = null;
}
/**/////////////////////////////////////////////////////////////////////////////////////////////////////
static void CDB_UpdateGameView(int x, int y) ...{
int lx = x >> 4; // x / GAME.G_TILE_W;
int ly = y >> 4; // y / GAME.G_TILE_H;
int olx = _oldLevelX >> 4; // _oldLevelX / GAME.G_TILE_W;
int oly = _oldLevelY >> 4; // _oldLevelY / GAME.G_TILE_H;
if ((lx == _lastTileX) && (ly == _lastTileY)) ...{
_oldLevelX = x;
_oldLevelY = y;
return;
}
if ((_lastTileX == -1 && _lastTileY == -1) ||
(Abs(lx - olx) >= DEF.CDB_TW) || (Abs(ly - oly) >= DEF.CDB_TH)) ...{
CDB_InvalidateTiles(lx, ly, lx + DEF.CDB_TW, ly + DEF.CDB_TH);
} else ...{
int minX, maxX, minY, maxY;
int x1, x2, y1, y2;
minX = ((x >= _oldLevelX) ? olx : lx);
minY = ((y >= _oldLevelY) ? oly : ly);
maxX = lx + olx - minX;
maxY = ly + oly - minY;
x1 = (_oldLevelX < x) ? (minX + DEF.CDB_TW) : minX;
x2 = (x1 - minX + maxX);
if (x1 == x2) ...{
if (x > _oldLevelX) ...{
x1--;
} else if (x < _oldLevelX) ...{
x2++;
}
}
y1 = (_oldLevelY <= y) ? maxY : minY;
y2 = y1 + DEF.CDB_TH;
CDB_InvalidateTiles(x1, y1, x2, y2);
x1 = maxX;
x2 = minX + DEF.CDB_TW;
y1 = (_oldLevelY < y) ? (minY + DEF.CDB_TH) : minY;
y2 = (_oldLevelY < y) ? (maxY + DEF.CDB_TH) : maxY;
if (y1 == y2) ...{
if (y > _oldLevelY) ...{
y1--;
} else if (y < _oldLevelY) ...{
y2++;
}
}
CDB_InvalidateTiles(x1, y1, x2, y2);
}
// record level coords (for not redrawin every time ...)
_lastTileX = lx;
_lastTileY = ly;
_oldLevelX = x;
_oldLevelY = y;
}
/**/////////////////////////////////////////////////////////////////////////////////////////////////////
// Repaint a portion of game background into the circular dirty buffer
static void CDB_InvalidateTiles(int minX, int minY, int maxX, int maxY) ...{
int x, y;
if (maxX > map_sizeX)
maxX = map_sizeX;
if (maxY > map_sizeY)
maxY = map_sizeY;
if (minX == maxX || minY == maxY)
return;
_gCDB.setClip(0, 0, DEF.CDB_W, DEF.CDB_H);
for (x = minX; x < maxX; x++) ...{
for (y = minY; y < maxY; y++) ...{
int t = x + y * map_sizeX;
int x1 = (x * DEF.TILE_W) % DEF.CDB_W;
int y1 = (y * DEF.TILE_H) % DEF.CDB_H;
DrawTile(_gCDB, t, x1, y1);
}
}
}
/**/////////////////////////////////////////////////////////////////////////////////////////////////////
// CDB -> Screen
static void CDB_DrawGameView() ...{
int bb_pos_x = (s_camX % DEF.CDB_W);
int bb_pos_y = (s_camY % DEF.CDB_H);
int widthR = (DEF.CDB_W - bb_pos_x);
int widthL = (DEF.CDB_W - widthR);
int heightB = (DEF.CDB_H - bb_pos_y);
int heightT = (DEF.CDB_H - heightB);
// blit backbuffer on screen ...
// ************* top left *************
CDB_Copy2Screen(DEF.SV_X, DEF.SV_Y, widthR, heightB,
DEF.SV_X - bb_pos_x, DEF.SV_Y - bb_pos_y);
// ************* top right *************
CDB_Copy2Screen(DEF.SV_X + widthR, DEF.SV_Y, widthL, heightB,
DEF.SV_X + widthR, DEF.SV_Y - bb_pos_y);
// ************* bottom left *************
CDB_Copy2Screen(DEF.SV_X, DEF.SV_Y + heightB, widthR, heightT,
DEF.SV_X - bb_pos_x, DEF.SV_Y + heightB);
// ************* bottom right *************
CDB_Copy2Screen(DEF.SV_X + widthR, DEF.SV_Y + heightB, widthL, heightT,
DEF.SV_X + widthR, DEF.SV_Y + heightB);
}
/**/////////////////////////////////////////////////////////////////////////////////////////////////////
private static void CDB_Copy2Screen(int x, int y, int w, int h, int xx,
int yy) ...{
//clip viewport
if (x < _vp_x) ...{
w = w - _vp_x + x;
x = _vp_x;
}
if (y < _vp_y) ...{
h = h - _vp_y + y;
y = _vp_y;
}
if (x + w > _vp_x + _vp_w) ...{
w = _vp_x + _vp_w - x;
}
if (y + h > _vp_y + _vp_h) ...{
h = _vp_y + _vp_h - y;
}
if (w <= 0 || h <= 0)
return;
s_g.setClip(x, y, w, h);
s_g.drawImage( _imgCDB, xx, yy, Graphics.LEFT | Graphics.TOP);
}
相关文章推荐
- Supervised Descent Method Face Alignment 代码下载 和 算法研究 之一
- 基于灰度投影的电子稳像算法研究 (matlab代码)毕业设计
- 关于卡马克算法和系统函数库方法快速开平方根快慢的研究
- (转)卡马克卷轴算法研究
- 聚类方法:DBSCAN算法研究(2)--matlab代码实现
- HEVC码率控制算法研究与HM相应代码分析(二)——新的码率控制模型
- H.264码率控制算法研究及JM相应代码分析(二)
- H.264码率控制算法研究及JM相应代码分析(三)
- H.264码率控制算法研究及JM相应代码分析(一)
- 两种图像骨架提取算法的研究(2)实现代码
- HEVC码率控制算法研究与HM相应代码分析(三)——算法及代码分析
- 聚类方法:DBSCAN算法研究(3)--C++代码实现及与matlab实例结果比较
- 基于灰度投影的电子稳像算法研究 (matlab代码)毕业设计
- 卡马克卷轴算法研究_地图双缓冲
- 卡马克卷轴算法研究
- (转)卡马克卷轴算法研究
- 程序员应该用程序来思维,有空来研究一下狼 羊 草和农夫过河,将算法转换为代码《转》
- 基于灰度投影的电子稳像算法研究 (matlab代码)毕业设计
- (转)卡马克卷轴算法研究
- “共轭变换”图像处理算法在FPGA 上实现的研究