SFML平台在c++中使用贴图
SFML platformer using tile map in c++
嗨,我有一个类我的映射,它是绘制到屏幕上,但它只绘制垂直向下的窗口左侧。我能找出我哪里出错了。任何帮助都将非常感激。我很确定这和我在draw函数中的for循环有关。
void Map::Initialise(const char *filename)
{
std::ifstream openfile(filename);
std::string line;
std::vector <int> tempvector;
while(!openfile.eof())
{
std::getline(openfile, line);
for(int i =0; i < line.length(); i++)
{
if(line[i] != ' ') // if the value is not a space
{
char value[1] = {line[i]}; // store the character into the line variable
tempvector.push_back(atoi(value)); // then push back the value stored in value into the temp vector
}
mapVector.push_back(tempvector); // push back the value of the temp vector into the map vector
tempvector.clear(); // clear the temp vector readt for the next value
}
}
}
void Map::DrawMap(sf::RenderWindow &Window)
{
sf::Shape rect = sf::Shape::Rectangle(0, 0, BLOCKSIZE, BLOCKSIZE, sf::Color(255, 255, 255, 255));
sf::Color rectCol;
sf::Sprite sprite;
for(int i = 0; i < mapVector.size(); i++)
{
for(int j = 0; j < mapVector[i].size(); j++)
{
if(mapVector[i][j] == 0)
rectCol = sf::Color(44, 117, 255);
else if (mapVector[i][j] == 1)
rectCol = sf::Color(255, 100, 17);
rect.SetPosition(j * BLOCKSIZE, i * BLOCKSIZE);
rect.SetColor(rectCol);
Window.Draw(rect);
}
}
}
首先,使您的提取while
循环的条件:
while(std::getline(openfile, line))
{
// ...
}
使用openfile.eof()
是一个非常糟糕的主意;仅仅因为您还没有到达文件的末尾,并不意味着下一次提取将会成功。你只是不知道你是否真的有一个有效的line
。
其次,这将不能正常工作:
char value[1] = {line[i]}; // store the character into the line variable
tempvector.push_back(atoi(value));
我可以看到你为什么使用char[1]
-你想把它转换成一个指针,因为atoi
需要一个const char*
。然而,atoi
也期望它所指向的数组以空结束。你的不是。这只是一个char
。
有一种更好的方法将char
(一个数字)转换为它所代表的整数。所有数字字符的值保证是连续的:
char value = line[i];
tempvector.push_back(value - '0');
现在,真正的问题来了。在读取一个字符后,将tempvector
推入mapVector
。您需要将它移到for
循环之外的行:
while(std::getline(openfile, line))
{
for(int i =0; i < line.length(); i++)
{
// ...
}
// Moved here
mapVector.push_back(tempvector); // push back the value of the temp vector into the map vector
tempvector.clear(); // clear the temp vector readt for the next value
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- C/C++ - 查询平台相关的换行符(用于内存映射文件)
- 如何基于平台在制作文件中制作if语句?
- 在 Linux 平台的 C++ 中动态加载 DLL
- 无法使用 aarch64-unknown-nto-qnx7.0.0-g++-5.4.0 编译器交叉编译 QNX 平台的
- 如何判断类型双关语在我的平台上是否可以?
- 如何使用可视化代码和平台IO将环境变量注入CPP文件?
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- 操作系统平台支持宏和提升库
- 平台游戏 - 下车时避免与平台碰撞
- NPM 安装:找不到平台工具集 = v141
- 如何在 64 位平台上计算 32 位哈希C++?
- 我的代码在不同的平台上给出不同的输出
- 是否有任何 2 补充平台/编译器,其中有符号移位不做通常的事情?
- 哪些 C++ 数据类型是 Metal 和所有其他 C++ 平台共有的
- 如何创建独立于平台的宏来包装编译器扩展?
- 多平台库设计.最佳实践
- 如何在linux平台上表达openGL glDebugOutput / glDebugMessageCallback
- 将vector<vector<double>>从x86平台中创建的一个进程发送到x64中构建的另一个进程的最快方法是什么
- 构建在VS 2013中找不到平台工具集= 'v141'