TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 227 subscribers
Post #25859 5
Which API design should my framework have?

right now in vexa(my game/graphics library) how you initialize window with a renderer is rather straightforward:

```cpp
\#include <vexa/vexa.hpp>

using namespace vexa;

int main()
{
Engine::Init(Engine::VIDEO);

Window window = Window().setSize({1280, 720}).setRenderer({}).create();

// ...

```
YES, it uses builder pattern, not very C++-ish but i like it.
However, there are two problems with it.
first one is that usually you have builder struct building to target type so that you dont need something like `.create()` that creates classic `if (m_created) ...` problem a.k.a two phase construction. but bigger problem that caused me two want to switch to this API style is about memory management. and it is now C++'s problem.. Even rust cant handle it, because this is what you tell it to do.
so lets dont talk too much:

```cpp

Engine::Init(Engine::FONT); // calls `TTF_Init();` by SDL_TTF behind the scenes

Font font = Font::Load("...");
...
Engine::Quit();
```

A Font has destructor running on scope exit. which means before font object gets destroyed on '}' Engine::Quit() so TTF_Quit() runs. You see, when Font object gets unloaded at scope end the subsystem is not live anymore, we get a segfault. to fix this i've made Engine have a Guard struct which does Engine::Quit() automatically. But then decided that this is not i want for my API. I would priorotize explicitness for the master deinitialization.

to fix this im thinking of new API design style:
```cpp
Window window = Engine::BuildWindow().setSize({1280, 720}).setRenderer().create();
```

where BuildWindow() returns WindowBuilder not a Window, unlike the previous(so bad) choice.
for fonts, as you might already guess:

```cpp
Font font = Engine::LoadFont("...");
```

which creates a font and returns reference to it, resulting Engine to know about font count and Quit or not based on that.

code: https://github.com/monjaris/vexa

https://redd.it/1x8gtg7
@r_cpp
GitHub GitHub - monjaris/vexa: Modern cross-platform game/graphics framework written in C++23 Modern cross-platform game/graphics framework written in C++23 - monjaris/vexa
More from @r_cpp
  1. Oct 17, 2026Please consider bundling C++ STL with your application Systems targeted by C++ toolchains…
  2. Oct 17, 2026A C++ Trick Question - Blog Post https://greenlightning.eu/programming/cpp-trick-question/…
  3. Oct 17, 2026A Cross Toolkit Animation Timeline Control https://www.youtube.com/watch?v=131P4BNCTbI htt…
  4. Oct 17, 2026C++ Under the sea conference I just attended the C++ Under the Sea 2-day conference. I rea…
  5. Oct 17, 2026"C++23 - The Complete Guide" now as Paperback C++23 - The Complete Guide is now also avail…
  6. Oct 17, 2026A quick C++ standard container overview https://meetingcpp.com/blog/items/A-quick-Cpp-stan…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →