10#ifndef BLITTER_COMMON_HPP
11#define BLITTER_COMMON_HPP
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)
41 if (dx == 0 && dy == 0) {
43 if (x1 >= 0 && x1 < screen_width && y1 >= 0 && y1 < screen_height) set_pixel(x1, y1);
47 int frac_diff = width * std::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;
59 frac_max = frac_test - 1;
65 if (dash == 0) dash = 1;
73 if (x2 < 0 || x1 >= screen_width)
return;
77 int frac_low = dy - frac_diff / 2;
78 int frac_high = dy + frac_diff / 2;
80 while (frac_low < -dx) {
84 while (frac_high >= dy) {
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));
94 int quotient = frac / dx;
95 int remainder = frac % dx;
96 y_bound += (1 + quotient) * stepy;
97 frac = remainder - dx;
102 frac_low = adjust_frac(frac_low, y_low);
103 frac_high = adjust_frac(frac_high, y_high);
107 if (x2 > screen_width) {
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);
121 if (frac_high >= 0) {
128 if (++dash_count >= dash + gap) dash_count = 0;
136 if (y2 < 0 || y1 >= screen_height)
return;
140 int frac_low = dx - frac_diff / 2;
141 int frac_high = dx + frac_diff / 2;
143 while (frac_low < -dy) {
147 while (frac_high >= dx) {
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));
157 int quotient = frac / dy;
158 int remainder = frac % dy;
159 x_bound += (1 + quotient) * stepx;
160 frac = remainder - dy;
165 frac_low = adjust_frac(frac_low, x_low);
166 frac_high = adjust_frac(frac_high, x_high);
170 if (y2 > screen_height) {
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);
184 if (frac_high >= 0) {
191 if (++dash_count >= dash + gap) dash_count = 0;
197 void Blitter::MovePixels(
const T *src, T *dst,
size_t width,
size_t height, ptrdiff_t pitch)
199 if (src == dst)
return;
202 for (
size_t i = 0; i < height; ++i) {
203 std::move_backward(src, src + width, dst + width);
208 for (
size_t i = 0; i < height; ++i) {
209 std::move(src, src + width, dst);