我正在使用C ++ SFML,我一直在加载tilemaps和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
}
}
}
}
答案 0 :(得分:-1)
我创建了一个模板类,用作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_ */