SayoriOS  0.3.3
mala.c
1 // Måla v0.1 (Swedish - Draw) (read as Mola) by NDRAEY (c) 2023
2 
3 #include <kernel.h>
4 
5 #define BUFSIZE(width, height) (width * height * 4)
6 #define STATUSBAR_HEIGHT 32
7 
8 uint8_t* buffer = 0;
9 
10 size_t canvas_width = 0;
11 size_t canvas_height = 0;
12 
13 size_t cursor_pos_x = 0;
14 size_t cursor_pos_y = 0;
15 
16 size_t current_color = 0x000000;
17 bool is_click = false;
18 
19 size_t buffer_size = 0;
20 size_t brush_size = 3;
21 
22 size_t old_x = 0;
23 size_t old_y = 0;
24 
25 bool change_coords = false;
26 
27 #define COLORS 8
28 
29 typedef struct {
30  size_t x;
31  size_t y;
32  size_t width;
33  size_t height;
34  size_t color;
35 } ColorZone_t;
36 
37 ColorZone_t colors[COLORS] = {
38  {400, 4, 25, 25, 0xFF0000},
39  {430, 4, 25, 25, 0x00FF00},
40  {460, 4, 25, 25, 0x0000FF},
41  {490, 4, 25, 25, 0x000000},
42  {520, 4, 25, 25, 0xFFFF00},
43  {550, 4, 25, 25, 0x00FFFF},
44  {580, 4, 25, 25, 0xFF00FF},
45  {610, 4, 25, 25, 0xFFFFFF}
46 };
47 
48 char text_buffer[16] = {0};
49 
50 void mala_init() {
51  canvas_width = getScreenWidth();
52  canvas_height = getScreenHeight() - STATUSBAR_HEIGHT;
53 
54  buffer_size = BUFSIZE(canvas_width, canvas_height);
55 
56  buffer = kmalloc(buffer_size);
57 
58  memset(buffer, 0xFF, buffer_size);
59 }
60 
61 void mala_flush() {
62  memcpy((char*)getFrameBufferAddr() + BUFSIZE(canvas_width, STATUSBAR_HEIGHT), buffer, buffer_size);
63 
64 
65  for(int i = 0; i < COLORS; i++) {
66  // Border
67 
68  drawRect(
69  colors[i].x - 2,
70  colors[i].y - 2,
71  colors[i].width + 4,
72  colors[i].height + 4,
73  0
74  );
75 
76  // Color
77  drawRect(
78  colors[i].x,
79  colors[i].y,
80  colors[i].width,
81  colors[i].height,
82  colors[i].color
83  );
84  }
85 
86  drawRect(cursor_pos_x, cursor_pos_y, 16, 16, current_color);
87 
88  punch();
89 }
90 
91 void mala_exit() {
92  kfree(buffer);
93 
94  set_cursor_enabled(true);
95 
96  clean_tty_screen();
97 }
98 
99 bool check_colors() {
100  for(int i = 0; i < COLORS; i++) {
101  if(point_in_rect(
102  cursor_pos_x,
103  cursor_pos_y,
104  colors[i].x,
105  colors[i].y,
106  colors[i].width,
107  colors[i].height
108  ) && is_click) {
109  current_color = colors[i].color;
110  return true;
111  }
112  }
113  return false;
114 }
115 
116 void mala_control() {
117  drawRect(0, 0, canvas_width, STATUSBAR_HEIGHT, 0x666666);
118 
119  draw_vga_str("Mala v0.2", 9, 16, 10, 0);
120 
121  itoh(current_color, text_buffer);
122  drawRect(100, 0, 2, STATUSBAR_HEIGHT, 0);
123  draw_vga_str(text_buffer, 6, 108, 10, 0);
124 
125  memset(text_buffer, 0, 16);
126  itoa(cursor_pos_x, text_buffer);
127  drawRect(172, 0, 2, STATUSBAR_HEIGHT, 0);
128  draw_vga_str(text_buffer, 4, 180, 10, 0);
129 
130  memset(text_buffer, 0, 16);
131  itoa(cursor_pos_y, text_buffer);
132  draw_vga_str(text_buffer, 4, 212, 10, 0);
133 
134  cursor_pos_x = mouse_get_x();
135  cursor_pos_y = mouse_get_y();
136 
137  is_click = (bool)mouse_get_b1();
138 
139  bool in_canvas = point_in_rect(cursor_pos_x, cursor_pos_y, 0, STATUSBAR_HEIGHT, canvas_width - 1, canvas_height - 1);
140 
141  if(check_colors())
142  return;
143 
144  if(is_click) {
145  draw_line_extern(
146  buffer,
147  canvas_width,
148  canvas_height,
149  old_x,
150  old_y - STATUSBAR_HEIGHT,
151  cursor_pos_x,
152  cursor_pos_y - STATUSBAR_HEIGHT,
153  brush_size,
154  current_color
155  );
156  }
157 
158  if(in_canvas) {
159  old_x = cursor_pos_x;
160  old_y = cursor_pos_y;
161  }
162 }
163 
164 uint32_t mala_draw(uint32_t argc, char* argv[]) {
165  // Disable TTY cursor
166 
167  mala_init();
168 
169  set_cursor_enabled(false);
170 
171  while(1) {
172  if(getCharRaw() == 1) {
173  mala_exit();
174  break;
175  }
176 
177  if(getCharRaw() == 48) {
178  memset(buffer, 0xFF, buffer_size);
179  }
180 
181  mala_control();
182 
183  mala_flush();
184  }
185 
186  return 0;
187 }
void * memset(void *ptr, char value, size_t num)
Заполнение массива указанными символами
Definition: string.c:203
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
size_t itoa(int32_t n, char *buffer)
Конвертируем число в символы
Definition: string.c:623
void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t color)
Рисуем залитый прямоугольник
Definition: pixel.c:25