我正在尝试用C ++创建一个无缝的2D tilemap渲染器。当玩家从每个地图前进到另一个地图时,目标是几乎没有加载屏幕。
例如:
上图中的每个方块都是区域/瓦片图;红色方块是可视的相机。在这个例子中,每个区域是256x256并存储在std::vector<std::vector<int> >
内 - 还可以说相机的可视区域为60x60(32x32块)
我目前的问题是如何决定:
略有关联,如果有人有兴趣创造这项技术,我非常愿意付钱:)
我是一名贸易软件工程师,但图形不是我的强项。最终目标是使用带有着色器支持的OpenGL 3.2开发一个旧学校RPG。
谢谢大家!
答案 0 :(得分:0)
// 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..