2011-04-02 12:55:54 +01:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-01-16 23:23:52 +00:00
|
|
|
/** @file common.hpp Common functionality for all blitter implementations. */
|
|
|
|
|
|
|
|
#ifndef BLITTER_COMMON_HPP
|
|
|
|
#define BLITTER_COMMON_HPP
|
2011-04-02 12:55:54 +01:00
|
|
|
|
|
|
|
#include "base.hpp"
|
2011-04-02 17:39:30 +01:00
|
|
|
#include "../core/math_func.hpp"
|
2011-04-02 12:55:54 +01:00
|
|
|
|
2018-01-17 02:52:40 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2018-01-16 23:23:52 +00:00
|
|
|
template <typename SetPixelT>
|
2021-05-26 19:51:17 +01:00
|
|
|
void Blitter::DrawLineGeneric(int x1, int y1, int x2, int y2, int screen_width, int screen_height, int width, int dash, SetPixelT set_pixel)
|
2011-04-02 12:55:54 +01:00
|
|
|
{
|
|
|
|
int dy;
|
|
|
|
int dx;
|
|
|
|
int stepx;
|
|
|
|
int stepy;
|
|
|
|
|
2021-05-26 19:51:17 +01:00
|
|
|
dy = (y2 - y1) * 2;
|
2011-04-02 12:55:54 +01:00
|
|
|
if (dy < 0) {
|
|
|
|
dy = -dy;
|
|
|
|
stepy = -1;
|
|
|
|
} else {
|
|
|
|
stepy = 1;
|
|
|
|
}
|
|
|
|
|
2021-05-26 19:51:17 +01:00
|
|
|
dx = (x2 - x1) * 2;
|
2011-04-02 12:55:54 +01:00
|
|
|
if (dx < 0) {
|
|
|
|
dx = -dx;
|
|
|
|
stepx = -1;
|
|
|
|
} else {
|
|
|
|
stepx = 1;
|
|
|
|
}
|
|
|
|
|
2011-05-08 19:20:21 +01:00
|
|
|
if (dx == 0 && dy == 0) {
|
|
|
|
/* The algorithm below cannot handle this special case; make it work at least for line width 1 */
|
2021-05-26 19:51:17 +01:00
|
|
|
if (x1 >= 0 && x1 < screen_width && y1 >= 0 && y1 < screen_height) set_pixel(x1, y1);
|
2011-05-08 19:20:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:16:18 +00:00
|
|
|
int frac_diff = width * std::max(dx, dy);
|
2011-04-02 17:39:30 +01:00
|
|
|
if (width > 1) {
|
2011-04-09 12:32:11 +01:00
|
|
|
/* compute frac_diff = width * sqrt(dx*dx + dy*dy)
|
|
|
|
* Start interval:
|
|
|
|
* max(dx, dy) <= sqrt(dx*dx + dy*dy) <= sqrt(2) * max(dx, dy) <= 3/2 * max(dx, dy) */
|
2023-05-08 18:01:06 +01:00
|
|
|
int64_t frac_sq = ((int64_t) width) * ((int64_t) width) * (((int64_t) dx) * ((int64_t) dx) + ((int64_t) dy) * ((int64_t) dy));
|
2011-04-09 12:32:11 +01:00
|
|
|
int frac_max = 3 * frac_diff / 2;
|
|
|
|
while (frac_diff < frac_max) {
|
|
|
|
int frac_test = (frac_diff + frac_max) / 2;
|
2023-05-08 18:01:06 +01:00
|
|
|
if (((int64_t) frac_test) * ((int64_t) frac_test) < frac_sq) {
|
2011-04-09 12:32:11 +01:00
|
|
|
frac_diff = frac_test + 1;
|
|
|
|
} else {
|
|
|
|
frac_max = frac_test - 1;
|
|
|
|
}
|
|
|
|
}
|
2011-04-02 17:39:30 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:41:20 +01:00
|
|
|
int gap = dash;
|
|
|
|
if (dash == 0) dash = 1;
|
|
|
|
int dash_count = 0;
|
2011-04-02 12:55:54 +01:00
|
|
|
if (dx > dy) {
|
2018-01-17 02:52:40 +00:00
|
|
|
if (stepx < 0) {
|
2021-05-26 19:51:17 +01:00
|
|
|
std::swap(x1, x2);
|
|
|
|
std::swap(y1, y2);
|
2018-01-17 02:52:40 +00:00
|
|
|
stepy = -stepy;
|
|
|
|
}
|
2021-05-26 19:51:17 +01:00
|
|
|
if (x2 < 0 || x1 >= screen_width) return;
|
2018-01-17 02:52:40 +00:00
|
|
|
|
2021-05-26 19:51:17 +01:00
|
|
|
int y_low = y1;
|
|
|
|
int y_high = y1;
|
2011-04-02 17:39:30 +01:00
|
|
|
int frac_low = dy - frac_diff / 2;
|
|
|
|
int frac_high = dy + frac_diff / 2;
|
|
|
|
|
2018-01-17 02:52:40 +00:00
|
|
|
while (frac_low < -(dx / 2)) {
|
2011-04-02 17:39:30 +01:00
|
|
|
frac_low += dx;
|
|
|
|
y_low -= stepy;
|
|
|
|
}
|
2018-01-17 02:52:40 +00:00
|
|
|
while (frac_high >= dx / 2) {
|
2011-04-02 17:39:30 +01:00
|
|
|
frac_high -= dx;
|
|
|
|
y_high += stepy;
|
|
|
|
}
|
2018-01-17 02:52:40 +00:00
|
|
|
|
2021-05-26 19:51:17 +01:00
|
|
|
if (x1 < 0) {
|
|
|
|
dash_count = (-x1) % (dash + gap);
|
2023-05-08 18:01:06 +01:00
|
|
|
auto adjust_frac = [&](int64_t frac, int &y_bound) -> int {
|
|
|
|
frac -= ((int64_t) dy) * ((int64_t) x1);
|
2018-01-17 02:52:40 +00:00
|
|
|
if (frac >= 0) {
|
|
|
|
int quotient = frac / dx;
|
|
|
|
int remainder = frac % dx;
|
|
|
|
y_bound += (1 + quotient) * stepy;
|
|
|
|
frac = remainder - dx;
|
|
|
|
}
|
|
|
|
return frac;
|
|
|
|
};
|
|
|
|
frac_low = adjust_frac(frac_low, y_low);
|
|
|
|
frac_high = adjust_frac(frac_high, y_high);
|
2021-05-26 19:51:17 +01:00
|
|
|
x1 = 0;
|
2018-01-17 02:52:40 +00:00
|
|
|
}
|
|
|
|
x2++;
|
|
|
|
if (x2 > screen_width) {
|
|
|
|
x2 = screen_width;
|
|
|
|
}
|
2011-04-02 17:39:30 +01:00
|
|
|
|
2021-05-26 19:51:17 +01:00
|
|
|
while (x1 != x2) {
|
2023-02-26 19:23:31 +00:00
|
|
|
if (dash_count < dash) {
|
|
|
|
for (int y = y_low; y != y_high; y += stepy) {
|
|
|
|
if (y >= 0 && y < screen_height) set_pixel(x1, y);
|
|
|
|
}
|
|
|
|
}
|
2011-04-02 17:39:30 +01:00
|
|
|
if (frac_low >= 0) {
|
|
|
|
y_low += stepy;
|
|
|
|
frac_low -= dx;
|
|
|
|
}
|
|
|
|
if (frac_high >= 0) {
|
|
|
|
y_high += stepy;
|
|
|
|
frac_high -= dx;
|
2011-04-02 12:55:54 +01:00
|
|
|
}
|
2021-05-26 19:51:17 +01:00
|
|
|
x1++;
|
2011-04-02 17:39:30 +01:00
|
|
|
frac_low += dy;
|
|
|
|
frac_high += dy;
|
2013-10-23 20:41:20 +01:00
|
|
|
if (++dash_count >= dash + gap) dash_count = 0;
|
2011-04-02 12:55:54 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-17 02:52:40 +00:00
|
|
|
if (stepy < 0) {
|
2021-05-26 19:51:17 +01:00
|
|
|
std::swap(x1, x2);
|
|
|
|
std::swap(y1, y2);
|
2018-01-17 02:52:40 +00:00
|
|
|
stepx = -stepx;
|
|
|
|
}
|
2021-05-26 19:51:17 +01:00
|
|
|
if (y2 < 0 || y1 >= screen_height) return;
|
2018-01-17 02:52:40 +00:00
|
|
|
|
2021-05-26 19:51:17 +01:00
|
|
|
int x_low = x1;
|
|
|
|
int x_high = x1;
|
2011-04-02 17:39:30 +01:00
|
|
|
int frac_low = dx - frac_diff / 2;
|
|
|
|
int frac_high = dx + frac_diff / 2;
|
|
|
|
|
2018-01-17 02:52:40 +00:00
|
|
|
while (frac_low < -(dy / 2)) {
|
2011-04-02 17:39:30 +01:00
|
|
|
frac_low += dy;
|
|
|
|
x_low -= stepx;
|
|
|
|
}
|
2018-01-17 02:52:40 +00:00
|
|
|
while (frac_high >= dy / 2) {
|
2011-04-02 17:39:30 +01:00
|
|
|
frac_high -= dy;
|
|
|
|
x_high += stepx;
|
|
|
|
}
|
2018-01-17 02:52:40 +00:00
|
|
|
|
2021-05-26 19:51:17 +01:00
|
|
|
if (y1 < 0) {
|
|
|
|
dash_count = (-y1) % (dash + gap);
|
2023-05-08 18:01:06 +01:00
|
|
|
auto adjust_frac = [&](int64_t frac, int &x_bound) -> int {
|
|
|
|
frac -= ((int64_t) dx) * ((int64_t) y1);
|
2018-01-17 02:52:40 +00:00
|
|
|
if (frac >= 0) {
|
|
|
|
int quotient = frac / dy;
|
|
|
|
int remainder = frac % dy;
|
|
|
|
x_bound += (1 + quotient) * stepx;
|
|
|
|
frac = remainder - dy;
|
|
|
|
}
|
|
|
|
return frac;
|
|
|
|
};
|
|
|
|
frac_low = adjust_frac(frac_low, x_low);
|
|
|
|
frac_high = adjust_frac(frac_high, x_high);
|
2021-05-26 19:51:17 +01:00
|
|
|
y1 = 0;
|
2018-01-17 02:52:40 +00:00
|
|
|
}
|
|
|
|
y2++;
|
|
|
|
if (y2 > screen_height) {
|
|
|
|
y2 = screen_height;
|
|
|
|
}
|
2011-04-02 17:39:30 +01:00
|
|
|
|
2021-05-26 19:51:17 +01:00
|
|
|
while (y1 != y2) {
|
2023-02-26 19:23:31 +00:00
|
|
|
if (dash_count < dash) {
|
|
|
|
for (int x = x_low; x != x_high; x += stepx) {
|
|
|
|
if (x >= 0 && x < screen_width) set_pixel(x, y1);
|
|
|
|
}
|
|
|
|
}
|
2011-04-02 17:39:30 +01:00
|
|
|
if (frac_low >= 0) {
|
|
|
|
x_low += stepx;
|
|
|
|
frac_low -= dy;
|
|
|
|
}
|
|
|
|
if (frac_high >= 0) {
|
|
|
|
x_high += stepx;
|
|
|
|
frac_high -= dy;
|
2011-04-02 12:55:54 +01:00
|
|
|
}
|
2021-05-26 19:51:17 +01:00
|
|
|
y1++;
|
2011-04-02 17:39:30 +01:00
|
|
|
frac_low += dx;
|
|
|
|
frac_high += dx;
|
2013-10-23 20:41:20 +01:00
|
|
|
if (++dash_count >= dash + gap) dash_count = 0;
|
2011-04-02 12:55:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 23:23:52 +00:00
|
|
|
|
|
|
|
#endif /* BLITTER_COMMON_HPP */
|