获取临时SDL_Rect的地址

时间:2017-03-28 22:14:05

标签: c++ pointers sdl

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

//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不是指针有关。

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

    SDL_Rect *getRect()
    {
        return ▭
    }

然后你可以这样打电话给SDL_RenderCopy

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