我正在 SDL 中编写基于图块的地图,并且Map
该类的构造函数用于设置用于表示其中MapCell
包含的每个对象的图像。但是,我的Sprite
类的析构函数存在问题,该析构函数用于释放SDL_Surface*
对象持有。析构函数被提前调用,我不完全确定为什么。这是我的构造函数的精简版本,Map
仅显示了单元格的精灵是如何分配的。
Map::Map(string fileName, int tileWidth, int tileHeight)
{
string mapData = ReadMap(fileName);
_cells = new MapCell[_width*_height];
for(int y = 0; y < _height; y++)
{
for(int x = 0; x < _width; x++)
{
int currentCell = y*_width+x;
if(mapData[currentCell] == '0' || mapData[currentCell] == 'P' || mapData[currentCell] == 'X')
{
_cells[currentCell]._sprite = Sprite(Point2D(x*tileWidth, y*tileHeight), "Assets/Graphics/Grass.bmp");
if(mapData[currentCell] == 'P')
_cells[currentCell]._sprite = Sprite(Point2D(x*tileWidth, y*tileHeight), "Assets/Graphics/Player.bmp");
if (mapData[currentCell] == 'X')
_cells[currentCell]._sprite = Sprite(Point2D(x*tileWidth, y*tileHeight), "Assets/Graphics/Target.bmp");
}
else if(mapData[currentCell] == '1')
_cells[currentCell]._sprite = Sprite(Point2D(x*tileWidth, y*tileHeight), "Assets/Graphics/Wall.bmp");
}
}
}
析构函数似乎在 Sprite 对象创建后立即被调用。我在这里想念什么?我也尝试在堆上分配_sprite
成员MapCell
,但这会导致同样的问题。据我所知,它并没有超出范围,因为Sprite
创建的对象是对象的一部分Map
。
以下是我的Sprite
类的构造函数和析构函数:
Sprite::Sprite(void)
{
_texture = NULL;
_position = Point2D::Zero();
}
Sprite::Sprite(Point2D position, std::string texPath)
{
_texture = Content::LoadBMP(texPath);
_position = position;
}
Sprite::~Sprite(void)
{
SDL_FreeSurface(_texture);
}
如果它有帮助的话,这是我的主要内容:
int main( int argc, char* args[] )
{
const int TILEWIDTH = 32;
const int TILEHEIGHT = 32;
// Initialization
InitSDL();
Map map = Map("Assets/Maps/Map3.txt", TILEWIDTH, TILEHEIGHT);
Window::SetSize(Rectangle(0, 0, map.GetWidth()*TILEWIDTH, map.GetHeight()*TILEHEIGHT));
PathFinder pathFinder = PathFinder();
List<Point2D> path = pathFinder.FindPath(map, map.GetPlayerStart(), map.GetTarget());
List<Sprite> PathNodes = List<Sprite>();
for(int i = 0; i < path.GetCount(); i++)
PathNodes.Add(Sprite(*path(i)*32, "Assets/Graphics/PathNode.bmp"));
bool quit = false;
SDL_Event Event;
while(quit == false)
{
while(SDL_PollEvent(&Event))
{
if(Event.type == SDL_QUIT)
quit = true;
}
map.Draw();
for(int i = 0; i < path.GetCount(); i++)
{
if(PathNodes(i)->GetPosition() != map.GetPlayerStart()*32 && PathNodes(i)->GetPosition() != map.GetTarget()*32)
PathNodes(i)->Blit();
}
Window::Flip();
}
//Quit SDL
SDL_Quit();
return 0;
}