gpt4 book ai didi

c++ - 获取临时 SDL_Rect 的地址

转载 作者:行者123 更新时间:2023-11-28 05:13:41 25 4
gpt4 key购买 nike

我正在尝试通过类为 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
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());

关于c++ - 获取临时 SDL_Rect 的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43080912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com