首页 文章

获取临时SDL_Rect的地址

提问于
浏览
0

我试图通过类为SDL中的文本创建一个更简单的容器 . 该类应该包含一个指向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

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

    SDL_Rect *getRect()
        {
            return ▭
        }
    

    然后你可以像这样调用 SDL_RenderCopy

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

相关问题