Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Questions regarding C++, learning to use the language correctly.  (Read 4326 times)

0 Members and 2 Guests are viewing this topic.

Eritey

  • Newbie
  • *
  • Posts: 12
    • View Profile
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?
« Last Edit: March 22, 2013, 01:18:25 am by Eritey »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Questions regarding C++, learning to use the language correctly.
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Eritey

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Questions regarding C++, learning to use the language correctly.
« Reply #2 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?
« Last Edit: March 22, 2013, 03:58:12 am by Eritey »

Eritey

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Questions regarding C++, learning to use the language correctly.
« Reply #3 on: March 23, 2013, 12:38:27 pm »
Is my code that terrible that no one wants to even attempt helping me?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Questions regarding C++, learning to use the language correctly.
« Reply #4 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. 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.
« Last Edit: March 23, 2013, 01:06:36 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: Questions regarding C++, learning to use the language correctly.
« Reply #5 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.
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Questions regarding C++, learning to use the language correctly.
« Reply #6 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?

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Questions regarding C++, learning to use the language correctly.
« Reply #7 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.
I use the latest build of SFML2

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Questions regarding C++, learning to use the language correctly.
« Reply #8 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Questions regarding C++, learning to use the language correctly.
« Reply #9 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.

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

#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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything