c++sfml横向加载映射
c++ sfml loading maps sideways
Im使用C++SFML,并且我一直在加载tilemap和colision映射但它们总是侧向加载,所以我必须反转x,y来纠正它们这意味着在计算运动和碰撞时困惑,我做错了什么吗。
然后我知道我正在加载线路,但有没有办法加载我的地图横着走。
void LoadColMap(const char*filename)
{
std::ifstream openfile(filename);
std::vector<int> tempMap;
colMap.clear();
if(openfile.is_open())
{
while(!openfile.eof())
{
std::string str, value;
std::getline(openfile,str);
std::stringstream stream(str);
while(std::getline(openfile,str))
{
for(int i =0; i < str.length(); i++)
{
if(str[i] != ' ') // if the value is not a space
{
char value = str[i];
tempMap.push_back(value - '0');
}
}
colMap.push_back(tempMap); // push back the value of temp vector into the map vector
tempMap.clear(); // clear the temp vector readt for the next value
}
}
}
}
我制作了一个模板类,它用作C++2D数组,但只是维度为columns * rows
的普通C++数组。它比嵌套矢量或手动嵌套数组更容易使用,也更直观。我知道它不能直接解决你的问题,但它在过去的一个项目中对我帮助很大,所以我觉得有必要分享它。
如何使用
// some map data
int mapData = 123;
// somewhere you initialize the map (start of program)
Util::TMatrix<int> test(40, 40);
test.fill(0); // fill with default value
// then when loading data, just set directly, without temp vector or array
test.set(12, 32, mapData);
// you can use the total size to loop through all the data without nested for loops.
cout << "The array (40 x 40) is of total size: " << test.size() << endl;
cout << "Testing array bounds: " << test.isInsideBounds(12, 32) << endl;
cout << "Retrieve map data: " << test.at(12, 32) << endl;
int key = test.getKey(12, 32);
cout << "Our map data is really just at key: " << key << endl;
输出
The array (40 x 40) is of total size: 1600
Testing array bounds: 1
Retrieve map data: 123
Our map data is really just at key: 512
声明
#ifndef TMatrix_H_
#define TMatrix_H_
#include <algorithm>
namespace Util {
/*
*
*/
template<typename T>
class TMatrix {
public:
/**
* Must call Fill(T value) to use the TMatrix
* it will fill it by default with "value"
* and init it.
*/
TMatrix(unsigned int rows, unsigned int cols);
virtual ~TMatrix();
// Dimensions retrieval
unsigned int rows() const;
unsigned int cols() const;
unsigned int size() const;
unsigned int bytes() const;
/**
* Fill "mArray" with "val"
*
*/
void fill(T val);
/**
* get value AT (x, y)
*/
T at(unsigned int x, unsigned int y) const;
/**
* Get value AT "key", if you know the key
*/
T at(unsigned int key) const;
/**
* set value "element" AT (x, y)
*/
void set(unsigned int x, unsigned int y, T element);
/**
* set value "element" AT (key)
*/
void set(unsigned int key, T element);
/**
* @return the key of the 1 dimensionnal array mArray
*/
unsigned int getKey(unsigned int x, unsigned int y) const;
bool isInsideBounds(unsigned int key) const;
bool isInsideBounds(unsigned int x, unsigned int y) const;
private:
unsigned int mRows;
unsigned int mCols;
T * mArray;
};
实施
/**
* Must call Fill(T value) to use the TMatrix
* it will fill it by default with "value"
* and init it.
*/
template<typename T>
inline TMatrix<T>::TMatrix(unsigned int rows, unsigned int cols) :
mRows(rows),
mCols(cols) {
mArray = new T[mRows * mCols];
}
template<typename T>
inline TMatrix<T>::~TMatrix() {
delete[] mArray;
}
// Dimensions retrieval
template<typename T>
inline unsigned int TMatrix<T>::rows() const {
return mRows;
}
template<typename T>
inline unsigned int TMatrix<T>::cols() const {
return mCols;
}
template<typename T>
inline unsigned int TMatrix<T>::size() const {
return rows() * cols();
}
template<typename T>
inline unsigned int TMatrix<T>::bytes() const {
return size() * sizeof(T);
}
/**
* Fill "mArray" with "val"
*
*/
template<typename T>
inline void TMatrix<T>::fill(T val) {
//std::uninitialized_fill_n(mArray, Size(), val);
std::fill_n(mArray, size(), val);
}
/**
* get value AT (x, y)
*/
template<typename T>
inline T TMatrix<T>::at(unsigned int x, unsigned int y) const {
return mArray[getKey(x, y)];
}
/**
* Get value AT "key", if you know the key
*/
template<typename T>
inline T TMatrix<T>::at(unsigned int key) const {
return mArray[key];
}
/**
* set value "element" AT (x, y)
*/
template<typename T>
inline void TMatrix<T>::set(unsigned int x, unsigned int y, T element) {
if (isInsideBounds(x, y)) {
mArray[getKey(x, y)] = element;
}
}
/**
* set value "element" AT (key)
*/
template<typename T>
inline void TMatrix<T>::set(unsigned int key, T element) {
if (isInsideBounds(key)) {
mArray[key] = element;
}
}
/**
* Returns the key of the 1 dimensionnal array mArray
* returns -1 on OOB.
*/
template<typename T>
inline unsigned int TMatrix<T>::getKey(unsigned int x, unsigned int y) const {
return x * cols() + y;
}
template<typename T>
inline bool TMatrix<T>::isInsideBounds(unsigned int key) const {
return key < size();
}
template<typename T>
inline bool TMatrix<T>::isInsideBounds(unsigned int x, unsigned int y) const {
return isInsideBounds(getKey(x, y));
}
} // namespace Util
#endif /* TMatrix_H_ */
相关文章:
- std::原子加载和存储都需要吗
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 为什么加载SDF会导致Mobilizer创建闭环错误
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- 如何使用tinyxml2从XML加载父实体和子实体
- 如何在C++中使用pybind11加载一个pickle python列表
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 当我尝试加载内核模块时,如何修复C++中的这个 malloc() 错误?
- 树莓上的 Libtorch 无法加载 pt 文件,但在 ubuntu 上工作
- Python ctypes:不会按预期加载 dll
- 为什么我的纹理加载到OpenGL中,未正确映射
- ATL ActiveX DLL 作为映射类型图像和数据加载了两次
- c++sfml横向加载映射
- 简单的进程加载程序内存映射
- 重载内存映射文件加载程序(C++)
- 从文本文件加载标准::映射
- 用DevIL在DDS文件中加载一个立方体映射
- 在c++中将文字从文本文件加载到映射中
- 在c++中将令牌加载到映射容器中