SFML community forums
Help => General => Topic started by: codelyoko373 on August 26, 2017, 02:57:45 am
-
I'm quite new to SFML and the first tutorial I watched for SFML was how to basically make pong, this tutorial: https://www.youtube.com/watch?v=GHr6rFCPl5k
It this tutorial he taught me how to create this basic menu system. All menu's such as the Main Menu and the actual game would inherit this "BaseState" subclass: https://image.prntscr.com/image/Jx0fTP5SRGySmurrJ-_img.png
And then the actual class had code to manage these menus:
https://image.prntscr.com/image/radyTH_xRkuqrbcmEIHMuw.png
https://image.prntscr.com/image/7Mgd1AxhTpmxMJ6RVkixAg.png
The way you would change state is using the SetState function like this:
CoreState.SetState(new MainMenu());
but I've noticed that the new keyword is used but the created MainMenu object shown is this example isn't actually deleted anywhere, the only things that are deleted are the pointers used within these menus. It would have to be deleted wouldn't it or am I missing something?
I've tried deleting the menu with "delete this" in the destroy function that is called when the menu changes but it doesn't seem to work.
Sorry for the long post
-
For the state stack it's understandable to use heap allocation as it stores the states as pointer to base class (http://www.cplusplus.com/doc/tutorial/polymorphism/) - although modern C++ would prefer a smart pointer (aka RAII (http://bromeon.ch/articles/raii.html)) which can be stored in a vector using std::move() (http://en.cppreference.com/w/cpp/utility/move) and takes care of the new/delete problem. Members such as font and text have no need for heap allocation, and can be initialised via the constructor's initialiser list (http://en.cppreference.com/w/cpp/language/initializer_list) rather than using double initialisation (which is still true even if you do decide to use smart pointers). It seems unusual to write 'this' everywhere (to me at least), although it appears to be a preference thing - many people instead prefix members with m_ or _. If you can, check out the first SFML game development book (https://www.sfml-dev.org/learn.php#books), it has a great example of a state stack, and is probably the best of the bunch.
-
For the state stack it's understandable to use heap allocation as it stores the states as pointer to base class (http://www.cplusplus.com/doc/tutorial/polymorphism/) - although modern C++ would prefer a smart pointer (aka RAII (http://bromeon.ch/articles/raii.html)) which can be stored in a vector using std::move() (http://en.cppreference.com/w/cpp/utility/move) and takes care of the new/delete problem. Members such as font and text have no need for heap allocation, and can be initialised via the constructor's initialiser list (http://en.cppreference.com/w/cpp/language/initializer_list) rather than using double initialisation (which is still true even if you do decide to use smart pointers). It seems unusual to write 'this' everywhere (to me at least), although it appears to be a preference thing - many people instead prefix members with m_ or _. If you can, check out the first SFML game development book (https://www.sfml-dev.org/learn.php#books), it has a great example of a state stack, and is probably the best of the bunch.
Apparently the guy used "this" a lot to just show the viewers what variables he was referencing. And that state pointer is a raw pointer so surely it's bad that It's not being deleted anywhere