我得到了矛盾的答案,我应该如何处理我的代码?

发布于 2024-11-29 16:11:25 字数 892 浏览 1 评论 0原文

我正在用 C++ 和 DirectX 制作 RPG 游戏。

我将游戏的所有数据存储在 .txt 文件中,并使用“ifstream/ofstream”读取/写入它。到目前为止,在谈论生物统计数据时,这对我来说效果很好,并且我对生物名称进行了一些修改,但这正在成为一个问题。

我可以将字符串存储在 txt 文件中并读取它们,但我在使用它们时遇到问题。对于单个单词,我有一个技巧,但现在我开始了解角色互相交谈的故事情节,这是一个真正的问题。

我在 gamedevelopment.stackexchange 上询问如何将文本放在屏幕上,并被告知使用 D3Dtext,但它只接受 C 样式字符串,而且我只能从文本文件中读取 C++ 字符串。现在这是一个大问题,我愿意回去重新考虑需要解决的问题,因为在解决这个问题之前无法取得任何进展。

所以现在我有很多问题,我不知道先问哪个:

  1. 我想要一种像图形一样绘制字母的方法。有人告诉我这就是 D3Dtext 所做的,但如果可以的话,我想自己实现它,我只需要有关如果有人知道怎么做的信息?

  2. 如果我要像所谓的专家建议那样使用 D3Dtext,我必须使用 C 样式字符串。那么如何将 C 风格的字符串转换为 C++ 字符串呢?我现在有一个方法,但需要对每个字符串使用 new 和 delete 运算符,随着复杂性的增加,我可以看到这是一个大问题?

  3. 有没有办法读取 C 风格的字符串?也许是 ifstream 的替代品。我想保留 txt 文件,因为我真的不想使用 xml,但如果它是可行的解决方案,我可以更改文件格式?

  4. 过早的优化我知道,但我计划对游戏中的每一段文本使用相同的函数,那么在速度方面这样做的好方法是什么(为什么我不想为每个字符串创建新/删除)?

我很乐意提供任何需要帮助我的信息,尽管询问。

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:

  1. 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?

  2. 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?

  3. 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?

  4. 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 技术交流群。

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

发布评论

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

评论(3

咋地 2024-12-06 16:11:25
std::string mystr = "Hello World.";
mystr.c_str(); // gets a null terminated const char* C-style string

如果您需要如上所述访问 c 字符串,请按照当前正在执行的操作读取您的文件。

std::string mystr = "Hello World.";
mystr.c_str(); // gets a null terminated const char* C-style string

Read your file as you are currently doing then if you need to access the c strings as above.

残疾 2024-12-06 16:11:25

您可以在 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 use my_cpp_string.c_str() to get the C-string representation of a C++ string, and std::string my_cpp_string(my_c_string) to initialize a new std::string from a C-style string.

涙—继续流 2024-12-06 16:11:25

2) 使用 c_str() 方法将 C++ 字符串传递给 D3Dtext

some_D3Dtext_function(some_text.c_str())

3 和 4,然后就不再有问题了。

2) Use the c_str() method to pass your C++ strings to D3Dtext

some_D3Dtext_function(some_text.c_str())

3 and 4 then become non-issues.

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