我试图通过类为SDL中的文本创建一个更简单的容器。该类应该包含一个指向SDL_Texture和SDL_Rect的指针,以及一些从类实例中获取信息的方法。当我尝试使用以下功能将纹理涂抹到屏幕上时,出现了我的问题:

//Assume that renderer is already created
SDL_RenderCopy(renderer, texture.getTexture(), NULL, &texture.getRect());


编译器使我注意到第四个参数,并声明以下内容:

error: taking address of temporary [-fpermissive]


我班上的代码是:

//Class
class Texture
{
    private:
        SDL_Texture* texture;
        SDL_Rect        rect;
    public:
        Texture(){/*Don't call any of the functions when initialized like this*/}
        Texture(SDL_Texture* texure)
        {
            this->texture = texture;
        }
        SDL_Texture* getTexture()
        {
            return texture;
        }
        SDL_Rect getRect()
        {
            return rect;
        }
        void setRect(int posX, int posY, int scale, SDL_Texture* texture)
        {
            int textW = 0;
            int textH = 0;
            SDL_QueryTexture(texture, NULL, NULL, &textW, &textH);
            this->rect = {posX,posY,textW*scale, textH*scale};
        }
};


我的主程序的代码是:

//Main Program
TTF_Font* font = TTF_OpenFont("./PKMNRSEU.FON", 17);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

Texture texture(renderText(font, "Hello", white, renderer));
texture.setRect(100, 100, 5, texture.getTexture());

bool running = true;

Uint32 startingTick;
SDL_Event event;

while (running)
{
    startingTick = SDL_GetTicks();
    while (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
        {
            running = false;
            break;
        }
    }

    SDL_RenderCopy(renderer, texture.getTexture(), NULL, &texture.getRect());
    SDL_RenderPresent(renderer);

    clockTick(startingTick);
}
SDL_DestroyRenderer(renderer);

TTF_CloseFont(font);


我也尝试过实例化我的对象,如下所示:

Texture* texture = new Texture(renderText(font, "Hello", white, renderer));


但是我仍然遇到同样的错误。我怀疑这与SDL_Rect不是指针有关。

提前致谢!

最佳答案

一个简单的解决方案可能是更改getRect的签名/实现,如下所示:

    SDL_Rect *getRect()
    {
        return ▭
    }


然后,您可以像这样调用SDL_RenderCopy

SDL_RenderCopy(renderer, texture.getTexture(), NULL, texture.getRect());

10-04 14:02