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

我正在尝试加载一个Spritesheet,其中水平排成一排的多个图像(在同一图像中)

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时遇到段错误,传入的指针均不为NULL,并且任何表面均未锁定或类似的东西。我确定我的矩形在每个表面的范围内。

以下是gdb segfaults之前的一些值:

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

一旦我更正,segfault就消失了。

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

关于c++ - 第二次调用SDL_BlitSurface时出现段故障,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48377559/

10-10 05:07