简体   繁体   English

在程序C ++中更改包含/目标文件

[英]Changing include/object files mid program C++

Me and some buddies are trying to construct a game engine that can run multiple games in c++. 我和一些伙伴正在尝试构建一个可以在c ++中运行多个游戏的游戏引擎。 The games are similar enough that they all share similar mechanics and interfaces that I want to make some files that apply to each game. 这些游戏非常相似,以至于它们都共享相似的机制和界面,因此我想制作一些适用于每个游戏的文件。 Many of the source files will be the same like our GUI's or Game Engine files. 许多源文件将与我们的GUI或Game Engine文件相同。 However, some files that deal with individual game mechanics will change from game to game. 但是,某些处理各个游戏机制的文件会因游戏而异。 If this expands to a large library, I don't want to simply include every file from every library to play one game. 如果扩展到一个大型库,我不想简单地包含每个库中的每个文件来玩一个游戏。 I also don't want to have a duplicate of GUI and game engine files in every folder in the library. 我也不想在库中的每个文件夹中都有GUI和游戏引擎文件的副本。

I am currently thinking about the idea of a launcher that will change the #include tags to match the choice made by the launcher, although this may require recompiling. 我目前正在考虑一个启动程序的想法,该程序将更改#include标签以匹配该启动程序所做的选择,尽管这可能需要重新编译。 Is there a way to make this/ is it a good technique, or is their a better way. 有没有办法使这个/是一种好技术,还是他们更好的方法? (keep in mind i'd prefer int main to remain in game engine/ a shared file, and not make a main for each game) (请记住,我宁愿将int main保留在游戏引擎/共享文件中,而不是为每个游戏制作main)

Summarizing your requirements: 总结您的要求:

  • You have some game engine code that is sufficiently general to be shared between several different games; 您有一些通用的游戏引擎代码,可以在多个不同的游戏之间共享;
  • However the engine depends also on some game specific code that is specific to each game; 但是,引擎还取决于每个游戏特有的某些游戏特有代码。
  • You currently solve this, hardwiring the game specific behavior at compile time; 您目前可以解决此问题,在编译时就严格按照游戏的特定行为进行操作;
  • You'd like a solution where it would be easier to share the game engine between different games, for example to bundle several games an let the user chose at startup (through a launcher). 您想要一个解决方案,在该解决方案中,可以更轻松地在不同游戏之间共享游戏引擎,例如将多个游戏捆绑在一起,让用户在启动时选择(通过启动器)。

If this is correct, here some thoughts to help you further: 如果正确,这里有一些想法可以进一步帮助您:

  • There's no way to elegantly change compile time elements (the includes) at run time. 无法在运行时优雅地更改编译时元素(包括)。
  • Building a launcher that accesses all the source code to recompile on the flow seems not to be a valid solution (because the user would need to have a compiling environment preinstalled, with all the libraries, dependencies, and toolchains installed, and anyway, it would take too long to launch the game). 构建访问所有源代码以在流上进行重新编译的启动器似乎不是有效的解决方案(因为用户需要预先安装编译环境,并安装所有库,依赖项和工具链,无论如何,它将需要太长时间才能启动游戏)。
  • The right approach would then to make more use of polymorphism , and refactor your game engine to rely on encapsulated game specific objects (using a clear interface, and keeping the coupling as low as possible, for example using SOLID design). 正确的方法将更多地利用多态性 ,并重构您的游戏引擎以依赖封装的游戏特定对象(使用清晰的界面,并保持尽可能低的耦合,例如使用SOLID设计)。
  • Once such degree of polymorphism is achieved, you can at game startup injecting game specific elements into the engine (for example an abstract game factory that would let the engine initiate families of related game objects without having to know the game specifics). 一旦达到了这种程度的多态性,您就可以在游戏启动时将特定于游戏的元素注入引擎(例如, 一个抽象游戏工厂 ,该工厂可以让引擎启动相关游戏对象的族,而不必了解游戏细节)。 Up to you to chose whether having a separate main for each game (compile time choice of game), or leaving the user choose between several ones at startup (run time choice). 由您决定是为每个游戏选择一个单独的主游戏(选择游戏的编译时间),还是在启动时让用户在多个游戏之间进行选择(运行时选择)。
  • Alternatively, you could also architect your system around a clear API between the game engine, and a dynamic library (eg DLL under windows), that is dynamically loaded at run-time (and which could then load newly delivered games, packaged as DLL+game assets, and added to the already installed games when needed. ( John recommended this nice link in the comments, there's also this one , and certainly a couple of others. But be aware that this is a level of additional difficulty that needs to understand very well the limitations of DLL - so maybe keep this as a later step for your refactoring) 另外,您也可以围绕游戏引擎和动态库(例如Windows下的DLL)之间的清晰API构建系统,该库在运行时动态加载(然后可以加载打包成DLL +的新发行游戏)游戏资源,并在需要时添加到已安装的游戏中。( 约翰在评论中建议使用此链接 ,其中还有一个 ,当然还有其他几个。但是请注意,这是需要理解的额外难度DLL的局限性很好-因此请将其保留为以后的重构步骤)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM