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

Author Topic: Any documentation/tutorial on how to program with SFML OOP-wise?  (Read 4295 times)

0 Members and 2 Guests are viewing this topic.

Blingu

  • Newbie
  • *
  • Posts: 15
    • View Profile
Hi, I'm new to SFML and I'd like to know how to program something by doing it object oriented. The problem is, when I make a static method for rendering a window for example, the object won't be recognized outside of the method in the class. I'd like to make classes such as Graphics where I can collect all the textures, Render, Player for physics and so on. Down below you can see one of the opportunities I've tried, I'm aware that it cannot work because the object window can be only seen within the method "createWindow". I just need an advice on how it works with SFML so I can get forward. Thanks in advance.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

class Game
{
public:
static void createWindow()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
}
static void render()
{
while (window.isOpen()) //i've also tried Game::window and so on
{
window.clear();
window.display();
}
}
};
int main()
{
Game::createWindow();
Game::render();

}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Any documentation/tutorial on how to program with SFML OOP-wise?
« Reply #1 on: May 22, 2018, 05:40:22 pm »
I recommend to read a book on C++ itself, as per your example, you don't even seem to know what member variables are. The basic concept of classes (and OOP) should be understood on their own before trying to squeeze in more problems by using a library that by its own requies some more understanding.

For a good approach to SFML and game programming, I recommend the book SFML Game Development: https://www.packtpub.com/game-development/sfml-game-development
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Blingu

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Any documentation/tutorial on how to program with SFML OOP-wise?
« Reply #2 on: May 23, 2018, 04:06:02 pm »
I know what a member variable is, I also don't want to buy a book if there's a documentation and tutorials on how SFML classes work etc. I wanted to know how I can program with SFML by doing it OOP-wise, like a short tutorial that would show some examples of a class that creates a window object or something like that.

Blingu

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Any documentation/tutorial on how to program with SFML OOP-wise?
« Reply #3 on: May 24, 2018, 04:09:07 pm »
Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

class Game
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Game");
public:
//void render();
//void update();

};

int main()
{

}

I've done that but the compiler says it expects a ")", identifiers, constant and string. It's just like it wouldn't be recognized within the class.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Any documentation/tutorial on how to program with SFML OOP-wise?
« Reply #4 on: May 24, 2018, 04:17:47 pm »
I know it might be hard to accept that fact and I'm not trying to be rude, but you're level of C++ understanding simply isn't at the point, where you should be progressing with SFML. Instead you really need to learn more about classes and probably many other things first.

You can't just learn C++ or OOP or SFML with "a short tutorial". C++ is one of the more complex languages out there, has a lot of pitfalls and not the greatest tooling. I highly recommend to get a good book on C++.
OOP is a concept that might be generally described in a simple way, but actually applying it to code isn't always trivial and you really need a good understanding of the basics.
SFML has a lot of resources, starting with the official tutorials, documentation and the already linked book.

Regardless how you proceed, this forum is for questions and help requests about SFML and it's not a place to ask general C++ question nor a place where people teach you C++. For that there are other sites like StackOverflow and similar. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Any documentation/tutorial on how to program with SFML OOP-wise?
« Reply #5 on: May 25, 2018, 01:11:19 am »
Quick fix:
class Game
{
private:
        static sf::RenderWindow window;
        static sf::CircleShape circle;
public:
        static void render()
        {
                while (window.isOpen())
                {
                        window.clear();
                        circle.move(0.1f, 0.0f);
                        window.draw(circle);
                        window.display();
                }
        }
};

sf::RenderWindow Game::window(sf::VideoMode(800, 600), "Test");
sf::CircleShape Game::circle(50.0f);

int main()
{
        Game game;
        Game::render();
}
 
There are many ways to structure your code. This is just a quick fix in case you are wondering how to "make it work" modifying the code you provided the minimum as possible. I would definitely NOT recommend it.

 

anything