SFML 2.* using threads
This commit is contained in:
parent
b002b25520
commit
548d8ecf25
9
.clang-format
Normal file
9
.clang-format
Normal file
@ -0,0 +1,9 @@
|
||||
Language: Cpp
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 2
|
||||
ColumnLimit: 132
|
||||
SortIncludes: true
|
||||
AlignAfterOpenBracket: DontAlign
|
||||
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
# AllowShortNamespacesOnASingleLine: true
|
1
gui/SFML/Threads/.clang-format
Symbolic link
1
gui/SFML/Threads/.clang-format
Symbolic link
@ -0,0 +1 @@
|
||||
../../../.clang-format
|
3
gui/SFML/Threads/.gitignore
vendored
Normal file
3
gui/SFML/Threads/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# xmake
|
||||
.xmake/
|
||||
build/
|
8
gui/SFML/Threads/inc/main.hh
Normal file
8
gui/SFML/Threads/inc/main.hh
Normal 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_
|
11
gui/SFML/Threads/inc/render.hh
Normal file
11
gui/SFML/Threads/inc/render.hh
Normal 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_
|
8
gui/SFML/Threads/inc/thread.hh
Normal file
8
gui/SFML/Threads/inc/thread.hh
Normal 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_
|
52
gui/SFML/Threads/src/main.cc
Normal file
52
gui/SFML/Threads/src/main.cc
Normal 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;
|
||||
}
|
22
gui/SFML/Threads/src/render.cc
Normal file
22
gui/SFML/Threads/src/render.cc
Normal 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();
|
||||
}
|
||||
}
|
13
gui/SFML/Threads/src/thread.cc
Normal file
13
gui/SFML/Threads/src/thread.cc
Normal 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);
|
||||
}
|
||||
}
|
16
gui/SFML/Threads/xmake.lua
Normal file
16
gui/SFML/Threads/xmake.lua
Normal 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")
|
Loading…
Reference in New Issue
Block a user