0

我正在尝试在 C++ 中创建一个无缝的 2D tilemap 渲染器。我们的目标是在玩家从每张地图到另一张地图的过程中几乎没有加载屏幕。

例如: 每个方块都是一个区域/瓷砖地图; 红色方块是可视区域 上图中的每个方块都是一个zone/tilemap;红色方块是可视摄像头。在此示例中,每个区域为 256x256 并存储在一个std::vector<std::vector<int> >- 也可以说相机的可视区域为 60x60(32x32 瓷砖)

我目前的问题是如何决定:

  1. 从每个给定区域显示哪些图块。
  2. 如何计算绘制所述瓷砖的偏移量。

有点相关,如果有人对创建这项技术感兴趣,我非常愿意为此付费:)

我是一名软件工程师,但图形并不是我的强项。最终目标是使用支持着色器的 OpenGL 3.2 开发一款老式 RPG。

感谢大家!

4

1 回答 1

1

哇,这让我回来了.. 好吧,这就是我当年实现它的方式(使用 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..
于 2013-03-26T04:10:18.767 回答