簡體   English   中英

SDL 2.0放大紋理

[英]SDL 2.0 enlarge texture

為了制作按鈕,我以這種方式創建和渲染紋理:

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

問題是我得到的紋理大小等於標簽之一。 如何在不拉伸紋理像素的情況下增加紋理像素的大小,即在其側面添加空白?

就像是

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的填充大小,您需要自己選擇合適的東西。

找到了一種方法。 要放大紋理,可以創建代表按鈕背景的表面,然后將它們組合:

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);
    SDL_Surface* back = SDL_CreateRGBSurface(0, surface->w+10, surface->h+10,
                                            32, 0, 0, 0, 0);// create a black background
    SDL_Rect t = {5, 5, back->w, back->w}; // place in a background to place label
    SDL_BlitSurface(surface, NULL, back, &t); // combining surfaces
    btn.pos.w = back->w;
    btn.pos.h = back->h;

    btn.texture = SDL_CreateTextureFromSurface(renderer, back);
    SDL_FreeSurface(surface);
    return btn;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM