gpt4 book ai didi

c++ sfml横向加载 map

转载 作者:太空狗 更新时间:2023-10-29 23:20:32 25 4
gpt4 key购买 nike

我正在使用 C++ SFML,我一直在加载 tilemaps 和 colision maps但它们总是侧向加载,所以我必须反转 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++ 二维数组的模板类,但它只是维度 columns * rows 的普通 C++ 数组。它比嵌套 vector 或手动嵌套数组更容易、更直观地使用。我知道它不能直接解决你的问题,但它在过去的项目中对我有很大帮助,所以我觉得有必要分享它。

如何使用

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

关于c++ sfml横向加载 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29532207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com