SFML sf::Event question

Hey everyone, so I am trying to create a simple little card game program and I'm the type of person that likes everything grouped together neatly. So I'm trying to hide my massive event list in a different file. For example it usually looks like this (Or the tutorial says it does)

#include <SFML/Window.hpp>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    sf::Window window(sf::VideoMode(800, 600), "My window");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            // All the events here
            if (event.type == sf::Event::Closed)
                window.close();
            // Ect. Ect.
        }
    }
    return 0;
}


Now that doesn't look to bad but with a bunch of event's for the window wouldn't it get kind of crowded in main?

My question is this would something like this work? Is there another way that I can store my event loop away in a class?

Events.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef EVENTS_H_INCLUDED
#define EVENTS_H_INCLUDED

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

class Events
{
    public:
        void catchEvents(sf::Window &window);

    private:
        sf::Event event;
};


#endif // EVENTS_H_INCLUDED 


Events.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Events.h"

void Events::catchEvents(sf::Window &window)
{
    while (window.pollEvent(event))
    {
        switch(event.type)
        {
            case sf::Event::Closed:
                window.close();
                break;
        }
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Events.h"

int main()
{
    sf::Window mainWindow(sf::VideoMode(1000, 800), "Window")

    Events eventMain;
    while (mainWindow.isOpen())
    {
        eventMain.catchEvents(mainWindow);
    }
    
    return EXIT_SUCCESS;
}


I can't test this out since I don't have a compiler availible right now, but in theory it seems to work right? I'm still a beginner in SFML and graphics programming though. Any help or suggestions would be nice to.
My question is this would something like this work? Is there another way that I can store my event loop away in a class?
yes it works, but i don't see any gain from it. You may consider event handler that are created according the event that occurs and you want to deal with.


you should also consider to change your nick
Topic archived. No new replies allowed.