OpenTTD Source 20250905-master-g122023be8d
common.hpp
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 BLITTER_COMMON_HPP
11#define BLITTER_COMMON_HPP
12
13#include "base.hpp"
14#include "../core/math_func.hpp"
15
16#include <utility>
17
18template <typename SetPixelT>
19void Blitter::DrawLineGeneric(int x1, int y1, int x2, int y2, int screen_width, int screen_height, int width, int dash, SetPixelT set_pixel)
20{
21 int dy;
22 int dx;
23 int stepx;
24 int stepy;
25
26 dy = (y2 - y1) * 2;
27 if (dy < 0) {
28 dy = -dy;
29 stepy = -1;
30 } else {
31 stepy = 1;
32 }
33
34 dx = (x2 - x1) * 2;
35 if (dx < 0) {
36 dx = -dx;
37 stepx = -1;
38 } else {
39 stepx = 1;
40 }
41
42 if (dx == 0 && dy == 0) {
43 /* The algorithm below cannot handle this special case; make it work at least for line width 1 */
44 if (x1 >= 0 && x1 < screen_width && y1 >= 0 && y1 < screen_height) set_pixel(x1, y1);
45 return;
46 }
47
48 int frac_diff = width * std::max(dx, dy);
49 if (width > 1) {
50 /* compute frac_diff = width * sqrt(dx*dx + dy*dy)
51 * Start interval:
52 * max(dx, dy) <= sqrt(dx*dx + dy*dy) <= sqrt(2) * max(dx, dy) <= 3/2 * max(dx, dy) */
53 int64_t frac_sq = ((int64_t) width) * ((int64_t) width) * (((int64_t) dx) * ((int64_t) dx) + ((int64_t) dy) * ((int64_t) dy));
54 int frac_max = 3 * frac_diff / 2;
55 while (frac_diff < frac_max) {
56 int frac_test = (frac_diff + frac_max) / 2;
57 if (((int64_t) frac_test) * ((int64_t) frac_test) < frac_sq) {
58 frac_diff = frac_test + 1;
59 } else {
60 frac_max = frac_test - 1;
61 }
62 }
63 }
64
65 int gap = dash;
66 if (dash == 0) dash = 1;
67 int dash_count = 0;
68 if (dx > dy) {
69 if (stepx < 0) {
70 std::swap(x1, x2);
71 std::swap(y1, y2);
72 stepy = -stepy;
73 }
74 if (x2 < 0 || x1 >= screen_width) return;
75
76 int y_low = y1;
77 int y_high = y1;
78 int frac_low = dy - frac_diff / 2;
79 int frac_high = dy + frac_diff / 2;
80
81 while (frac_low < -dx) {
82 frac_low += dx;
83 y_low -= stepy;
84 }
85 while (frac_high >= dy) {
86 frac_high -= dx;
87 y_high += stepy;
88 }
89
90 if (x1 < 0) {
91 dash_count = (-x1) % (dash + gap);
92 auto adjust_frac = [&](int64_t frac, int &y_bound) -> int {
93 frac -= ((int64_t) dy) * ((int64_t) (x1 + 1));
94 if (frac >= 0) {
95 int quotient = frac / dx;
96 int remainder = frac % dx;
97 y_bound += (1 + quotient) * stepy;
98 frac = remainder - dx;
99 }
100 frac += dy;
101 return frac;
102 };
103 frac_low = adjust_frac(frac_low, y_low);
104 frac_high = adjust_frac(frac_high, y_high);
105 x1 = 0;
106 }
107 x2++;
108 if (x2 > screen_width) {
109 x2 = screen_width;
110 }
111
112 while (x1 != x2) {
113 if (dash_count < dash) {
114 for (int y = y_low; y != y_high; y += stepy) {
115 if (y >= 0 && y < screen_height) set_pixel(x1, y);
116 }
117 }
118 if (frac_low >= 0) {
119 y_low += stepy;
120 frac_low -= dx;
121 }
122 if (frac_high >= 0) {
123 y_high += stepy;
124 frac_high -= dx;
125 }
126 x1++;
127 frac_low += dy;
128 frac_high += dy;
129 if (++dash_count >= dash + gap) dash_count = 0;
130 }
131 } else {
132 if (stepy < 0) {
133 std::swap(x1, x2);
134 std::swap(y1, y2);
135 stepx = -stepx;
136 }
137 if (y2 < 0 || y1 >= screen_height) return;
138
139 int x_low = x1;
140 int x_high = x1;
141 int frac_low = dx - frac_diff / 2;
142 int frac_high = dx + frac_diff / 2;
143
144 while (frac_low < -dy) {
145 frac_low += dy;
146 x_low -= stepx;
147 }
148 while (frac_high >= dx) {
149 frac_high -= dy;
150 x_high += stepx;
151 }
152
153 if (y1 < 0) {
154 dash_count = (-y1) % (dash + gap);
155 auto adjust_frac = [&](int64_t frac, int &x_bound) -> int {
156 frac -= ((int64_t) dx) * ((int64_t) (y1 + 1));
157 if (frac >= 0) {
158 int quotient = frac / dy;
159 int remainder = frac % dy;
160 x_bound += (1 + quotient) * stepx;
161 frac = remainder - dy;
162 }
163 frac += dx;
164 return frac;
165 };
166 frac_low = adjust_frac(frac_low, x_low);
167 frac_high = adjust_frac(frac_high, x_high);
168 y1 = 0;
169 }
170 y2++;
171 if (y2 > screen_height) {
172 y2 = screen_height;
173 }
174
175 while (y1 != y2) {
176 if (dash_count < dash) {
177 for (int x = x_low; x != x_high; x += stepx) {
178 if (x >= 0 && x < screen_width) set_pixel(x, y1);
179 }
180 }
181 if (frac_low >= 0) {
182 x_low += stepx;
183 frac_low -= dy;
184 }
185 if (frac_high >= 0) {
186 x_high += stepx;
187 frac_high -= dy;
188 }
189 y1++;
190 frac_low += dx;
191 frac_high += dx;
192 if (++dash_count >= dash + gap) dash_count = 0;
193 }
194 }
195}
196
197template <typename T>
198/* static */ void Blitter::MovePixels(const T *src, T *dst, size_t width, size_t height, ptrdiff_t pitch)
199{
200 if (src == dst) return;
201
202 if (src < dst) {
203 for (size_t i = 0; i < height; ++i) {
204 std::move_backward(src, src + width, dst + width);
205 src += pitch;
206 dst += pitch;
207 }
208 } else {
209 for (size_t i = 0; i < height; ++i) {
210 std::move(src, src + width, dst);
211 src += pitch;
212 dst += pitch;
213 }
214 }
215}
216
217#endif /* BLITTER_COMMON_HPP */
Base for all blitters.