diff --git a/src/viewport.cpp b/src/viewport.cpp index f38cf813da..01d6c70897 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -205,12 +205,6 @@ static Point MapXYZToViewport(const Viewport &vp, int x, int y, int z) return p; } -void DeleteWindowViewport(Window *w) -{ - delete w->viewport; - w->viewport = nullptr; -} - /** * Initialize viewport of the window for use. * @param w Window to use/display the viewport in @@ -226,7 +220,7 @@ void InitializeWindowViewport(Window *w, int x, int y, { assert(w->viewport == nullptr); - ViewportData *vp = new ViewportData(); + auto vp = std::make_unique(); vp->left = x + w->left; vp->top = y + w->top; @@ -272,9 +266,10 @@ void InitializeWindowViewport(Window *w, int x, int y, vp->overlay = nullptr; - w->viewport = vp; vp->virtual_left = 0; vp->virtual_top = 0; + + w->viewport = std::move(vp); } static Point _vp_move_offs; @@ -415,7 +410,7 @@ Viewport *IsPtInWindowViewport(const Window *w, int x, int y) if (w->viewport == nullptr) return nullptr; const Viewport &vp = *w->viewport; - if (IsInsideMM(x, vp.left, vp.left + vp.width) && IsInsideMM(y, vp.top, vp.top + vp.height)) return w->viewport; + if (IsInsideMM(x, vp.left, vp.left + vp.width) && IsInsideMM(y, vp.top, vp.top + vp.height)) return w->viewport.get(); return nullptr; } diff --git a/src/viewport_func.h b/src/viewport_func.h index 0a45dab931..88e00edd73 100644 --- a/src/viewport_func.h +++ b/src/viewport_func.h @@ -21,7 +21,6 @@ static const int TILE_HEIGHT_STEP = 50; ///< One Z unit tile height difference i void SetSelectionRed(bool); -void DeleteWindowViewport(Window *w); void InitializeWindowViewport(Window *w, int x, int y, int width, int height, std::variant focus, ZoomLevel zoom); Viewport *IsPtInWindowViewport(const Window *w, int x, int y); Point TranslateXYToTileCoord(const Viewport &vp, int x, int y, bool clamp_to_map = true); diff --git a/src/window.cpp b/src/window.cpp index 415abbe5f1..0e36096d2a 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1090,8 +1090,6 @@ Window::~Window() { /* Make sure the window is closed, deletion is allowed only in Window::DeleteClosedWindows(). */ assert(*this->z_position == nullptr); - - if (this->viewport != nullptr) DeleteWindowViewport(this); } /** diff --git a/src/window_gui.h b/src/window_gui.h index 9b27432192..6da0e607a9 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -316,7 +316,7 @@ public: Owner owner = INVALID_OWNER; ///< The owner of the content shown in this window. Company colour is acquired from this variable. - ViewportData *viewport = nullptr; ///< Pointer to viewport data, if present. + std::unique_ptr viewport; ///< Pointer to viewport data, if present. const NWidgetCore *nested_focus = nullptr; ///< Currently focused nested widget, or \c nullptr if no nested widget has focus. std::map querystrings{}; ///< QueryString associated to WWT_EDITBOX widgets. std::unique_ptr nested_root{}; ///< Root of the nested tree.