- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 SFML2 编写一个基于等距图 block 的简单游戏。 map 中的每个图 block 都由 Tile 类表示。该类包含 sf::Sprite 对象和 draw()
应该在屏幕上绘制 Sprite 的函数。问题是调用 window.draw()
导致段错误。我读到它通常是由关联的 sf::Texture 指针无效引起的,但我已经检查过,事实并非如此。
(使用 ResourceManager<sf::Texture>
对象将纹理引用传递给 Tile 构造函数)
瓷砖.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
}
map .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;
};
map .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);
}
}
}
资源管理器.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 源代码 (Graphics/Texture.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 句柄已删除,但仍存储在 res
的拷贝中,您将其插入到 _resources
中。
要解决此问题,请改为存储资源类的指针:
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
中。但见this link为什么那是不可能的。
关于c++ - 绘制 Sprite 导致 Segmentation Fault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943153/
这是一个类作业,用于将有序插入到已排序的链表中。我已经避免访问空指针,并且添加了一些调试输出行以将段错误缩小到 while 循环本身的条件语句。 我可以将一个节点添加到空列表并将具有较小键的节点添加到
一个小型测试程序在 64 位 Linux 上使用 gfortran (4.4.5) 返回段错误。 n=2_8**22_8 时不存在故障。 gdb 指示在循环的第一次迭代期间函数 mylen 中发生段错
我正在使用 Spring-WS,当我通过 webServiceTemplate 中的 marshalSendAndReceive 调用 Web 服务时,我收到错误代码为 500 的 SOAP 错误。不
我有一个具有此方法的单例数据管理器: -(NSArray*)fetchItems { NSEntityDescription *entity = [NSEntityDescription
我有一个调用 Web 服务的 Java 应用程序 (A)。 1)然后我有另一个使用 A jar 的应用程序 (B)。调用成功,返回需要的数据。 2)然后我有一个也使用A jar 的网络应用程序。在这种
我正在 Java 应用程序中对第三方网络服务进行 SOAP 网络服务调用。用于查找 Web 服务、传输数据和发出 SOAP 请求的 Java 类是使用来自 WSDL 的 Apache CXF 库生成的
我正在尝试学习 SOAP 协议(protocol)。因此,在使用以下命令借助 wsimport 生成 net.webservicex 代码后 C:\Program Files\Java\jdk1.8.
我正在构建一个基于配备 arm64 CPU 的 UltraScale+ FPGA 的数据采集系统。数据通过 DMA 传输到 RAM。驱动程序中的 DMA 缓冲区保留如下: virt_buf[i] =
我使用 QtDesigner 创建了两个对话框“listdialog.ui”和“editdialog.ui”,已发布使用Qt5.3.1,然后添加到项目“phone book.pro”“带有源代码”使用
有谁知道空中交通管制系统是如何实现软件容错的? 一些 URL 会很有帮助。 最佳答案 Lockheed 不久前发表了一些关于此的文章。看看here . 许多系统都使用 Ada,它明确支持验证命题和其他
我认为阅读内存应该不会引起任何问题,例如 char *d=""; char *d2="test"; memcmp(d,d2,10); memcmp() 永远 会失败吗? 最佳答案 您的假设是不正确的,
这个程序在我的 UNIX 机器上导致了段错误。我将原因缩小到 memset() 的第二次调用。 为什么会出现这种行为?第一个“ block ”代码与第二个几乎相同,不是吗?为什么 第一次 调用 mem
我很困惑进程在使用虚拟内存时如何可能出现段错误。据我了解,“虚拟”内存允许进程访问所有可用内存,然后将其映射到“实际”硬件内存。通过这种转换,进程怎么可能尝试访问不允许访问的内存部分? 最佳答案 听起
Tritwise操作(向右旋转和疯狂操作)无法正确运行,并在Malbolge编译器/解释器中引发分段错误。 在看到有关Coding Challenges和Code Golf的惊人答案之后,我决定开始在
这是我的代码 #include #include #include /************************************************** a is a poin
我有下面的代码。当我取消注释 temperature(i,j) = anode_temperature 时,我会出现 SegFault。 forall(i=0:Cells(1), j=0:Cells(
我是 C 编程新手,我的代码中出现了段错误。该程序使用返回函数来询问用户他们的银行帐户中有多少钱。稍后我将添加代码来计算利息。感谢您为我查看此内容,因为我很难找出为什么会出现此错误。 #include
为了测试,我编写了一个代码来计算 #include int main(void) { int p, i, primes[50], index; boo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
考虑 const a = [1,2,3] console.log(a[5]) // returns undefined 我是一名 C 和 Fortran 程序员,我预计会发生段错误。这里的内存是如何管
我是一名优秀的程序员,十分优秀!