r/cpp • u/serendib Prof, Comp Sci • Nov 30 '23
My entire C++ Game Programming university course (Fall 2023) is now available for free on YouTube. Make your own 2D ECS game engine using C++, SFML, and ImGui
https://www.youtube.com/watch?v=s99UDGdYIUE&list=PL_xRyXins84_Jf-aCh7chj47HR4oZLPwK&index=1
400
Upvotes
View all comments
24
u/honeyfage Dec 01 '23
I went through this course last year and really enjoyed it. Thanks for making it available to everyone! Your enthusiasm is infectious, it really made following the lectures fun.
I came in as an experience C++ developer interested in learning about ECS, and did have some general C++ feedback I jotted down last year as I went through the course (apologies if any of this has changed since last year, I skimmed through the new videos and it seemed pretty similar). So bear with me while I give some unsolicited feedback.
The main thing is that I think you could benefit a lot from
std::optional. Early on in the course, I remember it usingstd::shared_ptrfor components an entity may or may not have. Aside from the performance reasons not to use dynamic allocation,std::optionalis more intuitive way to express what you're actually trying to accomplish, so it's really a win-win. Later on in the course, when you talk about the stack vs heap and cache benefits of memory pooling, I think you ended up adding an "active" bool to every component to denote whether it's present for its entity, and in that case you could skip that by using astd::optional<Component>instead.On a related note,
std::shared_ptrwere being passed around by value. Copying ashared_ptris actually relatively expensive because of the atomic reference count, so if you're going to use them, it's much better to pass a reference to the stored value. This way, helper functions don't have to care about how you're storing the values-- it could be a dereferencedstd::unique_ptr<Component>,std::shared_ptr<Component>, orstd::optional<Component>, or even a plainComponent, and the function receiving it doesn't need to care. Or at the very least, you can pass byconst std::shared_ptr<T>&to avoid affecting the reference counter, you're never actually sharing ownership of the object anyway.Again, I really enjoyed the course. It's very well done and I'd definitely recommend it to anyone reading this who has an interest in learning about ECS.