SayoriOS 0.3.3

line.c
1 #include <common.h>
2 #include <io/screen.h>
3 #include "lib/math.h"
4 #include "io/tty.h"
5
6 void draw_line(int x0, int y0, int x1, int y1, int thickness, int color) {
7 // Calculate the differences and absolute values of x and y coordinates
8 int dx = ABS(x1 - x0);
9 int dy = ABS(y1 - y0);
10
11 // Determine the direction for incrementing x and y
12 int sx = (x0 < x1) ? 1 : -1;
13 int sy = (y0 < y1) ? 1 : -1;
14
15 // Calculate the initial error term
16 int error = dx - dy;
17
18 // Start drawing the line
19 while (1) {
20 // Set pixels for the line segment with the specified thickness
21 for (int i = -thickness / 2; i <= thickness / 2; i++) {
22 set_pixel(x0 + i, y0, color); // Draw a horizontal line segment
23 }
24
25 // Check if we have reached the end point
26 if (x0 == x1 && y0 == y1) {
27 break;
28 }
29
30 int error2 = error * 2;
31
32 // Adjust the coordinates based on the error term
33 if (error2 > -dy) {
34 error -= dy;
35 x0 += sx;
36 }
37
38 if (error2 < dx) {
39 error += dx;
40 y0 += sy;
41 }
42 }
43 }
44
45
46 void draw_line_extern(uint8_t *buffer, size_t width, size_t height, int x0, int y0, int x1, int y1, int thickness, int color) {
47 // Calculate the differences and absolute values of x and y coordinates
48 int dx = ABS(x1 - x0);
49 int dy = ABS(y1 - y0);
50
51 // Determine the direction for incrementing x and y
52 int sx = (x0 < x1) ? 1 : -1;
53 int sy = (y0 < y1) ? 1 : -1;
54
55 // Calculate the initial error term
56 int error = dx - dy;
57
58 // Start drawing the line
59 while (1) {
60 // Set pixels for the line segment with the specified thickness
61 for (int i = -thickness / 2; i <= thickness / 2; i++) {
62 buffer_set_pixel4(buffer, width, height, x0 + i, y0, color); // Draw a horizontal line segment
63 }
64
65 // Check if we have reached the end point
66 if (x0 == x1 && y0 == y1) {
67 break;
68 }
69
70 int error2 = error * 2;
71
72 // Adjust the coordinates based on the error term
73 if (error2 > -dy) {
74 error -= dy;
75 x0 += sx;
76 }
77
78 if (error2 < dx) {
79 error += dx;
80 y0 += sy;
81 }
82 }
83 }
common.h
Основные определения ядра
buffer_set_pixel4
void buffer_set_pixel4(uint8_t *buffer, size_t width, size_t height, size_t x, size_t y, size_t color)
Устновливает пиксель RGB в буфере в котором все пиксели представляют собой RGBA (альфа канал игнориру...
Definition: tty.c:177