root / proj / src / proj.c @ 240
History | View | Annotate | Download (11.3 KB)
1 | 144 | up20180655 | #include <lcom/lcf.h> |
---|---|---|---|
2 | #include <lcom/proj.h> |
||
3 | #include <lcom/liblm.h> |
||
4 | 185 | up20180655 | #include <math.h> |
5 | 144 | up20180655 | |
6 | 149 | up20180655 | #include "proj_macros.h" |
7 | 179 | up20180642 | #include "proj_func.h" |
8 | 147 | up20180655 | |
9 | 149 | up20180655 | #include "kbc.h" |
10 | #include "timer.h" |
||
11 | #include "keyboard.h" |
||
12 | 150 | up20180655 | #include "mouse.h" |
13 | 189 | up20180655 | #include "graph.h" |
14 | 234 | up20180655 | #include "rtc.h" |
15 | 153 | up20180655 | #include "interrupts_func.h" |
16 | 149 | up20180655 | |
17 | 179 | up20180642 | #include "graph.h" |
18 | #include "sprite.h" |
||
19 | #include "rectangle.h" |
||
20 | 182 | up20180642 | #include "font.h" |
21 | 192 | up20180642 | #include "ent.h" |
22 | 168 | up20180642 | |
23 | 192 | up20180642 | #include "crosshair.h" |
24 | #include "shooter.h" |
||
25 | #include "pistol.h" |
||
26 | #include "nothing.h" |
||
27 | 237 | up20180642 | #include "bullet.h" |
28 | 216 | up20180642 | #include "map1.h" |
29 | 183 | up20180642 | |
30 | 226 | up20180642 | #include "list.h" |
31 | |||
32 | 235 | up20180642 | #ifdef DIOGO
|
33 | #include "uart_macros.h" |
||
34 | #include "test7.h" |
||
35 | #endif
|
||
36 | |||
37 | 144 | up20180655 | int main(int argc, char* argv[]) { |
38 | |||
39 | lcf_set_language("EN-US");
|
||
40 | |||
41 | 235 | up20180642 | lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
|
42 | 144 | up20180655 | |
43 | 189 | up20180655 | lcf_log_output("/home/lcom/labs/proj/output.txt");
|
44 | 144 | up20180655 | |
45 | if (lcf_start(argc, argv)) return 1; |
||
46 | |||
47 | lcf_cleanup(); |
||
48 | |||
49 | return 0; |
||
50 | } |
||
51 | 147 | up20180655 | |
52 | 149 | up20180655 | int(proj_main_loop)(int argc, char *argv[]) { |
53 | 170 | up20180642 | |
54 | int r;
|
||
55 | |||
56 | 235 | up20180642 | #ifdef DIOGO
|
57 | uint8_t conf; |
||
58 | 240 | up20180655 | if((r = util_sys_inb(0x3F8+3, &conf))) return 1; printf("0x%02X\n", conf); |
59 | 235 | up20180642 | conf = 0x19; //00011001 |
60 | 240 | up20180655 | printf("0x%02X\n", conf);
|
61 | 235 | up20180642 | if((r = sys_outb(0x3F8+3, conf))) return 1; |
62 | 240 | up20180655 | uint8_t s; |
63 | if((r = util_sys_inb(0x3F8+3, &s))) return 1; printf("S: 0x%02X\n", s); |
||
64 | return 0; |
||
65 | |||
66 | 235 | up20180642 | #endif
|
67 | |||
68 | 221 | up20180642 | font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
|
69 | 192 | up20180642 | if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; } |
70 | |||
71 | 170 | up20180642 | /// subscribe interrupts
|
72 | if (subscribe_all()) { return 1; } |
||
73 | |||
74 | /// initialize graphics
|
||
75 | 166 | up20180642 | if(graph_init(GRAPH_MODE)){
|
76 | printf("%s: failed to initalize graphics.\n", __func__);
|
||
77 | if (cleanup()) printf("%s: failed to cleanup.\n", __func__); |
||
78 | 152 | up20180642 | return 1; |
79 | } |
||
80 | |||
81 | 188 | up20180642 | /// Load stuff
|
82 | 216 | up20180642 | basic_sprite_t *bsp_crosshair = NULL;
|
83 | basic_sprite_t *bsp_shooter = NULL;
|
||
84 | basic_sprite_t *bsp_pistol = NULL;
|
||
85 | basic_sprite_t *bsp_nothing = NULL;
|
||
86 | 237 | up20180642 | basic_sprite_t *bsp_bullet = NULL;
|
87 | map_t *map1 = NULL;
|
||
88 | 216 | up20180642 | sprite_t *sp_crosshair = NULL;
|
89 | 188 | up20180642 | { |
90 | graph_clear_screen(); |
||
91 | text_t *txt = text_ctor(consolas, "Loading...");
|
||
92 | text_draw(txt); |
||
93 | text_dtor(txt); |
||
94 | graph_draw(); |
||
95 | 192 | up20180642 | |
96 | bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n"); |
||
97 | bsp_shooter = get_shooter (); if(bsp_shooter == NULL) printf("Failed to get shooter\n"); |
||
98 | bsp_pistol = get_pistol (); if(bsp_pistol == NULL) printf("Failed to get pistol\n"); |
||
99 | bsp_nothing = get_nothing (); if(bsp_nothing == NULL) printf("Failed to get nothing\n"); |
||
100 | 237 | up20180642 | bsp_bullet = get_bullet (); if(bsp_bullet == NULL) printf("Failed to get bullet\n"); |
101 | 216 | up20180642 | map1 = get_map1 (); if(map1 == NULL) printf("Failed to get map1\n"); |
102 | 192 | up20180642 | |
103 | sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n"); |
||
104 | 188 | up20180642 | } |
105 | 192 | up20180642 | |
106 | 159 | up20180642 | #ifdef DIOGO
|
107 | 234 | up20180655 | /*
|
108 | 188 | up20180642 | graph_clear_screen();
|
109 | 183 | up20180642 | |
110 | 188 | up20180642 | rectangle_t *rect = rectangle_ctor(0,0,400,100);
|
111 | rectangle_set_pos(rect,
|
||
112 | graph_get_XRes()/2 - rectangle_get_w(rect)/2,
|
||
113 | graph_get_YRes()*0.25 - rectangle_get_h(rect)/2);
|
||
114 | rectangle_set_fill_color(rect, BLACK);
|
||
115 | 179 | up20180642 | rectangle_set_outline_width(rect, 2);
|
116 | 188 | up20180642 | rectangle_set_outline_color(rect, WHITE);
|
117 | 179 | up20180642 | rectangle_draw(rect);
|
118 | |||
119 | 188 | up20180642 | text_t *txt = text_ctor(consolas, "Hello world!");
|
120 | text_set_color(txt, 0x888888);
|
||
121 | 182 | up20180642 | |
122 | 188 | up20180642 | text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
|
123 | rectangle_get_y(rect)+rectangle_get_h(rect)/2);
|
||
124 | text_set_valign(txt, text_valign_center);
|
||
125 | text_set_halign(txt, text_halign_center);
|
||
126 | 183 | up20180642 | text_draw(txt);
|
127 | text_dtor(txt);
|
||
128 | |||
129 | graph_draw();
|
||
130 | 188 | up20180642 | rectangle_dtor(rect);
|
131 | 183 | up20180642 | |
132 | 226 | up20180642 | list_t *l = list_ctor();
|
133 | int *p = NULL;
|
||
134 | for(int i = 10; i < 20; ++i){
|
||
135 | p = malloc(sizeof(int));
|
||
136 | *p = i;
|
||
137 | printf("INSERTING %d\n", i);
|
||
138 | list_insert(l, list_end(l), p);
|
||
139 | printf("INSERTED, SIZE=%d\n", list_size(l));
|
||
140 | }
|
||
141 | list_node_t *it = list_begin(l);
|
||
142 | while(it != list_end(l)){
|
||
143 | printf("%d\n", **(int**)list_node_val(it));
|
||
144 | it = list_node_next(it);
|
||
145 | }
|
||
146 | while(list_size(l) > 0){
|
||
147 | printf("ERASING\n");
|
||
148 | void *p = list_erase(l, list_begin(l));
|
||
149 | printf("ERASED %d, SIZE=%d\n", *(int*)p, list_size(l));
|
||
150 | free(p);
|
||
151 | }
|
||
152 | printf("DONE\n");
|
||
153 | if(list_dtor(l)) printf("COULD NOT DESTRUCT LIST\n");
|
||
154 | |||
155 | |||
156 | 234 | up20180655 | tickdelay(micros_to_ticks(1000000));*/
|
157 | 183 | up20180642 | |
158 | 234 | up20180655 | // RTC
|
159 | 235 | up20180642 | /*
|
160 | 234 | up20180655 | uint8_t date[4], time[3];
|
161 | |||
162 | if (rtc_read_time(time)) return 1;
|
||
163 | |||
164 | if (rtc_read_date(date)) return 1;
|
||
165 | |||
166 | printf("Hour: %02d:%02d:%02d\n", time[2], time[1], time[0]);
|
||
167 | |||
168 | printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
|
||
169 | 235 | up20180642 | */
|
170 | //UART
|
||
171 | /*
|
||
172 | printf("got %d\n", ser_test_conf(COM1_ADDR));
|
||
173 | printf("\n");
|
||
174 | printf("got %d\n", ser_test_set(COM1_ADDR, 6, 1, 0, 9800));
|
||
175 | tickdelay(micros_to_ticks(1000000));
|
||
176 | printf("got %d\n", ser_test_conf(COM1_ADDR));
|
||
177 | */
|
||
178 | |||
179 | |||
180 | 159 | up20180642 | #endif
|
181 | 152 | up20180642 | |
182 | 171 | up20180655 | #ifdef TELMO
|
183 | 216 | up20180642 | ent_set_scale(DEFAULT_SCALE); |
184 | 192 | up20180642 | |
185 | 236 | up20180642 | list_t *shooter_list = list_ctor(); |
186 | |||
187 | 201 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
188 | 232 | up20180655 | gunner_set_spawn(shooter1, 75, 75); |
189 | 222 | up20180655 | gunner_set_pos(shooter1, 75, 75); |
190 | 192 | up20180642 | |
191 | 201 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing); |
192 | 232 | up20180655 | gunner_set_spawn(shooter2, 975, 75); |
193 | 231 | up20180655 | gunner_set_pos(shooter2, 775, 75); |
194 | 192 | up20180642 | |
195 | 236 | up20180642 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
196 | list_insert(shooter_list, list_end(shooter_list), shooter2); |
||
197 | |||
198 | 231 | up20180655 | //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
|
199 | 202 | up20180655 | |
200 | 236 | up20180642 | list_t *bullet_list = list_ctor(); |
201 | 231 | up20180655 | |
202 | 216 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
203 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
204 | 214 | up20180642 | |
205 | 200 | up20180655 | uint32_t refresh_count_value = sys_hz() / REFRESH_RATE; |
206 | 231 | up20180655 | |
207 | uint8_t last_lb = 0;
|
||
208 | 171 | up20180655 | #endif
|
209 | |||
210 | 149 | up20180655 | /// loop stuff
|
211 | 170 | up20180642 | int ipc_status;
|
212 | 149 | up20180655 | message msg; |
213 | 147 | up20180655 | int good = 1; |
214 | 168 | up20180642 | |
215 | 183 | up20180642 | #ifdef DIOGO
|
216 | good = 0;
|
||
217 | #endif
|
||
218 | |||
219 | 147 | up20180655 | while (good) {
|
220 | /* Get a request message. */
|
||
221 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
222 | printf("driver_receive failed with %d", r);
|
||
223 | continue;
|
||
224 | } |
||
225 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
226 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
227 | case HARDWARE: /* hardware interrupt notification */ |
||
228 | 153 | up20180655 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
229 | if (msg.m_notify.interrupts & n) {
|
||
230 | interrupt_handler(i); |
||
231 | 167 | up20180655 | if ((scancode[0]) == ESC_BREAK_CODE) good = 0; |
232 | 171 | up20180655 | #ifdef TELMO
|
233 | 187 | up20180655 | if (i == 0) { |
234 | if (no_interrupts % refresh_count_value == 0) { |
||
235 | 240 | up20180655 | update_movement(map1, shooter1, shooter2); |
236 | 228 | up20180655 | //bullet_update_movement(bullet);
|
237 | 232 | up20180655 | |
238 | if (no_interrupts % 180 == 0) gunner_set_pos(shooter2, 775, 75); |
||
239 | |||
240 | 236 | up20180642 | update_game_state(map1, shooter_list, bullet_list); |
241 | 216 | up20180642 | |
242 | 220 | up20180655 | if(map_collides_gunner(map1, shooter1)){
|
243 | 216 | up20180642 | printf("COLLIDING\n");
|
244 | } |
||
245 | |||
246 | 200 | up20180655 | update_scale(); |
247 | 192 | up20180642 | |
248 | 216 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
249 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
250 | 214 | up20180642 | |
251 | 192 | up20180642 | sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y()); |
252 | 187 | up20180655 | double angle = get_mouse_angle(shooter1);
|
253 | 201 | up20180642 | gunner_set_angle(shooter1, angle - M_PI_2); |
254 | 187 | up20180655 | graph_clear_screen(); |
255 | 207 | up20180642 | |
256 | clock_t t = clock(); |
||
257 | |||
258 | 216 | up20180642 | map_draw (map1); |
259 | 201 | up20180642 | gunner_draw(shooter2); |
260 | gunner_draw(shooter1); |
||
261 | 231 | up20180655 | bullet_draw_list(bullet_list); |
262 | 207 | up20180642 | |
263 | 222 | up20180655 | t = clock()-t; //printf("%d\n", (t*1000)/CLOCKS_PER_SEC);
|
264 | 207 | up20180642 | |
265 | 192 | up20180642 | sprite_draw(sp_crosshair); |
266 | 187 | up20180655 | graph_draw(); |
267 | } |
||
268 | 184 | up20180655 | } |
269 | 220 | up20180655 | if (i == 12) { |
270 | if (counter_mouse_ih >= 3) { |
||
271 | struct packet pp = mouse_parse_packet(packet_mouse_ih);
|
||
272 | 231 | up20180655 | update_mouse(&pp); |
273 | 220 | up20180655 | //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
|
274 | counter_mouse_ih = 0;
|
||
275 | 231 | up20180655 | |
276 | if (last_lb ^ get_key_presses()->lb_pressed && get_key_presses()->lb_pressed) {
|
||
277 | 237 | up20180642 | shoot_bullet(shooter1, bullet_list, bsp_bullet); |
278 | 231 | up20180655 | } |
279 | |||
280 | last_lb = get_key_presses()->lb_pressed; |
||
281 | 220 | up20180655 | } |
282 | } |
||
283 | 171 | up20180655 | #endif
|
284 | 153 | up20180655 | } |
285 | 147 | up20180655 | } |
286 | 167 | up20180655 | |
287 | 147 | up20180655 | break;
|
288 | default:
|
||
289 | break; /* no other notifications expected: do nothing */ |
||
290 | } |
||
291 | } else { /* received standart message, not a notification */ |
||
292 | /* no standart message expected: do nothing */
|
||
293 | } |
||
294 | } |
||
295 | 149 | up20180655 | |
296 | 192 | up20180642 | #ifdef TELMO
|
297 | 236 | up20180642 | while(list_size(shooter_list) > 0){ |
298 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
299 | gunner_dtor(p); |
||
300 | } |
||
301 | |||
302 | 231 | up20180655 | while(list_size(bullet_list) > 0){ |
303 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
304 | 236 | up20180642 | bullet_dtor(p); |
305 | 231 | up20180655 | } |
306 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
307 | 192 | up20180642 | #endif
|
308 | 188 | up20180642 | |
309 | 216 | up20180642 | basic_sprite_dtor (bsp_crosshair); bsp_crosshair = NULL;
|
310 | basic_sprite_dtor (bsp_shooter ); bsp_shooter = NULL;
|
||
311 | sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
||
312 | basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL;
|
||
313 | basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL;
|
||
314 | map_dtor (map1 ); map1 = NULL;
|
||
315 | font_dtor (consolas ); consolas = NULL;
|
||
316 | 192 | up20180642 | |
317 | 153 | up20180655 | // Unsubscribe interrupts
|
318 | 155 | up20180655 | if (unsubscribe_all()) {
|
319 | if (cleanup())
|
||
320 | printf("%s: failed to cleanup.\n", __func__);
|
||
321 | 152 | up20180642 | return 1; |
322 | } |
||
323 | |||
324 | 155 | up20180655 | |
325 | if (cleanup()) {
|
||
326 | printf("%s: failed to cleanup.\n", __func__);
|
||
327 | 152 | up20180642 | return 1; |
328 | } |
||
329 | |||
330 | 149 | up20180655 | return 0; |
331 | 147 | up20180655 | } |