Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sometimes the picture does not render  (Read 724 times)

0 Members and 1 Guest are viewing this topic.

kurator125

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
sometimes the picture does not render
« on: January 26, 2014, 03:47:01 am »
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
« Last Edit: January 26, 2014, 03:52:12 am by kurator125 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: sometimes the picture does not render
« Reply #1 on: January 26, 2014, 11:59:35 am »
Are you coming from Java programming? At least the excessive and unnessecary use of pointers and "new" calls suggest so...

All your pointers could live on the stack without problem, there's no need for them and need for new calls. Also C++ doesn't have global garbage collection, so manually allocated resources need to freed, but in the end you shouldn't use manual nemoy management anyways, instead use RAII via smart pointers.

Next there isn't really any gain in using a separated thread for rendering and logic and will generate a lof of problems, especially if you have no idea on concurrency. ;)

In the drawThread you lock the mutex twice, thus blocking you own thread.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kurator125

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: sometimes the picture does not render
« Reply #2 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
:(
« Last Edit: January 26, 2014, 03:20:40 pm by kurator125 »