OpenTTD Source  20241120-master-g6d3adc6169
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 
22 inline int ScaleByZoom(int value, ZoomLevel zoom)
23 {
24  return value << zoom;
25 }
26 
34 inline int UnScaleByZoom(int value, ZoomLevel zoom)
35 {
36  return (value + (1 << zoom) - 1) >> zoom;
37 }
38 
45 inline int AdjustByZoom(int value, int zoom)
46 {
47  return zoom < 0 ? UnScaleByZoom(value, ZoomLevel(-zoom)) : ScaleByZoom(value, ZoomLevel(zoom));
48 }
49 
56 inline int ScaleByZoomLower(int value, ZoomLevel zoom)
57 {
58  return value << zoom;
59 }
60 
67 inline int UnScaleByZoomLower(int value, ZoomLevel zoom)
68 {
69  return value >> zoom;
70 }
71 
77 inline int UnScaleGUI(int value)
78 {
79  return UnScaleByZoom(value, ZOOM_LVL_GUI);
80 }
81 
88 {
89  return std::clamp(ZoomLevel(value + (ZOOM_LVL_GUI - ZOOM_LVL_NORMAL)), ZOOM_LVL_MIN, ZOOM_LVL_MAX);
90 }
91 
98 {
99  return std::clamp(ZoomLevel(value - (ZOOM_LVL_GUI - ZOOM_LVL_NORMAL)), ZOOM_LVL_MIN, ZOOM_LVL_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 */
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 > ZOOM_LVL_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 > ZOOM_LVL_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 > ZOOM_LVL_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 > ZOOM_LVL_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
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:21
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition: zoom_type.h:42
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition: zoom_type.h:41