SDL2_image 渲染不正常

SDL2_image rendering not working properly

我正在尝试在 SDL2 中使用 SDL_image v2.0.0 渲染图像。

我的 res/img/ 文件夹中有一张名为 Red.png 的图像。当我尝试加载纹理并使用 SDL_QueryTexture() 时,它获得了大小并且一切都很好。但是在渲染实际图像的时候,我放了一个矩形轮廓,知道图像在哪里,但是盒子里没有图像。

我用来加载和渲染纹理的class:

class LTexture
{
public:

    ~LTexture()
    {
        SDL_DestroyTexture(image_);
        renderer_ = nullptr;
        image_ = nullptr;
    }

    void init(SDL_Renderer* renderer)
    {
        printf("init texture\n");
        renderer_ = renderer;
    }

    void loadBMP(std::string filename)
    {
        printf("load texture\n");
        image_ = IMG_LoadTexture(renderer_, ("res/img/"+filename).c_str());
        SDL_QueryTexture(image_, NULL, NULL, &imgrect.w, &imgrect.h);
    }

    void render(int x, int y)
    {
        imgrect.x = x;
        imgrect.y = y;
        SDL_SetRenderDrawColor(renderer_, 128, 128, 128, 255);
        if (image_ != nullptr && renderer_ != nullptr)
        {
            printf("%i,  %i\n", imgrect.x, imgrect.y);
            SDL_RenderDrawRect(renderer_, &imgrect);
            SDL_RenderCopy(renderer_, image_, &imgrect, &imgrect);
        }
    }

    bool isLoaded()
    {
        return image_ != nullptr;
    }

private:
    SDL_Renderer* renderer_ = nullptr;
    SDL_Texture* image_ = nullptr;
    SDL_Rect imgrect;

};

我知道它正确地获取渲染器并加载图像,因为 DrawRect 函数有效,如果您没有猜到名称,Red.png 是一个红色矩形。

在您的 SDL_RenderCopy() 调用中为 srcrect 传递 nullptr

SDL_RenderCopy(renderer_, image_, nullptr, &imgrect);

现在,如果 x and/or y 大于 image_,SDL 会将 srcrect 裁剪到 image_ 的范围,结束用一个空的 rect,什么也不做。

示例:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#include <cstdlib>
#include <string>

class LTexture
{
public:

    LTexture()
        : renderer_( nullptr )
        , image_( nullptr )
    { }

    ~LTexture()
    {
        SDL_DestroyTexture(image_);
        renderer_ = nullptr;
        image_ = nullptr;
    }

    void init(SDL_Renderer* renderer)
    {
        printf("init texture\n");
        renderer_ = renderer;
    }

    void loadBMP(std::string filename)
    {
        printf("load texture\n");
        image_ = IMG_LoadTexture(renderer_, filename.c_str());
        SDL_QueryTexture(image_, NULL, NULL, &imgrect.w, &imgrect.h);
    }

    void render(int x, int y)
    {
        imgrect.x = x;
        imgrect.y = y;
        SDL_SetRenderDrawColor(renderer_, 128, 128, 128, 255);
        if (image_ != nullptr && renderer_ != nullptr)
        {
            printf("%i,  %i\n", imgrect.x, imgrect.y);
            SDL_RenderDrawRect(renderer_, &imgrect);
            SDL_RenderCopy(renderer_, image_, nullptr, &imgrect);
        }
    }

    bool isLoaded()
    {
        return image_ != nullptr;
    }

private:
    SDL_Renderer* renderer_;
    SDL_Texture* image_;
    SDL_Rect imgrect;
};


int main( int argc, char** argv )
{
    SDL_Init( SDL_INIT_EVERYTHING );
    IMG_Init( IMG_INIT_PNG );

    SDL_Window * window = SDL_CreateWindow
        (
        "SDL2", 
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
        300, 300, 
        SDL_WINDOW_SHOWN
        );

    SDL_Renderer* renderer = SDL_CreateRenderer
        (
        window,
        0,
        SDL_RENDERER_ACCELERATED
        );

    LTexture tex;
    tex.init( renderer );
    tex.loadBMP( "red.png" );

    bool running = true;
    while( running )
    {
        SDL_Event ev;
        while( SDL_PollEvent( &ev ) )
        {
            if ( ev.type == SDL_QUIT )
                running = false;
        }

        SDL_SetRenderDrawColor( renderer, 0, 0, 0, 255 );
        SDL_RenderFillRect( renderer, NULL );

        tex.render( 50, 50 );

        SDL_RenderPresent( renderer );
        SDL_Delay( 33 );
    }

    SDL_DestroyRenderer( renderer );
    SDL_DestroyWindow( window );

    IMG_Quit();
    SDL_Quit();

    return 0;
}

red.png供参考:

我不知道这是否仍然真实,但我在使用 SDL_RENDERER_ACCELERATED 标志(在 Linux Mint 下)时遇到问题,当我使用 SW 加速时,SDL_Render* 有效。