为什么我在SDL_UpdateTexture中具有“ LockRect():INVALIDCALL”?

问题描述 投票:0回答:1

Windows 10。

MSYS2 MinGW 64位。

SDL2。

我正在使用来自表面的像素数据更新流式纹理。两者具有相同的尺寸和相同的像素格式。下面的代码在调用“ SDL_UpdateTexture”时返回错误“ LockRect():INVALIDCALL”:

    // this->tex is a class private pointer to a SDL_Texture
    // this->sur is a class private pointer to a SDL_Surface
    // Update the texture with the surface
    // lock the texture
    void *texPixels;
    int pitch;
    if (SDL_LockTexture(this->tex, NULL, &texPixels, &pitch) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
    }
    // Get the pixels information from the surface
    void *surPixels = this->sur->pixels;
    int surPitch = this->sur->pitch;
    // update the texture with the pixels from the surface
    if (SDL_UpdateTexture(this->tex, NULL, surPixels, surPitch) < 0){
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't Update texture: %s\n", SDL_GetError());
    }
    // unlock the texture
    SDL_UnlockTexture(this->tex);

所以我决定只用这种方式复制像素:

    // this->tex is a pointer to a SDL_Texture
    // this->sur is a pointer to a SDL_Surface
    // Update the texture with the surface
    // lock the texture
    void *texPixels;
    int pitch;
    if (SDL_LockTexture(this->tex, NULL, &texPixels, &pitch) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
    }
    // Get the pixels information from the surface
    void *surPixels = this->sur->pixels;
    int surPitch = this->sur->pitch;
    // update the texture with the pixels from the surface
    memcpy(texPixels, surPixels, (surPitch * this->sur->h));    
    // unlock the texture
    SDL_UnlockTexture(this->tex);

这很完美。因此,我的问题是:为什么“ SDL_UpdateTexture”失败?我在做什么错?

第二个问题,但是不太重要:我可以解决这个问题吗?

memory sdl-2
1个回答
0
投票

您的第二个代码片段是您应该如何更新流式纹理。锁定的意思是“给我记忆以写入像素,完成后我将解锁”。 UpdateTexture与之冲突-您要求SDL执行更新,但纹理已被您锁定。如果它没有因错误而失败,结果将更加糟糕-您的解锁操作只会写入垃圾数据,因为您尚未修改像素数组。您可以使用不带锁定/解锁功能的UpdateTexture,但是在某些渲染器实现中,它的流传输效率可能较低。

© www.soinside.com 2019 - 2024. All rights reserved.