C++/SDL:矢量/曲面问题
C++ / SDL: vector / surface issues
这是我遇到的最新问题。我有一个课叫"炮弹",它掌握了炮弹的基本组成。它的坐标,以及它使用的图像。以下是基本结构:
class Projectile
{
private:
void load();
public:
SDL_Surface *surface;
SDL_Surface* returnSurface()
{
return surface;
}
Projectile( int );
void coordinates( int, int );
int type;
int width, height;
int positionX, positionY;
bool alive;
};
Projectile::Projectile( int type )
{
type = 1;
alive = true;
width = 83;
height = 46;
}
void Projectile::load()
{
SDL_Surface* loadedImage = NULL;
loadedImage = IMG_Load( "hero.png" );
surface = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
void Projectile::coordinates( int x, int y )
{
positionX = x;
positionY = y;
}
现在,我也有我的英雄类,它将投射物保持在一个向量中,比如:
向量<射弹>射弹;
我在英雄类中有一个方法,它制造一个新的投射物,并将其推入这个向量中,如下所示:
void Hero::newProjectile( int type )
{
projectiles.push_back( Projectile( type ) );
projectileCount++;
}
然后是一个绘制方法,它在我的主循环的最后被调用,它执行以下操作:
void Hero::drawProjectileState( SDL_Surface* destination )
{
for( int i = 0; i < projectileCount; i++ )
{
SDL_Rect offset;
offset.x = positionX;
offset.y = positionY;
SDL_BlitSurface( projectiles[i].returnSurface(), NULL, destination, &offset );
}
}
从概念上讲,我认为这会很好。最初,我的射弹类将所有射弹坐标保存在自己的向量中,但当我想删除它们时,遇到了一个问题。由于他们都使用相同的表面资源,在屏幕上删除一个会导致游戏崩溃。我以为这会解决问题(每个人都有自己的地表资源(,但我得到了
读取位置0xccccccf8时发生访问冲突。
当它试图将炮弹拉向时
SDL_BlitSurface( projectiles[i].returnSurface(), NULL, destination, &offset );
我有一种感觉,我误解了表面参照的工作方式。最好的方法是给每枚炮弹自己的表面,这样我就可以独立删除它们了?
编辑:为了消除可能的混乱,我希望能够独立地释放曲面。一枚炮弹死亡后释放一个表面,但另一枚仍在屏幕上,这是最初造成坠机的原因。
Projectile::Projectile( int type )
{
type = 1;
alive = true;
width = 83;
height = 46;
}
void Hero::newProjectile( int type )
{
projectiles.push_back( Projectile( type ) );
projectileCount++;
}
在上面的代码中,您从未加载过曲面。你甚至从未初始化过surface
,所以它在空间中指向外,因此当你这样做时,drawProjectileState
中的访问违规:
SDL_BlitSurface( projectiles[i].returnSurface(), NULL, destination, &offset );
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- C++/SDL:矢量/曲面问题
- SDL中由SDL_Rect向量分割曲面的问题
- SDL_曲面透明度问题