2010-07-30 60 views
0

我有這樣的:SDL/C++:如何使此功能變短(er)?

void showNumbers(){ 
    nrBtn1 = TTF_RenderText_Blended(fontnrs, "1", sdlcolors[0]); 
    nrBtn2 = TTF_RenderText_Blended(fontnrs, "2", sdlcolors[1]); 
    nrBtn3 = TTF_RenderText_Blended(fontnrs, "3", sdlcolors[2]); 
    nrBtn4 = TTF_RenderText_Blended(fontnrs, "4", sdlcolors[3]); 
    nrBtn5 = TTF_RenderText_Blended(fontnrs, "5", sdlcolors[4]); 
    nrBtn6 = TTF_RenderText_Blended(fontnrs, "6", sdlcolors[5]); 
    nrBtn7 = TTF_RenderText_Blended(fontnrs, "7", sdlcolors[6]); 
    nrBtn8 = TTF_RenderText_Blended(fontnrs, "8", sdlcolors[7]); 
    nrBtn9 = TTF_RenderText_Blended(fontnrs, "9", sdlcolors[8]); 

    SDL_Rect rcnrBtn1 = { 40, 32, 0, 0 }; 
    SDL_Rect rcnrBtn2 = { 70, 32, 0, 0 }; 
    SDL_Rect rcnrBtn3 = { 100, 32, 0, 0 }; 
    SDL_Rect rcnrBtn4 = { 130, 32, 0, 0 }; 
    SDL_Rect rcnrBtn5 = { 160, 32, 0, 0 }; 
    SDL_Rect rcnrBtn6 = { 190, 32, 0, 0 }; 
    SDL_Rect rcnrBtn7 = { 220, 32, 0, 0 }; 
    SDL_Rect rcnrBtn8 = { 250, 32, 0, 0 }; 
    SDL_Rect rcnrBtn9 = { 280, 32, 0, 0 }; 

    SDL_BlitSurface(nrBtn1, NULL, screen, &rcnrBtn1); SDL_FreeSurface(nrBtn1); 
    SDL_BlitSurface(nrBtn2, NULL, screen, &rcnrBtn2); SDL_FreeSurface(nrBtn2); 
    SDL_BlitSurface(nrBtn3, NULL, screen, &rcnrBtn3); SDL_FreeSurface(nrBtn3); 
    SDL_BlitSurface(nrBtn4, NULL, screen, &rcnrBtn4); SDL_FreeSurface(nrBtn4); 
    SDL_BlitSurface(nrBtn5, NULL, screen, &rcnrBtn5); SDL_FreeSurface(nrBtn5); 
    SDL_BlitSurface(nrBtn6, NULL, screen, &rcnrBtn6); SDL_FreeSurface(nrBtn6); 
    SDL_BlitSurface(nrBtn7, NULL, screen, &rcnrBtn7); SDL_FreeSurface(nrBtn7); 
    SDL_BlitSurface(nrBtn8, NULL, screen, &rcnrBtn8); SDL_FreeSurface(nrBtn8); 
    SDL_BlitSurface(nrBtn9, NULL, screen, &rcnrBtn9); SDL_FreeSurface(nrBtn9); 
} 

但是對於60層的按鈕。有沒有辦法如何做到這一點:

void showNumbers() 
{ 
    SDL_Rect rcnrBtn1 = { 40, 32, 0, 0 }; 
    SDL_Rect rcnrBtn2 = { 70, 32, 0, 0 }; 
    SDL_Rect rcnrBtn3 = { 100, 32, 0, 0 }; 
    SDL_Rect rcnrBtn4 = { 130, 32, 0, 0 }; 
    SDL_Rect rcnrBtn5 = { 160, 32, 0, 0 }; 
    SDL_Rect rcnrBtn6 = { 190, 32, 0, 0 }; 
    SDL_Rect rcnrBtn7 = { 220, 32, 0, 0 }; 
    SDL_Rect rcnrBtn8 = { 250, 32, 0, 0 }; 
    SDL_Rect rcnrBtn9 = { 280, 32, 0, 0 }; 


for(int x=1; x<=60;x++){ 
    nrBtn+x = TTF_RenderText_Blended(fontnrs, x, sdlcolors[x-1]); 
    SDL_BlitSurface(nrBtn+x, NULL, screen, &rcnrBtn+x); SDL_FreeSurface(nrBtn+x); 
} 

} 
+5

每當你命名變量'something1','something2'等時,你都有一個數組的主要候選者。 – GManNickG 2010-07-30 05:53:09

回答

3

你需要使用一個數組。

E.g.

SDL_Rect rcnrBtn[60]; 
for(int x = 0; x < 60; x++) { 
    rcnrBtn[x].x = 30 * x + 10; 
    rcnrBtn[x].y = 32; 
    rcnrBtn[x].w = 100; 
    rcnrBtn[x].h = 24; 
} 

陣列總是開始於0,這尤其是一個在59給出總共60種元素的結束。

+0

數組總是被它們的名字引用,而不是它們的類型。讓我解決這個問題:-) – paxdiablo 2010-07-30 06:05:35

+0

哈哈,oops :-)謝謝。 – 2010-07-30 06:26:30

0

你可以做這樣的事情:

void showNumbers() { 
    SDL_Surface* nrBtn = NULL; 
    int nbr = 1; 
    int x = 30; // your starting x 
    int i = 0; 
    for (; i < 60; ++i) { 
    nrBtn = TTF_RenderText_Blended(fontnrs, nbr_to_str(nbr), sdlcolors[i]); // You'll have to code nbr_to_str 
    SDL_Rect rect = {x, 32, 0, 0}; // Are you sure that width and height are 0 ? 
    SDL_BlitSurface(nrBtn, NULL, screen, &rect); 
    SDL_FreeSurface(nrBtn); 
    x += 30; 
    } 
    return; 
}
0

看來你的整個功能可以與下面的基於陣列的變化來代替。假設你nrBtnXX變量定義在函數外,你想改變的範圍最小化,你應該看看這樣的:

#define BUTTON_COUNT 60 
SDL_Surface *nrBtn[BUTTON_COUNT]; 

void showNumbers() { 
    char textNum[3]; 
    for (int i = 0; i < BUTTON_COUNT; i++) { 
     sprintf (textNum, "%d", i); 
     nrBtn[i] = TTF_RenderText_Blended(fontnrs, textNum, sdlcolors[i]); 
    } 

    SDL_Rect rcnrBtn[BUTTON_COUNT]; 
    for (int i = 0; i < BUTTON_COUNT; i++) { 
     rcnrBtn[i].x = 40 + i * 30; // use formulae for all these. 
     rcnrBtn[i].y = 32; 
     rcnrBtn[i].w = 0; 
     rcnrBtn[i].h = 0; 
    } 

    for (int i = 0; i < BUTTON_COUNT; i++) { 
     SDL_BlitSurface(nrBtn[i], NULL, screen, &rcnrBtn[i]); 
     SDL_FreeSurface(nrBtn[i]); 
    } 
} 

的想法是存儲在陣列中的一切,讓你不必處理個體變量。如果nrBtn變量需要是一個非數組,然後我會成立指向他們的單個陣列所以這種方法仍然工作,是這樣的:

SDL_Surface *nrBtnPtr[] = { &nrBtn1, &nrBtn2 ..., &nrBtn60 }; 

你也應該設定公式智能地爲xy座標,因爲您可能不需要他們的60x1矩陣。考慮把它製作成12x5或者同樣緊湊的東西。