root / proj / src / graph.c @ 225
History | View | Annotate | Download (14 KB)
1 | 178 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | 168 | up20180642 | #include "graph.h" |
4 | 183 | up20180642 | |
5 | 168 | up20180642 | #include "errors.h" |
6 | #include <stdio.h> |
||
7 | |||
8 | 174 | up20180642 | /// MACROS
|
9 | #define FAR2PHYS(n) ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF)) |
||
10 | |||
11 | /// STRUCT
|
||
12 | typedef struct __attribute__((packed)) { |
||
13 | |||
14 | char VbeSignature[4] ; |
||
15 | uint16_t VbeVersion ; |
||
16 | uint32_t OemStringPtr ; |
||
17 | uint8_t Capabilities[4] ;
|
||
18 | uint32_t VideoModePtr ; |
||
19 | uint16_t TotalMemory ; |
||
20 | |||
21 | uint16_t OemSoftwareRev ; |
||
22 | uint32_t OemVendorNamePtr ; |
||
23 | uint32_t OemProductNamePtr ; |
||
24 | uint32_t OemProductRevPtr ; |
||
25 | char Reserved[222] ; |
||
26 | |||
27 | char OemData[256] ; |
||
28 | } VbeInfoBlock; |
||
29 | |||
30 | 168 | up20180642 | static vbe_mode_info_t vbe_mem_info;
|
31 | |||
32 | 174 | up20180642 | /// PRIVATE GET
|
33 | static uint16_t (graph_get_bits_pixel) (void){ return vbe_mem_info.BitsPerPixel; } |
||
34 | static phys_bytes (graph_get_phys_addr) (void){ return vbe_mem_info.PhysBasePtr; } |
||
35 | static unsigned (graph_get_vram_size) (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); } |
||
36 | //static uint16_t (graph_get_RedMaskSize) (void){ return vbe_mem_info.RedMaskSize ; }
|
||
37 | //static uint16_t (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
|
||
38 | //static uint16_t (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
|
||
39 | |||
40 | 208 | up20180642 | /// PUBLIC GET
|
41 | uint16_t (graph_get_XRes) (void){ return vbe_mem_info.XResolution; } |
||
42 | uint16_t (graph_get_YRes) (void){ return vbe_mem_info.YResolution; } |
||
43 | uint16_t (graph_get_bytes_pixel) (void){ return (graph_get_bits_pixel() + 7) >> 3; } |
||
44 | |||
45 | ///
|
||
46 | 174 | up20180642 | static int (get_permission)(unsigned int base_addr, unsigned int size) { |
47 | 168 | up20180642 | struct minix_mem_range mmr;
|
48 | mmr.mr_base = base_addr; |
||
49 | mmr.mr_limit = base_addr + size; |
||
50 | return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
|
||
51 | } |
||
52 | |||
53 | 174 | up20180642 | //static int (get_permissions_first_mbyte)(void) {
|
54 | 168 | up20180642 | // return get_permission(MBYTE_BASE, MBYTE_SIZE);
|
55 | //}
|
||
56 | |||
57 | 174 | up20180642 | /// MEMORY
|
58 | 178 | up20180642 | static uint8_t *video_mem = NULL; /** @brief Frame-buffer VM address. */ |
59 | 174 | up20180642 | static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */ |
60 | static mmap_t mem_map;
|
||
61 | static int (graph_free_memory)(void) { |
||
62 | int r = SUCCESS;
|
||
63 | free(video_buf); video_buf = NULL;
|
||
64 | r = !lm_free(&mem_map); |
||
65 | return r;
|
||
66 | } |
||
67 | static int (graph_map_vram)(void) { |
||
68 | int r;
|
||
69 | const unsigned vram_base = graph_get_phys_addr(); |
||
70 | const unsigned vram_size = graph_get_vram_size(); |
||
71 | if ((r = get_permission(vram_base, vram_size))) {
|
||
72 | if (graph_free_memory()) {
|
||
73 | printf("%s: lm_free failed\n", __func__);
|
||
74 | } |
||
75 | panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
||
76 | 168 | up20180642 | } |
77 | |||
78 | 174 | up20180642 | video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
|
79 | 168 | up20180642 | |
80 | 174 | up20180642 | if (video_mem == MAP_FAILED) {
|
81 | if (graph_free_memory()) {
|
||
82 | printf("%s: lm_free failed\n", __func__);
|
||
83 | } |
||
84 | panic("%s: couldn't map video memory.", __func__);
|
||
85 | } |
||
86 | |||
87 | video_buf = malloc(vram_size); |
||
88 | |||
89 | 168 | up20180642 | return SUCCESS;
|
90 | } |
||
91 | |||
92 | 174 | up20180642 | /// INFO GET
|
93 | static int (vbe_get_mode_information)(uint16_t mode) { |
||
94 | 168 | up20180642 | memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values |
95 | |||
96 | struct reg86 reg_86;
|
||
97 | memset(®_86, 0, sizeof(struct reg86)); // reset struct |
||
98 | |||
99 | vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
|
||
100 | |||
101 | reg_86.intno = VC_BIOS_SERV; |
||
102 | reg_86.ah = VBE_CALL; |
||
103 | reg_86.al = VBE_MD_INFO; |
||
104 | reg_86.cx = mode; |
||
105 | reg_86.es = PB2BASE(mem_map.phys); |
||
106 | reg_86.di = PB2OFF(mem_map.phys); |
||
107 | // BIOS CALL
|
||
108 | if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
||
109 | printf("%s: sys_int86 failed\n", __func__);
|
||
110 | if (graph_free_memory()) {
|
||
111 | printf("%s: lm_free failed\n", __func__);
|
||
112 | } |
||
113 | return BIOS_CALL_ERROR;
|
||
114 | } |
||
115 | |||
116 | memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size); |
||
117 | return SUCCESS;
|
||
118 | } |
||
119 | 174 | up20180642 | /*
|
120 | static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
|
||
121 | 168 | up20180642 | memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
|
122 | |||
123 | mmap_t controller_map;
|
||
124 | |||
125 | struct reg86 reg_86;
|
||
126 | memset(®_86, 0, sizeof(struct reg86)); // reset struct
|
||
127 | |||
128 | VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
|
||
129 | |||
130 | uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
|
||
131 | |||
132 | virtual_addr->VbeSignature[0] = 'V';
|
||
133 | virtual_addr->VbeSignature[1] = 'B';
|
||
134 | virtual_addr->VbeSignature[2] = 'E';
|
||
135 | virtual_addr->VbeSignature[3] = '2';
|
||
136 | |||
137 | |||
138 | reg_86.intno = VC_BIOS_SERV;
|
||
139 | reg_86.ah = VBE_CALL;
|
||
140 | reg_86.al = VBE_CTRL_INFO;
|
||
141 | reg_86.es = PB2BASE(controller_map.phys);
|
||
142 | reg_86.di = PB2OFF(controller_map.phys);
|
||
143 | // BIOS CALL
|
||
144 | if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
||
145 | printf("%s: sys_int86 failed\n", __func__);
|
||
146 | if (!lm_free(&controller_map)) {
|
||
147 | printf("%s: lm_free failed\n", __func__);
|
||
148 | }
|
||
149 | return BIOS_CALL_ERROR;
|
||
150 | }
|
||
151 | |||
152 | info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
|
||
153 | info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
|
||
154 | info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
|
||
155 | info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
|
||
156 | |||
157 | uint8_t lsb, msb;
|
||
158 | util_get_LSB(virtual_addr->VbeVersion, &lsb);
|
||
159 | util_get_MSB(virtual_addr->VbeVersion, &msb);
|
||
160 | info_p->VBEVersion[0] = lsb;
|
||
161 | info_p->VBEVersion[1] = msb;
|
||
162 | |||
163 | info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
|
||
164 | |||
165 | // Convert Far Far Pointer to Virtual Address
|
||
166 | |||
167 | uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
|
||
168 | uint32_t virtual_ptr = phys_ptr + virtual_base;
|
||
169 | info_p->OEMString = (char*)(virtual_ptr);
|
||
170 | |||
171 | phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
|
||
172 | virtual_ptr = phys_ptr + virtual_base;
|
||
173 | info_p->VideoModeList = (uint16_t*)(virtual_ptr);
|
||
174 | |||
175 | phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
|
||
176 | virtual_ptr = phys_ptr + virtual_base;
|
||
177 | info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
|
||
178 | |||
179 | phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
|
||
180 | virtual_ptr = phys_ptr + virtual_base;
|
||
181 | info_p->OEMProductNamePtr = (char*)(virtual_ptr);
|
||
182 | |||
183 | phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
|
||
184 | virtual_ptr = phys_ptr + virtual_base;
|
||
185 | info_p->OEMProductRevPtr = (char*)(virtual_ptr);
|
||
186 | |||
187 | if (!lm_free(&controller_map)) {
|
||
188 | printf("%s: lm_free failed\n", __func__);
|
||
189 | return LCF_ERROR;
|
||
190 | }
|
||
191 | |||
192 | return SUCCESS;
|
||
193 | }
|
||
194 | 174 | up20180642 | */
|
195 | 168 | up20180642 | |
196 | 174 | up20180642 | /// INIT
|
197 | /**
|
||
198 | * @brief
|
||
199 | * @param mode
|
||
200 | * @return
|
||
201 | */
|
||
202 | static int (graph_set_mode)(uint16_t mode) { |
||
203 | 168 | up20180642 | struct reg86 reg_86;
|
204 | |||
205 | memset(®_86, 0, sizeof(struct reg86)); // reset struct |
||
206 | |||
207 | // Set Reg86
|
||
208 | reg_86.intno = VC_BIOS_SERV; |
||
209 | reg_86.ah = VBE_CALL; |
||
210 | reg_86.al = SET_VBE_MD; |
||
211 | reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD; |
||
212 | |||
213 | // BIOS CALL
|
||
214 | if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
||
215 | printf("%s: sys_int86 failed\n", __func__);
|
||
216 | return BIOS_CALL_ERROR;
|
||
217 | } |
||
218 | |||
219 | return SUCCESS;
|
||
220 | } |
||
221 | 174 | up20180642 | int (graph_init)(uint16_t mode){
|
222 | if (vbe_get_mode_information(mode)) {
|
||
223 | printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
||
224 | return 1; |
||
225 | } |
||
226 | 168 | up20180642 | |
227 | 174 | up20180642 | graph_map_vram(); // if function fails it aborts program
|
228 | |||
229 | if (graph_set_mode(mode)) {
|
||
230 | printf("%s: failed to set graphic mode %x.\n", __func__, mode);
|
||
231 | return 1; |
||
232 | }; |
||
233 | return SUCCESS;
|
||
234 | } |
||
235 | |||
236 | /// CLEANUP
|
||
237 | int (graph_cleanup)(void){ |
||
238 | int r = SUCCESS;
|
||
239 | if ((r = vg_exit()))
|
||
240 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
241 | if ((r = graph_free_memory()))
|
||
242 | printf("%s: lm_free failed\n", __func__);
|
||
243 | return r;
|
||
244 | } |
||
245 | |||
246 | /// PIXEL DRAWING
|
||
247 | 168 | up20180642 | int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
|
248 | 183 | up20180642 | if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) { |
249 | 173 | up20180655 | //printf("%s: invalid pixel.\n", __func__);
|
250 | 168 | up20180642 | return OUT_OF_RANGE;
|
251 | } |
||
252 | unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel(); |
||
253 | memcpy(video_buf + pos, &color, graph_get_bytes_pixel()); |
||
254 | return SUCCESS;
|
||
255 | } |
||
256 | 208 | up20180642 | void (graph_set_pixel_pos)(unsigned pos, uint32_t color){ |
257 | 207 | up20180642 | memcpy(video_buf + pos, &color, graph_get_bytes_pixel()); |
258 | 168 | up20180642 | } |
259 | 208 | up20180642 | int (graph_clear_screen)(void){ memset(video_buf, 0, graph_get_vram_size()); return SUCCESS; } |
260 | int (graph_draw)(void){ memcpy(video_mem, video_buf, graph_get_vram_size()); return SUCCESS; } |
||
261 | 174 | up20180642 | |
262 | 211 | up20180642 | ///SPRITE
|
263 | 212 | up20180642 | #include "sprite.h" |
264 | |||
265 | 211 | up20180642 | #include "utils.h" |
266 | #include "fast_math.h" |
||
267 | #include <math.h> |
||
268 | |||
269 | struct basic_sprite{
|
||
270 | uint8_t *map; |
||
271 | uint16_t w, h; |
||
272 | int16_t u0, v0; |
||
273 | }; |
||
274 | basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){ |
||
275 | basic_sprite_t *ret = malloc(sizeof(basic_sprite_t));
|
||
276 | if(ret == NULL) return NULL; |
||
277 | enum xpm_image_type type = XPM_8_8_8_8;
|
||
278 | xpm_image_t img; |
||
279 | ret->map = xpm_load((xpm_map_t)xpm, type, &img); |
||
280 | if(ret->map == NULL){ |
||
281 | 216 | up20180642 | basic_sprite_dtor(ret); |
282 | 211 | up20180642 | return NULL; |
283 | } |
||
284 | ret->w = img.width; |
||
285 | ret->h = img.height; |
||
286 | ret->u0 = u0; |
||
287 | ret->v0 = v0; |
||
288 | return ret;
|
||
289 | } |
||
290 | void (basic_sprite_dtor)(basic_sprite_t *p){
|
||
291 | if(p == NULL) return; |
||
292 | free(p->map); |
||
293 | free(p); |
||
294 | } |
||
295 | const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; } |
||
296 | uint16_t (basic_sprite_get_w) (const basic_sprite_t *p){ return p->w ; } |
||
297 | uint16_t (basic_sprite_get_h) (const basic_sprite_t *p){ return p->h ; } |
||
298 | int16_t (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; } |
||
299 | int16_t (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; } |
||
300 | |||
301 | 216 | up20180642 | /*
|
302 | struct basic_sprite_alpha{
|
||
303 | uint8_t *map;
|
||
304 | uint16_t w, h;
|
||
305 | int16_t u0, v0;
|
||
306 | };
|
||
307 | basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0){
|
||
308 | basic_sprite_alpha_t *ret = malloc(sizeof(basic_sprite_t));
|
||
309 | if(ret == NULL) return NULL;
|
||
310 | enum xpm_image_type type = XPM_8_8_8_8;
|
||
311 | xpm_image_t img;
|
||
312 | ret->map = NULL;
|
||
313 | uint8_t *m = xpm_load((xpm_map_t)xpm, type, &img);
|
||
314 | if(m == NULL){
|
||
315 | basic_sprite_alpha_dtor(ret);
|
||
316 | return NULL;
|
||
317 | }
|
||
318 | ret->map = m;
|
||
319 | if(ret->map == NULL){
|
||
320 | basic_sprite_alpha_dtor(ret);
|
||
321 | return NULL;
|
||
322 | }
|
||
323 | ret->w = img.width;
|
||
324 | ret->h = img.height;
|
||
325 | ret->u0 = u0;
|
||
326 | ret->v0 = v0;
|
||
327 | return ret;
|
||
328 | }
|
||
329 | void (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p){
|
||
330 | if(p == NULL) return;
|
||
331 | free(p->map);
|
||
332 | free(p);
|
||
333 | }
|
||
334 | const uint8_t* (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p){ return p->map; }
|
||
335 | uint16_t (basic_sprite_alpha_get_w) (const basic_sprite_alpha_t *p){ return p->w ; }
|
||
336 | uint16_t (basic_sprite_alpha_get_h) (const basic_sprite_alpha_t *p){ return p->h ; }
|
||
337 | int16_t (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p){ return p->u0 ; }
|
||
338 | int16_t (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p){ return p->v0 ; }
|
||
339 | */
|
||
340 | |||
341 | 211 | up20180642 | struct sprite{
|
342 | const basic_sprite_t *bsp;
|
||
343 | int16_t x, y; //position in screen
|
||
344 | double theta, s, c;
|
||
345 | double scale;
|
||
346 | }; |
||
347 | sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){
|
||
348 | sprite_t *ret = malloc(sizeof(sprite_t));
|
||
349 | if(ret == NULL) return NULL; |
||
350 | ret->bsp = bsp; |
||
351 | ret->x = 0;
|
||
352 | ret->y = 0;
|
||
353 | sprite_set_angle(ret, 0.0); |
||
354 | ret->scale = 1.0; |
||
355 | return ret;
|
||
356 | } |
||
357 | void (sprite_dtor)(sprite_t *p){
|
||
358 | if(p == NULL) return; |
||
359 | free(p); |
||
360 | } |
||
361 | void (sprite_set_pos) (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; }
|
||
362 | void (sprite_set_angle) (sprite_t *p, double angle ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); } |
||
363 | void (sprite_set_scale) (sprite_t *p, double scale ){ p->scale = scale; } |
||
364 | int16_t (sprite_get_x)(const sprite_t *p){ return p->x; } |
||
365 | int16_t (sprite_get_y)(const sprite_t *p){ return p->y; } |
||
366 | 216 | up20180642 | uint16_t (sprite_get_w)(const sprite_t *p){ return basic_sprite_get_w(p->bsp); } |
367 | uint16_t (sprite_get_h)(const sprite_t *p){ return basic_sprite_get_h(p->bsp); } |
||
368 | 211 | up20180642 | void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){ |
369 | double dx = (x - p->x)/p->scale;
|
||
370 | double dy = (y - p->y)/p->scale;
|
||
371 | 215 | up20180642 | int16_t du = dx*p->c - dy*p->s - 0.5; |
372 | int16_t dv = dx*p->s + dy*p->c - 0.5; |
||
373 | 211 | up20180642 | *u = du + basic_sprite_get_u0(p->bsp); |
374 | *v = dv + basic_sprite_get_v0(p->bsp); |
||
375 | } |
||
376 | void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){ |
||
377 | int16_t du = u - basic_sprite_get_u0(p->bsp); |
||
378 | int16_t dv = v - basic_sprite_get_v0(p->bsp); |
||
379 | double dx = du*p->c + dv*p->s;
|
||
380 | double dy = -du*p->s + dv*p->c;
|
||
381 | *x = dx*p->scale + 0.5 + p->x; |
||
382 | *y = dy*p->scale + 0.5 + p->y; |
||
383 | } |
||
384 | void (sprite_draw)(const sprite_t *p){ |
||
385 | const uint16_t w = basic_sprite_get_w(p->bsp);
|
||
386 | const uint16_t h = basic_sprite_get_h(p->bsp);
|
||
387 | int16_t xmin, xmax, ymin, ymax; { |
||
388 | int16_t x, y; |
||
389 | sprite_pic2src(p, 0, 0, &x, &y); |
||
390 | xmin = x; xmax = x; ymin = y; ymax = y; |
||
391 | sprite_pic2src(p, w, 0, &x, &y);
|
||
392 | xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax); |
||
393 | sprite_pic2src(p, 0, h, &x, &y);
|
||
394 | xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax); |
||
395 | sprite_pic2src(p, w, h, &x, &y); |
||
396 | xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax); |
||
397 | 214 | up20180642 | xmin = max(xmin-(int16_t)p->scale-2, 0); xmax = min(xmax+(int16_t)p->scale+2, graph_get_XRes()); |
398 | ymin = max(ymin-(int16_t)p->scale-2, 0); ymax = min(ymax+(int16_t)p->scale+2, graph_get_YRes()); |
||
399 | 211 | up20180642 | } |
400 | const uint8_t *map = basic_sprite_get_map(p->bsp);
|
||
401 | const uint16_t bytes_pixel = graph_get_bytes_pixel();
|
||
402 | for(int16_t u, v, y = ymin; y < ymax; ++y){
|
||
403 | 212 | up20180642 | uint8_t *place = video_buf + (xmin + y*graph_get_XRes())*bytes_pixel; |
404 | for(int16_t x = xmin; x < xmax; ++x, place += bytes_pixel){
|
||
405 | 211 | up20180642 | sprite_src2pic(p, x, y, &u, &v); |
406 | if(0 <= u && u < w && 0 <= v && v < h){ |
||
407 | 212 | up20180642 | const uint8_t *c_p = map+(v*w+u)*4; |
408 | 216 | up20180642 | if(*(c_p+3) < ALPHA_THRESHOLD) //alpha |
409 | 212 | up20180642 | memcpy(place, c_p, bytes_pixel); |
410 | 211 | up20180642 | } |
411 | } |
||
412 | } |
||
413 | } |