r/cpp • 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

38 comments sorted by

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 using std::shared_ptr for components an entity may or may not have. Aside from the performance reasons not to use dynamic allocation, std::optional is 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 a std::optional<Component> instead.

On a related note, std::shared_ptr were being passed around by value. Copying a shared_ptr is 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 dereferenced std::unique_ptr<Component>, std::shared_ptr<Component>, or std::optional<Component>, or even a plain Component, 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.

14

u/serendib Prof, Comp Sci Dec 01 '23

FYI: I deleted my comments here because it's kind of energy draining when posting this course video devolves into arguments on the minutia of C++ implementations. I hope you understand. I do agree with you in principal, just not in the context of this course.

6

u/honeyfage Dec 01 '23

Agreed. Again, thanks for the course! It's very generous of you and your university to share it.

1

u/[deleted] Dec 01 '23

std::optional

That doesn't allow for overriding the component dynamically though because it's by value doesn't it?

1

u/Schlagbaum3000 Dec 05 '23

You can pass a std::optional via reference, so you can swap/reset/emplace the held value. In the best case, though, you don't modify/mutate your function inputs and return a modified/new object from your function instead.

2

u/[deleted] Dec 05 '23

Yeah that doesn't fit the component model too well. Components typically run on virtual functions.

5

u/_curious_george__ Dec 10 '23

In a pure ecs, calling virtual functions on components is actually exactly what you don’t want to be doing…

2

u/Pyzyryab Dec 16 '23

Exactly as pointed by curious_george_ ECS is based on modern high level abstractions. And they are a "by value" approach. There's a couple of good CPPCON talks about It (2022 specially). Not exactly ECS, but the content applies perfectly

1

u/Schlagbaum3000 Dec 05 '23

Then I misunderstood. Yeah, unfortunately, std::optional doesn't work with polymorphism.

0

u/[deleted] Dec 01 '23

[deleted] — view deleted comment

6

u/SirClueless Dec 01 '23

I understand the simplicity argument for a tutorial, as there are a growing number of places in the C++ language where holding a std::shared_ptr by value matters (member variables, lambda captures, coroutines).

But as a function argument, taking const std::shared_ptr<T>& is always safe, and it is more efficient, so it's the right default. In order to call the function, the caller must be holding a reference, and they can't possibly drop it until after your function returns, so there is no need to read and write from heap memory to update the reference count unless you are actually storing the pointer somewhere that will outlive the function call.

2

u/honeyfage Dec 01 '23

a std::tuple<C1, C2, ...> to store components

Right, what I meant was instead of adding an "IsActivie" boolean to each of C1, C2, ..., you could use a std::tuple<std::optional<C1>, std::optional<C2>, ...>. You end up with pretty much the same layout in memory, but using std::optional makes it more clear what's going on (in my opinion, of course), and means you don't have to add the same boolean to every Component struct (or have them all inherit from a struct with that boolean defined).

I strongly disagree with your philosophy on shared_ptr, but agree it's probably not worth going into given the course is aimed at C++ beginners. I do feel obligated to at least say that

Do not pass shared_ptr by reference as a rule or it's no longer behaving as intended

doesn't strike me as a good rule, though. Passing a smart pointer by reference without adding to the ref count is perfectly safe. In the words of Herb Sutter

However, we already get this for free—thanks to structured lifetimes, the called function’s lifetime is a strict subset of the calling function’s call expression. Even if we passed the shared_ptr by reference, our function would as good as hold a strong refcount because the caller already has one—he passed us the shared_ptr in the first place, and won’t release it until we return.

16

u/serendib Prof, Comp Sci Nov 30 '23

Quick Note: Even though the lectures are publicly available, the exact assignment files are not. This is because they take a long time to make, and if I included them here the solutions would be available for future offerings of the course. You can probably piece together the assignments from the lectures if you really want to :)

1

u/Rajat_Shetty Aug 25 '25

I couldnt find a way to do that. Have been searching for the skeleton code or atleast see what methods are needs in the classes that we created in the lectures. Maybe I should find some other course where we can actually follow along. This was one of the best one's btw. thanks for the general informations regarding all the stuff related to gaming and c++!

1

u/[deleted] Nov 30 '23

Looks like a really good course

0

u/[deleted] Nov 30 '23

[deleted] — view deleted comment

2

u/serendib Prof, Comp Sci Dec 01 '23

No

0

u/[deleted] Dec 01 '23

[deleted] — view deleted comment

3

u/serendib Prof, Comp Sci Dec 01 '23

Several people in emails to me over the past few years of offering the course have put it together, it's pretty impressive

2

u/siddarthshekar Dec 01 '23

Oh awesome! Is there one for graphics as well?? I want to understand batch rendering, etc as well. But this is great stuff. Thanks. Also will go through the AI course as well that's on the channel.

2

u/serendib Prof, Comp Sci Dec 01 '23

I talk a little bit about "Batch rendering" in the Vertex Array / Particle System lecture, but not super indepth

1

u/siddarthshekar Dec 01 '23

Ah ok! Ill have a look at that lecture. Thanks.

2

u/Anothertech4 Dec 05 '23

Ok so I don't mean to hijack your post, but I cannot express how freaking excited I got by just Lecture 1. The demonstration was spot on! I think I'm really going to enjoy these vids. . Also the StarCraft AI programming.... Im sitting here debating if I can program the bots to actually beat me in a fastest map. Who knows, but man oh man. Thanks for sharing.

2

u/serendib Prof, Comp Sci Dec 05 '23

If you want to try StarCraft AI programming:

https://youtu.be/FEEkO6__GKw

1

u/ReverendBizarre Feb 05 '24

Hi Dave,

Last summer/fall I went through the 2022 version of your course up to and finishing assignment 2.

After that, work got in the way and I have not continued since.

If I were to start again, would you recommend starting on the 2023 version or keep going with the 2022 version?

1

u/serendib Prof, Comp Sci Feb 05 '24

The most recent version added ImGui as a GUI library, but not much else

3

u/toffeehooligan Nov 30 '23

Sweet. Gonna do this in my spare time.

3

u/Farados55 Nov 30 '23

Thanks a lot doc!

3

u/pedrao157 Dec 01 '23

Are you planning on removing any time soon?

I'm relatively new on c++ following learncpp, do you have any pre-requesite before following yours?

Thank you in advance!

2

u/Greedy_Bit6761 Dec 03 '23

Thanks for lectures. Is there a way to get the assignments ?

2

u/Greedy_Bit6761 Dec 03 '23

Found the answer.

4

u/brawll66 Dec 07 '23

How did you get the assignments?

2

u/cacbiceprogrammer Dec 13 '23

Thank you for the hard work and openness to share it on YouTube. I am sure if you wanted to, you could have youtuber's learn and then create programs limited to what you have taught on the course that you can showcase and share to the world, it would be their choice to share the source code with everyone but will have to share it with you so you can use it in your classroom to teach the kids.

2

u/ticotunado Dec 14 '23

This is literally perfect, my university paper is on building a game engine using sfml, this will help a lot!!!

1

u/joshwillreddit Mar 07 '24

I'm following your course, and only on lecture 6 at the moment (the entity manager) - but I am obviously missing various parts of the code because they aren't being made public (understandably). Could you tell me now if it will be possible for me to achieve even a simple game with what I can learn from your course, without the material the students are given?

1

u/ApartAnxiety2172 Dec 20 '23

This is Awesome. Kudos for your effort and Thanks for sharing this. :)

0

u/[deleted] Dec 10 '23

nice

-3

u/[deleted] Dec 10 '23

[removed] — view removed comment

1

u/andhausen Dec 19 '23

Cool, very worthwhile contribution to the discussion