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

Author Topic: Proper way to use classes and user created files  (Read 1133 times)

0 Members and 1 Guest are viewing this topic.

Scotchdew

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Proper way to use classes and user created files
« on: April 17, 2016, 01:12:03 am »
Hey guys. I'm going over classes and I'm not sure how I'm supposed to use them. Are classes only supposed to have one job? For example, if I create a class to create a game window, should that class *just* create a game window like this:

#include "window.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
 void gameWindow::createWindow()
    {
    sf::RenderWindow window;
    window.create(sf::VideoMode(500, 500), "Move the Ball");
    window.display();


    while (window.isOpen())
     {
        sf::Event event;
        while (window.pollEvent(event))
         {
        if (event.type == sf::Event::Closed)
            window.close();
         }

     }
    }

I'm creating a game called "Move the Ball" (settle down, you'll get to play it soon enough) and I'm literally just trying to move a ball around in a window but I've already run into a problem. If this window.cpp and window.h strictly has one job to create a window, I can't access the window object from Renderwindow.window to use in my ball.cpp to actually draw the ball in this window. Am I doing this right or do I have the idea of uses classes totally wrong? Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Proper way to use classes and user created files
« Reply #1 on: April 17, 2016, 12:03:35 pm »
If you have no idea how to use classes, then I suggest you get a good C++ book and learn it. SFML provides an easy API, but that doesn't mean one doesn't have to understand the basics of the programming language. There are so many resources out there that will teach you C++ better than some quick forum post ever could, so use that.
Besides we try to focus on SFML specific problems and don't have the capacity to also teach people C++. ;)

As a general tip: Don't just use classes because you read about classes, but understand what advantage they bring if used in the proper context.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: Proper way to use classes and user created files
« Reply #2 on: April 19, 2016, 05:50:54 am »
My advice would be to keep your window loop and objects in your main() function for now.

You should start learning classes by creating a class that has very little responsibility instead of managing your game window.  Over time, as you learn about classes you can make more complex classes and you'll have the confidence to let them do more complex things.