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

Author Topic: Event, is it a diffrent between Debug and Release mode  (Read 3708 times)

0 Members and 1 Guest are viewing this topic.

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Event, is it a diffrent between Debug and Release mode
« on: March 04, 2017, 03:19:05 pm »
Hello, I started to wondering if its a diffrent between Debug and Release when it comes to Sf::Event. When I run my program in debug mode the events are very fast(Like when I type a letter it will pop up fast). But in Release mode the events are so slow so I cant type a letter, I moust spam my keyboard around 5 - 10 seconds befor it pop up.
I wonder why it is so.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Event, is it a diffrent between Debug and Release mode
« Reply #1 on: March 04, 2017, 05:19:04 pm »
They are the same, but the compiler will optimize things in release mode.
It sounds like you have a bug in your code somewhere.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: Event, is it a diffrent between Debug and Release mode
« Reply #2 on: March 04, 2017, 05:28:49 pm »
Would you post a minimal, complete and verifiable example of this behavior?

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Event, is it a diffrent between Debug and Release mode
« Reply #3 on: March 04, 2017, 06:06:59 pm »
Okay, here is a minimal code

sf::Event event;
        while (window->pollEvent(event))
        {
                if (event.type == sf::Event::TextEntered)
                {

                       

                                if (event.text.unicode == '\b')
                                {
                                        if (s1.getSize() > 0)
                                                s1.erase(s1.getSize() - 1, 1);
                                }
                                else
                                {
                                        if (s1.getSize() < 10)
                                        {
                                                s1 += static_cast<char>(event.text.unicode);
                                                if (event.text.unicode < 128)
                                                {
                                                        this->characterName.setString(s1);
                                                }
                                        }
                                }
                }
        }
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Event, is it a diffrent between Debug and Release mode
« Reply #4 on: March 04, 2017, 06:12:23 pm »
It's minimal but not complete.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Event, is it a diffrent between Debug and Release mode
« Reply #5 on: March 04, 2017, 06:21:15 pm »
I dont know how to send a complete code of this problem. I have made so I have diffrent states and then in this state I called a update function in main.cpp between window.clear and window.display. Then I have a window pointer that comes from the main.cpp I use sf::event with.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Event, is it a diffrent between Debug and Release mode
« Reply #6 on: March 04, 2017, 06:37:22 pm »
By starting a completely new project, creating one main function, copying the relevant code over, until you get a minimal setup of your application that reproduces the problem.
Remember, if it's an issue with SFML, then it can pretty much always be reproduced with less than 100 lines of code.

It's also useful to know how to use a debugger and a profiler, with the former you can find out what is going on in your code and with the later you can find bottlenecks in your code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Event, is it a diffrent between Debug and Release mode
« Reply #7 on: March 04, 2017, 07:33:05 pm »
Okay, I will try to fix a minimal exampel and then try to find the issue, If I dont find It i will come back!  :P

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Event, is it a diffrent between Debug and Release mode
« Reply #8 on: March 05, 2017, 02:37:00 pm »
Okay, I have fix a complete try program


Main function
#include <SFML\Graphics.hpp>
#include "game_state.h"
#include "main_menu.h"

game_state coreState;

int main(int argc, char* argv[])
{
        sf::RenderWindow window(sf::VideoMode(1280, 768), "Test", sf::Style::Close);

        window.setVerticalSyncEnabled(true);
        coreState.setWindow(&window);
        coreState.SetState(new main_menu());

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        }
                }
                window.clear();

                coreState.Update();
                coreState.Rendere();

                window.display();
        }
        return 0;
}

 

game_State.h
#pragma once
#include "SFML\Graphics.hpp"
#include "SFML\Audio.hpp"
class tiny_state {
public:
        virtual void Initialize(sf::RenderWindow *window)
        {
        }
        virtual void Update(sf::RenderWindow *window)
        {
        }
        virtual void Render(sf::RenderWindow *window)
        {
        }
        virtual void Destroy(sf::RenderWindow *window)
        {
        }
};
class game_state {
public:
        game_state()
        {
                this->state = NULL;
        }
        void setWindow(sf::RenderWindow *window)
        {
                this->window = window;

        }
        void SetState(tiny_state *state)
        {
                if (this->state != NULL)
                {
                        this->state->Destroy(this->window);

                }
                this->state = state;
                if (this->state != NULL) {
                        this->state->Initialize(this->window);
                }
        }
        void Update()
        {
                if (this->state != NULL)
                {
                        this->state->Update(this->window);
                }

        }
        void Rendere()
        {
                if (this->state != NULL)
                {
                        this->state->Render(this->window);
                }
        }
private:
        sf::RenderWindow *window;
        tiny_state *state;
};
extern game_state coreState;
 

Main_menu.h
#pragma once
#include "game_state.h"

class main_menu : public tiny_state
{
public:
        void Initialize(sf::RenderWindow *window);
        void Update(sf::RenderWindow *window);
        void Render(sf::RenderWindow *window);
        void Destroy(sf::RenderWindow *window);
private:
        sf::Font font;
        sf::String s1;
        sf::Text characterName;
};
 

Main_menu.cpp
#include "main_menu.h"
#include <string>

void main_menu::Initialize(sf::RenderWindow *window)
{
        this->font.loadFromFile("Morris.ttf");
        this->characterName.setString(s1);
        this->characterName.setFont(this->font);
        this->characterName.setCharacterSize(15U);
        this->characterName.setPosition(500, 500);
}
void main_menu::Update(sf::RenderWindow *window)
{
        sf::Event event;
        while (window->pollEvent(event))
        {
                if (event.type == sf::Event::TextEntered)
                {

                        if (event.text.unicode == '\b')
                        {
                                if (s1.getSize() > 0)
                                        s1.erase(s1.getSize() - 1, 1);
                        }
                        else
                        {
                                if (s1.getSize() < 10)
                                {
                                        s1 += static_cast<char>(event.text.unicode);
                                        if (event.text.unicode < 128)
                                        {
                                                this->characterName.setString(s1);
                                        }
                                }
                        }
                }
        }
        this->characterName.setString(s1);
}

void main_menu::Render(sf::RenderWindow *window)
{
        window->draw(this->characterName);
}
void main_menu::Destroy(sf::RenderWindow *window)
{

}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Event, is it a diffrent between Debug and Release mode
« Reply #9 on: March 05, 2017, 03:43:23 pm »
You can't have two event loops. Events that are popped in the first one won't be seen in the second one.

A possible solution is to have one event loop in your main(), and then dispatch events to whoever needs them from that event loop.

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                window.close();
                break;
            }

            coreState.ProcessEvent(event); // <---------- HERE
        }
        window.clear();

        coreState.Update();
        coreState.Rendere();

        window.display();
    }
Laurent Gomila - SFML developer

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Event, is it a diffrent between Debug and Release mode
« Reply #10 on: March 06, 2017, 06:54:32 pm »
Thanks!, Got It work now!! ;D