tags:

views:

62

answers:

3

I have this:

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

But for 60 buttons. Is there a way how to do something like:

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

}
+2  A: 

You need to use an array.

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

Arrays always start at 0, and this particular one ends at 59 giving a total of 60 elements.

Vincent McNabb
Arrays always are referenced by their name, not their type. Let me fix that for ya :-)
paxdiablo
Haha, oops :-) Thanks.
Vincent McNabb
A: 
Opera
A: 

It appears your entire function could be replaced with the following array-based variation. Assuming that your nrBtnXX variables are defined outside the function and you want to minimise the scope of changes, you should look at something like:

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

The idea is to store everything in arrays so that you don't have to deal with individual variables. If the nrBtn variables are required to be a non-array, then I would set up a single array of pointers to them so this approach would still work, something like:

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

You should also set the formulae intelligently for the x and y coordinates since you probably don't want a 60x1 matrix for them. Look into making it 12x5 or something equally as compact.

paxdiablo