OpenTTD Source: src/blitter/common.hpp Source File

OpenTTD Source 15.0-RC2

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