12 #ifndef BLITTER_COMMON_HPP 13 #define BLITTER_COMMON_HPP 16 #include "../core/math_func.hpp" 20 template <
typename SetPixelT>
21 void Blitter::DrawLineGeneric(
int x,
int y,
int x2,
int y2,
int screen_width,
int screen_height,
int width,
int dash, SetPixelT set_pixel)
44 if (dx == 0 && dy == 0) {
46 if (x >= 0 && x < screen_width && y >= 0 && y < screen_height) set_pixel(x, y);
50 int frac_diff = width *
max(dx, dy);
55 int64 frac_sq = ((int64) width) * ((int64) width) * (((int64) dx) * ((int64) dx) + ((int64) dy) * ((int64) dy));
56 int frac_max = 3 * frac_diff / 2;
57 while (frac_diff < frac_max) {
58 int frac_test = (frac_diff + frac_max) / 2;
59 if (((int64) frac_test) * ((int64) frac_test) < frac_sq) {
60 frac_diff = frac_test + 1;
62 frac_max = frac_test - 1;
68 if (dash == 0) dash = 1;
76 if (x2 < 0 || x >= screen_width)
return;
80 int frac_low = dy - frac_diff / 2;
81 int frac_high = dy + frac_diff / 2;
83 while (frac_low < -(dx / 2)) {
87 while (frac_high >= dx / 2) {
93 dash_count = (-x) % (dash + gap);
94 auto adjust_frac = [&](int64 frac,
int &y_bound) ->
int {
95 frac -= ((int64) dy) * ((int64) x);
97 int quotient = frac / dx;
98 int remainder = frac % dx;
99 y_bound += (1 + quotient) * stepy;
100 frac = remainder - dx;
104 frac_low = adjust_frac(frac_low, y_low);
105 frac_high = adjust_frac(frac_high, y_high);
109 if (x2 > screen_width) {
114 if (dash_count < dash) {
115 for (
int y = y_low; y != y_high; y += stepy) {
116 if (y >= 0 && y < screen_height) set_pixel(x, y);
123 if (frac_high >= 0) {
130 if (++dash_count >= dash + gap) dash_count = 0;
138 if (y2 < 0 || y >= screen_height)
return;
142 int frac_low = dx - frac_diff / 2;
143 int frac_high = dx + frac_diff / 2;
145 while (frac_low < -(dy / 2)) {
149 while (frac_high >= dy / 2) {
155 dash_count = (-y) % (dash + gap);
156 auto adjust_frac = [&](int64 frac,
int &x_bound) ->
int {
157 frac -= ((int64) dx) * ((int64) y);
159 int quotient = frac / dy;
160 int remainder = frac % dy;
161 x_bound += (1 + quotient) * stepx;
162 frac = remainder - dy;
166 frac_low = adjust_frac(frac_low, x_low);
167 frac_high = adjust_frac(frac_high, x_high);
171 if (y2 > screen_height) {
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, y);
185 if (frac_high >= 0) {
192 if (++dash_count >= dash + gap) dash_count = 0;
static T max(const T a, const T b)
Returns the maximum of two values.