2016-09-13 3 views
0

, 내가 만들고 방법 텍스쳐 렌더링 :SDL 2.0 확대 텍스처

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

문제는 내가 크기 레이블 중 하나에 해당 질감을 얻을입니다. 텍스처 픽셀 크기를 늘리지 않고 늘릴 수 있습니다. 즉, 텍스처 픽셀의 측면에 빈 공간을 추가하려면 어떻게해야합니까? 사각형의 변화의 크기,

+1

지금까지 시도한 것은 무엇입니까? 어떤 버전의 SDL? 그리고 단추를 렌더링하는 코드는 어디에 있습니까? –

+0

@ E_net4 편집 된 질문은 렌더링과 ver 모두를 나타냅니다. SDL의 어떤 아이디어로도 나올 수 없어서 아무 것도 시도하지 않았습니다. – osamurai

답변

0

뭔가

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)를 뽑아, 당신은 뭔가 자신을 적절한 선택하는 것이 좋습니다.

+0

답변 해 주셔서 감사합니다! 비슷한 방법에 대해 생각하고 있었는데, 마우스 움직임을 처리 할 때 문제가 생길 수 있으므로 다른 SDL_Rect 필드를 구조체에 추가해야합니다. – osamurai

+0

@osamurai 두 가지, 방금 게시 한 코드의 버그가 수정되었습니다. , 그리고 두 개, 아니, 버튼 구조체의'SDL_Rect'는 변경되지 않으므로 항상 같은 장소에서 렌더링됩니다. 버그는 내가 임시 SDL_Rect' – Spudd86

+0

(마우스 처리 함수에서)을 다음과 같이 거짓 코드를 사용하도록'SDL_RenderDrawRect()를 변경하는 것을 잊었습니다 : 'if ((x> = btn.pos.x && x < = btn.pos.x + btn.pos.w) && ...)) btn.hovered = true;'이것은 임시 변수를 복제해야 함을 의미합니다. – osamurai

0

방법이 있습니다. 단추의 배경을 나타내는 서페이스를 만드는 텍스처를 확대하려면 다음을 결합하십시오.

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