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