第二次呼叫SDL_BlitSurface时出现故障

Seg Fault when calling SDL_BlitSurface a second time

本文关键字:故障 BlitSurface 呼叫 SDL 第二次      更新时间:2023-10-16

我正在使用SDL和SDL_Image来加载图像以用作opengl的纹理。

我正在尝试加载一个精灵表,其中多个图像排列在水平行中(在同一图像中(

void load_spritesheet(std::string key, const char *file_name, int width, int height, int nframes) {
    GLuint *texture = new GLuint[nframes];
    auto src = IMG_Load(file_name);
    auto dstrect = new SDL_Rect{0, 0, width, height};
    for(int i = 0; i < nframes; i++) {
        auto dst = SDL_CreateRGBSurface(0, width, height, 1, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask);
        auto rect = new SDL_Rect { i*width, 0, width, height };
        SDL_BlitSurface(src, rect, dst, dstrect);
        load_gltex(dst, &texture[i]);
        SDL_FreeSurface(dst);
    }
    SPRITESHEET_CACHE[key] = texture;
    SDL_FreeSurface(src);
}

我逐步完成了代码,在循环的第一次迭代中,它工作正常。在第二次迭代中,我在调用 SDL_BlitSurface 时遇到 seg 错误,传入的指针都不是 NULL,也没有一个表面被锁定或类似的东西。我确定我的矩形在每个表面的范围内。

以下是 gdb 在段错误之前的一些值:

print i
1
print *src
{flags = 0, format = 0x847100, w = 416, h = 32, pitch = 1664, pixels = 0x87c5f0, userdata = 0x0, locked = 0, lock_data = 0x0, clip_rect = {x = 0, y = 0, w = 416, h = 32}, map = 0x8537b0, refcount = 1}
print *dst
{flags = 0, format = 0x855ec0, w = 32, h = 32, pitch = 4, pixels = 0x84d1f0, userdata = 0x0, locked = 0, lock_data = 0x0, clip_rect = {x = 0, y = 0, w = 32, h = 32}, map = 0x6bdfc0, refcount = 1}
print *rect
{x = 32, y = 0, w = 32, h = 32}
print *dstrect
{x = 0, y = 0, w = 32, h = 32}

在同一表面上两次调用SDL_BlitSurface或类似的东西是不安全的吗?谢谢。

啊,错误是由于不正确地设置了调用的深度而导致的SDL_CreateRGBSurface

当我真的应该传递正确的值时,我正在传递1(在这种情况下32(

一旦我纠正说段错误消失了。

https://wiki.libsdl.org/SDL_CreateRGBSurface#Remarks