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(point a, point b, 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(point position, ksys_color_t color)
34 {
35 _ksys_draw_pixel(position.x, position.y, color);
36 }
37
43 void DrawCircle(point coord, unsigned Radius, unsigned detalization = 36, ksys_color_t color = OS::sys_color_table.work_graph)
44 {
45 point buff;
46 unsigned b = Radius;
47 unsigned c = 0;
48 for (unsigned angle = 1; angle <= detalization * 10; angle += 36 / detalization)
49 {
50 buff = {coord.x + b, coord.y + c};
51 b = Radius * cos(angle);
52 c = sqrt((Radius*Radius) - (b*b));
53 point n = {coord.x + b, coord.y + c};
54 DrawLine(buff, n);
55 }
56 }
57
62 void DrawRectangleFill(point position, point size, ksys_color_t color = OS::sys_color_table.work_graph)
63 {
64 position.x *= AAANUMBER;
65 position.y *= AAANUMBER;
66
67 __asm__ __volatile__(
68 "int $0x40" ::"a"(13), "b"(position.x + size.x), "c"(position.y + size.y), "d"(color));
69 }
70
75 void DrawCircleFill(point coord, unsigned Radius, ksys_color_t color = OS::sys_color_table.work_graph)
76 {
77 DrawCircle(coord, Radius, color);
78 unsigned b = Radius * cos(90+45);
79 unsigned c = sqrt((Radius * Radius) - (b * b));
80 point n = {coord.x + b, coord.y + c};
81 DrawRectangleFill(n, {(coord.x - n.x) * 2, c * 2}, color);
82 for (unsigned i = Radius; i > (Radius -(coord.x - n.x)); i--)//Дозакрашивание пробелов между квадратом и границами груга
83 {
84 DrawCircle(coord, i, NULL, color);
85 }
86 }
87
93 void DrawPoint(point position, unsigned size, ksys_color_t color = OS::sys_color_table.work_graph, bool a = false)
94 {
95 if(size < 3)
96 {
97 a = true;
98 }
99 if (a)
100 {
101 DrawCircle(position, size, color);
102 }
103 else
104 {
105 DrawCircleFill(position, size, color);
106 }
107 }
108
109
113 void DrawRectangleLines(point a, point b, ksys_color_t color = OS::sys_color_table.work_graph)
114 {
115 DrawLine(a, {b.x, a.y}, color);
116 DrawLine(a, {a.x, b.y}, color);
117 DrawLine({b.x, a.y}, b, color);
118 DrawLine({a.x, b.y}, b, color);
119 }
120
121
122
128 inline void DrawTriangle(point a, point b, point c, ksys_color_t color = OS::sys_color_table.work_graph)
129 {
130 DrawLine(a, b, color);
131 DrawLine(a, c, color);
132 DrawLine(b, c, color);
133 }
134
135 //=============================================================================================================================================================
136
138 class Image
139 {
140 private:
141 std::vector<rgb_t> _img;
142 point _size;
143 public:
144 Image(std::string Path);
145 ~Image();
146
150 bool LoadImage(std::string Path);
151
156 void DrawImage(point coord = {0,0}, point size = {0,0});
157 };
158
159 Image::Image(std::string Path)
160 {
161 LoadImage(Path);
162 }
163
164 Image::~Image()
165 {
166 }
167
168 bool Image::LoadImage(std::string Path)
169 {
170 if(filesystem::Exist(Path))
171 {
172 std::string Type;
173 std::ifstream img;
174 {
175 unsigned i = Path.length();
176
177 while (Path[i] != '.')
178 {
179 Type.push_back(Path[i]);
180 i--;
181 }
182 }
183
184 if (Type == "png")//Потом канибудь надо допилить открытие png
185 {
186 img.open(Path.c_str(), std::ios::binary);
187
188 char buff[8];
189
190 for(unsigned i = 0; i < 8; i++)
191 {
192 img >> buff[8];
193 }
194 if(buff[0] == 89)
195 {
196 //тут двльше
197 }
198 }
199
200 img.close();
201 }
202 else
203 {
204 return false;
205 }
206 }
207
208 void Image::DrawImage(point coord, point size)
209 {
210 if(size.x == 0 && size.y == 0)
211 {
212 size = _size;
213 }
214
215 rgb_t *d = (rgb_t*)malloc(_img.size() * sizeof(rgb_t));
216
217 for(unsigned i = 0; i < _img.size(); i++)
218 {
219 d[i] = _img[i];
220 }
221
222 _ksys_draw_bitmap(d,coord.x, coord.y, size.x, size.y);
223
224 free(d);
225 }
226 //=============================================================================================================================================================
227
228
229 }
230} // namespace KolibriLib
231
232
233#endif // __GRAPHIC_H__
Класс для работы с изображениями
Definition graphic.hpp:139
void DrawImage(point coord={0, 0}, point size={0, 0})
Вывести изображение
Definition graphic.hpp:208
bool LoadImage(std::string Path)
Загрузить изображение из файла
Definition graphic.hpp:168
ksys_colors_table_t sys_color_table
Таблица стандартных(системных) цветов
Definition base.hpp:37
bool Exist(const std::string Path)
проверяет существует ли файл или папки
Definition filesystem.hpp:108
Основное пространство имён
Definition base.hpp:17
Просто точка
Definition base.hpp:22