OpenTTD Source 20250528-master-g3aca5d62a8
zoom_func.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef ZOOM_FUNC_H
11#define ZOOM_FUNC_H
12
13#include "zoom_type.h"
14
22inline int ScaleByZoom(int value, ZoomLevel zoom)
23{
24 return value << to_underlying(zoom);
25}
26
34inline int UnScaleByZoom(int value, ZoomLevel zoom)
35{
36 return (value + (1 << to_underlying(zoom)) - 1) >> to_underlying(zoom);
37}
38
45inline int AdjustByZoom(int value, int zoom)
46{
47 return zoom < 0 ? UnScaleByZoom(value, static_cast<ZoomLevel>(-zoom)) : ScaleByZoom(value, static_cast<ZoomLevel>(zoom));
48}
49
56inline int ScaleByZoomLower(int value, ZoomLevel zoom)
57{
58 return value << to_underlying(zoom);
59}
60
67inline int UnScaleByZoomLower(int value, ZoomLevel zoom)
68{
69 return value >> to_underlying(zoom);
70}
71
77inline int UnScaleGUI(int value)
78{
79 return UnScaleByZoom(value, ZOOM_LVL_GUI);
80}
81
88{
89 return std::clamp(value + (ZOOM_LVL_GUI - ZoomLevel::Normal), ZoomLevel::Min, ZoomLevel::Max);
90}
91
98{
99 return std::clamp(value - (ZOOM_LVL_GUI - ZoomLevel::Normal), ZoomLevel::Min, ZoomLevel::Max);
100}
101
107inline int ScaleSpriteTrad(int value)
108{
109 return UnScaleGUI(value * ZOOM_BASE);
110}
111
117inline int ScaleGUITrad(int value)
118{
119 return value * _gui_scale / 100;
120}
121
122#endif /* ZOOM_FUNC_H */
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
int _gui_scale
GUI scale, 100 is 100%.
Definition gfx.cpp:63
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:22
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107
ZoomLevel UnScaleZoomGUI(ZoomLevel value)
UnScale zoom level relative to GUI zoom.
Definition zoom_func.h:97
int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition zoom_func.h:117
int ScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZoomLevel::Min)
Definition zoom_func.h:56
ZoomLevel ScaleZoomGUI(ZoomLevel value)
Scale zoom level relative to GUI zoom.
Definition zoom_func.h:87
int AdjustByZoom(int value, int zoom)
Adjust by zoom level; zoom < 0 shifts right, zoom >= 0 shifts left.
Definition zoom_func.h:45
int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min)
Definition zoom_func.h:67
int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition zoom_func.h:77
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:34
Types related to zooming in and out.
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ Max
Maximum zoom level.
@ Min
Minimum zoom level.
@ Normal
The normal zoom level.