1
General discussions / How to use drawing in main thread and read from the keyboard in another thread
« on: December 13, 2013, 06:27:04 pm »
Hi All,
I am trying to draw in main thread and read from the keyboard in another thread without success. I can draw but can not read number 5. Does anyone have any suggestions? Below my code:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
using namespace std;
bool trigger = false;
void eventThread(sf::RenderWindow* window)
{
sf::Event event;
while(trigger)
{
while (window->pollEvent(event))
{
// check the type of the event...
switch (event.type)
{
// key pressed
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Num5) {
cout << "5" << endl;
}
break;
} // end of switch
} // end of Poll Event
} // end of while of trigger
} // end of function
int main(int argc, char *argv[])
{
// create the window
sf::RenderWindow window(sf::VideoMode(1600, 900), "REG", sf::Style::Fullscreen);
window.setActive(false);
trigger = true;
sf::Thread thread(&eventThread, &window);
thread.launch();
bool quit = false;
while (!quit) {
draw my staff with success
}
trigger = false;
thread.wait();
return 0;
}
Thank you,
Uril.
I am trying to draw in main thread and read from the keyboard in another thread without success. I can draw but can not read number 5. Does anyone have any suggestions? Below my code:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
using namespace std;
bool trigger = false;
void eventThread(sf::RenderWindow* window)
{
sf::Event event;
while(trigger)
{
while (window->pollEvent(event))
{
// check the type of the event...
switch (event.type)
{
// key pressed
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Num5) {
cout << "5" << endl;
}
break;
} // end of switch
} // end of Poll Event
} // end of while of trigger
} // end of function
int main(int argc, char *argv[])
{
// create the window
sf::RenderWindow window(sf::VideoMode(1600, 900), "REG", sf::Style::Fullscreen);
window.setActive(false);
trigger = true;
sf::Thread thread(&eventThread, &window);
thread.launch();
bool quit = false;
while (!quit) {
draw my staff with success
}
trigger = false;
thread.wait();
return 0;
}
Thank you,
Uril.