为什么我在 SDL_UpdateTexture 中有 "LockRect(): INVALIDCALL"?

Why am I having "LockRect(): INVALIDCALL" in SDL_UpdateTexture?

Windows10.

MSYS2 MinGW 64 位。

SDL2.

我正在使用表面的像素数据更新流纹理。两者具有相同的大小和相同的像素格式。下面的代码 return 错误 "LockRect(): INVALIDCALL" 调用 "SDL_UpdateTexture":

    // 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"?我做错了什么?

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

你的第二个代码片段是你应该如何更新流纹理。锁定的意思是"give me memory to write pixels into, I'll unlock when I'm done"。 UpdateTexture 与此冲突 - 您要求 SDL 执行更新,但纹理已被您锁定。如果它没有因错误而失败,结果会更糟——你的解锁只会写入垃圾数据,因为你没有修改像素数组。您可以在没有 Lock/Unlock 的情况下使用 UpdateTexture,但在某些渲染器实现中,它的流式传输效率可能较低。