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