SFML community forums

Help => System => Topic started by: AndreeU17 on March 23, 2017, 06:07:06 am

Title: Not understanding threads in SFML?
Post by: AndreeU17 on March 23, 2017, 06:07:06 am
I was reading that in a Mac OS X, i'm only able to run window and handle events within the main thread. I'm not sure i understand that! According to the wiki, the main thread is main function. What classifies another form of thread? I havent touch that topic of threads in my C++ book, nor do i think they talk about it.

Here is my code and how i have it orientated.
//
//  Engine.hpp
//  RetroPongGame
//
//  Created by Jonathan Vazquez on 3/22/17.
//  Copyright © 2017 Jonathan Vazquez. All rights reserved.
//

#ifndef Engine_hpp
#define Engine_hpp

#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>

class Engine{

    public:
        Engine();
        ~Engine();
   
        void gameLoop();
        void eventHandler();
        void render();
       
   
    private:
        std::string winTitle;
        unsigned int winHeight, winWidth;
        sf::RenderWindow window;
   
};

#endif /* Engine_hpp */
 

//
//  Engine.cpp
//  RetroPongGame
//
//  Created by Jonathan Vazquez on 3/22/17.
//  Copyright © 2017 Jonathan Vazquez. All rights reserved.
//

#include "Engine.hpp"

Engine::Engine(): winTitle("Retro Pong Game v2.0"),winHeight(800),winWidth(1200){
    window.create(sf::VideoMode(winWidth, winHeight), winTitle, sf::Style::Titlebar | sf::Style::Close);
    window.setVerticalSyncEnabled(true);
}
Engine::~Engine(){
}

void Engine::gameLoop(){
    while(window.isOpen()){
        eventHandler();
    }
   
    // Clear screen
    window.clear();
   
    //rendering
    render();
   
    // Update the window
    window.display();
}

void Engine::eventHandler(){
    // Process events
    sf::Event event;
    while (window.pollEvent(event))
    {
        // Close window: exit
        if (event.type == sf::Event::Closed)
            window.close();
       
        // Escape pressed: exit
        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
            window.close();
    }
}

void Engine::render(){
    // Draw the sprite
    //window.draw(sprite);
   
    // Draw the string
    // window.draw(text);
}

#include "Engine.hpp"

// Here is a small helper for you! Have a look.
#include "ResourcePath.hpp"

int main(int, char const**)
{
   
    Engine engine;
    engine.gameLoop();
   
    return EXIT_SUCCESS;
}

Which is the main thread and which isnt? Is the thread classified by a function or by extra files?
Title: Not understanding threads in SFML?
Post by: eXpl0it3r on March 23, 2017, 08:57:12 am
If you don't use a separate thread, which you shouldn't anyways, you'll be using the main thread.
Title: Re: Not understanding threads in SFML?
Post by: Laurent on March 23, 2017, 09:08:47 am
Quote
Which is the main thread and which isnt?
Additional threads are created explicitly by you, with std::thread or sf::Thread. So if you have no idea what threads are, and thus are not using these classes, your code will only run in a single thread, the main one, and you don't have to care about threading issues at all.
Title: Re: Not understanding threads in SFML?
Post by: AndreeU17 on March 25, 2017, 09:36:02 pm
So in other words, if i'm not explicitly creating a new thread, then its all running in the main thread?

Okay if thats the case, then its all too easy. Thanks  :D
Title: Re: Not understanding threads in SFML?
Post by: Laurent on March 26, 2017, 10:18:26 am
Yes.