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.


Topics - Nical

Pages: [1]
1
Graphics / Dragging whole scene
« on: May 20, 2020, 11:58:38 am »
Hi all,

I'm trying to implement a scene drag functionality. When the middle mouse button is pressed, I want to translate all my scene positions from the exact amount of pixel the mouse traveled.

Here is a POC of what I did. I represented the scene graph by a simple rectangle.

int main(int argc, char* argv[])
{
  sf::RenderWindow window(sf::VideoMode(400, 400), "Drag world");

  sf::Vector2f sceneOrigin(100., 100.);
  sf::RectangleShape scene({50., 50.});

  bool isDragging = false;
  sf::Vector2i lastMousePosition = sf::Mouse::getPosition(window);
  sf::Transform transform;

  while (window.isOpen())
  {
    sf::Event event;

    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::MouseButtonPressed)
      {
        if (event.mouseButton.button == sf::Mouse::Middle)
          isDragging = true;
      }
      else if (event.type == sf::Event::MouseButtonReleased)
      {
        if (event.mouseButton.button == sf::Mouse::Middle)
          isDragging = false;
      }
      else if (event.type == sf::Event::MouseMoved and isDragging)
      {
        auto mouseDelta =
          window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y))
          - window.mapPixelToCoords(lastMousePosition)
        ;

        transform.translate(mouseDelta);
      }
      else if (event.type == sf::Event::Resized)
      {
        window.setView(sf::View(sf::FloatRect(0., 0., event.size.width, event.size.height)));
      }
    }

    lastMousePosition = sf::Mouse::getPosition(window);

    scene.setPosition(transform.transformPoint(sceneOrigin));

    window.clear(sf::Color::Black);
    window.draw(scene);
    window.display();
  }

  return EXIT_SUCCESS;
}
 

When I test it, the rectangle (scene) is moved, but faster than the mouse cursor. It seems that my mouse delta is too high.
I tried the same thing raw mouse position (without the call to mapPixelToCoords), but the result is the same.

I don't understand what I missed here. Any help ?

2
Graphics / SFML and CEGUI (openGL3 rendering problem)
« on: May 17, 2020, 01:17:44 pm »
I'm trying to setup an SFML project with CEGUI for GUI management.

Here is a reproduction of the bug.
I removed any GUI configuration and event management since they are not part of the issue.

int main(int argc, char* argv[])
{
  sf::RenderWindow window(sf::VideoMode(400, 400), "Foo");
  sf::Font font;
  font.loadFromFile("/home/nico/sandboxes/sfml/DejaVuSansMono.ttf");

  // init CEGUI
  CEGUI::OpenGL3Renderer& guiRenderer = CEGUI::OpenGL3Renderer::bootstrapSystem();

  // stripped: CEGUI resource management
  // stripped: A window is created and set as CEGUI root window

  sf::Clock clock;

  sf::Text text;
  text.setFont(font);
  text.setCharacterSize(24);
  text.setString("hello");
  text.setPosition(50, 50);
  text.setFillColor(sf::Color::Red);

  while (window.isOpen())
  {
    sf::Event event;
    float frameTime = clock.restart().asSeconds();

    guiContext.injectTimePulse(frameTime);
    CEGUI::System::getSingleton().injectTimePulse(frameTime);

    while (window.pollEvent(event))
    {
      // stripped: CEGUI event injection
    }

    window.clear(sf::Color::Black);
    text.move(frameTime * 10, 0);
    window.draw(text);
    // render GUI
    CEGUI::System::getSingleton().renderAllGUIContexts();

    window.display();
  }

  // destroy CEGUI
  CEGUI::OpenGL3Renderer::destroySystem();

  return EXIT_SUCCESS;
}

The GUI is correctly displayed, the text is moving but is not rendered correctly:


If I remove the line triggering the GUI rendering, the text is correctly rendered and is moving.
All is fine, but I obviously I miss the GUI...

If I remove the call to sf::RenderWindow::clear(), the text is also correctly rendered, so is the GUI.
But the text is not moving anymore. The GUI is responding though.

I had a similar issue when using SFGUI, but it was solved by calling window.resetGLStates() after the RenderWindow creation.
I also tried a similar approach, but in the current case it does not change anything.

I don't know what to do at this point. Any help ?

EDIT: I replaced the CEGUI renderer from the OpenGL3 to OpenGL (seems to be version 1.2 of openGL) and the problem was solved. I still don't understand what could be goin on... I can of course stay with this renderer, but I would really like to use the more up to date version of the renderer. Is this issue is SFML related or is it CEGUI that is to blame. I may be that I am the one to blame for not using all this tools correctly...

Pages: [1]
anything