SDL游戏之路(二十)--对话框-多行

时间:2020-12-03 12:05:34


对话框-多行

前面实现了,单行内容显示。如果要实现多行,怎么弄呢?

多行测试的例子:

void button_show_dialog(void * pData, SkButton * pButton) {
SkDialogInfo * pDialog = new SkDialogInfo();
SkConfXml conf;
conf.init("conf/view/ViewFirst.DlGameInfo.conf.xml");
pDialog->initInfo("游戏发行公司:GuuD\n测试对话\n\n框", conf);
g_SkDialogManager.addDialog(pDialog);
}


编写一个获取多行surface的实现:

SDL_Surface * SkWord::getMultilineSurface(TTF_Font *font, const char *text,
SDL_Color fg) {
vector<string> vctStr;
g_SkString.getVctStr(vctStr, text, "\n", false);
if (vctStr.size() > 1) {
Sint32 totalHeight = 0;
Sint32 maxWidth = 0;
vector<SDL_Surface *> surfaces;
for (size_t i = 0; i < vctStr.size(); i++) {
SDL_Surface * partialSurface = TTF_RenderUTF8_Solid(font,
(vctStr.at(i) + " ").c_str(), fg);
if (partialSurface == NULL) {
continue;
}
surfaces.push_back(partialSurface);
totalHeight += partialSurface->h;
maxWidth = std::max(maxWidth, partialSurface->w);
}

SDL_Surface * surface = SDL_CreateRGBSurface(SDL_SWSURFACE, maxWidth,
totalHeight, 8, 0, 0, 0, 0);
SDL_Palette* palette;
palette = surface->format->palette;
palette->colors[0].r = 255 - fg.r;
palette->colors[0].g = 255 - fg.g;
palette->colors[0].b = 255 - fg.b;
palette->colors[1].r = fg.r;
palette->colors[1].g = fg.g;
palette->colors[1].b = fg.b;
SDL_SetColorKey( surface, SDL_TRUE, 0 );
int aligment = 0;
totalHeight = 0;
for (size_t i = 0; i < surfaces.size(); i++) {
SDL_Rect dst = { 0, totalHeight, 0, 0 };
switch (aligment) {
case 1: //right
dst.x = maxWidth - surfaces[i]->w;
break;
case 2: //center
dst.x = (maxWidth - surfaces[i]->w) / 2;
break;
case 3: //left
default:
break;
}
totalHeight += surfaces[i]->h;
SDL_BlitSurface(surfaces[i], NULL, surface, &dst);
SDL_FreeSurface(surfaces[i]);
}
return surface;
} else {
return TTF_RenderUTF8_Solid(font, text, fg);
}
}


效果如下:

SDL游戏之路(二十)--对话框-多行