OpenTTD Source: src/zoom_func.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef ZOOM_FUNC_H
11#define ZOOM_FUNC_H
12
13#include "zoom_type.h"
14
22 inline int ScaleByZoom(int value, ZoomLevel zoom)
23{
24 return value << to_underlying(zoom);
25}
26
34 inline int UnScaleByZoom(int value, ZoomLevel zoom)
35{
36 return (value + (1 << to_underlying(zoom)) - 1) >> to_underlying(zoom);
37}
38
45 inline 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
56 inline int ScaleByZoomLower(int value, ZoomLevel zoom)
57{
58 return value << to_underlying(zoom);
59}
60
67 inline int UnScaleByZoomLower(int value, ZoomLevel zoom)
68{
69 return value >> to_underlying(zoom);
70}
71
77 inline int UnScaleGUI(int value)
78{
79 return UnScaleByZoom(value, _gui_zoom);
80}
81
87 inline ZoomLevel ScaleZoomGUI(ZoomLevel value)
88{
89 return std::clamp(value + (_gui_zoom - ZoomLevel::Normal), ZoomLevel::Min, ZoomLevel::Max);
90}
91
97 inline ZoomLevel UnScaleZoomGUI(ZoomLevel value)
98{
99 return std::clamp(value - (_gui_zoom - ZoomLevel::Normal), ZoomLevel::Min, ZoomLevel::Max);
100}
101
107 inline int ScaleSpriteTrad(int value)
108{
109 return UnScaleGUI(value * ZOOM_BASE);
110}
111
117 inline int ScaleGUITrad(int value)
118{
119 return value * _gui_scale / 100;
120}
121
122#endif /* ZOOM_FUNC_H */
to_underlying
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
_gui_zoom
ZoomLevel _gui_zoom
GUI Zoom level.
Definition gfx.cpp:61
_gui_scale
int _gui_scale
GUI scale, 100 is 100%.
Definition gfx.cpp:63
ScaleByZoom
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
ScaleSpriteTrad
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107
UnScaleZoomGUI
ZoomLevel UnScaleZoomGUI(ZoomLevel value)
UnScale zoom level relative to GUI zoom.
Definition zoom_func.h:97
ScaleGUITrad
int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition zoom_func.h:117
ScaleByZoomLower
int ScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZoomLevel::Min)
Definition zoom_func.h:56
ScaleZoomGUI
ZoomLevel ScaleZoomGUI(ZoomLevel value)
Scale zoom level relative to GUI zoom.
Definition zoom_func.h:87
AdjustByZoom
int AdjustByZoom(int value, int zoom)
Adjust by zoom level; zoom < 0 shifts right, zoom >= 0 shifts left.
Definition zoom_func.h:45
UnScaleByZoomLower
int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min)
Definition zoom_func.h:67
UnScaleGUI
int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition zoom_func.h:77
UnScaleByZoom
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
zoom_type.h
Types related to zooming in and out.
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
ZoomLevel::Max
@ Max
Maximum zoom level.
ZoomLevel::Min
@ Min
Minimum zoom level.
ZoomLevel::Normal
@ Normal
The normal zoom level.