Revision 174
refactored graph
proj/graph.c | ||
---|---|---|
6 | 6 |
|
7 | 7 |
#include <stdio.h> |
8 | 8 |
|
9 |
static void *video_mem = NULL; /** @brief Frame-buffer VM address. */ |
|
10 |
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */ |
|
9 |
/// MACROS |
|
10 |
#define FAR2PHYS(n) ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF)) |
|
11 |
|
|
12 |
/// STRUCT |
|
13 |
typedef struct __attribute__((packed)) { |
|
14 |
|
|
15 |
char VbeSignature[4] ; |
|
16 |
uint16_t VbeVersion ; |
|
17 |
uint32_t OemStringPtr ; |
|
18 |
uint8_t Capabilities[4] ; |
|
19 |
uint32_t VideoModePtr ; |
|
20 |
uint16_t TotalMemory ; |
|
21 |
|
|
22 |
uint16_t OemSoftwareRev ; |
|
23 |
uint32_t OemVendorNamePtr ; |
|
24 |
uint32_t OemProductNamePtr ; |
|
25 |
uint32_t OemProductRevPtr ; |
|
26 |
char Reserved[222] ; |
|
27 |
|
|
28 |
char OemData[256] ; |
|
29 |
} VbeInfoBlock; |
|
30 |
|
|
11 | 31 |
static vbe_mode_info_t vbe_mem_info; |
12 |
static mmap_t mem_map; |
|
13 | 32 |
|
14 |
int (get_permission)(unsigned int base_addr, unsigned int size) { |
|
33 |
/// PUBLIC GET |
|
34 |
uint16_t (graph_get_XRes) (void){ return vbe_mem_info.XResolution; } |
|
35 |
uint16_t (graph_get_YRes) (void){ return vbe_mem_info.YResolution; } |
|
36 |
|
|
37 |
/// PRIVATE GET |
|
38 |
static uint16_t (graph_get_bits_pixel) (void){ return vbe_mem_info.BitsPerPixel; } |
|
39 |
static uint16_t (graph_get_bytes_pixel) (void){ return (graph_get_bits_pixel() + 7) >> 3; } |
|
40 |
static phys_bytes (graph_get_phys_addr) (void){ return vbe_mem_info.PhysBasePtr; } |
|
41 |
static unsigned (graph_get_vram_size) (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); } |
|
42 |
//static uint16_t (graph_get_RedMaskSize) (void){ return vbe_mem_info.RedMaskSize ; } |
|
43 |
//static uint16_t (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; } |
|
44 |
//static uint16_t (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; } |
|
45 |
|
|
46 |
static int (get_permission)(unsigned int base_addr, unsigned int size) { |
|
15 | 47 |
struct minix_mem_range mmr; |
16 | 48 |
mmr.mr_base = base_addr; |
17 | 49 |
mmr.mr_limit = base_addr + size; |
18 | 50 |
return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr); |
19 | 51 |
} |
20 | 52 |
|
21 |
//int (get_permissions_first_mbyte)(void) { |
|
53 |
//static int (get_permissions_first_mbyte)(void) {
|
|
22 | 54 |
// return get_permission(MBYTE_BASE, MBYTE_SIZE); |
23 | 55 |
//} |
24 | 56 |
|
25 |
int (graph_init)(uint16_t mode){ |
|
26 |
if (vbe_get_mode_information(mode)) { |
|
27 |
printf("%s: failed to get information for mode %x.\n", __func__, mode); |
|
28 |
return 1; |
|
57 |
/// MEMORY |
|
58 |
static void *video_mem = NULL; /** @brief Frame-buffer VM address. */ |
|
59 |
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); |
|
29 | 76 |
} |
30 | 77 |
|
31 |
graph_map_vram(); // if function fails it aborts program
|
|
78 |
video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
|
|
32 | 79 |
|
33 |
if (graph_set_mode(mode)) { |
|
34 |
printf("%s: failed to set graphic mode %x.\n", __func__, mode); |
|
35 |
return 1; |
|
36 |
}; |
|
80 |
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 |
|
|
37 | 89 |
return SUCCESS; |
38 | 90 |
} |
39 | 91 |
|
40 |
int (graph_cleanup)(void){ |
|
41 |
int r = SUCCESS; |
|
42 |
if ((r = vg_exit())) |
|
43 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
|
44 |
if ((r = graph_free_memory())) |
|
45 |
printf("%s: lm_free failed\n", __func__); |
|
46 |
return r; |
|
47 |
} |
|
48 |
|
|
49 |
int (vbe_get_mode_information)(uint16_t mode) { |
|
92 |
/// INFO GET |
|
93 |
static int (vbe_get_mode_information)(uint16_t mode) { |
|
50 | 94 |
memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values |
51 | 95 |
|
52 | 96 |
struct reg86 reg_86; |
... | ... | |
72 | 116 |
memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size); |
73 | 117 |
return SUCCESS; |
74 | 118 |
} |
75 |
|
|
76 |
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) { |
|
119 |
/* |
|
120 |
static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
|
|
77 | 121 |
memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values |
78 | 122 |
|
79 | 123 |
mmap_t controller_map; |
... | ... | |
147 | 191 |
|
148 | 192 |
return SUCCESS; |
149 | 193 |
} |
194 |
*/ |
|
150 | 195 |
|
151 |
phys_bytes (graph_get_phys_addr) (void){ return vbe_mem_info.PhysBasePtr; } |
|
152 |
unsigned (graph_get_vram_size) (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); } |
|
153 |
uint16_t (graph_get_XRes) (void){ return vbe_mem_info.XResolution; } |
|
154 |
uint16_t (graph_get_YRes) (void){ return vbe_mem_info.YResolution; } |
|
155 |
uint16_t (graph_get_bits_pixel) (void){ return vbe_mem_info.BitsPerPixel; } |
|
156 |
uint16_t (graph_get_bytes_pixel) (void){ return (vbe_mem_info.BitsPerPixel + 7) >> 3; } |
|
157 |
uint16_t (graph_get_RedMaskSize) (void){ return vbe_mem_info.RedMaskSize ; } |
|
158 |
uint16_t (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; } |
|
159 |
uint16_t (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; } |
|
160 |
|
|
161 |
int (graph_map_vram)(void) { |
|
162 |
int r; |
|
163 |
const unsigned vram_base = graph_get_phys_addr(); |
|
164 |
const unsigned vram_size = graph_get_vram_size(); |
|
165 |
if ((r = get_permission(vram_base, vram_size))) { |
|
166 |
if (graph_free_memory()) { |
|
167 |
printf("%s: lm_free failed\n", __func__); |
|
168 |
} |
|
169 |
panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r); |
|
170 |
} |
|
171 |
|
|
172 |
video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size); |
|
173 |
|
|
174 |
if (video_mem == MAP_FAILED) { |
|
175 |
if (graph_free_memory()) { |
|
176 |
printf("%s: lm_free failed\n", __func__); |
|
177 |
} |
|
178 |
panic("%s: couldn't map video memory.", __func__); |
|
179 |
} |
|
180 |
|
|
181 |
video_buf = malloc(vram_size); |
|
182 |
|
|
183 |
return SUCCESS; |
|
184 |
} |
|
185 |
|
|
186 |
int (graph_free_memory)(void) { |
|
187 |
int r = SUCCESS; |
|
188 |
free(video_buf); video_buf = NULL; |
|
189 |
r = !lm_free(&mem_map); |
|
190 |
return r; |
|
191 |
} |
|
192 |
|
|
193 |
int (graph_set_mode)(uint16_t mode) { |
|
196 |
/// INIT |
|
197 |
/** |
|
198 |
* @brief |
|
199 |
* @param mode |
|
200 |
* @return |
|
201 |
*/ |
|
202 |
static int (graph_set_mode)(uint16_t mode) { |
|
194 | 203 |
struct reg86 reg_86; |
195 | 204 |
|
196 | 205 |
memset(®_86, 0, sizeof(struct reg86)); // reset struct |
... | ... | |
209 | 218 |
|
210 | 219 |
return SUCCESS; |
211 | 220 |
} |
221 |
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 |
} |
|
212 | 226 |
|
227 |
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 |
|
213 | 247 |
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) { |
214 | 248 |
if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) { |
215 | 249 |
//printf("%s: invalid pixel.\n", __func__); |
... | ... | |
235 | 269 |
//return set_pixel(x,y,color); |
236 | 270 |
} |
237 | 271 |
|
272 |
int (graph_clear_screen)(void){ |
|
273 |
//return graph_paint_screen(BLACK); |
|
274 |
memset(video_buf, 0, graph_get_vram_size()); |
|
275 |
return SUCCESS; |
|
276 |
} |
|
277 |
|
|
278 |
int (graph_draw)(void){ |
|
279 |
memcpy((uint8_t*)video_mem, video_buf, graph_get_vram_size()); |
|
280 |
return 0; |
|
281 |
} |
|
282 |
|
|
283 |
/// RECTANGLE |
|
238 | 284 |
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){ |
239 | 285 |
int r; |
240 | 286 |
for (uint16_t i = 0; i < len; i++) |
241 | 287 |
if ((r = graph_set_pixel(x + i, y, color))) return r; |
242 | 288 |
return SUCCESS; |
243 | 289 |
} |
244 |
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){ |
|
245 |
return graph_draw_hline(x,y,len,color); |
|
246 |
} |
|
247 | 290 |
|
248 | 291 |
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color) { |
249 | 292 |
int r; |
... | ... | |
251 | 294 |
if ((r = graph_draw_hline(x, y + i, width, color))) return r; |
252 | 295 |
return SUCCESS; |
253 | 296 |
} |
254 |
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color){ |
|
255 |
return graph_draw_rectangle(x,y,width,height, color); |
|
256 |
} |
|
257 |
|
|
258 |
int (graph_paint_screen)(uint32_t color){ |
|
259 |
return graph_draw_rectangle(0,0,graph_get_XRes(),graph_get_YRes(),color); |
|
260 |
} |
|
261 |
|
|
262 |
int (graph_clear_screen)(void){ |
|
263 |
//return graph_paint_screen(BLACK); |
|
264 |
memset(video_buf, 0, graph_get_vram_size()); |
|
265 |
return SUCCESS; |
|
266 |
} |
|
267 |
|
|
268 |
int (graph_draw)(void){ |
|
269 |
memcpy((uint8_t*)video_mem, video_buf, graph_get_vram_size()); |
|
270 |
return 0; |
|
271 |
} |
proj/graph.h | ||
---|---|---|
4 | 4 |
#include <lcom/lcf.h> |
5 | 5 |
#include <stdint.h> |
6 | 6 |
|
7 |
#define GET_ALP(n) (0xFF & ((n) >> 24)) |
|
7 |
/// MACROS |
|
8 |
//#define GET_ALP(n) (0xFF & ((n) >> 24)) |
|
8 | 9 |
#define GET_RED(n) (0xFF & ((n) >> 16)) |
9 | 10 |
#define GET_GRE(n) (0xFF & ((n) >> 8)) |
10 | 11 |
#define GET_BLU(n) (0xFF & (n )) |
11 |
#define SET_ALP(n) (((n)&0xFF) << 24) |
|
12 |
//#define SET_ALP(n) (((n)&0xFF) << 24)
|
|
12 | 13 |
#define SET_RED(n) (((n)&0xFF) << 16) |
13 | 14 |
#define SET_GRE(n) (((n)&0xFF) << 8) |
14 | 15 |
#define SET_BLU(n) (((n)&0xFF) ) |
15 |
#define SET_RGB(r,g,b) ( SET_RED(r) | SET_GRE(g) | SET_BLU(b)) |
|
16 |
#define SET_ARGB(a,r,g,b) (SET_ALP(a) | SET_RED(r) | SET_GRE(g) | SET_BLU(b)) |
|
17 |
#define FAR2PHYS(n) ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF)) |
|
16 |
#define SET_RGB(r,g,b) (SET_RED(r) | SET_GRE(g) | SET_BLU(b)) |
|
18 | 17 |
|
19 |
typedef struct __attribute__((packed)) { |
|
18 |
/// PUBLIC GET |
|
19 |
uint16_t (graph_get_XRes) (void); |
|
20 |
uint16_t (graph_get_YRes) (void); |
|
20 | 21 |
|
21 |
char VbeSignature[4] ; |
|
22 |
uint16_t VbeVersion ; |
|
23 |
uint32_t OemStringPtr ; |
|
24 |
uint8_t Capabilities[4] ; |
|
25 |
uint32_t VideoModePtr ; |
|
26 |
uint16_t TotalMemory ; |
|
22 |
/// INIT |
|
23 |
int (graph_init)(uint16_t mode); |
|
27 | 24 |
|
28 |
uint16_t OemSoftwareRev ; |
|
29 |
uint32_t OemVendorNamePtr ; |
|
30 |
uint32_t OemProductNamePtr ; |
|
31 |
uint32_t OemProductRevPtr ; |
|
32 |
char Reserved[222] ; |
|
33 |
|
|
34 |
char OemData[256] ; |
|
35 |
} VbeInfoBlock; |
|
36 |
|
|
37 |
int (get_permission)(unsigned int base_addr, unsigned int size); |
|
38 |
|
|
39 |
//int (get_permissions_first_mbyte)(void); |
|
40 |
|
|
41 |
int (graph_init)(uint16_t mode); |
|
25 |
/// CLEANUP |
|
42 | 26 |
int (graph_cleanup)(void); |
43 | 27 |
|
44 |
int (vbe_get_mode_information)(uint16_t mode); |
|
28 |
/// PIXEL DRAWING |
|
29 |
int (graph_set_pixel) (uint16_t x, uint16_t y, uint32_t color); |
|
30 |
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha); |
|
45 | 31 |
|
46 |
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p); |
|
32 |
/// SCREEN |
|
33 |
int (graph_clear_screen)(void); |
|
47 | 34 |
|
48 |
phys_bytes (graph_get_phys_addr) (void); |
|
49 |
unsigned (graph_get_vram_size) (void); |
|
50 |
uint16_t (graph_get_XRes) (void); |
|
51 |
uint16_t (graph_get_YRes) (void); |
|
52 |
uint16_t (graph_get_bits_pixel) (void); |
|
53 |
uint16_t (graph_get_bytes_pixel) (void); |
|
54 |
uint16_t (graph_get_RedMaskSize) (void); |
|
55 |
uint16_t (graph_get_GreenMaskSize)(void); |
|
56 |
uint16_t (graph_get_BlueMaskSize) (void); |
|
35 |
/// DRAW |
|
36 |
int (graph_draw)(void); |
|
57 | 37 |
|
58 |
int (graph_map_vram)(void); |
|
59 | 38 |
|
60 |
int (graph_free_memory)(void); |
|
61 | 39 |
|
62 |
int (graph_set_pixel) (uint16_t x, uint16_t y, uint32_t color); |
|
63 |
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha); |
|
40 |
/// RECTANGLE |
|
64 | 41 |
|
65 |
/** |
|
66 |
* @brief |
|
67 |
* @param mode |
|
68 |
* @return |
|
69 |
*/ |
|
70 |
int (graph_set_mode)(uint16_t mode); |
|
71 | 42 |
|
72 | 43 |
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color); |
73 |
|
|
74 | 44 |
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color); |
75 | 45 |
|
76 |
int (graph_paint_screen)(uint32_t color); |
|
77 |
|
|
78 |
int (graph_clear_screen)(void); |
|
79 |
|
|
80 |
int (graph_draw)(void); |
|
81 |
|
|
82 | 46 |
#endif /* end of include guard: GRAPH_H_INCLUDED */ |
proj/proj.c | ||
---|---|---|
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
#ifdef DIOGO |
66 |
printf("%d\n", 1000000-(int)(1000000*fm_sin(0.5*M_PI))); |
|
67 |
printf("%d\n", (int)(1000000*fm_cos(0.5*M_PI))); |
|
68 |
|
|
66 |
//printf("%d\n", 1000000-(int)(1000000*fm_sin(0.5*M_PI)));
|
|
67 |
//printf("%d\n", (int)(1000000*fm_cos(0.5*M_PI)));
|
|
68 |
/* |
|
69 | 69 |
clock_t t = clock(); |
70 | 70 |
sprite_t *shooter1 = get_shooter(); sprite_set_pos(shooter1, 100, 100); |
71 | 71 |
for(double angle = 0; angle <= 6.283185; angle += 0.006283185){ |
72 | 72 |
sprite_set_angle(shooter1, angle); |
73 |
//graph_paint_screen(0x777777); |
|
74 | 73 |
graph_clear_screen(); |
75 | 74 |
sprite_draw(shooter1); |
76 | 75 |
graph_draw(); |
77 | 76 |
} |
78 |
t = clock() - t; //printf("%d\n", CLOCKS_PER_SEC); |
|
79 |
//double dt = ((double)t)/(double)CLOCKS_PER_SEC; |
|
77 |
t = clock() - t; |
|
80 | 78 |
printf("Time taken: %d/%d \n", t, CLOCKS_PER_SEC); |
81 | 79 |
sprite_dtor(shooter1); |
80 |
*/ |
|
82 | 81 |
#endif |
83 | 82 |
|
84 | 83 |
#ifdef TELMO |
85 |
sprite_t *crosshair = get_crosshair(); |
|
86 |
graph_clear_screen(); |
|
87 |
sprite_draw(crosshair); |
|
88 |
graph_draw(); |
|
84 |
sprite_t *crosshair = get_crosshair();
|
|
85 |
graph_clear_screen();
|
|
86 |
sprite_draw(crosshair);
|
|
87 |
graph_draw();
|
|
89 | 88 |
#endif |
90 | 89 |
|
91 | 90 |
/// loop stuff |
... | ... | |
132 | 131 |
} else { /* received standart message, not a notification */ |
133 | 132 |
/* no standart message expected: do nothing */ |
134 | 133 |
} |
135 |
|
|
136 |
switch (get_hor_movement()) { |
|
137 |
case LEFT: |
|
138 |
printf("GOING LEFT.\n"); |
|
139 |
break; |
|
140 |
case RIGHT: |
|
141 |
printf("GOING RIGHT.\n"); |
|
142 |
break; |
|
143 |
} |
|
144 |
switch (get_ver_movement()) { |
|
145 |
case UP: |
|
146 |
printf("GOING UP.\n"); |
|
147 |
break; |
|
148 |
case DOWN: |
|
149 |
printf("GOING DOWN.\n"); |
|
150 |
break; |
|
151 |
} |
|
134 |
#ifdef TELMO |
|
135 |
switch (get_hor_movement()) { |
|
136 |
case LEFT: |
|
137 |
printf("GOING LEFT.\n"); |
|
138 |
break; |
|
139 |
case RIGHT: |
|
140 |
printf("GOING RIGHT.\n"); |
|
141 |
break; |
|
142 |
} |
|
143 |
switch (get_ver_movement()) { |
|
144 |
case UP: |
|
145 |
printf("GOING UP.\n"); |
|
146 |
break; |
|
147 |
case DOWN: |
|
148 |
printf("GOING DOWN.\n"); |
|
149 |
break; |
|
150 |
} |
|
151 |
#endif |
|
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
// Unsubscribe interrupts |
Also available in: Unified diff