哇,这让我回来了.. 好吧,这就是我当年实现它的方式(使用 directdraw 7,但实际上这背后的图形引擎是无关紧要的)。此代码仅绘制相机在任何给定点可以看到的内容(这实际上是您应该绘制的唯一内容)。还要记住,当我第一次开始学习如何开发时,这段代码是旧的。它不会很漂亮:
// set original render location to x = 0, y = 0, width = tileWidth, height = tileHeight
// this is the top left corner of the screen
RECT renderLoc = { 0, 0, g_MapData.m_TileSize, g_MapData.m_TileSize };
// the first column of visible tiles is given by the x coordinate divided
// by the tilesize and first visible row is given by the y coordinate/tileWidth
int xStartCol = g_MapData.m_xCamera / g_MapData.m_TileSize;
int yStartRow = g_MapData.m_yCamera / g_MapData.m_TileSize;
// calculate the number of tiles in the current resolution that are visible
int xVisibleTiles = (dd7.m_ScreenWidth/g_MapData.m_TileSize);
int yVisibleTiles = (dd7.m_ScreenHeight/g_MapData.m_TileSize);
// if the tilesize is not divisible by the screensize then the
// number of visible tiles will not calculate correctly. It will
// be a float and since this is truncated, it will contain a row or
// column less than what it requires. This code compensates for that
// by just adding one more row to round up instead of down.
if (dd7.m_ScreenWidth%g_MapData.m_TileSize) { xVisibleTiles++; }
if (dd7.m_ScreenHeight%g_MapData.m_TileSize) { yVisibleTiles++; }
// now just add the size in tiles of the visible screen to get the end
int xEndCol = xStartCol + xVisibleTiles;
int yEndRow = yStartRow + yVisibleTiles;
// Visible tiles referring to the tiles that can be displayed across and
//down on the screen (this depends on the resolution set)
// now check if the camera coordinates are divisible by the tile size
int x, y, l; // variables for loops and checks
x = g_MapData.m_xCamera % g_MapData.m_TileSize;
y = g_MapData.m_yCamera % g_MapData.m_TileSize;
if (!x)
{
// remove a column to draw since it divided perfectly. We originally
//added one to the visible rows and columns
xEndCol--;
}
else
{
// need to move renderLoc RECT. Since there could be half a column
//exposed which forces us to draw that portion. we only want to draw what
//is currently on the viewable screen to save memory.
renderLoc.left -= x;
renderLoc.right -= x;
}
// now do the same for rows
if (!y)
{
yEndRow--;
else
{
renderLoc.top -= y;
renderLoc.bottom -= y;
}
// now check to make sure we're not exceeding map size
if (xEndCol > g_MapData.m_xMaxTiles) { xEndCol = g_MapData.m_xMaxTiles; }
if (yEndRow > g_MapData.m_yMaxTiles) { yEndRow = g_MapData.m_yMaxTiles; }
// Now Draw!
// for each layer
for ( l = 0; l < g_MapData.m_Layers; l++) {
// draw the rows
for ( x = xStartCol; x <= xEndCol; x++) {
// column by column
for ( y = yStartRow; y <= yEndRow; y++) {
tileToRender = g_MapData.m_Tiles[x][y][l];
// Finish your drawing..