gpt4 book ai didi

c++ - SDL 2.0放大纹理

转载 作者:行者123 更新时间:2023-11-30 16:57:29 25 4
gpt4 key购买 nike

为了制作按钮,我以这种方式创建和渲染纹理:

typedef struct{
SDL_Rect pos;
SDL_Texture* texture;
int hovered;
} button;

button getButton(int x, int y, char * label, TTF_Font* font, SDL_Color color){
button btn;
btn.hovered = false;
btn.pos.x = x;
btn.pos.y = y;
SDL_Surface* surface = TTF_RenderText_Solid(font, label, color);
btn.pos.w = surface->w;
btn.pos.h = surface->h;
btn.texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return btn;
}

void drawButton(button btn){
SDL_RenderCopyEx( renderer, btn.texture, NULL, &btn.pos, 0, NULL, SDL_FLIP_NONE);
if(btn.hovered){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0x00);
SDL_RenderDrawRect(renderer, &btn.pos);
}

问题是我得到的纹理大小等于标签之一。如何在不拉伸(stretch)纹理像素大小的情况下增加纹理像素大小,即在其侧面添加空白?

最佳答案

类似

void drawButton(button btn){
SDL_RenderCopyEx( renderer, btn.texture, NULL, &btn.pos, 0, NULL, SDL_FLIP_NONE);
if(btn.hovered){
int padding = 10;
SDL_Rect pos = {btn.pos.x - padding, btn.pos.y - padding,
btn.pos.w + 2*padding, btn.pos.h + 2*padding };
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0x00);
SDL_RenderDrawRect(renderer, &pos);
}
}

这样只有矩形的大小发生变化,显然我只是凭空拉出了 10 作为填充的大小,你需要自己选择合适的东西。

关于c++ - SDL 2.0放大纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39478973/

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