c++sfml横向加载映射

c++ sfml loading maps sideways

本文关键字:映射 加载 横向 c++sfml      更新时间:2023-10-16

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_ */