绘制精灵会导致分割错误



我正在用SFML2编写一个简单的游戏,它基于等距图块。地图中的每个图块都由 Tile 类表示。该类包含 sf::Sprite 对象和draw()函数,该函数应该在屏幕上绘制精灵。问题是调用window.draw()会导致分段错误。我读过它通常是由相关的 sf::纹理指针无效引起的,但我已经检查过了,事实并非如此。

(纹理引用使用对象传递给 Tile 构造函数ResourceManager<sf::Texture>)

磁贴.hpp:

#pragma once
#include <SFML/Graphics.hpp>
#include "resource_manager.hpp"
class Tile
{
public:
Tile(const sf::Texture& texture);
void draw(sf::RenderWindow& window, const float dt);
private:
sf::Sprite _sprite;
};

磁贴.cpp:

#include "tile.hpp"
Tile::Tile(const sf::Texture& texture)
{
_sprite.setTexture(texture);
}
void Tile::draw(sf::RenderWindow& window, const float dt)
{
std::cout << _sprite.getTexture()->getSize().x << std::endl; //Texture pointer works fine
window.draw(_sprite); //Segmentation Fault here
}

地图.hpp:

#pragma once
#include <vector>
#include <SFML/Graphics.hpp>
#include "resource_holder.hpp"
#include "tile.hpp"
class Map
{
public:
Map(ResourceHolder& resources, int width, int height);
void draw(sf::RenderWindow& window, const float dt);
private:
ResourceHolder& _resources;
int _width, _height;
std::vector<Tile> _tiles;
};

地图.cpp:

#include "map.hpp"
#include <iostream>
Map::Map(ResourceHolder& resources, int width, int height)
: _resources(resources), _width(width), _height(height)
{
_tiles.reserve(width * height);
for(int y = 0; y < _height; ++y)
{
for(int x = 0; x < _width; ++x)
{
_tiles[x + y * _width] = Tile(_resources.textures["groundTile_NE"]);
}
}
}
void Map::draw(sf::RenderWindow& window, const float dt)
{
for(int x = 0; x < _width; ++x)
{
for(int y = 0; y < _height; ++y)
{
_tiles[x + y * _width].draw(window, dt);
}
}
}

resource_manager.hpp:

#pragma once
#include <unordered_map>
#include <iostream>
template<typename Resource>
class ResourceManager
{
public:
ResourceManager(const std::string& directory, const std::string& extension)
: _directory("assets/" + directory + "/"), _extension("." + extension)
{}
Resource& operator[](const std::string& name)
{
return get(name);
}
Resource& get(const std::string& name)
{
if(!exists(name))
load(name);
return _resources.at(name);
}
bool exists(const std::string& name) const
{
return _resources.find(name) != _resources.end();
}
void load(const std::string& name)
{
Resource res;
if(!res.loadFromFile(_directory + name + _extension))
{
res.loadFromFile(_directory + "_fail_" + _extension);
}
_resources.insert({name, res});
}
private:
const sf::String _directory;
const sf::String _extension;
std::unordered_map<std::string, Resource> _resources;
};

从 SFML 源代码(图形/纹理.cpp)中,析构函数会导致删除 OpenGL 纹理缓冲区:

Texture::~Texture()
{
// Destroy the OpenGL texture
if (m_texture)
{
TransientContextLock lock;
GLuint texture = static_cast<GLuint>(m_texture);
glCheck(glDeleteTextures(1, &texture));
}
}

load方法中,您可以在堆栈上分配对象res。这意味着当该方法返回时,将在res对象上调用Resource的析构函数。您的 OpenGL 句柄将被删除,但仍存储在插入到_resources中的res副本中。

若要解决此问题,请改为存储资源类的指针

std::unordered_map<std::string, Resource*> _resources;
...
void load(const std::string& name)
{
Resource* res = new Resource();
if (!res->loadFromFile(_directory + name + _extension))
{
res->loadFromFile(_directory + "_fail_" + _extension);
}
_resources.insert({name, res});
}
Resource* operator[](const std::string& name)
{
return get(name);
}
Resource* get(const std::string& name)
{
if (!exists(name))
load(name); // or return nullptr
return _resources.at(name);
}

但是,当ResourceManager对象被销毁时,您还需要销毁这些指针:

~ResourceManager()
{
for (auto& pair : _resources)
delete pair.second;
}

注意:您可能想将引用存储在_resources中。但是请参阅此链接,了解为什么这是不可能的。

最新更新