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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Quit

Pages: [1]
1
The moment i clicked "post" i came to the same conclusion and have made a biggest facepalm the world has seen yet =) I wanted to delete the post but it wouldn't let me... At least I know now why there should always be a separation between getting input and updating the program state =)
One way or another, thank you for a quick reply, we can always count on you guys =) Keep up the awesome work!

2
Hello

i've encountered a strange problem and I'm not sure how to tackle it. At the moment my program consists of a renderwindow and a console(debug build). The problem is that when the render has "no events to capture" (i move mouse outside of the window region, minimize the window, or simply leave the cursor within the window but motionless) the class responsible for graphics (the one that has the renderwindow as its member) just holds execution. The messages it would print to the console won't appear until the moment i move the mouse within the render window. The messages generated by a separate class appear in the console normally. What can be the cause of this behavior? I would like my program to continue execution regardless if the window has focus or not/ if a user sits in front of the pc or not. The code that represents the program structure (and partially recreates the problem - partially because in here both classes stop "working" when the i don't move the mouse/move it outside of the window) is below

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

class Window
{
public:
    Window(): timer(2), window(sf::VideoMode(800, 600), "SFML window"){}
    timeFunc()
    {
        sf::Time measure = clock.getElapsedTime();
        if (measure.asSeconds() >= timer)
        {
            std::cout<< "window func" << std::endl;
            clock.restart();
        }

    }
    sf::RenderWindow window;
private:
    int timer;
    sf::Clock clock;
};

class Terminal
{
public:
    Terminal(): timer(2){}
    timeFunc()
    {
        sf::Time measure = clock.getElapsedTime();
        if (measure.asSeconds() >= timer)
        {
            std::cout<< "terminal func" << std::endl;
            clock.restart();
        }
    }
private:
    int timer;
    sf::Clock clock;
};

class base
{
public:
    base(){running = true;}
    Window window;
    Terminal terminal;
    bool running;
};

int main()
{
    base a;
    while(a.running)
    {
        sf::Event event;
        while(a.window.window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                a.running = false;

            a.window.timeFunc();
            a.terminal.timeFunc();

            a.window.window.clear();
            a.window.window.display();
        }
    }

    return 0;
}

3
Window / Re: TextEntered generates multiple events
« on: March 10, 2016, 10:01:48 pm »
Quote
There is no event loop here.
You are polling once, then processing the entire frame using that one event. This isn't technically incorrect but it's not taking into account if pollEvent() fails. The event is no longer valid as it's still the same event as it was before and you're still processing it as if it's just received a new one. This is why all event processing usually goes within a specific event loop that only processes the event if polling was successful.

Another lesson in humility, another lesson in careful analysis. Naturally, in my prototype I had
Code: [Select]
sf::Event event;
while (window.pollEvent(event))
hence it worked like a charm. Added a while loop within while(mRunning) and bingo. You, Sir, are a kind genius and a scholar.

Quote
Also, you might consider creating (or resetting) the event each frame, just before the event polling. That way, the event is - at least - in its default state, rather than its previous state.

However, since an event can't hold "no event has occurred" (and why would it?), you would be processing the event as if the default event has occurred (which, I think would be Closed so if this closes your window immediately, you can see why)

I'm not sure if I follow here. I tried both versions now and they behaved in a same way. Could you explain the potential benefits of resetting the event object prior to each poll operation?

4
Window / Re: TextEntered generates multiple events
« on: March 10, 2016, 07:26:27 pm »
Quote
also output multiple times when you press a key?

unfortunately yes. just tapped 'a' and the console is full of output (23 messages to be exact)

Quote
It's not at all obvious as you didn't show it.

sorry, didn't mean it like that. I meant that if it wasn't in a loop, the app would freeze right after a startup, not produce an excessive amount of input.

Quote
Are you polling the events in a different thread?

No, everything runs in one main thread. It is organized more/less in the following way

Quote
int main()
{
     Program program;  //this object contains the sf::Event mEvent and sf::RenderWindow* mWindow object, which is then shared with all other subsystems

     program.run();
}

Quote
program.run()
{
                while (mRunning)
                {
                                mWindow->pollEvent(mEvent);
                                if (mEvent.type == sf::Event::Closed)
                                                mRunning = false;

                                mLoadedSoftware->fillPages();
                                mLoadedSoftware->updatePages();
                                mLoadedSoftware->drawPages();
                }
}

and then in Software object, to which the pointer to mEvent is passed, we have updatePages()

Quote
void Software::updatePages()
{
   mActiveState->updatePages();
}

which for each state calls it's own input handling. Maybe also worth noting is that all the input, regardless of the state, behaves in the same way: i.e. one mouse click generates tens of click events. But all of this was easy to work around with bool triggers. For typing it's no good as using the bool trigger 'pressed/unpressed' makes the program miss half of the letters typed

edit: also, right after creating a new sf::RenderWindow object i put
   mWindow->setKeyRepeatEnabled(false);
but it doesn't change a thing in the program's behavior

5
Window / Re: TextEntered generates multiple events
« on: March 09, 2016, 02:52:36 pm »
Yup, obviously it is. I don't think there is smth wrong with the loop itself. For one, other functionalities that are already implemented and that are based on input work just fine (clicking on buttons, turning pages with arrow keys). Moreover, the code that i pasted actually comes straight from my small prototype where i was checking how i would go about text edition and back then it worked as expected (one button press = one 'type' event). The only difference is that in the prototype the unicode characters were being pushed into a vector of char's when you were typing. The vector was then fed to a stringstream via for loop and turned into a string that was used to update sf::Text object. In this version, the unicode chars are pushed directly into the string that is the source for an sf::Text object. I'm no expert but i don't think this should have any influence on how the input is 'gathered'. As if that weren't enough, in prototype version i didn't even disable the key repeat in the window object. The only difference i believe may have ANY impact is that in the prototype i was polling events directly on a renderwindow object, while in this version it's a pointer and window is created via "new" tag

6
Window / TextEntered generates multiple events
« on: March 09, 2016, 12:02:15 am »
Hi there. I'm trying to make a notepad-like app, and i'm stuck at the core function -entering text. What happens is that whenever i press a "text" key on my keyboard, instead of one letter, at least 10 are added to the output string (immediately, without any delay). I tried disabling key repeat, i tried some weird bool flags to check if a key was 'unpressed' before you could press it again. The function for text capture goes as follows
if (mEvent->type == sf::Event::EventType::TextEntered)
        {
               
                if (mEvent->text.unicode < 256)
                {
                        if (mEvent->text.unicode ==  8)
                        {
                                size_t i = std::distance(mBuffer.begin(), mIt);

                                if (i == 0 || i > mBuffer.size())
                                {
                                }
                                else
                                        mIt = mBuffer.erase(mBuffer.begin() + (i - 1));
                        }
                        else if (mEvent->text.unicode == 13)
                        {
                                mIt = mBuffer.insert(mIt, '\n') + 1;
                        }
                        else
                        {
                                mIt = mBuffer.insert(mIt, static_cast<char>(mEvent->text.unicode)) + 1;
                                printf("%s\n", mBuffer.c_str());
                        }
                }
        }
 
mBuffer is an std::string, mIt is an std::string::iterator

Pages: [1]
anything