C++ / SDL:矢量/表面问题

发布于 2024-12-05 03:40:26 字数 1952 浏览 2 评论 0原文

这是我遇到的最新问题。我有一个名为 Projectiles 的类,它包含射弹的基本构成。它是坐标及其使用的图像。这是基本结构:

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 );

我有一种感觉,我误解了表面引用的工作方式。为每个射弹提供自己的表面,以便我可以独立删除它们的最佳方法是什么?

编辑:只是为了消除可能的混乱,我希望能够独立地释放表面。一旦一枚射弹死亡,但另一枚仍在屏幕上,则释放表面是最初导致崩溃的原因。

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;

I have a method in the hero class which makes a new projectile and pushes it into this vector like so:

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:

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-12-12 03:40:26
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 );
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:

SDL_BlitSurface( projectiles[i].returnSurface(), NULL, destination, &offset );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文