1
General / Re: sometimes the picture does not render
« on: January 26, 2014, 03:10:33 pm »
was programming in c++ later in C# and now again in C++;
Thanks for the information about RAII;
I changed it to one thread and changed the pointers and still sometimes is white screen(Only when trying to open more than one window);
int main(int argc, char* argv[])
{
game *gra = new game();
gra->start();
game *gra2 = new game();
gra2->start();
game *gra3 = new game();
gra3->start();
game *gra23 = new game();
gra23->start();
for (;;);
return 0;
}
class game
{
public:
sf::RenderWindow window;
sf::View widok1;
obiektList listacial;
sf::Thread *tmainThread;// (&game::drawThread, this);
void mainThread();
void start();
game();
~game();
};
void game::start()
{
tmainThread->launch();
}
game::game()
{
tmainThread = new sf::Thread(&game::mainThread, this);
}
game::~game()
{
}
void game::mainThread()
{
window.create(sf::VideoMode(1600, 900), "SFML works!");
widok1.setCenter(-800,-450);
widok1.setSize(1600,900);//(sf::FloatRect(-800, -450, 1600, 900));
widok1.zoom(10.f);
window.setView(widok1);
listacial.add(
10000000.0f,
sf::Vector2f(12000.0f, 1000.0f),
sf::Vector2f(-6000.0f, 1500.0f),
sf::Color::Blue,
50.0f
);
listacial.add(
99900000000.0f,
sf::Vector2f(0.0f, 0.0f),
sf::Vector2f(0.0f, 0.0f),
sf::Color::Green,
70.0f
);
listacial.add(
100000.0f,
sf::Vector2f(12000.0f, 1600.0f),
sf::Vector2f(-6200.0f, 1500.0f),
sf::Color::Red,
20.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);
}
}
if (window.isOpen())
{
window.clear();
window.setView(widok1);
listacial.draw2(window);
window.display();
}
}
}
it had to be quickly written and the code is:
-not readable
-does not work properly with more than one window
Thanks for the information about RAII;
I changed it to one thread and changed the pointers and still sometimes is white screen(Only when trying to open more than one window);
int main(int argc, char* argv[])
{
game *gra = new game();
gra->start();
game *gra2 = new game();
gra2->start();
game *gra3 = new game();
gra3->start();
game *gra23 = new game();
gra23->start();
for (;;);
return 0;
}
class game
{
public:
sf::RenderWindow window;
sf::View widok1;
obiektList listacial;
sf::Thread *tmainThread;// (&game::drawThread, this);
void mainThread();
void start();
game();
~game();
};
void game::start()
{
tmainThread->launch();
}
game::game()
{
tmainThread = new sf::Thread(&game::mainThread, this);
}
game::~game()
{
}
void game::mainThread()
{
window.create(sf::VideoMode(1600, 900), "SFML works!");
widok1.setCenter(-800,-450);
widok1.setSize(1600,900);//(sf::FloatRect(-800, -450, 1600, 900));
widok1.zoom(10.f);
window.setView(widok1);
listacial.add(
10000000.0f,
sf::Vector2f(12000.0f, 1000.0f),
sf::Vector2f(-6000.0f, 1500.0f),
sf::Color::Blue,
50.0f
);
listacial.add(
99900000000.0f,
sf::Vector2f(0.0f, 0.0f),
sf::Vector2f(0.0f, 0.0f),
sf::Color::Green,
70.0f
);
listacial.add(
100000.0f,
sf::Vector2f(12000.0f, 1600.0f),
sf::Vector2f(-6200.0f, 1500.0f),
sf::Color::Red,
20.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);
}
}
if (window.isOpen())
{
window.clear();
window.setView(widok1);
listacial.draw2(window);
window.display();
}
}
}
it had to be quickly written and the code is:
-not readable
-does not work properly with more than one window