r/cpp_questions • u/Pure_Function7183 • 4d ago
OPEN Is this possible?
I’m working on a game engine as a way to learn C++ and CMake. I’m attempting to create an editor for my engine, and I want to use ImGui. I’ve been going back and forth about whether I want ImGui on the engine or editor, but I think I want it on the engine because the engine owns the main loop, so it can just do the imgui frame lifecycle stuff. Also, if I ever decide to learn Vulkan and switch to it (unlikely), I don’t think I’d need to update the editor.
The problem with putting it on the engine is that the game will now have ImGui and this code running each frame, not actually doing anything. I’m sure this has little to no impact on performance, but I feel like it’s unnecessary?
Anyways, I’m wondering if it’s possible to make it so that if I run the editor, then the ImGui code runs otherwise, it’s stripped out. I know there is conditional compilation I just don’t know if that is something that can be done in this scenario?
5
u/Typical-NonPC 4d ago
Tis possible to separate it. I think the most beneficial thing for you to do is learn about software architecture.
For instance, what is an engine? Software is made up of distinct parts and it's up to you to figure out how to best set that up.
Does the 'engine' need to own the main loop?
Note that the editor and game are both 'running' using the same 'engine'. But is the engine the same on each app?
Note that a lot of things are set up as a program opens. So maybe for the app, you'd just register a scene. For the editor, you'd register a scene and a gui 'layer'.
4
u/Suitable_Plate4943 4d ago
your game may need to render text anyway
2
u/wrosecrans 4d ago
If you make your build configurable, you can make it an optional dependency. If a certain game doesn't make use of the GUI feature, set a preprocessor flag to disable it and don't link to it. Any code that is behind that preprocessor flag being defined won't get compiled.
It's just a question of how much extra engineering work like that you want to do segregating that stuff to avoid the overhead of linking to what is really not a particularly large dependency.
1
u/saxbophone 4d ago
If you're worried that a simple "if imgui enabled, then render imgui" check in your game loop is going to hurt performance when imgui is not active, I recommend you benchmark it to find out. I doubt this has the performance impact that you assume it does. Remember the software development maxim: Premature Optimisation is the root of all evil.
1
u/n1ghtyunso 4d ago
You likely don't really need to worry about this, and even if you do want to worry, you can also structure your runtime core loop such that the imgui code only runs with an editor attached anyway.
You might even find out that having imgui available for in-game dev tools is really nice after all.
That being said, unreal engine for example uses a macro WITH_EDITOR to conditionally compile some code and their build tool will define this for you automatically, based on the build mode.
1
u/Discordbothost 2d ago
Give the engine loop an optional draw callback and put the ImGui code in an editor-only .cpp. In CMake, add that file and link ImGui on the editor target. The game can use the same engine library and leave the callback empty.