SFML 2.* using threads

This commit is contained in:
Alexander Popov 2025-03-03 03:55:46 +03:00
parent b002b25520
commit 548d8ecf25
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
10 changed files with 143 additions and 0 deletions

9
.clang-format Normal file
View File

@ -0,0 +1,9 @@
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 2
ColumnLimit: 132
SortIncludes: true
AlignAfterOpenBracket: DontAlign
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
# AllowShortNamespacesOnASingleLine: true

View File

@ -0,0 +1 @@
../../../.clang-format

3
gui/SFML/Threads/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# xmake
.xmake/
build/

View File

@ -0,0 +1,8 @@
#ifndef MAIN_HPP_
#define MAIN_HPP_
inline bool isRunning = true;
inline float x_pos = 0;
inline float y_pos = 0;
#endif // #ifndef MAIN_HPP_

View File

@ -0,0 +1,11 @@
#ifndef RENDER_HPP_
#define RENDER_HPP_
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
void renderingThread(sf::RenderWindow *window);
#endif // #ifndef RENDER_HPP_

View File

@ -0,0 +1,8 @@
#ifndef THREAD_HPP_
#define THREAD_HPP_
#include <thread>
void testThread(const std::string message, const std::chrono::milliseconds duration);
#endif // #ifndef THREAD_HPP_

View File

@ -0,0 +1,52 @@
#include <cstdlib>
#include <thread>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "main.hh"
#include "render.hh"
#include "thread.hh"
#include <X11/Xlib.h>
using namespace std::chrono_literals;
int main(int argc, char const *argv[]) {
XInitThreads();
sf::RenderWindow window;
window.create(sf::VideoMode(320, 240), "GUI", sf::Style::Default);
window.setActive(false);
window.setFramerateLimit(60);
std::thread firstThread(testThread, std::string("firstThread while"), 200ms);
std::thread secondThread(testThread, std::string("secondThread while"), 300ms);
std::thread render(renderingThread, &window);
sf::Event event;
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
isRunning = false;
window.close();
}
/* Обработчик клавиш */
if (event.type == sf::Event::KeyPressed) {
// Закрывает программу при нажатии Escape
if (event.key.code == sf::Keyboard::Escape) {
isRunning = false;
window.close();
}
}
}
}
render.join();
firstThread.join();
secondThread.join();
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,22 @@
#include "render.hh"
#include "main.hh"
void renderingThread(sf::RenderWindow *window) {
window->setActive(true);
while (window->isOpen()) {
if (!isRunning) break;
window->clear(sf::Color(16, 16, 36));
sf::CircleShape shape(50.f);
shape.setFillColor(sf::Color(100, 250, 50));
shape.setPosition({x_pos, y_pos});
window->draw(shape);
if (x_pos <= 320) x_pos++;
else x_pos = 0;
window->display();
}
}

View File

@ -0,0 +1,13 @@
#include "thread.hh"
#include "main.hh"
#include <iostream>
using namespace std::chrono_literals;
void testThread(const std::string message, const std::chrono::milliseconds duration) {
while (isRunning) {
std::cout << message << std::endl;
std::this_thread::sleep_for(duration);
}
}

View File

@ -0,0 +1,16 @@
set_project("sfml-threads")
set_languages("cxx17")
add_rules("mode.debug", "mode.release")
add_includedirs("inc")
target("gui")
if is_mode("debug") then
set_symbols("debug")
set_optimize("none")
end
set_kind("binary")
add_syslinks("m", "stdc++", "pthread", "X11")
add_syslinks("sfml-graphics", "sfml-audio", "sfml-window", "sfml-system")
add_files("src/*.cc")