I want to use multi-thread for a level editor with test levels feature, now i want to test multi-thread on this piece of code:
#include <TGUI/TGUI.hpp>
#include <SFML/System.hpp>
sf::View Canvas_view;
// Move canvas view
void Move()
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
Canvas_view.move(0, -0.2f);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
Canvas_view.move(0, 0.2f);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
Canvas_view.move(-0.2f, 0);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
Canvas_view.move(-0.2f, 0);
}
sf::Texture texture;
sf::Sprite sprite;
// Test thread
void test(tgui::Canvas::Ptr canvas)
{
Move();
canvas->clear();
canvas->draw(sprite);// Draw sprites
canvas->setView(Canvas_view);// Set view
canvas->display();
}
int main()
{
sf::RenderWindow Window;
Window.create(sf::VideoMode(800, 400), "app", sf::Style::Default);
tgui::Gui App;
App.setTarget(Window);
sf::View view = Window.getDefaultView();
// Canvas creaton
tgui::Canvas::Ptr canvas = tgui::Canvas::create();
canvas->setPosition(100, 100);
canvas->setSize(200, 200);
canvas->clear();
App.add(canvas, "Canvas");
texture.loadFromFile("test.png");
sprite.setTexture(texture);
sf::Event event;
while (Window.isOpen())
{
while (Window.pollEvent(event))
{
// General stuff
if(event.type == sf::Event::Closed)
Window.close();
if(event.type == sf::Event::Resized)
{
view.reset(sf::FloatRect(0 , 0, Window.getSize().x, Window.getSize().y));
App.setView(view);
}
App.handleEvent(event);
}
Window.clear();
App.draw();
Window.setView(view);
Window.display();
}
return 0;
}
And i don't know how to do it properly without crashing the whole program or the computer