SayoriOS 0.3.3

jse_canvas.c
1 #include "portability.h"
3 #include <io/ports.h>
4 #include "io/tty.h"
5 #include "lib/stdio.h"
6
7 #include "../elk_config.h"
8 #include "../elk.h"
9
12
20
32
40
44
50
64
70
73
80
87
88
89 //uint8_t* jse_createBuffer(){
90 // uint8_t *jse_framebuffer_addr;
91 // uint32_t jse_framebuffer_size = (getScreenHeight()) * (getDisplayPitch());
92 // jse_framebuffer_addr = (uint8_t*)kcalloc(jse_framebuffer_size, 1);
93 // qemu_note("[JSE] [Canvas] Create buffer: %x | Size: %d", framebuffer_addr, jse_framebuffer_size);
94 // return jse_framebuffer_addr;
95 //}
96
97 jsval_t jse_ext_canvas_fillStyle(struct js *js, jsval_t *args, int nargs) {
98 if (nargs != 1) return js_mknum(js->Canvas.fillStyle);
99 int c = jse_func_atoi(js_str(js,(args[0]))); // Fetch 1st arg
100 qemu_log(" [JSE] [EXT] [Canvas] [fillStyle] Color:%x", c);
101 js->Canvas.fillStyle = c;
102 return js_mknum(js->Canvas.fillStyle);
103 }
104
105 jsval_t jse_ext_canvas_setPixel(struct js *js, jsval_t *args, int nargs) {
106 if (nargs != 3) return js_mkfalse();
107 int x = jse_func_atoi(js_str(js,(args[0]))); // Fetch 1st arg
108 int y = jse_func_atoi(js_str(js,(args[1]))); // Fetch 2st arg
109 int c = jse_func_atoi(js_str(js,(args[2]))); // Fetch 3st arg
110 qemu_log(" [JSE] [EXT] [Canvas] [setPixel] X: %d | Y: %d | Color:%x", x, y, c);
111 set_pixel(x, y, c);
112 return js_mktrue();
113 }
114
115
116 jsval_t jse_ext_canvas_lineWidth(struct js *js, jsval_t *args, int nargs) {
117 if (nargs != 1) return js_mknum(js->Canvas.lineWidth);
118 int c = jse_func_atoi(js_str(js,(args[0]))); // Fetch 1st arg
119 qemu_log(" [JSE] [EXT] [Canvas] [lineWidth] lineWidth:%d", c);
120 js->Canvas.lineWidth = c;
121 return js_mknum(js->Canvas.lineWidth);
122 }
123
124
125 jsval_t jse_ext_canvas_width(struct js *js, jsval_t *args, int nargs) {
126 return js_mknum(js->Canvas.width);
127 }
128
129
130 jsval_t jse_ext_canvas_height(struct js *js, jsval_t *args, int nargs) {
131 return js_mknum(js->Canvas.height);
132 }
133
134 jsval_t jse_ext_canvas_fillRect(struct js *js, jsval_t *args, int nargs) {
135 if (nargs != 4) return js_mkundef();
136 int x = jse_getInt(js,args[0]); // Fetch 1st arg
137 int y = jse_getInt(js,args[1]); // Fetch 2nd arg
138 int w = jse_getInt(js,args[2]); // Fetch 3nd arg
139 int h = jse_getInt(js,args[3]); // Fetch 4nd arg
140
141 int color = js->Canvas.fillStyle;
142 qemu_log(" [JSE] [EXT] [Canvas] [fillRect] X:%d Y:%d W:%d H:%d Color:%x", x, y, w, h, color);
143 drawRect(x, y, w, h, color);
144
145 return js_mktrue();
146 }
147
148
149 jsval_t jse_ext_canvas_clearRect(struct js *js, jsval_t *args, int nargs) {
150 if (nargs != 4) return js_mkundef();
151 int x = jse_getInt(js,args[0]); // Fetch 1st arg
152 int y = jse_getInt(js,args[1]); // Fetch 2nd arg
153 int w = jse_getInt(js,args[2]); // Fetch 3nd arg
154 int h = jse_getInt(js,args[3]); // Fetch 4nd arg
155
156 drawRect(x, y, w, h, 0xFF000000);
157 qemu_log(" [JSE] [EXT] [Canvas] [clearRect] X:%d Y:%d W:%d H:%d", x, y, w, h);
158
159 return js_mktrue();
160 }
161
162
163 jsval_t jse_ext_canvas_strokeRect(struct js *js, jsval_t *args, int nargs) {
164 if (nargs != 4) return js_mkundef();
165 int x = jse_getInt(js,args[0]); // Fetch 1st arg
166 int y = jse_getInt(js,args[1]); // Fetch 2nd arg
167 int w = jse_getInt(js,args[2]); // Fetch 3nd arg
168 int h = jse_getInt(js,args[3]); // Fetch 4nd arg
169 int lineWidth = js->Canvas.lineWidth; // Ширина линии
170 int color = js->Canvas.fillStyle;
171 if (lineWidth == 0) return js_mkfalse();
172
173 for (int i = 0; i < lineWidth; i++) {
174 int new_width = w + 2 * i;
175 int new_height = h + 2 * i;
176 set_pixel(x - i, y - i, color); // Верхняя граница
177 set_pixel(x - i + new_width, y - i, color); // Правая граница
178 set_pixel(x - i, y - i + new_height, color); // Нижняя граница
179 set_pixel(x - i + new_width, y - i + new_height, color); // Левая граница
180 }
181
182 for (int i = 0; i < lineWidth; i++) {
183 int new_x = x + i;
184 int new_y = y + i;
185 int new_width = w - 2 * i;
186 int new_height = h - 2 * i;
187 for (int j = new_y; j < new_y + new_height; j++) {
188 set_pixel(new_x, j, color); // Левая граница
189 set_pixel(new_x + new_width, j, color); // Правая граница
190 }
191 for (int j = new_x; j < new_x + new_width; j++) {
192 set_pixel(j, new_y, color); // Верхняя граница
193 set_pixel(j, new_y + new_height, color); // Нижняя граница
194 }
195 }
196 qemu_log(" [JSE] [EXT] [Canvas] [strokeRect] X:%d Y:%d W:%d H:%d Color:%x", x, y, w, h, color);
197
198 //drawRect(x, y, w, h, 0xFF000000);
199
200 return js_mktrue();
201 }
202
203
204 void jse_canvas_config(struct js* js){
205 // Установка значений по умолчанию
206 JSE_CANVAS cfg = js->Canvas;
207 cfg.fillStyle = 0xFFFFFF;
208 cfg.lineWidth = 2;
209 cfg.width = 800;
210 cfg.height = 600;
211 // Регистрация функционала
212 qemu_note("[JSE] [EXT] [Canvas] Registration of functions");
213 js_set(js, js_glob(js), "canvas_fillRect", js_mkfun(jse_ext_canvas_fillRect));
214 js_set(js, js_glob(js), "canvas_clearRect", js_mkfun(jse_ext_canvas_clearRect));
215 js_set(js, js_glob(js), "canvas_strokeRect", js_mkfun(jse_ext_canvas_strokeRect));
216
217 js_set(js, js_glob(js), "canvas_height", js_mkfun(jse_ext_canvas_height));
218 js_set(js, js_glob(js), "canvas_width", js_mkfun(jse_ext_canvas_width));
219
220 js_set(js, js_glob(js), "canvas_fillStyle", js_mkfun(jse_ext_canvas_fillStyle));
221 js_set(js, js_glob(js), "canvas_lineWidth", js_mkfun(jse_ext_canvas_lineWidth));
222
224
225 js_set(js, js_glob(js), "canvas_setPixel", js_mkfun(jse_ext_canvas_setPixel));
226
228 js_set(js, js_glob(js), "canvas_rect", js_mkfun(jse_ext_canvas_fillRect));
229 return;
230 }
231
232 void jse_canvas_destroy(struct js* js){
233
234 }
drawRect
void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t color)
Рисуем залитый прямоугольник
Definition: pixel.c:25
js
Definition: elk.h:48