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

Author Topic: Not understanding threads in SFML?  (Read 5310 times)

0 Members and 1 Guest are viewing this topic.

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Not understanding threads in SFML?
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Not understanding threads in SFML?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Not understanding threads in SFML?
« Reply #2 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.
Laurent Gomila - SFML developer

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Not understanding threads in SFML?
« Reply #3 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Not understanding threads in SFML?
« Reply #4 on: March 26, 2017, 10:18:26 am »
Yes.
Laurent Gomila - SFML developer

 

anything