KolibriLib
Loading...
Searching...
No Matches
graphic.hpp
1#ifndef __GRAPHIC_H__
2#define __GRAPHIC_H__
3
4
5#include <sys/ksys.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#include <math.h>
11
12#include <fstream>
13
14#include "base.hpp"
15#include "filesystem.hpp"
16
17namespace KolibriLib
18{
19 // Графика
20 namespace graphic
21 {
25 inline void DrawLine(const point& a, const point& b, const ksys_color_t color = OS::sys_color_table.work_graph)
26 {
27 _ksys_draw_line(a.x, a.y, b.x, b.y, color);
28 }
29
33 inline void DrawPixel(const point& position, const ksys_color_t color = OS::sys_color_table.work_text)
34 {
35 _ksys_draw_pixel(position.x, position.y, color);
36 }
37
44 void DrawCircle(point coord, unsigned Radius, unsigned detalization = 36, ksys_color_t color = OS::sys_color_table.work_graph)
45 {
46 point buff;
47 unsigned b = Radius;
48 unsigned c = 0;
49 for (unsigned angle = 1; angle <= detalization * 10; angle += (36 / detalization))
50 {
51 buff = {coord.x + b, coord.y + c};
52 b = Radius * cos(angle);
53 c = sqrt((Radius*Radius) - (b*b));
54 point n = {coord.x + b, coord.y + c};
55 DrawLine(buff, n);
56 }
57 }
58
63 void DrawRectangleFill(point position, const point& size, const ksys_color_t color = OS::sys_color_table.work_graph)
64 {
65 __asm__ __volatile__(
66 "int $0x40"
67 ::"a"(13), "b"((position.x << 16) + size.x), "c"((position.y << 16) + size.y), "d"(color));
68 }
69
74 void DrawCircleFill(point coord, unsigned Radius, ksys_color_t color = OS::sys_color_table.work_graph)
75 {
76 DrawCircle(coord, Radius, color);
77 unsigned b = Radius * cos(90+45);
78 unsigned c = sqrt((Radius * Radius) - (b * b));
79 point n = {coord.x + b, coord.y + c};
80 DrawRectangleFill(n, {(coord.x - n.x) * 2, c * 2}, color);
81 for (unsigned i = Radius; i > (Radius -(coord.x - n.x)); i--)//Дозакрашивание пробелов между квадратом и границами груга
82 {
83 DrawCircle(coord, i, NULL, color);
84 }
85 }
86
93 void DrawPoint(const point& position, const unsigned& Radius, const ksys_color_t& color = OS::sys_color_table.work_graph, bool fill = false)
94 {
95 if(Radius < 3) //Если радиус меньше 3 пикселей, то не смысла его закрашивать
96 {
97 fill = false;
98 }
99 if (fill)
100 {
101 DrawCircleFill(position, Radius, color);
102 }
103 else
104 {
105 DrawCircle(position, Radius, color);
106 if(Radius < 3)
107 {
108 DrawPixel(position, color);
109 }
110 }
111 }
112
113
117 void DrawRectangleLines(point a, point b, ksys_color_t color = OS::sys_color_table.work_graph)
118 {
119 DrawLine(a, {b.x, a.y}, color);
120 DrawLine(a, {a.x, b.y}, color);
121 DrawLine({b.x, a.y}, b, color);
122 DrawLine({a.x, b.y}, b, color);
123 }
124
125
126
132 inline void DrawTriangle(point a, point b, point c, ksys_color_t color = OS::sys_color_table.work_graph)
133 {
134 DrawLine(a, b, color);
135 DrawLine(a, c, color);
136 DrawLine(b, c, color);
137 }
138
139 //=============================================================================================================================================================
140
142 class Image
143 {
144 private:
145 std::vector<rgb_t> _img;
146 point _size;
147 public:
148 Image(std::string Path);
149 ~Image();
150
154 bool LoadImage(std::string Path);
155
160 void DrawImage(point coord = {0,0}, point size = {0,0});
161 };
162
163 Image::Image(std::string Path)
164 {
165 LoadImage(Path);
166 }
167
168 Image::~Image()
169 {
170 }
171
172 bool Image::LoadImage(std::string Path)
173 {
174 bool extcode;
175 if(filesystem::Exist(Path)) //Если файл с изображением существует
176 {
177 std::string Type;
178 std::ifstream img;
179
180 {
181 unsigned i = Path.length();
182
183 while (Path[i] != '.')
184 {
185 Type.push_back(Path[i]);
186 i--;
187 }
188 }
189
190 if (Type == "png")//Потом канибудь надо допилить открытие png
191 {
192 img.open(Path.c_str(), std::ios::binary);
193
194 char buff[8];
195
196 for(unsigned i = 0; i < 8; i++)
197 {
198 img >> buff[8];
199 }
200 if(buff[0] == 89)
201 {
202 //тут двльше
203 }
204 }
205 else if (Type == "jpeg")
206 {
207 /* code */
208 }
209 else
210 {
211 extcode = false;
212 }
213
214
215 img.close();
216 }
217 else
218 {
219 return extcode = false;
220 }
221 return extcode;
222 }
223
224 void Image::DrawImage(point coord, point size)
225 {
226 if(size.x == 0 && size.y == 0)
227 {
228 size = _size;
229 }
230
231 rgb_t *d = (rgb_t*)malloc(_img.size() * sizeof(rgb_t));
232
233 for(unsigned i = 0; i < _img.size(); i++)
234 {
235 d[i] = _img[i];
236 }
237
238 _ksys_draw_bitmap(d,coord.x, coord.y, size.x, size.y);
239
240 free(d);
241 }
242 //=============================================================================================================================================================
243
244
245 }
246} // namespace KolibriLib
247
248
249#endif // __GRAPHIC_H__
Класс для работы с изображениями
Definition graphic.hpp:143
void DrawImage(point coord={0, 0}, point size={0, 0})
Вывести изображение
Definition graphic.hpp:224
bool LoadImage(std::string Path)
Загрузить изображение из файла
Definition graphic.hpp:172
ksys_colors_table_t sys_color_table
Таблица стандартных(системных) цветов
Definition base.hpp:36
bool Exist(const std::string &Path)
проверяет существует ли файл или папки
Definition filesystem.hpp:109
Основное пространство имён
Definition base.hpp:17
Просто точка
Definition base.hpp:26