2010-07-30 2 views
0

J'ai ceci:SDL/C++: Comment rendre cette fonction courte (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); 
} 

Mais pour 60 boutons. Y at-il un moyen de faire quelque chose comme:

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

Chaque fois que vous nommez les variables 'something1',' something2', etc., vous avez un premier candidat pour un tableau. – GManNickG

Répondre

3

Vous devez utiliser un tableau.

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

Les tableaux commencent toujours à 0, et celui-ci se termine à particulier 59 donnant un total de 60 éléments.

+0

Les tableaux sont toujours référencés par leur nom et non par leur type. Laissez-moi résoudre ce problème pour vous :-) – paxdiablo

+0

Haha, oops :-) Merci. –

0

Vous pouvez faire quelque chose comme ceci:

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

Il apparaît toute votre fonction pourrait être remplacée par la variation à base de tableau suivant. En supposant que vos nrBtnXX variables sont définies en dehors de la fonction et que vous voulez minimiser l'ampleur des changements, vous devriez regarder quelque chose comme:

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

L'idée est de stocker tout dans les tableaux afin que vous n'avez pas traiter avec des variables individuelles. Si les nrBtn variables sont nécessaires être un non-tableau, alors je mis en place un seul tableau de pointeurs vers eux pour cette approche fonctionne encore, quelque chose comme:

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

Vous devez également définir les formules intelligemment pour les coordonnées x et y puisque vous ne voulez probablement pas de matrice 60x1 pour eux. Cherchez à en faire 12x5 ou quelque chose d'aussi compact.

Questions connexes