SDL / C ++:如何使这个功能变短(呃)?

时间:2010-07-30 05:50:12

标签: c++ sdl

我有这个:

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

}

3 个答案:

答案 0 :(得分: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个元素。

答案 1 :(得分: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;
}

答案 2 :(得分: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或类似于紧凑的东西。