うわー、これは私を元に戻します..わかりました、これが私がその日にそれを実装した方法です(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..