Hi i have problem
sometimes the picture does not render (all is white)
this is my code(not all):
//main file:
#include "obiekt.h"
#include <iostream>
#include <functional>
#include <Windows.h>
int main(int argc, char* argv[])
{
game *gra1 = new game();
gra1->start();
game *gra2 = new game();
gra2->start();
for (;;);
return 0;
}
//obiekt.h
class game
{
public:
sf::RenderWindow *window;
sf::View *widok1;
sf::Mutex *mutex;
obiektList *listacial;
sf::Thread *tmainThread;// (&game::mainThread, this);
sf::Thread *tdrawThread;// (&game::drawThread, this);
void mainThread();
void drawThread();
void start();
game();
~game();
};
//obiekt.cpp
void game::start()
{
tmainThread->launch();
}
game::game()
{
tmainThread = new sf::Thread(&game::mainThread, this);
tdrawThread = new sf::Thread(&game::drawThread, this);
mutex = new sf::Mutex();
}
game::~game()
{
}
void game::mainThread()
{
mutex->lock();
listacial = new obiektList();
window = new sf::RenderWindow(sf::VideoMode(1600, 900), "SFML works!");
widok1 = new sf::View(sf::FloatRect(-800, -450, 1600, 900));
window->setActive(false);
widok1->zoom(10.f);
window->setView(*widok1);
mutex->unlock();
tdrawThread->launch();
listacial->add(
598910000.0f,
sf::Vector2f(0.0f, 0.0f),
sf::Vector2f(0.0f, 0.0f),
sf::Color::Yellow,
50.0f
);
listacial->add(
198910000.0f,
sf::Vector2f(0.0f, 2000.0f),
sf::Vector2f(2000.0f, 0.0f),
sf::Color::Yellow,
50.0f
);
bool mouseLeft = false;
sf::Vector2f startMousePos;
sf::Event event;
while (window->isOpen())
{
while (window->pollEvent(event))
{
if (event.type == sf::Event::Closed)
window->close();
if (event.type == sf::Event::MouseWheelMoved)
{
widok1->zoom(1.0f + (((float)event.mouseWheel.delta) / 10.0f));
}
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
sf::Vector2i windowPos = sf::Mouse::getPosition(*window);
startMousePos = window->mapPixelToCoords(windowPos);
mouseLeft = true;
}
}
if (event.type == sf::Event::MouseButtonReleased)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
mouseLeft = false;
}
}
if (event.type == sf::Event::MouseMoved && mouseLeft)
{
sf::Vector2i windowPos = sf::Mouse::getPosition(*window);
sf::Vector2f tempPos = window->mapPixelToCoords(windowPos);
tempPos = startMousePos - tempPos;
widok1->move(tempPos.x, tempPos.y);
}
}
}
}
void game::drawThread()
{
mutex->lock();
while (1)
{
if (window->isOpen())
{
mutex->lock();
std::cout << "mutex2 lock" << std::endl;
window->clear();
std::cout << "clear" << std::endl;
window->setView(*widok1);
listacial->draw2(*window);
window->display();
mutex->unlock();
std::cout << "drawing " << std::endl;
}
}
mutex->unlock();
}
SOMETIMES the picture does not render because in drawThread()
all where is window->...() thread is stopped and i dont know why
Can anyone tell me why? and what is wrong?
when the picture is white, on console i can see:
"mutex2 lock"
"mutex2 lock"
only
when all is correct
i can see: "mutex2 lock" and "clear" and "drawing" many times