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

Author Topic: How to create own classes from SFML classes  (Read 1788 times)

0 Members and 1 Guest are viewing this topic.

taspatsx

  • Newbie
  • *
  • Posts: 1
    • View Profile
How to create own classes from SFML classes
« on: February 09, 2013, 07:58:27 pm »
I'm guessing the title made you cringe from the amateurishness. But that's what I want to know, I started C++ and SFML last week, I know how to create a simple class in C++, I know how to draw, move etc. objects created from SFML classes, but what I do not know is how to combine those classes. For example if I want to make space invaders and have the player's object shoot bullets. How would I go about creating my own class that uses SFML for the player and the bullets? Do I inherit the class, like "class Player: public sf::RectangleShape" (assuming I want my player to be a rectangle)? Because that didn't work out so well.
I'm suspecting the majority of answers will be along the lines of: "Go read a book on C++".

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10991
    • View Profile
    • development blog
    • Email
Re: How to create own classes from SFML classes
« Reply #1 on: February 09, 2013, 08:35:01 pm »
As you noticed yourself you don't have a good enough 'feeling' for the language and thus you don't see how to work with them properly. The thing about C++ books is, that there are very bad ones and only a few very good ones, for the later one can find a nice list here.

As for your example, there's a good thumb rule to put composition before inheritance. Your player class should have a RectangleShape, but it doesn't necessarily need to be one. So what you want to do is make the RectangleShape a class member. If you don't know how to do that, then you're missing quite an important part of C++ knowledge which we can't really teach you. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/