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);
}
}
Chaque fois que vous nommez les variables 'something1',' something2', etc., vous avez un premier candidat pour un tableau. – GManNickG