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.

КОМЕНТАРІ • 100

  • @TheCherno
    @TheCherno  Рік тому +15

    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!

    • @ouardito
      @ouardito Рік тому

      Try to engage with your followers more bro, we love to talk to you ❤

  • @chriscross7671
    @chriscross7671 Рік тому +13

    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!

  • @denravonska
    @denravonska Рік тому +38

    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.

  • @ExediceWhyNot
    @ExediceWhyNot Рік тому +6

    "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

  • @timlind3129
    @timlind3129 Рік тому +14

    I don't even program in C++ but love watching Cherno's vids. :)

  • @timlind3129
    @timlind3129 Рік тому +1

    LOL - "all the budget went towards the enemies" , classic line.

  • @Sebanisu
    @Sebanisu Рік тому +1

    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.

  • @huaminghuaming
    @huaminghuaming Рік тому +1

    Thank you for your incredible teaching

  • @RyNiuu
    @RyNiuu Рік тому +12

    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?

  • @pastasawce
    @pastasawce Рік тому

    I liked this episode because it really shows some caveats and gotchas in a basic 2d setup, which is what im currently working on.

  • @sck3570
    @sck3570 Рік тому +3

    I like these code reviews

  • @vmarcelo49
    @vmarcelo49 Рік тому +4

    Am also trying to make a simple game using sdl2 and this was really helpful, i was using good practices without even noticing

  • @user-bk8rp9zj6q
    @user-bk8rp9zj6q Рік тому +2

    Looking forward for the second part. Those kinds of mistakes I would make.

  • @sck3570
    @sck3570 Рік тому +7

    What happend to you (17:41) to (17:42) , in one second 🤣

  • @jdgoldvoxvoxcast1168
    @jdgoldvoxvoxcast1168 Рік тому

    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!

  • @ogr3j3ct3d_o7
    @ogr3j3ct3d_o7 Рік тому +4

    You're a legend! You've given me ALOT to ponder on. Thank you for your lesson!

  • @kiferdon7243
    @kiferdon7243 Рік тому

    Please add this video to the Code Review playlist

  • @senpaiyaa
    @senpaiyaa Рік тому +1

    I really thought about this episode was about that classic youtube vid dog of wisdom 🐶

  • @MrAbrazildo
    @MrAbrazildo Рік тому

    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.

  • @zennmyst5347
    @zennmyst5347 Рік тому +1

    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.

  • @tomaskot9278
    @tomaskot9278 Рік тому +2

    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?

  • @dongiannisiliadis9018
    @dongiannisiliadis9018 Рік тому

    How would you implement a texture manager and also the game states?

  • @_CJ_
    @_CJ_ Рік тому

    It is funny how you follow all the rabbit holes same way I would do :D 👍

  • @andrewfraser2760
    @andrewfraser2760 Рік тому +3

    MS has really messed up what was one of the best IDE's with those really annoying error markers that are not errors 😞

  • @aloluk
    @aloluk Рік тому +2

    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.

  • @szymongniadek1206
    @szymongniadek1206 4 місяці тому +1

    pozdrawiam Mariusza naszego rodaka!!

  • @student99bg
    @student99bg Рік тому +4

    "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.

    • @lazyraccoon1526
      @lazyraccoon1526 Рік тому

      Whats the benefit of having the smallest scope possible?

    • @student99bg
      @student99bg Рік тому

      @@lazyraccoon1526 Less ways to make a mistake, easier to track what's going on, easier to test

    • @student99bg
      @student99bg Рік тому

      @@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.

    • @MsJavaWolf
      @MsJavaWolf Рік тому

      @@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.

  • @not_ever
    @not_ever Рік тому +1

    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.

    • @lavatasche2806
      @lavatasche2806 Рік тому +1

      no, the third dog appears after a delay

    • @not_ever
      @not_ever Рік тому

      @@lavatasche2806 oh good.

  • @polygonerror
    @polygonerror Рік тому

    how do you go through writing all those texture initialisation methods without thinking about automating it

  • @prince_lish5649
    @prince_lish5649 Рік тому +3

    This guy's game is fascinating 😂. it's the dropping of the bombs 😂

  • @jakubb4784
    @jakubb4784 Рік тому +1

    A minor question: in the main() file the variable int frameTime; is not initialized with a value - is that ok?

    • @durzua05
      @durzua05 Рік тому +1

      He can get away with it but the best practice is to always initialize all variables.

  • @nerdyguygaming4640
    @nerdyguygaming4640 Рік тому +4

    I feel proud to have thought that those init methods needs a loop
    Then you just did what i thought

  • @FRNathan13
    @FRNathan13 Рік тому

    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.

    • @ayoubbelatrous9914
      @ayoubbelatrous9914 Рік тому +2

      remember static global variables can't be accessed from other cpp files unless inside class.

    • @sidtronics
      @sidtronics Рік тому

      That's the whole point of static variables. It limits the scope of variable to the file it's defined in.

  • @khoda81
    @khoda81 Рік тому

    Question: is cpp's unique pointer similar to/same as Box in rust?

  • @Matej404
    @Matej404 Рік тому +1

    OVER ENGINEER IT, I would like to see the cleanest, smartest code possible out of this game.

  • @ezequielpereyra1900
    @ezequielpereyra1900 Рік тому +6

    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

    • @erwile
      @erwile Рік тому +3

      Yeah C/C++ is Hard... I learned mainly Java/JS/Python and not managing memory is a relief.

    • @ezequielpereyra1900
      @ezequielpereyra1900 Рік тому +2

      @@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

    • @erwile
      @erwile Рік тому +2

      @@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

    • @APaleDot
      @APaleDot Рік тому +1

      @@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.

    • @theintjengineer
      @theintjengineer Рік тому +1

      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.

  • @freezinfire
    @freezinfire Рік тому

    This series good.

  • @TrungNguyen-mf6kt
    @TrungNguyen-mf6kt Рік тому +1

    Hi bro, which theme you are using ?

  • @RobertSlepecki
    @RobertSlepecki Рік тому +8

    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?

    • @SomeRedTeapot
      @SomeRedTeapot Рік тому +2

      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

    • @frankklemm1471
      @frankklemm1471 Рік тому +1

      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.

    • @student99bg
      @student99bg Рік тому +1

      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.

    • @RobertSlepecki
      @RobertSlepecki Рік тому +1

      @@student99bg that's what I meant, probably should've been more precise on that :)

    • @user-sl6gn1ss8p
      @user-sl6gn1ss8p Рік тому

      @@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)

  • @nextlifeonearth
    @nextlifeonearth Рік тому +3

    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.

  • @citer5574
    @citer5574 Рік тому

    How do I get a code review

  • @vazthaxkrules5272
    @vazthaxkrules5272 Рік тому +1

    Can you make a Game object without a pointer?
    Like: Game game;
    What's the difference? And the advantage and disadvantage

    • @lavatasche2806
      @lavatasche2806 Рік тому +1

      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

    • @vazthaxkrules5272
      @vazthaxkrules5272 Рік тому

      @@lavatasche2806 thanks!

    • @vazthaxkrules5272
      @vazthaxkrules5272 Рік тому

      @@lavatasche2806 in languages like C# you can only allocate in the heap, right?

    • @lavatasche2806
      @lavatasche2806 Рік тому

      @@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.

  • @OneMeanDragon
    @OneMeanDragon Рік тому +3

    lol squish that code

  • @AMildCaseOfCovid
    @AMildCaseOfCovid Рік тому

    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...

  • @gdgamer1232
    @gdgamer1232 Рік тому +1

    why don't you use clion btw?

  • @SpaceApe020
    @SpaceApe020 Рік тому +6

    It's me

  • @dyanosis
    @dyanosis 11 місяців тому

    "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.

  • @fappylp2574
    @fappylp2574 Рік тому

    Ha hababa habababababa

  • @suprsoup
    @suprsoup Рік тому

    Yo

  • @yrucoding
    @yrucoding Рік тому

    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.

  • @Jkauppa
    @Jkauppa Рік тому

    if you need to ask, then everything goes wrong

  • @ahmed-alnajjar
    @ahmed-alnajjar Рік тому

    *Making my own programming language in Assembly
    The Cherno: Brahhh ......... Boring
    *A game about a flying dog
    Also The Cherno: Very Interesting

  • @scififan698
    @scififan698 Рік тому +60

    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 🤞

    • @andrewf8366
      @andrewf8366 Рік тому +34

      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.

    • @arjix8738
      @arjix8738 Рік тому +3

      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)

    • @alexanderdahlin1690
      @alexanderdahlin1690 Рік тому

      @@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.

    • @scififan698
      @scififan698 Рік тому

      @@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

    • @scififan698
      @scififan698 Рік тому +1

      @@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.