10#ifndef BLITTER_COMMON_HPP
11#define BLITTER_COMMON_HPP
14#include "../core/math_func.hpp"
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)
42 if (dx == 0 && dy == 0) {
44 if (x1 >= 0 && x1 < screen_width && y1 >= 0 && y1 < screen_height) set_pixel(x1, y1);
48 int frac_diff = width * std::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;
60 frac_max = frac_test - 1;
66 if (dash == 0) dash = 1;
74 if (x2 < 0 || x1 >= screen_width)
return;
78 int frac_low = dy - frac_diff / 2;
79 int frac_high = dy + frac_diff / 2;
81 while (frac_low < -dx) {
85 while (frac_high >= dy) {
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));
95 int quotient = frac / dx;
96 int remainder = frac % dx;
97 y_bound += (1 + quotient) * stepy;
98 frac = remainder - dx;
103 frac_low = adjust_frac(frac_low, y_low);
104 frac_high = adjust_frac(frac_high, y_high);
108 if (x2 > screen_width) {
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);
122 if (frac_high >= 0) {
129 if (++dash_count >= dash + gap) dash_count = 0;
137 if (y2 < 0 || y1 >= screen_height)
return;
141 int frac_low = dx - frac_diff / 2;
142 int frac_high = dx + frac_diff / 2;
144 while (frac_low < -dy) {
148 while (frac_high >= dx) {
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));
158 int quotient = frac / dy;
159 int remainder = frac % dy;
160 x_bound += (1 + quotient) * stepx;
161 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, y1);
185 if (frac_high >= 0) {
192 if (++dash_count >= dash + gap) dash_count = 0;
198 void Blitter::MovePixels(
const T *src, T *dst,
size_t width,
size_t height, ptrdiff_t pitch)
200 if (src == dst)
return;
203 for (
size_t i = 0; i < height; ++i) {
204 std::move_backward(src, src + width, dst + width);
209 for (
size_t i = 0; i < height; ++i) {
210 std::move(src, src + width, dst);