我得到了矛盾的答案,我应该如何处理我的代码?
我正在用 C++ 和 DirectX 制作 RPG 游戏。
我将游戏的所有数据存储在 .txt 文件中,并使用“ifstream/ofstream”读取/写入它。到目前为止,在谈论生物统计数据时,这对我来说效果很好,并且我对生物名称进行了一些修改,但这正在成为一个问题。
我可以将字符串存储在 txt 文件中并读取它们,但我在使用它们时遇到问题。对于单个单词,我有一个技巧,但现在我开始了解角色互相交谈的故事情节,这是一个真正的问题。
我在 gamedevelopment.stackexchange 上询问如何将文本放在屏幕上,并被告知使用 D3Dtext,但它只接受 C 样式字符串,而且我只能从文本文件中读取 C++ 字符串。现在这是一个大问题,我愿意回去重新考虑需要解决的问题,因为在解决这个问题之前无法取得任何进展。
所以现在我有很多问题,我不知道先问哪个:
我想要一种像图形一样绘制字母的方法。有人告诉我这就是 D3Dtext 所做的,但如果可以的话,我想自己实现它,我只需要有关如果有人知道怎么做的信息?
如果我要像所谓的专家建议那样使用 D3Dtext,我必须使用 C 样式字符串。那么如何将 C 风格的字符串转换为 C++ 字符串呢?我现在有一个方法,但需要对每个字符串使用 new 和 delete 运算符,随着复杂性的增加,我可以看到这是一个大问题?
有没有办法读取 C 风格的字符串?也许是 ifstream 的替代品。我想保留 txt 文件,因为我真的不想使用 xml,但如果它是可行的解决方案,我可以更改文件格式?
过早的优化我知道,但我计划对游戏中的每一段文本使用相同的函数,那么在速度方面这样做的好方法是什么(为什么我不想为每个字符串创建新/删除)?
我很乐意提供任何需要帮助我的信息,尽管询问。
I am making a RPG game in C++ and DirectX.
I store all the data for the game in .txt files and read/write it using `ifstream/ofstream. this has worked well for me so far when talking about creature stats and I have a hack together for creature names but this is becoming a problem.
I can store strings in the txt file and read them but I am having trouble using them. for single words I have a hack but now I am up to the story line where characters are talking to each other it is a real problem.
I asked on gamedevelopment.stackexchange how to put text on screen and was told to use D3Dtext but that only accepts C-style strings and I can only read C++ strings from the text file. This is such a big problem now that I am willing to go back and re-factor what need sit as no progress can be made until this is sorted.
So now I have a bunch of questions and I dont know which to ask first:
I want a way to draw the letters like graphics. I was told this is what D3Dtext does but I want to implement it my self if I can I just need info on how if someone knows?
If I am to use D3Dtext like so called experts advise I have to use C-style strings. so how can I convert C-style strings to C++ strings? I have a method now but that requires the new and delete operator for every string and I can see the being a big problem as it grows in complexity?
Is there a way to read C-style strings? Maybe a replacement for ifstream. I would like to keep the txt files as I really dont want to use xml but I could change the file format if it was a viable solution?
Premature optimisation I know but I plan to use the same function for every piece of text in the game so what would be a good way of doing this in terms of speed (why I dont want new/delete for every string)?
I am happy to provide any information that would be needed to help me, just ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要如上所述访问 c 字符串,请按照当前正在执行的操作读取您的文件。
Read your file as you are currently doing then if you need to access the c strings as above.
您可以在 C 样式字符串和 C++ 的
std::string
之间自由转换。只需使用 my_cpp_string.c_str() 获取 C++ 字符串的 C 字符串表示形式,并使用 std::string my_cpp_string(my_c_string) 初始化新的 std::string来自 C 风格的字符串。You can convert freely between C-style strings and C++'s
std::string
. Just usemy_cpp_string.c_str()
to get the C-string representation of a C++ string, andstd::string my_cpp_string(my_c_string)
to initialize a new std::string from a C-style string.2) 使用
c_str()
方法将 C++ 字符串传递给 D3Dtext3 和 4,然后就不再有问题了。
2) Use the
c_str()
method to pass your C++ strings to D3Dtext3 and 4 then become non-issues.