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

Author Topic: I'm having a bit of a delay  (Read 2400 times)

0 Members and 1 Guest are viewing this topic.

nfect

  • Newbie
  • *
  • Posts: 16
    • View Profile
I'm having a bit of a delay
« on: October 26, 2019, 04:38:21 pm »
Hello there, I don't know if I'm missing something, is there a slight delay with the Events (Mouse) in SFML?
I have a "View", inside I have a VertexArray, and when I'm dragging it, it do so with a very short delay. I don't know if I'm doing something wrong, I need help!


void Render::mouse_moved(sf::Event event) {

        sf::Vector2i mouse_position = sf::Mouse::getPosition(window);

        if (!is_dragging) {
                int loop = (int)(Sprite::viewer_vector.size() - 1);
                for (int j = loop; j >= 0; j--) {
                        if (Sprite::viewer_vector[j]->mouse_in(mouse_position.x, mouse_position.y)) {
                                if (Sprite::viewer_vector[j] != viewer_interact) {
                                        viewer_interact = Sprite::viewer_vector[j];
                                }
                                break;
                        }
                }
                int loop_in = (int)(viewer_interact->viewer_interactable_vector.size() - 1);
                bool sprite_found = false;

                for (int k = loop_in; k >= 0; k--) {
                        if (viewer_interact->viewer_interactable_vector[k]->mouse_in(mouse_position.x, mouse_position.y)) {
                                if (viewer_interact->viewer_interactable_vector[k] != sprite_interact) {
                                        if (sprite_is_over) {
                                                sprite_interact->on_out();
                                        }
                                        sprite_interact = viewer_interact->viewer_interactable_vector[k];
                                        sprite_interact->on_over();
                                        sprite_is_over = true;
                                }
                                sprite_found = true;
                                break;
                        }
                }
                if (!sprite_found) {
                        if (sprite_is_over) {
                                sprite_interact->on_out();
                        }
                        sprite_is_over = false;
                        sprite_interact = NULL;
                }
        }
        else {
                sprite_interact->drag(mouse_position.x,mouse_position.y);
        }
}

void Sprite::drag(float v1, float v2) {

        set_color(255, 255, 0, 255);

        if (!dragging) {

                x_move = v1 - x();
                y_move = v2 - y();
                dragging = true;

        }

        x(v1-x_move);
        y(v2-y_move);

        //std::cout << name << " start dragging" << std::endl;

}

 

I believed I used the shortest way to get this done, but I still have some delay

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: I'm having a bit of a delay
« Reply #1 on: October 27, 2019, 07:40:37 am »
You shouldn't mix events and realtime input (sf::Mouse::getPosition()), you can get the position information from the event itself.

Make sure you translate the mouse position from screen coordinates to world coordinates with mapPixelToCoords(): https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1RenderTarget.php#a0103ebebafa43a97e6e6414f8560d5e3
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nfect

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: I'm having a bit of a delay
« Reply #2 on: October 27, 2019, 11:47:46 am »
Thanks for your response, Exploiter .

I'm using the event.mouseMove now, it makes the code look cleaner, but it didn't get rid of the little lag. It's very small though, nothing I can't live with! I'm even wondering if it's not due to the way I translate my vertex arrays somewhere else in the code.

With the mapToPixelCoords, I've just read :

For render-windows, this function is typically used to find which point (or object) is located below the mouse cursor.

does this mean I can retrieve any object under the cursor? I'm using Views and VertexArrays.

If so, I can't really visualize how to get it to do it, newbie here!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I'm having a bit of a delay
« Reply #3 on: October 28, 2019, 01:19:23 am »
When you convert the mouse cursor's position into co-ordinates, you get the point according to the current view (or custom view if passed as a parameter).
If you want to know which object is under the mouse, you need to compare that co-ordinate with the objects to see if they are there.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

nfect

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: I'm having a bit of a delay
« Reply #4 on: November 05, 2019, 08:05:19 pm »
thanks a lot for your answers !