SDL / C++:如何使这个函数简短(呃)?

发布于 2024-09-11 22:30:30 字数 2448 浏览 5 评论 0原文

我有这个:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸 2024-09-18 22:30:30

您需要使用数组。

例如,

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 个元素。

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.

可爱咩 2024-09-18 22:30:30

你可以这样做:

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

You could do something like this :

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;
}
淡紫姑娘! 2024-09-18 22:30:30

看来您的整个函数可以替换为以下基于数组的变体。假设您的 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 或同样紧凑的尺寸。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文