SFML community forums

Help => General => Topic started by: Eritey on March 21, 2013, 11:53:47 pm

Title: Questions regarding C++, learning to use the language correctly.
Post by: Eritey on March 21, 2013, 11:53:47 pm
After learning SFML and playing around with it for just over a year, I've been making my own mini engine for me to use to make basic games. Even though it works well, I feel its extremely poorly programmed, I see a lot of people saying that manual memory allocation (= new object) is terrible and you should never use it.

I use it ALOT, so I feel its about the right time to change my ways and learn to program correctly. I feel as though uploading the code I've created so far would be the smart option so you can all see how and where I've gone wrong but I'm not really sure the best method.

Link to my git: https://github.com/ChrisMelling/basicGame

The way I went about the design was. Each GameState knows of the application class and methods to access its information then each Entity knows about the state it was created in, so therefore the entities are able to access practically all methods in the game, all the back to the Application class.

This seem'd to make sense in my head but you're all about too rip it to shreds but I welcome it with open learning arms.

Edit: I fear I'm going to be shouted at a lot for doing this, but how terrible is it to use reinterpret_cast/dymanic_cast?
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: eXpl0it3r on March 22, 2013, 12:26:51 am
Should I simply upload the project, or use something similar to git?
It really depends on what you want to achieve.
If you just want some code review, then a simple upload might be enough, but if you actually want to work on the engine, flatten out the problems other people point out or even get contributions, then you might want to use git or similar.

Besides that, it's anyways very useful to use git when working on your engine, so you will never lose your work and can test things and revert back, without any problem.

Edit: I fear I'm going to be shouted at a lot for doing this, but how terrible is it to use reinterpret_cast/dymanic_cast?
For the dynamic_cast it largely depends on what your doing and reinterpret_cast usually points out, that something with your code design is really wrong. With a good design you should be able to get around both of them.
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: Eritey on March 22, 2013, 01:31:31 am
After lots of failing, finally got it uploaded to git.

Question, I'm going to change all constructors in the project to use initialization lists but I have an issue.

If the super classes constructor is as follows;

Entity::Entity(GameState *state)
        :_isLoaded(false), _state(state), alive(true)
{

}

What do I do a child classes constructor? I've attempted lots of different variations but none seem to be working, this was the closest I got to expecting it too work.

Player::Player(GameState *state)
        : score(0), health(100), xp(0), rank(0), bulletSpeed(600),triShot(false),rapidFire(false)
        : Entity(state) : name(entityPlayer), collidable(true)

I originally had name and collidable as part of the player constructor and it errored but then was curious at, because they're part of the entity class if they should go with that? Either way nothing worked and it left me back at the beginning of the error.

SLIGHT FIX:

Player::Player(GameState *state)
        : Entity(state), score(0), health(100), xp(0), rank(0), bulletSpeed(600),triShot(false),rapidFire(false),_colour(sf::Color(128,255,86,255))
{

Complies without error but I'm unable to include the boolean "collidable" or the enum "name" as they appear to error no matter where I put them, So how does one include variables that are in the superclass using initialization lists?
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: Eritey on March 23, 2013, 12:38:27 pm
Is my code that terrible that no one wants to even attempt helping me?
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: Nexus on March 23, 2013, 01:03:34 pm
People usually don't have the time and motivation to dig into whole projects. You should rather ask specific questions, and provide minimal and complete code (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368). If we have to lookup an external page in order to understand your problem, you're doing it wrong.

In your case, it looks like you should read more about classes, inheritance and constructor initializer lists. I'm sure a lot of questions would be answered if you invested some time into C++ literature. Have you got a good C++ book? Tutorials and video tutorials in particular are very questionable, they often omit important details.
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: Roose Bolton of the Dreadfort on March 25, 2013, 06:03:57 pm
People say not to use 'New' or 'Delete' but don't forgot to fully learn about them and about memory management including 'Memcpy' and 'Memset' etc.. as these are all used fluently in the real Games Industry, if you ever go for an assessment day for a job at EA or Sony (and I can tell you from experience) they will completely hack at your low level memory knowledge and wont even care if you know how to use unique_ptr or any of the STL library at that.
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: Gobbles on March 26, 2013, 01:44:23 am
People say not to use 'New' or 'Delete' but ...
This confuses me, why would you not use Dynamic Memory?
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: OniLinkPlus on March 26, 2013, 02:11:08 am
People say not to use 'New' or 'Delete' but ...
This confuses me, why would you not use Dynamic Memory?
Because it's very dangerous and easy to screw up, and should be held off until you're comfortable with the language. Not to mention, newbies have a tendency to use new and delete everywhere they possibly can, when 99% of the time you either don't need it or are doing it wrong.
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: Gobbles on March 26, 2013, 02:39:10 am
Because it's very dangerous and easy to screw up, and should be held off until you're comfortable with the language. Not to mention, newbies have a tendency to use new and delete everywhere they possibly can, when 99% of the time you either don't need it or are doing it wrong.
[/quote]

Right, My mistake. Been a long time since I've seen a reserved approach towards it. I do completely agree, it can be tricky to get right and add layers of complication, but if you don't experiment and play around with it you'll never get that opportunity to get it right at all.
Title: Re: Questions regarding C++, learning to use the language correctly.
Post by: eXpl0it3r on March 26, 2013, 10:09:33 am
There's simply put no need for manual memory management in modern C++ anymore. Getting new/delete correctly done is nearly impossible and if you get more than just a few things to take care of, you've already lost. I always like to point towards Nexus' example in "Why RAII rocks!" thread (http://en.sfml-dev.org/forums/index.php?topic=9359.msg63566#msg63566).

But on topic: Yeah the codebase is quite big and nobody really wants to spend their time going through everything.
I just randomly opened some files and looked at them. If I'm not mistake I already found some memory leaks (https://github.com/ChrisMelling/basicGame/blob/master/test2/Quadtree.cpp#L28) in the Quadtree (new gets called, but delete doesn't), except if you delete the stuff outside of the class, which would be a horrible thing to do.
And another memory leak in GameObjectManager (https://github.com/ChrisMelling/basicGame/blob/master/test2/GameObjectManager.cpp#L9).

#include "SFML\System.hpp"
Two things are wrong here. 1) It should be #include <> 2) Don't EVER use \ for paths. / are supported with all compilers.

Use references over pointers.

Apply some const correctness.

And I think there's a lot more, that can get improved, but I don't want to go through the whole code. ;)