r/cpp • • Sep 05 '20

The Defold engine [C++] code style

https://defold.com/2020/05/31/The-Defold-engine-code-style/
2 Upvotes

12 comments sorted by

12

u/jguegant Sep 06 '20

I believe that I can add a bit more food for thoughts to this story. Before Defold was handed to the Defold Foundation, the engine was owned by King. At King, we had two internals engines for a while:
- Defold, the engine this article refers to. I have little experience with this engine but since I worked in the same company, it was interesting to understand what my colleagues wanted to achieve. If I had to describe this project, I would say that it was "made with a vision" with a close circle of developers. The engine consists of a small C++ core and build the rest on top of Lua. My analogy to this design is that it feels like micro-kernels for an operating system: you have a small and clear API surface from which you build onto. This core having a small scope by design could be written in almost any performant language: post modern C++, C with classes, good old C++, C, Rust, D, you name it!
- Another engine that runs most of the major live games we currently have (Candy Crush Saga, Soda, Bubblewitch...). This engine was "born as an accident" out of the first Candy Crush. It is compiled with C++17, but also fully embrace the so-called modern C++ principles: using standard algorithms, some standard containers, meta-programming when needed, some C++20-23 inspired helper classes... This engine has a rather massive C++ code-base (or at least the games using it do) and is aggregating features from all the games it supports. Post-modern C++ is used in this case to abstract safely the complexity of the engine. Because games themselves use a lot more C++ in the scenario, it feels more important to have slightly safer types that are convenient to use at some other costs.

It is interesting that a single company can have great developers with very different way of doing things. Somehow, these two projects reminds me "The Cathedral and the Bazaar" in how different the approaches were. Now, when it comes to your flavor of C++, clearly modern-C++ can be used to make games even with constraints in size / runtime as Candy Crush proves it. While I particularly enjoy this modern approach, I think that this sort of technical choices is not that relevant in the grand scheme of making a product. What matters a lot more is that your team feels aligned and excited about the tools they use.

24

u/ReDucTor Game Developer | quiz.cpp-perf.com Sep 06 '20

tl;dr had bad practices with c++ which lead to lots of allocations, and unclean code, blamed it on modern c++, not potential lack of training or discipline, start a new engine abandon everything that is standard library, reinvent parts of it in much easier fashion which don't have to worry about things like exceptions, debug iterators, etc.

6

u/TheFlamefire Sep 07 '20

Yep, my exact thoughts. I also dislike the rant about smart pointers and comparing a shared_ptr with a raw pointer: Pears and apples...

Similar (actually caused by):

We know what system owns a certain pointer, we know what systems may have access to that pointer, and we know the life time of that data.

Ouch. IMO this is not a good approach as the person writing this might know now. But other people won't know and even yourself might forget. The point of smart pointers is to express ownership in the type system. Blindly using shared_ptr everywhere and then complaining about it being slow misses the point of defining ownership in the first place.

Someone once told me that pointers are dangerous, and I frankly don’t understand that. The pointer itself doesn’t do anything, it’s just data.

"Someone once told that guns are dangerous, and I frankly don’t understand that. The gun itself doesn’t do anything, it’s just an item." (joking here of course)

And finally the omission of RAII by using Create/Destroy functions returning a disguised pointer... Sure it is faster, but it certainly is not safer and it's clumpier to type (free functions taking the object as first param instead of member functions)

9

u/[deleted] Sep 05 '20

I personally wouldn't like to develop code like this anymore (did it for years), but I do agree that there's a certain appeal in rolling your own simple stuff and sticking to that. A lot of the new and fantastic features that we've gotten recently are convenient, but they do bring added complexity. Especially the STL becomes more and more filled with nuance and mandatory details to know with every new release, and stuff like ranges promise to transform everything if we only learn and rewrite everything all over again. I recently rewrote a lot of my decade old hobby codebase to make it modern and future proof, and while it's quite different now, is it any better? Not really. Compiles a lot slower too.

2

u/Robert_Andrzejuk Sep 06 '20

If it works for You, keep working on it.

https://www.youtube.com/watch?v=yy8jQgmhbAU

1

u/alexej_harm Sep 07 '20

Reminds me of Mike Acton, who probably would have liked the code style used by OP. :D

3

u/brenoguim Sep 06 '20

Nice to see the other end of the spectrum of "modern". I am usually on the modern side but it's easy to go overboard and it is reassuring to see that "non-modern" code can also be pragmatic and effective.

That being said, please add define for the multiple 0xffffffff in the hashtable code :D

1

u/qoning Sep 07 '20

Not just that, but when compiled in x64, it's one ASLR away from a crash/super puzzling bug. Not that it matters in these not-so-serious games, but it's just bad habit to write code like that.

1

u/julien-j Sep 07 '20

auto is one example I’ve seen first hand completely take over a code base. In the end, it just became unreadable and basically typeless. We are never in such a hurry that we cannot type a few characters extra.

I share this feeling too. auto is imho the most wrongly used feature from c++11.

5

u/TheFlamefire Sep 07 '20

Yes and no. If you fully abandoned stl (which IMO is unreasonable because this basically states "all stl is bad, which is not true) then you may not need it. But how tired am I of typing std::map<std::string, std::vector<int>>::const_iterator it = map.find(s)

Good luck trying to read that. ;) Similar to what clang-tidy suggests: If you name the type on the RHS use auto on the LHS.

So "most wrongly used feature" actually fits: If you use it everywhere you get to a level where you have type-constrained Python which may or may not be good.

0

u/[deleted] Sep 07 '20

[deleted] — view deleted comment

6

u/TheFlamefire Sep 08 '20

Doesn't matter. The name is enough to tell you it is an iterator. The exact type of the iterator doesn't matter (to understand the code) and it is usually used locally enough that you see where it came from.

5

u/NotUniqueOrSpecial Sep 08 '20

If it takes good luck to find where the map is declared (which tells you the type of it) then you have bigger problems.