2D Flying Dog Game in C++ // Code Review
Вставка
- Опубліковано 28 тра 2024
- To try everything Brilliant has to offer-free-for a full 30 days, visit brilliant.org/thecherno. The first 200 of you will get 20% off Brilliant’s annual premium subscription!
Patreon ► / thecherno
Instagram ► / thecherno
Twitter ► / thecherno
Discord ► / discord
Code ► github.com/MariuszDot/Game_Fl...
CHAPTERS
0:00 - Intro
3:17 - Initial thoughts
6:30 - Playing the game
8:22 - Looking at the code
Game music is Achievement by Philip Anderson.
License:
Music from #Uppbeat (free for Creators!):
uppbeat.io/t/philip-anderson/...
License code: XB7FGDNM2YFC8FYL
This video is sponsored by Brilliant.
Hope you guys enjoyed this Code Review - more to come next time! Don't forget that now you can try everything Brilliant has to offer-free-for a full 30 days, visit brilliant.org/thecherno. The first 200 of you will get 20% off Brilliant’s annual premium subscription!
Try to engage with your followers more bro, we love to talk to you ❤
I think a video on how to architect a 2d game in terms of classes and scopes and how their instances communicate with each other would be great. For example, how does Game, Enemy, Player, Sound, etc. relate to each other. Apart from that, I really think the reviews are very helpful. Thank you!
One point of making sure objects get deleted even if they live for the entirety of the program could be to remove noise from leak tracker tools.
"so i'm this dog, i guess, and i'm in a plane and i'm shooting birds"
me when thinking of what i'm doing with my life
Underated Funny Comment
I don't even program in C++ but love watching Cherno's vids. :)
LOL - "all the budget went towards the enemies" , classic line.
Since SDL uses a lot of pointers for things if we wrapped them in some kinda make unique wrapper then we wouldn't need to remember to call the free commands for SDL or you could make a using typedef for unique pointer types that have the sdl free function listed in there.
Thank you for your incredible teaching
Hey THE Cherno,
Is there a chance you could tackle 3D cache if there's anything cover-worthy?
I simply wonder if there's anything the dev could do to optimize the game with more cache in mind or is it even a thing?
I liked this episode because it really shows some caveats and gotchas in a basic 2d setup, which is what im currently working on.
I like these code reviews
Am also trying to make a simple game using sdl2 and this was really helpful, i was using good practices without even noticing
good job
Looking forward for the second part. Those kinds of mistakes I would make.
What happend to you (17:41) to (17:42) , in one second 🤣
That was funny ngl xD
cherno comprehending the way to go for allocating objects on the heap
LOL
He had a mental breakdown and decided to cut it from the video.
this is great, it's similar to my own code. Never used smart pointers tho cuz my programs are usually quite small, I will try it next time, it seems easy to use!
You're a legend! You've given me ALOT to ponder on. Thank you for your lesson!
Please add this video to the Code Review playlist
I really thought about this episode was about that classic youtube vid dog of wisdom 🐶
13:10, I would not use unique_ptr. If objects won't change their places, there's no need to make that pointer: vector deals with memory managment. In most games order doesn't matter, so a vector is more than enough. But if it does matter, than it's possible to make another vector, of indexes: they can be, in most cases 1/8 less memory consumption, compared to pointers. And since containers (of characters, at least) use to not be large, won't has loss of speed by travelling those containers via indexes.
Best code review I've seen yet in terms of your feed back.
Although it seems like you never bother with the comments even though you asked us to post. Even just a thumbs up or down, on one or two of the comments would be good enough.
I don't really undestand the parallax effect between the grass foreground and the background with trees and buildings. Isn't it the other way around, shouldn't the grass move faster instead of slower?
How would you implement a texture manager and also the game states?
It is funny how you follow all the rabbit holes same way I would do :D 👍
MS has really messed up what was one of the best IDE's with those really annoying error markers that are not errors 😞
The game ptr doesn't even need to be allocated dynamically. Just have Game game at the top of the main function and use it as an object instead of a pointer.
Ah you got to that point eventually.
pozdrawiam Mariusza naszego rodaka!!
"You always want to limit your variables to the smallest scope possible". That's what I am doing right now but 1.5 years ago I did a very similar thing that this guy did (except even worse) with global variables. When I learn a new language and I am not proficient in it I tend to give bigger scopes to variables and as I am getting better and better at a language and how things are done in it I tend to write code whose variables have smaller and smaller scopes. It is possible that he did this because he is a Python programmer who has little experience with C++. If he was more experienced with C++ maybe he wouldn't have created global variables.
Whats the benefit of having the smallest scope possible?
@@lazyraccoon1526 Less ways to make a mistake, easier to track what's going on, easier to test
@@lazyraccoon1526 When talking about limiting the scope of a variable to a class by making a field private that also helps with decoupling the code. If you don't decouple your code the complexity of your code will grow exponentially as you add more functionality to it. By decoupling your code you are trying to make the complexity of your code grow linearly as you add more functionality to it.
@@lazyraccoon1526 It's easier to reason about the state of the variable. If a variable is only used within a small function and it has some weird value, you can set a breakpoint and you will know that this weird value was computed within that small function. If it's a global variable, there might be dozens of functions that could have set that variable to an incorrect value, so debugging becomes much harder. It becomes even harder if you use multithreading.
One of the dog's appears to be missing on the Splash screen after you did moved the global pointers into main. There were three doggos and you seem to have killed one off @10:49.
no, the third dog appears after a delay
@@lavatasche2806 oh good.
how do you go through writing all those texture initialisation methods without thinking about automating it
This guy's game is fascinating 😂. it's the dropping of the bombs 😂
A minor question: in the main() file the variable int frameTime; is not initialized with a value - is that ok?
He can get away with it but the best practice is to always initialize all variables.
I feel proud to have thought that those init methods needs a loop
Then you just did what i thought
11:56 I had issues declaring static variables with c++ when i was attempt to access from another CPP file in debugger was shown as null pointer even that variable was declared and initialized before.
remember static global variables can't be accessed from other cpp files unless inside class.
That's the whole point of static variables. It limits the scope of variable to the file it's defined in.
Question: is cpp's unique pointer similar to/same as Box in rust?
Equivalent
OVER ENGINEER IT, I would like to see the cleanest, smartest code possible out of this game.
oh man, im just learning the basics and there are a lot of things that i don't understand xD maybe later i will :) good video,i must learn too much
Yeah C/C++ is Hard... I learned mainly Java/JS/Python and not managing memory is a relief.
@@erwile yeah in a future i want to learn game development.. or unreal ,by example.. c ++ is really fast..and i love that.. the pain is temporal, the optimization is eternal :P
@@ezequielpereyra1900 yeah but it's about optimizing incrementally and for your needs. I prefer a complete game that runs at 40 fps in 1080p than an unfinished one that runs in 4k 120 fps. Our first projets is always bad in some way or another
@@ezequielpereyra1900
Luckily, Unreal manages it's own memory using UObjects, so you often don't have to worry about it. Or you can just use Blueprints.
Ezequiel, you're right. Go through the pain of C++. It's worth it. It'll make you a much better programmer, who will understand the machine much better. And if you later on, for some reason, end up downgrading to Python, it's ok.
The problem is starting with Python/Java/JS etc. straight away ahaha.
This series good.
Hi bro, which theme you are using ?
Looks kinda like Gruvbox
I was about to ask whether it was a good idea to just allocate the Game on the stack due to its lifetime when you said you could :D Although, I never knew we only get 1MB of stack if not defined otherwise. Given that most of the stuff I see in the Game class are pointers it should be fine though, right? Also, what would happen if we tried allocating more than 1MB in the stack? Would the application just crash straight away with a memory allocation error?
I guess the compiler might increase stack size, or give an error (since usually the size of stack-allocated objects must be known at compile time), or fail with stack overflow at runtime
The programme crashes when you enter the function that requires more than 1 MB. This includes inlined function. For command line programs this crash is an immediate termination of the program without error message. "Nice" to see this in the main() function. You can't start the program and you don't see why.
It is not just because of the lifetime of the Game object but also because you are initializing the game in the most outer scope of your program and it is not like the Game object is resizable so why not just put it on the stack.
@@student99bg that's what I meant, probably should've been more precise on that :)
@@frankklemm1471 I once took ages debugging a terminal minesweeper clone with "arbitrarily large" fields while learning c - turns out a recursion I had for opening fields was overflowing the stack (the solution I found was to limit how deep it could go and mark fields which were pending or already processed)
Also: so he did make a constructor/destructor, but had an explicit init/deinit. It's likely that this could simply be done with the constructor/destructor.
The levels are hardcoded in a switch. Seem unnecessary if you have dynamic levels anyways, so just get the current level like getCurrentLevel()->doTheThing() instead and set the level accordingly. You get a single call to each render, handleEvents etc. to the "current level", only the implementation is different.
The variable abstraction isn't ideal either. In one point it says "render" and in the next line it's "SDL_GetTicks()". Would make sense to abstract that.
Initialized is a u32 with the ticks in there? I would assume it would be a boolean. Be more clear. Something like startTime is clearer.
while game->running would make more sense than initialized. At one point if you initialize something, it has been initialized, even after uninitializing.
And this is just main... The code has too much wrong with it for me to mention it all.
How do I get a code review
Can you make a Game object without a pointer?
Like: Game game;
What's the difference? And the advantage and disadvantage
Yes, advantage: you dont have to deallocate it.
Disadvantage: Heap has more memory than the stack. If the game object uses to much memory it might crash, just as he said in the video.
Speed is not important here because it's a one time allocation
@@lavatasche2806 thanks!
@@lavatasche2806 in languages like C# you can only allocate in the heap, right?
@@vazthaxkrules5272 It depends. There are structs in c# and these can be allocated on the stack but that does not mean that they are always put on the stack. I dont know to much about allocation in c#. There are great articles about that online.
lol squish that code
When I first looked at the title, I thought it said "2D Flying Dong Game in C++" and was taken aback by how naughty the channel had become. Still, I was curious as to how the dong would be depicted. Would it be flaccid or tumescent? Would it have pulsating veins? Would additional organs be attached? What would be the perceived ethnicity of said dong? But then I looked at the title again...
why don't you use clion btw?
It's me
"I'm pretty happy with the game"
Meanwhile - 7:53-7:55 "If I lose (has 2800 points)" and then the Congrats screen says 2760 points. How does that happen? I guess we'll watch and find out.
Ha hababa habababababa
Yo
i think if you have true singleton like Game class you really don’t want to do destruction. because you might be paging in memory from disk just to delete them. it is better to just let the os reclaim things.
if you need to ask, then everything goes wrong
*Making my own programming language in Assembly
The Cherno: Brahhh ......... Boring
*A game about a flying dog
Also The Cherno: Very Interesting
As a 40+ years experience programmer who sees young recruits regularly, I would advise to refrain from leaving these 'low hanging fruit' things in your 'best code'. Reasoning: if you advertise it as your best code, but I see hardcoded magic values, I will think you missed an essential thing and by consequence are not smart programmer material. And rest assured that I will probably find a ton of other bad practices, mistakes, bug, memory leaks etc. Usually beginner and intermediate code is riddled with those, so don't think you can outsmart an experienced reviewer like that, it's insulting at best, and any competitor job applicant who plays it fair will win over you. Golden tip: don't mention python to a C++ reviewer, it will only paint you like a 'script kiddie', and we honestly have enough of those. Good luck 🤞
I'm not sure if it's healthy to view anyone who has touched python as a "script kiddie" I like C++ a lot and I'm working on becoming more comfortable with rust, but I'll still break out Python when it's the best tool for me to solve the problem I have.
I'd like to say "Hold it!" and say that even senior devs use magic values.
Explain 3.1415
(obviously this is a joke, but still true)
@@andrewf8366 I've interviewed a couple of C# and Python programmers claiming to know C++ and they just show a lack of understanding of C++ practices and how a computer works. We are talking about people that do not know what “virtual” implies and what a “cache” is. So I would argue that the “script kiddie” is somewhat accurate. I would argue that too many “programmers” (Quotes because real programmers should know these concepts) don't know how a computer works and what the strengths and weaknesses of the language they use are, which results in slow and poorly optimized applications.
If you know C++ and apply for a C++ position, advertise yourself as a C++ programmer and not a C# or Python programmer.
@@arjix8738 only crap senior devs. There are a lot of those too. That number you mention, btw, is not Pi, contrary to what many will try to tell you. It just looks like it, with a non-negligible margin of error. Lol
@@andrewf8366 sure, break out the vicious snake whenever you feel the need, but my point is that being proud of python knowledge on a C++ job interview will not score you many points and might shed a light on the kind of C++ mistakes you make. No language is ideal, and each language has its place.