Hacker Newsnew | past | comments | ask | show | jobs | submit | gregstula's commentslogin

Hi, I decided to get back into C++ by making a project with a clear criteria. This project uses some C++17 features like structured binding so a modern-ish compiler is required.

This is my first Show HN post, so feedback and code review is more than welcome!


It does get easier to read and code over time. As an aside, the C++ in this post is pretty much as elegant as it gets.


They do not have garbage collection. You're conflating game development with game engine development. When a big powerful, flexible, C++ engine has been written for you, you can use C# or JavaScript or BluePrints to use the engine to make a game.


Do you know why Microsoft rewrote Minecraft in C++?

Games can certainly be successful in spite of performance problems, but why purposefully introduce them in the first place?

In the case of Notch, he used Java because he was most skilled at Java programming. The garbage collection, procedural generation, and multi-platform availability of the JVM allowed him to successfully release a massive hit as a solo indie developer.

At least that's one way to interpret it. Another way is that, had Notch been as skilled in C++ or (these days) Rust he could have made just as good of a cross platform game (even more so on mobile), that didn't concern itself with resource management outside of RAII and reference counting, was way more performant (no pauses), and in similar time frame as a skilled Java developer.

If you have a great idea use your favorite language. No doubt about that. You might be successful in spite of its specific deficiencies. However, if you are seeking a new language for a certain problem domain - like video games - choosing one with a feature that actively fights one of your fundamental goals (consistent framerate is only going to be a decision your users will regret you made.

It's funny that people like to say "users don't care what language it's written in". The fact that 8th graders know the the minecraft's garbage collector is a problem, suggests that using a garbage collected language for soft-realtime video games results in a seriously leaky abstraction.


Minecraft is a special case, as the amount of voxels in memory and the constant loading and deloading of blocks of the map every few seconds, means that memory management is incredibly important to the game. That's why the C++ version was more performant, as John Carmack had full control of both the memory layouts and lifetimes, and could fine tune it all to fit the game.

This is the only reason C++ is still in wide use today, in order for games to limit load times they have to do nasty 'unsafe' tricks (such as loading a block of memory from disk straight into an address space, and just assigning pointers to it), in order to get good performance. But if your game isn't open world (such as minecraft, GTA, Fallout), then using a garbage collected language will be fine (as long it's not stop the world, but runs concurrently).


Can you give me an example of soft real time game that isn't hurt by the presence of a garbage collector?


I'm not sure what a soft real time game is. Here's two lists of some games with garbage collection

https://en.wikipedia.org/wiki/List_of_Unreal_Engine_games

https://unity3d.com/showcase/gallery


Unreal and Unity were built using C++.

They offer garbage collected languages on top of their C++ game engines for scripting game logic. Any significant modifications to the engine will need to be done in C++


https://wiki.unrealengine.com/Garbage_Collection_Overview

The link is for C++ garbage collection in UnrealEngine, not UnrealScript nor Blueprints


> Unreal's game-level memory management system uses reflection to implement garbage collection.

Yep. Game-level garbage collected C++. Which is exactly what I said, I just a didn't mention GC on C++ to avoid obscuring my point. I promise you the code at the game-engine-level is manually memory managed C++


You do realize that reference counting is considered a form of garbage collection right? https://en.wikipedia.org/wiki/Garbage_collection_(computer_s...

BTW: I've written 6 engines for AAA games. 3 of them used reference counting. AAA C++ games use garbage collection all the time. Maybe yours didn't. Mine did. Unreal does. So does Unity


That's like saying World of Warcarft uses garbage collection because it uses Lua. The higher level language on top of an engine isn't what was being talked about since Minecraft was written completely in Java and thus dealt with the garbage collector at every level.


Anything built with Unity, such as Cities Skylines. Additionally anything built with unreal engine [0] like Gears of War, the Arkham KNight series etc

[0] https://wiki.unrealengine.com/Garbage_Collection_Overview


Right, you use C++ to build game engines, you can use scripting languages on top of that once the engine is built.


> Abstractions like RAII, constructors and destructors, polymorphism, and exceptions were invented with the intention of solving problems that game programmers don’t have

This is the only part of the Jai philosophy I struggle to understand. How is an explicit delete keyword better than deterministic destruction? From my perspective, the former method reintroduces the problem the latter method solves.

Also, polymorphism is bad for games now? I absolutely cringe when I see abstract base classes, virtual functions and classical inheritance, outside of a generic interface in 2016. However, games might be the one case I would strongly consider using those features as part of my design. For example, a Final Fantasy-style job system seems to elegantly lend itself to the "Intro to OOP with C++98" style of runtime polymorphism*

As for exceptions, well I think the reputation of exceptions is one of the great tragedies of C++. Rust, Go, and other new languages seem to have decided that regressing to pervasive error checking boilerplate is more elegant than to have exception handlers.

I think Swift got it right with scoped exceptions and a defer statement - although, they selectively use "error handling" as double speak to avoid saying the e-word.

*Although, I personally think interface/protocol based solutions have proven to be the cleanest way to tackle polymorphism -- this is possibly true even in the case of an rpg with class inherentence as a game mechanic.


>> This is the only part of the Jai philosophy I struggle to understand. How is an explicit delete keyword better than deterministic destruction? From my perspective, the former method reintroduces the problem the latter method solves.

My interpretation of statements like "problems game programmer's don't have" is that they don't mean game programmers don't run into situations where things like RAII or polymorphism would be useful, just that the stereotypical game programmer doesn't care about using them because they have their own ways to get the same results. Which often involves programming practices that aren't considered safe for most other application domains.

The thing with 'game programmers' when referring to people like Jonathan Blow and e.g. Casey Muratori (who does Handmade Hero), is that they have been writing the same kinds of systems for so long, and have developed so much 'muscle memory' to avoid the pitfalls of their coding style, that they appear to have gotten a blind spot for all the deficiencies in the code they write. It works, it's efficient, and someone with the same mindset could make progress on it despite of the minefields they've created, but it's usually not 'good code'. The Handmade Hero code for example is atrocious if you ask me.

It never ceases to amaze me how people who are so smart, much smarter than myself, fail to acknowledge all the ways in which they could write better code without throwing out any of the goals they've set for themselves (performance, compilation speed, predictability, ...). All things considered, it does not surprise me that so many games ship in a half-broken state.


Out of interest, why do you think the Handmade Hero code is atrocious?

I haven't kept up with it so I don't have much of an opinion.


Listing all the things bad about it would take me the rest of the afternoon, and likely evoke many replies along the lines of "you don't understand why he does it that way, it's supposed to be handmade, so you can't have all the nice things", like last time I commented about the HH code.

The executive summary would be that the code is basically full of unsafe code, pointer chasing, half-hearted argument/input validity checking, no memory management, no abstractions of the higher-level parts of the code, it uses none of the idioms we've learned through years of writing crap code to avoid common mistakes (RAII would be a good example, etc), doesn't use 99% of the language features C++ offers over C. Casey himself dismisses these things as adding unnecessary bloat and/or mental overhead, that they have no benefits for his purpose, and that the way he writes code you don't need them, but I just flat-out disagree with that.

Let me make clear that I'm not saying this because I don't like HH or because there's nothing to be learned from it, just that the results you get from this programming style should not be taken as an example. The quality of the code is immediately apparent if you watch Casey work on it in the streams, almost every line he changes breaks something somewhere else in the code, often even multiple things.


Yeah from what I've seen I've noticed he jumps around a lot putting out fires when he changes anything. Like I said in another reply, he's too far in the camp of C++ being C with classes.

I think the sad thing will be people are learning C/C++ from him and so will try and program anything in these languages like this.


It's gross because it's written in "C with namespaces and overloading"

Preferring #define for constants over constexpr is 100% the result of C++ bigotry.

It's a laughable decision. constexpr gives you the power of typesafe, compile time evaluation of purely functional expressions with type deduction. #define gives you a compile time 1972-style copy-paste


I agree with things like these, I think Casey and other programmers like him are too far in the camp of C++ is just C with classes.

From what I have seen of the code and videos, I think his general structure of programs is off. I'm no advocate of "every function has to be at max N lines" or "every file has to be smaller than N lines". But I think there are issues there, and I don't think it can be defended just because it's a game, where a lot of general practices go out the window.


There are cases where virtual functions lend a lot of flexibility, but for games they are usually a sign of a programmer that doesn't understand how pointer chasing destroys performance. If you look at Ogre (and I think box2d) both architectures could be massively sped up by not using arrays of pointers.


Games (I am talking about AAA here, have no idea about all possible games) are not written in the style that leaves place for constructors/desturctors. The data is better in tables (i.e. arrays) than spread all over the memory and accessible through a pointer network. The reasons being: a) walking an array is orders of magnitude faster than walking a pointer chain and b) heap allocation wastes more memory

This, in turn, makes no sense for polymorphism since you already have uniform data in the arrays there is no need to do an expensive indirect jump to figure its type.


Why couldn't you use a structure to encapsulate the table and then have the destructor to deallocate the the array?

This is how vector works.


You could, but why? To compensate for the lack of construction/destruction of the array's elements?

Practically these arrays never go away so there is little point in deallocation anyways.


If you're using C++ you're still using default destructors even if you don't write them. In C, you at least get deterministic destruction for variables and structures on the stack. This isn't something you really opt out. It's there by default.

The parent was wrong about vector though, it won't safely free elements that do not have their own destructors to clean up their own resources. Another good reason to use RAII everywhere. It costs nothing, to encapsulate memory management.

> You could, but why?

Because there's zero overhead and you guarantee to be passively covered in the few edge cases where you do end up needing deallocation. Now you don't have to worry about special handling of edge cases and you have a more general purpose data structure.

A better question is, why not?

It's simply a better designed and more flexible container than a raw array. Other than a bias against C++isms, there's no good reason to avoid these useful features.


Default destructors are fine since they don't produce any code as long as you don't have any real destruction going on.

>There's zero overhead and you guarantee to be covered in the edge cases where you do end up needing deallocation.

If you suddenly find yourself in a position when you need deallocation for something that is not supposed to be dislocated then I'd rather have it fail with as much noise as possible than have it covered. E.g. I prefer game crashing on out of memory rather soon than going on with thrashing the heap till it crashes 8 hours into the soak test due to the heap fragmentation.

>It's simply a better designed and more flexible container than a raw array.

Tastes differ. I ship games myself and almost all programmers I know do the same. I don't know anybody who would agree with this. Just to be clear, I am talking about destructor of an array. Wrapping arrays in structures is fine and everybody does this.


> I'd rather have it fail with as much noise as possible than have it covered...

RAII is orthogonal to contiguous storage in memory. You are not opting into heap fragmentation by moving your "dealloc struct" function from the global namespace to a destructor. It has nothing to do with the the memory layout. It has to do with preventing memory leaks and undefined behavior.


> If you suddenly find yourself in a position when you need deallocation for something that is not supposed to be dislocated then I'd rather have it fail with as much noise as possible than have it covered. E.g. I prefer game crashing on out of memory rather soon than going on with thrashing the heap till it crashes 8 hours into the soak test due to the heap fragmentation.

How would this be different?

> I don't know anybody who would agree with this.

It isn't really something to agree on, in one scenario you have options for automation but don't give up anything, in the other scenario you have no ability to use ownership or scope semantics whether you want to or not.


>How would this be different?

In one case it takes 1-15 minutes to reproduce, in other - 8 hours.

>It isn't really something to agree on, in one scenario you have options for automation but don't give up anything, in the other scenario you have no ability to use ownership or scope semantics whether you want to or not.

Judging by your previous question I figure you don't ship games, do you?


I meant how would the error somehow happen more slowly using an STL data structure than in C?

> I figure you don't ship games, do you

No, I only write them, I've found it's a lot easier to let someone else handle the distribution.


>I meant how would the error somehow happen more slowly using an STL data structure than in C?

That I don't know. As I said, I was only talking about deallocation of arrays that were not supposed to be deallocated.


> I don't know anybody who would agree with this.

std::array<int,ARR_SIZE> is better than int arr[ARR_SIZE]

I don't know how you could disagree with that after looking at the facts. From what you're saying here, there seems to be a culture that favors "old school" C programming in games, but the reasons behind it seem like nothing more than a fear of the unknown. I don't mean to be disrespectful, it just seems like nothing but stubbornness to me.

> Just to be clear, I am talking about destructor of an array

Can you be clear about why this is bad?


>std::array<int,ARR_SIZE> is better than int arr[ARR_SIZE] I don't know how you could disagree with that...

What is the alignment of your std::array? What is the memory type (e.g. can the GPU read from it at all? Can it write? What are cache policies?). The alternative though is not a C array, it's an explicit memory mapping.

>Can you be clear about why this is bad?

Useless code at best (if your game runs properly it will never be deallocated by your code), obscuring bugs at worst (if it starts deallocating at runtime it will take longer to fail).


> What is the alignment of your std::array? What is the memory type (e.g. can the GPU read from it at all? Can it write? What are cache policies?). The alternative though is not a C array, it's an explicit memory mapping.

Guaranteed to be contiguous, and semantically equivalent to a C array in all cases.

If you don't trust your vendor's STL implementation take a look at the intrusive containers in EA's STL implementation. It's very very good for games. It's also safe, which is a good thing that doesn't obscure bugs it all.

https://github.com/electronicarts/EASTL

> e.g. can the GPU read from it at all? Can it write? What are cache policies?

Fun fact, you can write your own template container with specific features with no additional overhead from a C "array" that's also memory safe.

> if it starts deallocating at runtime it will take longer to fail).

It can't magically deallocate at runtime. It's deterministic. I don't think you understand you give up zero control. It's just a cleaner system with less room for human error.

> obscuring bugs

What's obscure about knowing exactly where are all memory management occurs without exception? C-style malloc and free scattered all over the project is way more prone to hiding bugs.


As I said, the alternative is not a C array. Neither is C style malloc and free are used in games. If you want a discussion - argue over what I've said or ask questions if you don't understand something. Otherwise have fun with your own mental image of game programming yourself.


You asked a question about the array and I answered it.

Yeah I missed the part where you said mmap. There's libraries that make that safer, but clearly there's a preference for working with the raw tools here.

> Otherwise have fun with your own mental image of game programming yourself.

Ignorance is bliss. Have fun ignoring the progress systems programming has made in the last 30 years. Why bother even looking into it right? If what works for you works... that's all that matters.


Disregarding the fact that std::array is allocated on the stack, you do realize that running under a debug mode means that there can be bounds checking assertions built into containers like this, not to mention iteration of the elements instead of iteration of the indices (which guarantees not going out of bounds) ?


std::array is not necessarily allocated on the stack, it's only allocated on the stack when it's a local variable. So I realize what it is and what it does. Do you realize that you have little control over where its memory goes and there are very different types of memory available to games? Do you know what is memory alignment? Do you realize you cannot grow/shrink it? Do you realize you can still do bounds checks if you need them?


If you need to grow or shrink it use a vector. If you need different types of memory or aligned memory, make an allocator and use that. Many people do, it is a very common use of allocators. Even if you don't want to use the STL you can encapsulate all of these things for reuse and modularity.

I'm not exactly sure why you think these things aren't achievable in C++ (and because they are achievable they are relatively straightforward to wrap in a way that they can be made generic while hiding the complexity so you can be done with it). I've even made variadic templates that fuse memory allocations together like Jai's proposed feature.

I've seen people who know C and seem forever hung up on it. It isn't really rationale these days now that there are C++11 compilers that are so mature. It's almost as if there are people who work in a constantly advancing field but don't want to learn anything new.


>If you need to grow or shrink it use a vector.

Thank you for the advise but what if I want to shrink one array that takes 200 MB of memory by 10MB and give it to another array that takes 50 MB of memory on a system with only 256 MB of memory?

> If you need different types of memory or aligned memory, make an allocator and use that.

std::array does not have allocators.

>I'm not exactly sure why you think these things aren't achievable in C++

I have no idea why you think so. I only used C++, assembly and various shader languages in every game I worked on. I did some C in drivers but I don't think it's a good language for games.


I've lost track of your point all together, are you still trying to say there is utility in raw arrays?

You can't possibly think memory allocation problems that are solved by custom allocators can be dismissed because std::array doesn't take an allocator when that it integral to the entire reason it exists. Are you trying to say that not only do you want aligned static memory but that there is nothing that exists that helps over a raw C array?


Here is a digest of the thread you had been replying to: gregstula said that std::array is better than what games use (imagining games use plain C arrays). I corrected them, saying that games use explicitly mapped memory and pointed at issues with std::array that prevents its usage in games. I never advocated use of raw arrays in this thread though they have utility since they are easily substituted with pointers if/when you decide that you care about the underlying memory and this is why I've never seen std::array in a game code. Note that I have not seen every game in existence, only few dozen AAA titles or so. I know people write indie/mobile/social games with stl, python and what-not but I am not really interested in that kind of game programming.


Hey there. I wrote the Jai Primer. Ideas there are my best interpretation of Blows ideas, which I generally agree with, but bear in mind they're not his.

To quote Joe Armstrong: You wanted a banana but you got the gorilla holding the banana and the whole jungle. You wanted a way to delete memory automatically, which sounds great but in practice most language's approaches to solving this problem come with a host of other problems that at scale make the given solution not worth it. RAII solves a big problem but it introduces a bunch of tiny problems like big mysterious constructors that implicitly do a lot of work and deconstructors that don't map to any particular line of code other than an ending brace. It's tough to examine in a debugger, it's tougher to reason about when it gets to nontrivial scale, etc etc. But human brains naturally weight a lot of small pains to be less bad than one big pain, so the solution looks legit.

Polymorphism is a way of modeling the world that runs along the lines of the categorizations that people tend to make, so it feels natural to create deep class hierarchies. But in practice it doesn't match the problem that you need to solve when you make games - you have data in state A and you need to get it into state B. Example: to do a physics integration on each simulated body, the CPU wants to do a for loop over a list of position vectors. But those vectors have been scattered all over memory by the class hierarchy. So that's one problem, you also get problems like needing RTTI and casting and yadeya. Class structures have largely fallen out of vogue in game development in favor of component systems, which is a pretty good step forward.


Would you mind explaining or linking to a good document on "C++98 style runtime polymorphism"?


You can do a google search for this information


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: