I'm trying to implement "canvas" where I will display loaded texture and select specific parts of it so I can put them on tilemap, I'm also trying to add zooming and panning only in "canvas" part of the window. I've tried using view and viewport but setting viewport size is what I'm struggling with the most. What I've tried:

First I set the view to be size of the window and its center is offset by (-250, -20) - this the point where properties window ends (x = 250) along with main menu bar (y = 20):

m_view.reset(sf::FloatRect(-250.0f, -20.0f, get_window().getSize().x, get_window().getSize().y));

Then if window has been resized I process the Resized event:

if (event.type == sf::Event::Resized)
    m_view.reset(sf::FloatRect(-250.0f, -20.0f, event.size.width, event.size.height));

But if I want to implement panning (moving the view) and zooming I need to set viewport so it will only render in "canvas" area. I can try to set viewport's FloatRect manually but what if window will get resized? How do I deal with this problem?

How it looks by now

Godot TileSet/TileMap Editor as reference


1 Answer 1


The Cherno on YouTube did something similar for his Scene Viewport in his Game Engine series.

It's written in C++/OpenGL/ImGui, but it's easily translated. From a high level, you need to create a viewport panel/window that you draw to as a "Scene Viewport".

  • \$\begingroup\$ I've actually tried drawing to the ImGui window and it would work if not for a few problems I've encountered: I don't know how to keep track of mouse position inside this window (for example when I click somewhere on the texture I want to draw there a selection rectangle, similar to Godot), and I also don't know how I would implement panning inside this window (but if I knew mouse position relative to this viewport I could come up with something). \$\endgroup\$
    – wixy0
    Mar 15, 2021 at 20:03
  • 1
    \$\begingroup\$ @wixy0 He goes over that. It's a long watch though over multiple videos starting with the one linked. \$\endgroup\$
    – Casey
    Mar 15, 2021 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .