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

Author Topic: How to make edge scrolling work with windowed mode?  (Read 3059 times)

0 Members and 1 Guest are viewing this topic.

deso

  • Newbie
  • *
  • Posts: 13
    • View Profile
How to make edge scrolling work with windowed mode?
« on: October 23, 2015, 03:13:47 am »
Hey.

Right now, my the game detects the edge of the screen (pixel 0 and pixel window_width), but once you go past it, it stops the scrolling.

I checked with std::cout, and it keeps registering the positions as negative, when out of screen, and that's the expected behaviour, but for some reason, it doesn't keep scrolling.

It works fine in fullscreen mode, but that's horrible for debugging, and I'm sure many players will want to play the game windowed.

How to solve this?

Here's the code:

Quote
      sf::Vector2i localPosition = sf::Mouse::getPosition(*window);
      sf::Vector2f view_position = view.getCenter();

      //Scrolling the map
      if (localPosition.x <= 0 && view_position.x >= view.getSize().x/2 +15) view.move(-15, 0);
      if (localPosition.x >= WINDOW_SIZE.x-1 && view_position.x <= map_size_pixels.x - view.getSize().x/2 -15) view.move(15, 0);      
      if (localPosition.y <= 0 && view_position.y >= view.getSize().y/2 +15) view.move(0, -15);
      if (localPosition.y >= WINDOW_SIZE.y-1 && view_position.y <= map_size_pixels.y - view.getSize().y/2 -15) view.move(0, 15);

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 878
    • View Profile
Re: How to make edge scrolling work with windowed mode?
« Reply #1 on: October 23, 2015, 11:11:55 am »
You'll essentially need this (or implement it yourself):

https://github.com/SFML/SFML/pull/614

Note that this branch has merge conflicts right now, but feel free to pull the code, fix the conflicts, then submit a PR with the fixes. :)

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to make edge scrolling work with windowed mode?
« Reply #2 on: October 23, 2015, 11:54:38 am »
I'm using View.Center property instead of MoveTo and camera moving across the map just perfectly with no any issues. It works in windowed and fullscreen mode and even with switch between these modes.

deso

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to make edge scrolling work with windowed mode?
« Reply #3 on: October 23, 2015, 02:23:20 pm »
I'm using View.Center property instead of MoveTo and camera moving across the map just perfectly with no any issues. It works in windowed and fullscreen mode and even with switch between these modes.

It works even if you go beyond the border of the window? Care to share that piece of code? :)

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: How to make edge scrolling work with windowed mode?
« Reply #4 on: October 24, 2015, 05:22:39 am »
I also move the camera the same way as you, using sf::Mouse::getPosition(), sf::View::getCenter() and sf::View::move(), and I have no problems with scrolling in windowed mode if the mouse is outside the window (on Windows 7 at least). In fact, I had to limit the scrolling to only happen if the cursor is inside the window because it's annoying to me when it scrolls when I move the mouse outside to do something else. Keep this in mind for your game, you could add an option to toggle "outside-of-window-scrolling" on and off, some people (like me) may appreciate it :P

I can't see anything wrong with your code (asumming that's the real one), so I can't really help you. Maybe it's something silly like both "if" statements happening at once for some strange reason, moving the view both left and right at the same time so they cancel each other out. You said that using std::cout shows the positions as negative, which is good, so maybe is the second part of the condition the problem (view_position.x >= view.getSize().x/2 +15, etc.).

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to make edge scrolling work with windowed mode?
« Reply #5 on: October 24, 2015, 12:35:39 pm »
I'm using View.Center property instead of MoveTo and camera moving across the map just perfectly with no any issues. It works in windowed and fullscreen mode and even with switch between these modes.

It works even if you go beyond the border of the window? Care to share that piece of code? :)

Yes, it works even if you move mouse outside window. I recalculate point on the map according to the mouse current position and in such case I get a point on the map which is actually outside of visible area. Then I move center of camera to the obtained point with damping filter. At render loop I just assign camera center to View.Center and thats it. So, I get smoothness and inertial camera moving.
Actually I'm also using camera zoom. I'm implemented it by assigning zoom factor to View.Zoom.
And also I assign RenderWindow.Size to View.Size to eliminate dependency of scale from window size.

here is how I implemented it:
public class Game
{
        private RenderWindow _window;


        void UpdateMouse()
        {
                var mousePos = (Vector2f)Mouse.GetPosition(_window);
                var worldPos = _camera.ViewToWorld(mousePos, (Vector2f)_window.Size);
                _camera.MoveTo(worldPos);
        }

        //...

        public void Render()
        {
                 _rendererWorld.Render(_window, _camera, ...);
        }
}

public class Camera
{
        public Vector2f Center { get; private set; }
        public float Scale { get; private set; }
       
        public Vector2f ViewToWorld(Vector2f point, Vector2f viewSize)
        {
                 return Center + (point - viewSize / 2F) / Scale;
        }

        //...
}

public class RendererWorld
{
        public void Render(
            RenderTarget target,
            Camera camera,
            ...)
        {
            _view.Size = (Vector2f)target.Size;
            _view.Zoom(1F / camera.Scale);
            _view.Center = camera.Center;
            target.SetView(_view);

            // ...
        }        
}
 
« Last Edit: October 24, 2015, 01:07:13 pm by mkalex777 »

deso

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to make edge scrolling work with windowed mode?
« Reply #6 on: October 25, 2015, 06:16:57 pm »
I also move the camera the same way as you, using sf::Mouse::getPosition(), sf::View::getCenter() and sf::View::move(), and I have no problems with scrolling in windowed mode if the mouse is outside the window (on Windows 7 at least). In fact, I had to limit the scrolling to only happen if the cursor is inside the window because it's annoying to me when it scrolls when I move the mouse outside to do something else. Keep this in mind for your game, you could add an option to toggle "outside-of-window-scrolling" on and off, some people (like me) may appreciate it :P

I can't see anything wrong with your code (asumming that's the real one), so I can't really help you. Maybe it's something silly like both "if" statements happening at once for some strange reason, moving the view both left and right at the same time so they cancel each other out. You said that using std::cout shows the positions as negative, which is good, so maybe is the second part of the condition the problem (view_position.x >= view.getSize().x/2 +15, etc.).

It's the real code, and guess what, if I copy that code to other project, it works. In the current project it works only on two directions (right and down). To the left and up I have to find the exact 0 pixel for it to work. I don't know what can be wrong, I'm going mad searching for it.

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: How to make edge scrolling work with windowed mode?
« Reply #7 on: October 25, 2015, 07:03:44 pm »
It's the real code, and guess what, if I copy that code to other project, it works. In the current project it works only on two directions (right and down). To the left and up I have to find the exact 0 pixel for it to work. I don't know what can be wrong, I'm going mad searching for it.

Well, if it works on other projects, then it's other part of your code what is causing the problem. What you can do is "minimize" your project (deleting or commenting code) until the problem dissappears, that way you'll discover the root of the problem, fix it, and then put the rest of the code back. This will be tedious as hell if your project is too big, though.

deso

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to make edge scrolling work with windowed mode?
« Reply #8 on: October 26, 2015, 11:36:07 am »
It's the real code, and guess what, if I copy that code to other project, it works. In the current project it works only on two directions (right and down). To the left and up I have to find the exact 0 pixel for it to work. I don't know what can be wrong, I'm going mad searching for it.

Well, if it works on other projects, then it's other part of your code what is causing the problem. What you can do is "minimize" your project (deleting or commenting code) until the problem dissappears, that way you'll discover the root of the problem, fix it, and then put the rest of the code back. This will be tedious as hell if your project is too big, though.

Found the problem. I was comparing an int with an unsigned int. Jesus. Hahaha.

The window::getSize() returns a sf::Vector2u. And the mouse position sf::Mouse::getPosition() returns a sf::Vector2i.

Just lovely :)
« Last Edit: October 26, 2015, 11:40:30 am by deso »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: How to make edge scrolling work with windowed mode?
« Reply #9 on: October 26, 2015, 11:37:44 am »
Found the problem. I was comparing an int with an unsigned int. Jesus. Hahaha.
That's why you activate compiler warnings and fix them. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/