ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
scaling.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 //=============================================================================
23 //
24 // Helper struct for scaling coordinates
25 //
26 // TODO: rewrite into proper Transform class.
27 // Maybe implement as real matrix and/or floats if that is better/runs faster.
28 //
29 //=============================================================================
30 
31 #ifndef AGS_SHARED_UTIL_SCALING_H
32 #define AGS_SHARED_UTIL_SCALING_H
33 
34 #include "ags/shared/core/types.h"
35 #include "ags/shared/util/geometry.h"
36 
37 namespace AGS3 {
38 namespace AGS {
39 namespace Shared {
40 
41 class AxisScaling {
42 public:
43  AxisScaling()
44  : _scale(kUnit)
45  , _unscale(kUnit)
46  , _srcOffset(0)
47  , _dstOffset(0) {
48  }
49 
50  bool IsIdentity() const {
51  return _scale == kUnit && _srcOffset == 0 && _dstOffset == 0;
52  }
53 
54  bool IsTranslateOnly() const {
55  return _scale == kUnit;
56  }
57 
58  void Init(const int32_t src_length, const int32_t dst_length, const int32_t src_offset = 0, const int32_t dst_offset = 0) {
59  _scale = kUnit;
60  _unscale = kUnit;
61  _srcOffset = src_offset;
62  _dstOffset = dst_offset;
63 
64  if (src_length != 0) {
65  int32_t scale = (dst_length << kShift) / src_length;
66  if (scale != 0) {
67  _scale = scale;
68  _unscale = scale;
69  int32_t scaled_val = ScaleDistance(src_length);
70  if (scaled_val < dst_length)
71  _scale++;
72  }
73  }
74  }
75 
76  void SetSrcOffset(int32_t x) {
77  _srcOffset = x;
78  }
79 
80  void SetDstOffset(int32_t x) {
81  _dstOffset = x;
82  }
83 
84  inline int32_t GetSrcOffset() const {
85  return _srcOffset;
86  }
87 
88  inline int32_t ScalePt(int32_t x) const {
89  return (((x - _srcOffset) * _scale) >> kShift) + _dstOffset;
90  }
91  inline int32_t ScaleDistance(int32_t x) const {
92  return ((x * _scale) >> kShift);
93  }
94  inline int32_t UnScalePt(int32_t x) const {
95  return ((x - _dstOffset) << kShift) / _unscale + _srcOffset;
96  }
97  inline int32_t UnScaleDistance(int32_t x) const {
98  return (x << kShift) / _unscale;
99  }
100 
101 private:
102  int32_t _scale;
103  int32_t _unscale;
104  int32_t _srcOffset;
105  int32_t _dstOffset;
106 };
107 
108 struct PlaneScaling {
109  AxisScaling X;
110  AxisScaling Y;
111 
112  bool IsIdentity() const {
113  return X.IsIdentity() && Y.IsIdentity();
114  }
115 
116  bool IsTranslateOnly() const {
117  return X.IsTranslateOnly() && Y.IsTranslateOnly();
118  }
119 
120  void Init(const Size &src_size, const Rect &dst_rect) {
121  X.Init(src_size.Width, dst_rect.GetWidth(), 0, dst_rect.Left);
122  Y.Init(src_size.Height, dst_rect.GetHeight(), 0, dst_rect.Top);
123  }
124 
125  void Init(const Rect &src_rect, const Rect &dst_rect) {
126  X.Init(src_rect.GetWidth(), dst_rect.GetWidth(), src_rect.Left, dst_rect.Left);
127  Y.Init(src_rect.GetHeight(), dst_rect.GetHeight(), src_rect.Top, dst_rect.Top);
128  }
129 
130  void SetSrcOffsets(int x, int y) {
131  X.SetSrcOffset(x);
132  Y.SetSrcOffset(y);
133  }
134 
135  void SetDestOffsets(int x, int y) {
136  X.SetDstOffset(x);
137  Y.SetDstOffset(y);
138  }
139 
140  inline Point Scale(const Point &p) const {
141  return Point(X.ScalePt(p.X), Y.ScalePt(p.Y));
142  }
143 
144  inline Rect ScaleRange(const Rect &r) const {
145  return RectWH(X.ScalePt(r.Left), Y.ScalePt(r.Top), X.ScaleDistance(r.GetWidth()), Y.ScaleDistance(r.GetHeight()));
146  }
147 
148  inline Point UnScale(const Point &p) const {
149  return Point(X.UnScalePt(p.X), Y.UnScalePt(p.Y));
150  }
151 
152  inline Rect UnScaleRange(const Rect &r) const {
153  return RectWH(X.UnScalePt(r.Left), Y.UnScalePt(r.Top), X.UnScaleDistance(r.GetWidth()), Y.UnScaleDistance(r.GetHeight()));
154  }
155 };
156 
157 } // namespace Shared
158 } // namespace AGS
159 } // namespace AGS3
160 
161 #endif
Definition: achievements_tables.h:27
Definition: geometry.h:87
Definition: scaling.h:41
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: scaling.h:108
Definition: geometry.h:219
Definition: geometry.h:148
Definition: ags.h:40