Skip to content

Fix wrong map-heights saved while zoomed and draw the map wrapped around for large views - #66

Open
littleant wants to merge 8 commits into
Return-To-The-Roots:masterfrom
littleant:zoom-fixes
Open

littleant wants to merge 8 commits into
Return-To-The-Roots:masterfrom
littleant:zoom-fixes

Conversation

@littleant

@littleant littleant commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This PR includes the commits of #37, rebased onto current master (pr37-rebased, see #37 (comment)), still authored by Morgan. If #37 is merged first, only my last two commits remain here. Otherwise this PR can be merged instead and #37 closed.

Fix wrong vertex heights saved while zoomed in or out
Heights were calculated back from the zoomed vertex positions using the default zoom factor, so they were wrong when saved while zoomed (e.g. 19 instead of 18). Now the stored height is saved directly.

Draw the map and the cursor wrapped around also for views larger than the map
When zoomed out, the view can be larger than the map, but it did not repeat correctly in every case. I fixed the following:

  • The map repeats in every direction, as often as needed. This replaces the special cases for the view crossing a map border.
  • Objects (e.g. trees) just outside the view that reach into it are drawn.
  • The view position is moved back into the map before every frame. Before, it was corrected by at most one map size.
  • Cursor fixes:
    • The last column couldn't be selected in even rows.
    • Below the last row, the wrong column was used.
    • Rows moved across the top or bottom border by raised or lowered terrain were skipped.
    • Next to raised or lowered vertices, the cursor jumped to the wrong row.
  • The cursor is drawn at the map copy closest to the mouse.
  • Removed MouseBlit, which hasn't been read anywhere since 2019.

Tested on Windows and Linux (Ubuntu).

This also covers changes proposed by #51 with the exception of a minimum window size, that I have not implemented and find unnecessary. So #51 can be closed if this PR merges.

Known Bug: In an Ubuntu VM with "mouse integration" enabled, scrolling does not work correctly. It never worked, but I just wanted to mention this here. I have a fix, but I'm not happy with it so I'm not including it in this PR.

@Flamefire Flamefire left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this, comments inline. It's quite big and seems to combine unrelated changes, so wondering if this can be split. But 7 commits are OK for a PR

And you missed a fixup commit in the rebase. Can you fold that into the original commit too?

Comment thread CIO/CFile.cpp
for(int i = 0; i < myMap->width; i++)
{
temp = myMap->getVertex(i, j).z / 5 + 0x0A; //-V807
temp = myMap->getVertex(i, j).h; //-V807

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this a bit? What is the difference between z and h and why is this an issue now? I don't see the relation to zooming.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h is the map value as stored in the map files. z is a derived value, representing how many pixels the vertex is lifted on the screen.
It is calculated in CMap.cpp:65 as z = triangleIncrease * (h - 0x0A). triangleIncrease is the number of pixels per height step. It is 5 at default zoom, but zooming changes it (range 1–10). 0x0A is the base level of a map.

I just found two instances where z is currently being used for comparison instead of h, making the logic more complicated. I will add them too, in the following commit.

Comment thread CGame_Event.cpp Outdated
if(!mapObj || !mapObj->getMap())
return false;
const int newInc = triangleIncrease + deltaInc;
if(newInc < 1 || newInc > 10)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use existing constants

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense.

Comment thread CGame_Event.cpp Outdated
if(y == 0)
break;

const Position deltaTri = (y > 0) ? Position(11, 5) : Position(-11, -5);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic numbers?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change tryZoom to this, unifying the scroll wheel and the F5-F7-keys and moving the triangle-width and height calculation inside tryZoom(). I don't think it makes sense to calculate width and height (deltaTri) outside of the function:

enum class ZoomDirection
{
    In,
    Out
};

/// Zoom one step in the given direction, unless the zoom limit is reached
bool tryZoom(ZoomDirection zoomDirection, CMap* mapObj)

Then your selected code will be removed and the calling code changes like this:

- if(!tryZoom(deltaTri, deltaInc, MapObj.get()))
+ if(!tryZoom((y > 0) ? ZoomDirection::In : ZoomDirection::Out, MapObj.get()))

Which makes it more readable imo.

F6 calls resetZoom() anyway, and doesn't rely on tryZoom. Which should probably also be unified somehow. But I didn't want to fixup others code in this commit, just add minimal bug-fixes.

Comment thread CMap.cpp Outdated
} else
lastMousePos_ = mousePos;

// Columns of the nearest vertex in odd and even rows (odd rows are shifted by half a triangle to the right)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely preexisting but can you double-check this? In the init code even rows are shifted, not add rows. Looks like a bug or inconsistency.
See https://github.com/littleant/s25edit/blob/225b89e8521bdfe46b2ba16d96a181bee3d19a64/CMap.cpp#L55-L58

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change it to:

// Columns of the nearest vertex in odd and even rows (even rows are shifted half a triangle to the left)

Comment thread CGame_Event.cpp Outdated

// Keep the map point under the cursor stable after zoom
DisplayRectangle dr = MapObj->getDisplayRect();
const PointF ratio(triangleWidth / oldTriSize.x, triangleHeight / oldTriSize.y);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to be zoom-adjusted too or why is triangleHeight correct here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I caught the bug during testing just now, but didn't know the cause. Will be fixed.

Comment thread CGame_Event.cpp Outdated
Comment on lines +422 to +424
const PointF newOrigin = (PointF(dr.getOrigin()) + mousePos) * ratio - mousePos;
dr.left = static_cast<Sint32>(newOrigin.x);
dr.top = static_cast<Sint32>(newOrigin.y);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the truncation intended or should this be rounding via const Position newOriginInt(newOrigin); (or directly in newOrigin)?
The 4 calculations also seem odd: You only move the rect, don't you? IIRC we have a member function for setting the rect origin without changing the size

In general whenever you have 2 lines with foo.{x,y} there is likely a better way for the operation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, should be rounding.

The view will be moved with setOrigin instead of setting all 4 sides. Also will replace the per-component ratio with a point division.

Comment thread CSurface.cpp Outdated
}
const int additionalRows = triangleIncrease * std::max(0, maxH - 0x0A) / triangleHeight;
// Vertices are moved up by raised and down by lowered terrain
const int raisedTolerance = triangleIncrease * std::max(0, maxH - 0x0A);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing but used here a couple times: Can we have a named constant for 0x0A?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Comment thread CSurface.cpp Outdated
Comment on lines +326 to +327
// Odd rows are shifted right by half a triangle
const int shift = positiveModulo(y, height) % 2;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new at this point. And I don't really understand it: You add 0 or 1 to the X-coord. Nothing seems to relate to the "half a triangle" here. Can you explain and enhance the comment?
Likely the positive modulo for the height is not required, IIRC map heights are always multiples of 2

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we do not need the height. I'll adjust it to:

// The two vertices below vertex x are in columns x - 1 and x if this row is even, x and x + 1 if it is odd,
// because even rows are shifted half a triangle to the left
const int shift = positiveModulo(y, 2);

Hope it's clearer that way?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, thanks!

morganchristiansson and others added 8 commits November 23, 2026 18:40
The saved heights were calculated back from the zoomed vertex positions
with the factor of the default zoom, so a map saved while zoomed in or
out got wrong heights (e.g. 19 instead of 18 one zoom step in).
… the map

- Draw the triangles around the view with vertex indices wrapped around
  the map instead of special-casing the view crossing a map border
- Draw the texture borders at the nearest map copy, also if the triangle
  is more than one map size away
- Draw objects of triangles just outside the view reaching into it
- Wrap the view origin around the map borders also for large offsets
  (e.g. after zooming or jumping to a position)
- Select the cursor vertex modulo the map size in pixels: The last column
  couldn't be selected in even rows
- Select the column of the first row correctly when the cursor is below
  the last row (it used the column of an odd row)
- Select the rows moved across the top or bottom map border by raised or
  lowered terrain: Rows behind the border were skipped
- Select the cursor row by the height of the closest vertex in that row:
  It used the height of the neighboring vertex on one side, so next to
  raised or lowered vertices the cursor jumped to the wrong row
- Draw the cursor at the map copy closest to the mouse
- Move positiveModulo to defines.h and add floorDiv for CSurface.cpp
- Use the zoom constants instead of literals, with a ZoomDirection enum
- Zoom around the cursor also with F5/F6/F7; anchor on the vertex under
  the cursor so terrain height doesn't make the view drift
- Round the new view origin and move it with setOrigin
- Add constants for the min, ground and max vertex heights
- Clarify the comments on the row offsets
@littleant

Copy link
Copy Markdown
Contributor Author

Yes, this PR is quite big, because I didn't want to touch the original code as you reviewed it already in #37.

I snuck a fix into the fix-commit right now: when zooming with the F-keys, the cursor is also the "target" like when zooming with the scroll-wheel.

@littleant
littleant requested a review from Flamefire November 23, 2026 17:51
@littleant

Copy link
Copy Markdown
Contributor Author

I think I've addressed all change requests. Do you want me to do anything else here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants