SDL2_image渲染无法正常工作

时间:2015-06-10 18:37:06

标签: c++ image textures sdl

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

我的res / img /文件夹中有一个名为Red.png的图像。当我尝试加载纹理,并使用SDL_QueryTexture()时,它获得了大小,一切都很好。但是当谈到渲染实际图像时,我已经设置了一个矩形轮廓来了解图像的位置,但框中没有图像。

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

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是一个红色矩形。

2 个答案:

答案 0 :(得分:1)

nullptr来电中通过srcrect SDL_RenderCopy()

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

现在,如果x和/或y大于image_,SDL会将srcrect剪切到image_的范围,最后会为空直接,什么都不做。

示例:

#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供参考:

red.png

答案 1 :(得分:0)

我不知道这是否仍然可行,但是当我使用SW加速时,使用SDL_RENDERER_ACCELERATED标志(在Linuix Mint下)会遇到问题,SDL_Render *可以工作。