- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
void DrawLine(Vec2Si32 a, Vec2Si32 b, Rgba color_a, Rgba color_b) { // https://pastebin.com/p26FvZdd
Vec2Si32 ab = b - a;
Vec2Si32 abs_ab(std::abs(ab.x), std::abs(ab.y));
if (abs_ab.x >= abs_ab.y) {
if (a.x > b.x) {
DrawLine(b, a, color_b, color_a);
} else {
Sprite back = GetEngine()->GetBackbuffer();
Vec2Si32 back_size = back.Size();
if (ab.x == 0) {
if (a.x >= 0 && a.x < back_size.x &&
a.y >= 0 && a.y < back_size.y) {
back.RgbaData()[a.x + a.y * back.StridePixels()] = color_a;
}
return;
}
Si32 x1 = std::max(0, a.x);
Si32 x2 = std::min(back_size.x - 1, b.x);
Si32 y1 = a.y + ab.y * (x1 - a.x) / ab.x;
Si32 y2 = a.y + ab.y * (x2 - a.x) / ab.x;
if (y1 < 0) {
if (y2 < 0) {
return;
}
// lower left -> upper right
y1 = 0;
x1 = a.x + ab.x * (y1 - a.y) / ab.y;
x1 = std::max(0, x1);
} else if (y1 >= back_size.y) {
if (y2 >= back_size.y) {
return;
}
// upper left -> lower right
y1 = back_size.y - 1;
x1 = a.x + ab.x * (y1 - a.y) / ab.y;
x1 = std::max(0, x1);
}
if (y2 < 0) {
// upper left -> lower right
y2 = 0;
x2 = a.x + ab.x * (y2 - a.y) / ab.y;
x2 = std::min(back_size.x - 1, x2);
} else if (y2 >= back_size.y) {
// lower left -> upper right
y2 = back_size.y - 1;
x2 = a.x + ab.x * (y2 - a.y) / ab.y;
x2 = std::min(back_size.x - 1, x2);
}
Vec4Si32 rgba_a(static_cast<Si32>(color_a.r),static_cast<Si32>(color_a.g),static_cast<Si32>(color_a.b),static_cast<Si32>(color_a.a));
Vec4Si32 rgba_b(static_cast<Si32>(color_b.r),static_cast<Si32>(color_b.g),static_cast<Si32>(color_b.b),static_cast<Si32>(color_b.a));
Vec4Si32 rgba_ab = rgba_b - rgba_a;
Vec4Si32 rgba_1 = rgba_a + rgba_ab * (x1 - a.x) / ab.x;
Vec4Si32 rgba_2 = rgba_a + rgba_ab * (x2 - a.x) / ab.x;
Vec4Si32 rgba_12 = rgba_2 - rgba_1;
if (x2 <= x1) {
if (x2 == x1) {
Rgba color(rgba_1.x, rgba_1.y, rgba_1.z, rgba_1.w);
back.RgbaData()[x1 + y1 * back.StridePixels()] = color;
}
return;
}
Vec4Si32 rgba_16 = rgba_1 * 65536;
Vec4Si32 rgba_12_16 = rgba_12 * 65536;
Vec4Si32 rgba_12_16_step = rgba_12_16 / (x2 - x1);
Si32 y_16 = y1 * 65536;
Si32 y12_16_step = ((y2 - y1) * 65536) / (x2 - x1);
Si32 stride = back.StridePixels();
for (Si32 x = x1; x <= x2; ++x) {
Rgba color(rgba_16.x >> 16,rgba_16.y >> 16,rgba_16.z >> 16,rgba_16.w >> 16);
back.RgbaData()[x + (y_16 >> 16) * stride] = color;
rgba_16 += rgba_12_16_step;
y_16 += y12_16_step;
}
}
} else { // тут почти тоже самое
if (a.y > b.y) {
DrawLine(b, a, color_b, color_a);
} else {
Sprite back = GetEngine()->GetBackbuffer();
Vec2Si32 back_size = back.Size();
if (ab.y == 0) {
if (a.y >= 0 && a.y < back_size.y && a.x >= 0 && a.x < back_size.x) {
back.RgbaData()[a.x + a.y * back.StridePixels()] = color_a;
}
return;
}
Si32 y1 = std::max(0, a.y);
Si32 y2 = std::min(back_size.y - 1, b.y);
Si32 x1 = a.x + ab.x * (y1 - a.y) / ab.y;
Si32 x2 = a.x + ab.x * (y2 - a.y) / ab.y;
if (x1 < 0) {
if (x2 < 0) {
return;
}
// lower left -> upper right
x1 = 0;
y1 = a.y + ab.y * (x1 - a.x) / ab.x;
y1 = std::max(0, y1);
} else if (x1 >= back_size.x) {
if (x2 >= back_size.x) {
Взято отсюда https://github.com/FrostyMorning/arctic/blob/master/engine/easy.cpp
https://pastebin.com/p26FvZdd
Комментарии (0) RSS
Добавить комментарий