Search code examples
c++pointerssdl

Taking address of temporary SDL_Rect


I am trying to make a simpler container for text in SDL by way of classes. The class is supposed to contain a pointer to an SDL_Texture and an SDL_Rect as well as some methods to get the information from the instances of the class. My problem arises when I try to blit the texture to the screen with the following function:

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

The compiler brings my attention to the fourth parameter and states the following:

error: taking address of temporary [-fpermissive]

The code for my class is:

//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};
        }
};

The code for my main program is:

//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);

I have also tried instantiating my object like this:

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

But I still get the same error. I suspect it is to do with the SDL_Rect not being a pointer.

Thanks in advance!


Solution

  • A simple solution could be to change the signature/implementation of getRect as follows:

        SDL_Rect *getRect()
        {
            return ▭
        }
    

    Then you can call SDL_RenderCopy like this:

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