如何让我的瓷砖表换行
How do I get my TileSheet to wrap?
>我从一个关卡文件加载地图,这些文件只是与瓷砖表中的瓷砖相对应的数字。
这是关卡文件
[Map]
0 1 0 0 0 0 0 0
0 0 20 0 0 0 0 0
0 0 0 40 0 0 0 0
0 0 0 0 63 0 0 0
0 0 0 0 0 79 0 0
0 0 0 0 0 0 0 0
这是解释它的代码
void LoadMap(const char *filename, std::vector< std::vector <int> > &map)
{
std::ifstream openfile(filename);
if(openfile.is_open())
{
std::string line, value;
int space;
while(!openfile.eof())
{
std::getline(openfile, line);
if(line.find("[TileSet]") != std::string::npos)
{
state = TileSet;
continue;
}
else if (line.find("[Map]") != std::string::npos)
{
state = Map;
continue;
}
switch(state)
{
case TileSet:
if(line.length() > 0)
tileSet = al_load_bitmap(line.c_str());
break;
case Map:
std::stringstream str(line);
std::vector<int> tempVector;
while(!str.eof())
{
std::getline(str, value, ' ');
if(value.length() > 0)
tempVector.push_back(atoi(value.c_str()));
}
map.push_back(tempVector);
break;
}
}
}
else
{
}
}
这就是它的样子https://i.stack.imgur.com/bPZSG.jpg
好的,所以我的瓷砖表是 1000 x 200,看起来像这样 https://i.stack.imgur.com/Pp4JH.png
在地图文件中输入 20 或 40 时,如何使其环绕到 20 或 40?
void DrawMap(std::vector <std::vector <int> > map)
{
for(int i, j = 0; i < map.size(); i ++)
{
for(j = 0; j < map[i].size(); j ++)
{
al_draw_bitmap_region(tileSet, map[i][j] * TileSizeX, 0, TileSizeX, TileSizeY, j * TileSizeX, i * TileSizeX, NULL);
}
}
}
此外,TileSizeX 和 TileSizeY 也是 50
您需要计算目标磁贴所在的图块集的哪个单元格。您有图块集索引。根据图块集的尺寸,使用一些数学来确定该磁贴的列和行。
//This is how many columns your tileset can have.
//You could even dynamically calculate this if you wanted.
static const int TILESET_COLCOUNT = 20;
void DrawMap(std::vector<std::vector<int> > map)
{
int mapRowCount = map.size();
for (int i = 0; i < mapRowCount; ++i)
{
int mapColCount = map[i].size();
for (int j = 0; j < mapColCount; ++j)
{
//This is your tileset index in your level map.
int tilesetIndex = map[i][j];
//The tileset row can be calculated by dividing the tileset index by the number of columns in a tileset row.
int tilesetRow = floor(tilesetIndex / TILESET_COLCOUNT);
//The tileset column can be calculated by retrieving the remainder of the modulus operation on the total number of columns in a row.
int tilesetCol = tilesetIndex % TILESET_COLCOUNT;
al_draw_bitmap_region(
tileSet, //The tileset
tilesetCol * TileSizeX, //multiply the tileset column by the size of a tile to get the source x
tilesetRow * TileSizeY, //multiply the tileset row by the size of a tile to get the source y
TileSizeX, //width
TileSizeY, //height
j * TileSizeX, //destination x
i * TileSizeX, //destination y
NULL //flags
);
}
}
}
相关文章:
- 提取后返回换行符的C++istream.get()
- 创建一个 2 行(不换行)的 QPushButton
- 不同换行符的正则表达式
- C++是否可以将带有换行符的文本传递给C++程序
- 如何使用ifstream c ++从带有换行符的文件读取字符串
- 如何在不添加换行符的情况下将文本附加到QPlainTextEdit,并保持滚动在底部
- 输出换行符的最有效方法
- 如何返回指向unique_ptr换行指针的指针
- 在 C++ 中,get() 和 getline() 对换行符的处理方式不同
- C++保留换行符的预处理器字符串化
- printf 在C++带有换行符的奇怪行为
- 如何解析后跟分号或换行符的条目(boost::spirit)
- C++ 换行符的分隔符,再次用于 :
- 使用lldb打印带有换行符的字符串
- Qt-如何检测换行后的行数增加
- 如何在yaml-cpp中发出带有额外换行符的映射序列
- CSV 文件转换为带换行符的 2D 数组 CPP
- 如何从包含空格和换行符的文本文件中提取特定数据
- 剥离换行字符的方法
- 程序来查找空白和换行字符的数量