[英]C++ / SDL: vector / surface issues
Here is the newest problem I am running into. 这是我遇到的最新问题。 I have a class called Projectiles which holds the basic makeup of a projectile. 我有一类叫做弹丸的类,其中包含弹丸的基本组成。 It's coordinates, and the image which it uses. 它是坐标以及它使用的图像。 Here is the basic structure: 基本结构如下:
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;
}
Now, I also have my hero class which holds the projectile objects in a vector like such: 现在,我还有一个英雄类,该类将弹丸对象保存在矢量中,例如:
vector< Projectile > projectiles; vector <弹丸>弹丸;
I have a method in the hero class which makes a new projectile and pushes it into this vector like so: 我在hero类中有一个方法,可以制作一个新的弹丸,并将其推入此向量中,如下所示:
void Hero::newProjectile( int type )
{
projectiles.push_back( Projectile( type ) );
projectileCount++;
}
and then a draw method which is called at the very end of my main loop which does the following: 然后在我的主循环的最后调用一个draw方法,它执行以下操作:
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 );
}
}
conceptually, I thought this would work just fine. 从概念上讲,我认为这会很好。 Originally my projectile class held ALL of the projectiles coordinates in its own vector, but I came across a problem when I wanted to delete them. 最初,我的弹丸类将所有弹丸坐标保存在其自己的向量中,但是当我想删除它们时遇到了一个问题。 Since they all used the same surface resource, deleting one while another was on the screen would cause the game to crash. 由于他们都使用相同的表面资源,因此在屏幕上同时删除一个表面资源会导致游戏崩溃。 I thought this would solve the problem ( each having their own surface resource ), but I am getting 我以为这可以解决问题(每个人都有自己的地面资源),但是我得到了
Access violation reading location 0xccccccf8. 访问冲突读取位置0xccccccf8。
when it attempts to draw the projectile at : 当它试图在以下位置绘制弹丸时:
SDL_BlitSurface( projectiles[i].returnSurface(), NULL, destination, &offset );
I have a feeling i am misunderstanding the way the surface referencing works. 我有一种误解,是表面参照的工作方式。 What would be the best way to give each projectile its own surface, so that I can delete them independently? 使每个弹丸具有其自己的表面,以便我可以独立删除它们的最佳方法是什么?
edit: just to clear up possible confusion, I want to be able to FREE the surfaces independently. 编辑:只是为了消除可能的混乱,我希望能够独立释放表面。 Freeing a surface once one projectile dies, but another was still on the screen is what was causing the crashes initially. 一旦一个弹丸死亡,而另一个仍留在屏幕上,则释放表面是最初导致坠毁的原因。
Projectile::Projectile( int type )
{
type = 1;
alive = true;
width = 83;
height = 46;
}
void Hero::newProjectile( int type )
{
projectiles.push_back( Projectile( type ) );
projectileCount++;
}
In the code above, you never loaded a surface. 在上面的代码中,您从未加载过曲面。 You never even initialized surface
, so it's pointing off in space, hence the access violation in drawProjectileState
when you do this: 您甚至都没有初始化surface
,因此它在空间中指向,因此执行以下drawProjectileState
时drawProjectileState
的访问冲突:
SDL_BlitSurface( projectiles[i].returnSurface(), NULL, destination, &offset );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.