C++ 使用Libtcod,如何控制台->;打印具有动态颜色量的字符串?
我有一个helper函数,它接受一个字符串和一个用于格式化字符串的颜色向量,现在我的解决方案是手动检查颜色向量的大小,并使用相同数量的颜色调用控制台打印 假设我有一个4的颜色向量,在代码中它会做如下的事情:C++ 使用Libtcod,如何控制台->;打印具有动态颜色量的字符串?,c++,libtcod,C++,Libtcod,我有一个helper函数,它接受一个字符串和一个用于格式化字符串的颜色向量,现在我的解决方案是手动检查颜色向量的大小,并使用相同数量的颜色调用控制台打印 假设我有一个4的颜色向量,在代码中它会做如下的事情: void helper_func(TCODConsole* con, std::string msg_str, std::vector<TCOD_colctrl_t> color_vector) { char* message = msg_str.c_str(); //t
void helper_func(TCODConsole* con, std::string msg_str, std::vector<TCOD_colctrl_t> color_vector)
{
char* message = msg_str.c_str();
//this is repeated 1 through 16, adding another color_vector.at(n) for each.
...
else if (color_vector.size() == 2)
//message might be "%cHello%c was in red"
console->print(x, y, message, color_vector.at(0), color_vector.at(1))
...
else if (color_vector.size() == 4)
//message might be "%cThe octopus%c shimmers at %cnight%c"
console->print(x, y, message, color_vector.at(0), color_vector.at(1), color_vector.at(2), color_vector.at(3))
...
}
void helper\u func(TCODConsole*con,std::string msg\u str,std::vector color\u vector)
{
char*message=msg_str.c_str();
//重复1到16次,为每个添加另一个颜色向量(n)。
...
else if(color_vector.size()=2)
//消息可能是“%cHello%c为红色”
控制台->打印(x,y,消息,颜色向量在(0),颜色向量在(1))
...
else if(color_vector.size()=4)
//消息可能是“%c八达通%c在%c灯光%c闪烁”
控制台->打印(x,y,消息,颜色向量在(0),颜色向量在(1),颜色向量在(2),颜色向量在(3))
...
}
虽然这是可行的,但它很糟糕,我正在寻找不同的方法来实现它,允许使用超过16种颜色,等等
我尝试对向量中的每种颜色执行sprintf
,将其添加到out_字符串并重复。我试着用ostringstream做同样的事情。我尝试在“%c”
上拆分msg_str,然后在向每个字符串中添加颜色后将结果字符串连接起来。它永远不会成功,总是要么使用第一种颜色,然后使用随机字符,而不是从那里开始使用颜色
我希望上述任何一项都能起作用,因为只要sprintf(out\u char,format\u msg,TCOD\u COLCTRL\u 1)
打印到控制台(使用console->print(out\u char)
)就可以了
我的问题是:有没有一种好方法可以将不同数量的颜色传递给console->print功能,并让它准确地显示这些颜色,而不会产生严重的代码冗余?
作为备用方案,我可以将字符串的一部分打印到第一种颜色,计算其大小,将
x
移动那么多,然后打印下一部分,但这并不理想
我想这个问题可以推广到对带替换的常规
printf
也提出同样的问题。变量函数的一个可能替代方法可能涉及解析“%c”的msg_str,并根据颜色向量以正确的颜色迭代打印字符串的每个段。我不确定下面的代码是否可以编译——我是在记事本中编写的,所以可能需要做一些工作。希望你能理解我建议的要点
void helper_func(TCODConsole* con, std::string msg_str, std::vector<TCOD_colctrl_t> color_vector)
{
std::string str2;
std::size_t pos;
std::size_t pos2;
pos = msg_str.find("%c");
if (pos != std::string::npos)
str2 = msg_str.substr(0,pos);
else
str2 = msg_str;
console->print(x, y, str2.c_str());
int n = 0;
while (pos != std::string::npos) {
pos2 = msg_str.find("%c",pos+1);
if (pos2 != std::string::npos)
str2 = msg_str.substr(pos+2,pos2);
else
str2 = msg_str.substr(pos2+2,msg_str.length()-pos2+2);
console->print(x, y, str2.c_str(),color_vector.at(n));
pos = pos2;
n++;
}
}
void helper\u func(TCODConsole*con,std::string msg\u str,std::vector color\u vector)
{
std::字符串str2;
标准:尺寸和位置;
标准:尺寸位置2;
pos=msg_str.find(“%c”);
if(pos!=std::string::npos)
str2=消息(0,位置);
其他的
str2=msg_str;
控制台->打印(x,y,str2.c_str());
int n=0;
while(pos!=std::string::npos){
pos2=msg_str.find(“%c”,pos+1);
if(pos2!=std::string::npos)
str2=信号序列子序列(位置+2,位置2);
其他的
str2=msg_str.substr(pos2+2,msg_str.length()-pos2+2);
控制台->打印(x,y,str2.c_str(),color_vector.at(n));
pos=pos2;
n++;
}
}
我想我应该提到,我的代码中有一个问题。第二个print语句中的x值每次都需要通过while循环计算,因为x随着pos2的变化而变化。否则,所有内容都将保持在同一位置打印。:)这应该是一个简单的改变…我相信你要找的标题类似于:“如何使用向量调用变量函数?”例如,ref,,谢谢,看起来有几个不同的问题与这个概念相匹配。我会看一看。@user2864740试图理解这个答案,但我不知道
mylist[1]
的数组在这里是如何应用的。在我的例子中,它更像是mylist[16]
。看起来会有帮助,但我不确定我错在哪里。我想,这里的评论不是询问此事的方式。请参阅(我甚至不确定该操作是否为va_列表黑客“定义良好”)