【问题标题】:c++ sfml loading maps sideways时间:2019-05-10 标签:c++sfmlloadingmapssideside
【发布时间】:2015-06-14 10:44:24
【问题描述】:

我正在使用 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
            }

         }
    }
}

【问题讨论】:

  • 你需要想象一下二维数组是如何出现在内存中的。诠释[A][B];所以你有 B 数组的 A 数组。所以第一个数组是第一行,其中包含 B 数组中的值。 A0: B0, B1, B2 A1: B0, B1, B2 所以 A 将是 Y 的大小,B 将是 X 的大小。如果对你来说很难,那么你可以使用创建 Collision 类和 TileMap 类并给它们函数 .get(x,y) 将映射值 x -> y 和 y -> x。
  • 如果你的地图是横向的,要么是横向加载,要么是横向保存。

标签: c++ sfml


【解决方案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_ */

【讨论】:

    猜你喜欢
    • 2020-06-15
    • 2020-10-01
    • 2011-07-08
    • 2012-02-03
    • 2017-04-15
    • 2021-07-31
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多