SDL_RenderCopy仅在Android上显示第一张图像?

时间:2019-03-18 23:46:50

标签: android c++ sdl

有人知道为什么使用下面的SDL2代码只显示第一张图像吗?

基本上,我使用函数SDL_RWFromFile读取两个图像的内容“ circle.png”和“ sdl-game-bck.png”,并且一旦使用函数SDL_CreateTextureFromSurface创建了纹理,就使用SDL_RenderCopy来在屏幕上显示它们,但仅显示第一张图像。

SDL_SetRenderDrawColor(this->renderer,
                           this->clearColor.r, this->clearColor.g,
                           this->clearColor.b, this->clearColor.a);

    SDL_RenderClear(this->renderer);


    SDL_RWops *file = SDL_RWFromFile("circle.png", "rb");
    SDL_Surface* image_circle = IMG_Load_RW(file, 1);
    SDL_Texture* texture_circle = SDL_CreateTextureFromSurface(this->renderer, image_circle);
    SDL_FreeSurface(image_circle);
    delete(file);

    SDL_Rect dstRectCircle = {200,200,700,700};
    SDL_Rect srcRectCircle = {0,0,500,500};

    if (SDL_RenderCopy(this->renderer, texture_circle, &srcRectCircle, &dstRectCircle) < 0) {
        SDL_Log("SDL_RenderCopy() failed: %s", SDL_GetError());
        //exit(1);
    }

    SDL_RWops *file_bck = SDL_RWFromFile("sdl-game-bck.png", "rb");
    SDL_Surface* image_bck = IMG_Load_RW(file_bck, 1);
    SDL_Texture* texture_bck = SDL_CreateTextureFromSurface(this->renderer, image_bck);
    SDL_FreeSurface(image_bck);
    delete(file_bck);

    SDL_Rect dstRect = {0,500,500,1100/*this->height - 600,this->width,600*/};
    SDL_Rect srcRect = {0,0,500,600};
    if (SDL_RenderCopy(this->renderer, texture_bck, &srcRect, &dstRect) < 0) {
        SDL_Log("SDL_RenderCopy() failed: %s", SDL_GetError());
        //exit(1);
    }


    SDL_RenderPresent(this->renderer);

1 个答案:

答案 0 :(得分:0)

好的,我能够解决我遇到的问题,代码中的主要问题是在创建纹理后,我删除了对SDL_RWops的两个引用(例如:delete(file);)。

这是我正在使用的代码。

void paint() {
    SDL_Color clearColor = {0,255,255};
    SDL_SetRenderDrawColor(renderer,
                           clearColor.r, clearColor.g,
                           clearColor.b, clearColor.a);

    SDL_RenderClear(renderer);

    SDL_RWops *file_circle = SDL_RWFromFile("circle.png", "rb");
    SDL_Surface* image_circle = IMG_Load_RW(file_circle, 1);
    SDL_Texture* texture_circle = SDL_CreateTextureFromSurface(renderer, image_circle);
    SDL_FreeSurface(image_circle);

    SDL_Rect dstRectCircle = {200,200,700,700};
    SDL_Rect srcRectCircle = {0,0,500,500};

    SDL_RWops *file_bck = SDL_RWFromFile("sdl-game-bck.png", "rb");
    SDL_Surface* image_bck = IMG_Load_RW(file_bck, 1);
    SDL_Texture* texture_bck = SDL_CreateTextureFromSurface(renderer, image_bck);
    SDL_FreeSurface(image_bck);

    SDL_Rect dstRect = {0,height - 600,width,height};
    SDL_Rect srcRect = {0,0,500,600};


    SDL_RenderCopy(renderer, texture_circle, &srcRectCircle, &dstRectCircle);
    SDL_RenderCopy(renderer, texture_bck, &srcRect, &dstRect);
    SDL_RenderPresent(renderer);
}