Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying SFML Blueprints
  • Table Of Contents Toc
  • Feedback & Rating feedback
SFML Blueprints

SFML Blueprints

By : Barbier
3.8 (4)
close
close
SFML Blueprints

SFML Blueprints

3.8 (4)
By: Barbier

Overview of this book

This book is for developers who have knowledge of the basics of the SFML library and its capabilities in 2D game development. Minimal experience with C++ is required.
Table of Contents (10 chapters)
close
close
9
Index

Adding multithreading to our games


We will now modify our Gravitris to paralyze the physics calculations from the rest of the program. We will need to change only two files: Game.hpp and Game.cpp.

In the header file, we will not only need to add the required header, but also change the prototype of the update_physics() function and finally add some attributes to the class. So here are the different steps to follow:

  1. Add #include <SFML/System.hpp>, this will allow us to have access to all the classes needed.

  2. Then, change the following code snippet:

    void updatePhysics(const sf::Time& deltaTime,const sf::Time& timePerFrame);

    to:

    void updatePhysics();

    The reason is that a thread is not able to pass any parameters to its wrapped function so we will use another solution: member variables.

  3. Add the following variables into the Game class as private:

    sf::Thread _physicsThread;
    sf::Mutex _mutex;
    bool _isRunning;
    int _physicsFramePerSeconds;

    All these variables will be used by the physics thread...

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
SFML Blueprints
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon