我有两个类World和WorldManager:
WorldManager.h
const sf::Vector2u roomSize = sf::Vector2u(30, 25);
const sf::Vector2u tileSize = sf::Vector2u(32, 32);
const unsigned int nb_tiles = 30*25;
const sf::Vector2u tilesetSize = sf::Vector2u(4, 9);
const sf::Vector2u textureSize = sf::Vector2u(360, 360);
class WorldManager
{
public:
WorldManager();
void createWorld(sf::Texture tileset);
std::vector<sf::Vertex> generateMap(std::array<unsigned int,nb_tiles> pattern);
private:
World m_world;
};
(顺便说一下,为什么我不能写const unsigned int nb_tiles = roomSize.x*roomSize.y;
?我告诉我我不能在std :: array的常量表达式中使用它
WorldManager.cpp
#include "WorldManager.h"
WorldManager::WorldManager() :
m_world()
{
sf::Texture tileset;
assert(tileset.loadFromFile("./sprites/tileset.png"));
std::cout << "step 0";
createWorld(tileset);
std::cout << "step 4";
}
void WorldManager::createWorld(sf::Texture tileset)
{
std::array<unsigned int,nb_tiles> pattern =
{
20, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 21,
19, 12, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 13, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 17,
19, 14, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15, 17,
22, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 23
};
std::cout << sizeof(pattern);
std::vector<sf::Vertex> room = generateMap(pattern);
std::cout << "step 1";
m_world.loadTileset(tileset);
std::cout << "step 2";
m_world.loadRoom(room);
std::cout << "step 3";
}
std::vector<sf::Vertex> WorldManager::generateMap(std::array<unsigned int,nb_tiles> pattern)
{
std::vector<sf::Vertex> vertices;
vertices.reserve(roomSize.x * roomSize.y * 4);
for(unsigned int i = 0; i < roomSize.x; ++i)
for(unsigned int j = 0; j < roomSize.y; ++j)
{
unsigned int cell = i * roomSize.x + j;
vertices[cell*4].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
vertices[cell*4+1].position = sf::Vector2f((i+1) * tileSize.x, j * tileSize.y);
vertices[cell*4+2].position = sf::Vector2f((i+1) * tileSize.x, (j+1) * tileSize.y);
vertices[cell*4+3].position = sf::Vector2f(i * tileSize.x, (j+1) * tileSize.y);
unsigned int texCol = pattern[cell] % tilesetSize.x;
unsigned int texLine = floor(pattern[cell] / tilesetSize.x);
vertices[cell*4].texCoords = sf::Vector2f(texCol * textureSize.x, texLine * textureSize.y);
vertices[cell*4+1].texCoords = sf::Vector2f((texCol+1) * textureSize.x, texLine * textureSize.y);
vertices[cell*4+2].texCoords = sf::Vector2f((texCol+1) * textureSize.x, (texLine+1) * textureSize.y);
vertices[cell*4+3].texCoords = sf::Vector2f(texCol * textureSize.x, (texLine+1) * textureSize.y);
}
return vertices;
}
World.h
class World
{
public:
World();
void loadTileset(sf::Texture tileset);
void loadRoom(std::vector<sf::Vertex> room);
private:
sf::Texture m_tileset;
std::vector<sf::Vertex> m_room;
};
World.cpp
#include "World.h"
World::World()
{
}
void World::loadTileset(sf::Texture tileset)
{
m_tileset = tileset;
}
void World::loadRoom(std::vector<sf::Vertex> room)
{
m_room = room;
}
sizeof返回3000个字节。
然后当我编译&amp;运行程序几次,我随机得到:
程序结束,我的控制台显示step0 step1 step2 step3 step4 std::bad_alloc
或程序崩溃并显示step0 step1 step2 step3
,这很奇怪,因为step4就在第3步之后。
我认为问题来自std :: array,也许它没有,但它是我唯一不熟悉的东西。那个数组不是那么大,不是吗?
有什么想法吗?
谢谢!
编辑:
// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type)
{ ::operator delete(__p); }