root / proj / src / proj.c @ 270
History | View | Annotate | Download (12.6 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 | 270 | up20180655 | #include "makecode_map.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 | 269 | up20180642 | #include "uart.h" |
36 | #include "hltp.h" |
||
37 | 235 | up20180642 | #endif
|
38 | |||
39 | 144 | up20180655 | int main(int argc, char* argv[]) { |
40 | |||
41 | lcf_set_language("EN-US");
|
||
42 | |||
43 | 235 | up20180642 | lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
|
44 | 144 | up20180655 | |
45 | 189 | up20180655 | lcf_log_output("/home/lcom/labs/proj/output.txt");
|
46 | 144 | up20180655 | |
47 | if (lcf_start(argc, argv)) return 1; |
||
48 | |||
49 | lcf_cleanup(); |
||
50 | |||
51 | return 0; |
||
52 | } |
||
53 | 147 | up20180655 | |
54 | 149 | up20180655 | int(proj_main_loop)(int argc, char *argv[]) { |
55 | 170 | up20180642 | |
56 | int r;
|
||
57 | |||
58 | 261 | up20180642 | font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
|
59 | if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; } |
||
60 | 192 | up20180642 | |
61 | 261 | up20180642 | /// subscribe interrupts
|
62 | if (subscribe_all()) { return 1; } |
||
63 | 170 | up20180642 | |
64 | 269 | up20180642 | #ifndef DIOGO
|
65 | /// initialize graphics
|
||
66 | if(graph_init(GRAPH_MODE)){
|
||
67 | printf("%s: failed to initalize graphics.\n", __func__);
|
||
68 | if (cleanup()) printf("%s: failed to cleanup.\n", __func__); |
||
69 | return 1; |
||
70 | } |
||
71 | #endif
|
||
72 | 152 | up20180642 | |
73 | 261 | up20180642 | /// Load stuff
|
74 | basic_sprite_t *bsp_crosshair = NULL;
|
||
75 | basic_sprite_t *bsp_shooter = NULL;
|
||
76 | basic_sprite_t *bsp_pistol = NULL;
|
||
77 | basic_sprite_t *bsp_nothing = NULL;
|
||
78 | basic_sprite_t *bsp_bullet = NULL;
|
||
79 | map_t *map1 = NULL;
|
||
80 | sprite_t *sp_crosshair = NULL;
|
||
81 | { |
||
82 | 269 | up20180642 | #ifndef DIOGO
|
83 | graph_clear_screen(); |
||
84 | text_t *txt = text_ctor(consolas, "Loading...");
|
||
85 | text_draw(txt); |
||
86 | text_dtor(txt); |
||
87 | graph_draw(); |
||
88 | #endif
|
||
89 | 183 | up20180642 | |
90 | 261 | up20180642 | bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n"); |
91 | bsp_shooter = get_shooter (); if(bsp_shooter == NULL) printf("Failed to get shooter\n"); |
||
92 | bsp_pistol = get_pistol (); if(bsp_pistol == NULL) printf("Failed to get pistol\n"); |
||
93 | bsp_nothing = get_nothing (); if(bsp_nothing == NULL) printf("Failed to get nothing\n"); |
||
94 | bsp_bullet = get_bullet (); if(bsp_bullet == NULL) printf("Failed to get bullet\n"); |
||
95 | map1 = get_map1 (); if(map1 == NULL) printf("Failed to get map1\n"); |
||
96 | 226 | up20180642 | |
97 | 261 | up20180642 | sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n"); |
98 | } |
||
99 | 226 | up20180642 | |
100 | 261 | up20180642 | ent_set_scale(DEFAULT_SCALE); |
101 | 183 | up20180642 | |
102 | 261 | up20180642 | text_timer_t *in_game_timer = timer_ctor(consolas); |
103 | 234 | up20180655 | |
104 | 262 | up20180642 | #ifndef DIOGO
|
105 | menu_t *main_menu = menu_ctor(consolas); |
||
106 | #endif
|
||
107 | 234 | up20180655 | |
108 | 261 | up20180642 | list_t *shooter_list = list_ctor(); |
109 | 234 | up20180655 | |
110 | 261 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
111 | gunner_set_spawn(shooter1, 75, 75); |
||
112 | gunner_set_pos(shooter1, 75, 75); |
||
113 | 234 | up20180655 | |
114 | 261 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing); |
115 | gunner_set_spawn(shooter2, 975, 75); |
||
116 | gunner_set_pos(shooter2, 775, 75); |
||
117 | 152 | up20180642 | |
118 | 261 | up20180642 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
119 | list_insert(shooter_list, list_end(shooter_list), shooter2); |
||
120 | 250 | up20180655 | |
121 | 261 | up20180642 | list_t *bullet_list = list_ctor(); |
122 | 251 | up20180655 | |
123 | 261 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
124 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
125 | 192 | up20180642 | |
126 | 262 | up20180642 | #ifndef DIOGO
|
127 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
||
128 | double angle; // mouse angle |
||
129 | int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y(); |
||
130 | uint8_t last_lb = 0;
|
||
131 | struct packet pp;
|
||
132 | keys_t *keys = get_key_presses(); |
||
133 | #endif
|
||
134 | 261 | up20180642 | /// loop stuff
|
135 | int ipc_status;
|
||
136 | message msg; |
||
137 | 262 | up20180642 | int game_state = MENU;
|
138 | 267 | up20180655 | |
139 | char buffer[1024]; // buffer |
||
140 | int buffer_pos = 0; |
||
141 | |||
142 | 262 | up20180642 | #ifndef DIOGO
|
143 | int click = 0; |
||
144 | #endif
|
||
145 | 269 | up20180642 | #ifdef DIOGO
|
146 | char *s = NULL; |
||
147 | #endif
|
||
148 | 236 | up20180642 | |
149 | 261 | up20180642 | while (game_state != EXIT) {
|
150 | /* Get a request message. */
|
||
151 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
152 | printf("driver_receive failed with %d", r);
|
||
153 | continue;
|
||
154 | } |
||
155 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
156 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
157 | case HARDWARE: /* hardware interrupt notification */ |
||
158 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
||
159 | if (msg.m_notify.interrupts & n) {
|
||
160 | interrupt_handler(i); |
||
161 | switch (i) {
|
||
162 | #ifndef DIOGO
|
||
163 | case TIMER0_IRQ:
|
||
164 | switch (game_state) {
|
||
165 | case MENU:
|
||
166 | if (no_interrupts % 2 == 0){ |
||
167 | graph_clear_screen(); |
||
168 | game_state = menu_update_state(main_menu, click); |
||
169 | menu_draw(main_menu); |
||
170 | 192 | up20180642 | |
171 | 261 | up20180642 | click = 0;
|
172 | 192 | up20180642 | |
173 | 261 | up20180642 | sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y); |
174 | sprite_draw(sp_crosshair); |
||
175 | graph_draw(); |
||
176 | } |
||
177 | break;
|
||
178 | case GAME:
|
||
179 | if (no_interrupts % 60 == 0) timer_update(in_game_timer); |
||
180 | update_movement(map1, shooter1, keys, shooter_list); |
||
181 | 202 | up20180655 | |
182 | 261 | up20180642 | update_game_state(map1, shooter_list, bullet_list); |
183 | 231 | up20180655 | |
184 | 261 | up20180642 | //update_scale();
|
185 | angle = get_mouse_angle(shooter1); |
||
186 | gunner_set_angle(shooter1, angle - M_PI_2); |
||
187 | 231 | up20180655 | |
188 | 261 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
189 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
190 | 171 | up20180655 | |
191 | 261 | up20180642 | graph_clear_screen(); |
192 | map_draw (map1); |
||
193 | gunner_draw_list(shooter_list); |
||
194 | bullet_draw_list(bullet_list); |
||
195 | 168 | up20180642 | |
196 | 261 | up20180642 | text_draw(in_game_timer->text); |
197 | 183 | up20180642 | |
198 | 261 | up20180642 | sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y); |
199 | sprite_draw(sp_crosshair); |
||
200 | graph_draw(); |
||
201 | break;
|
||
202 | } |
||
203 | break;
|
||
204 | 263 | up20180642 | #endif
|
205 | 261 | up20180642 | case KBC_IRQ:
|
206 | switch (game_state) {
|
||
207 | case MENU:
|
||
208 | if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT; |
||
209 | 267 | up20180655 | |
210 | else if ((scancode[0]) == ENTER_MAKE_CODE) { |
||
211 | 269 | up20180642 | buffer[buffer_pos] = '\0';
|
212 | printf("\nSending string -%s-\n", buffer);
|
||
213 | printf("Send string output: %d\n",
|
||
214 | hltp_send_string(COM1_ADDR, buffer)); |
||
215 | 267 | up20180655 | buffer_pos = 0;
|
216 | } |
||
217 | 270 | up20180655 | else {
|
218 | char c = map_makecode(scancode[0]); |
||
219 | if (c == ERROR_CODE) break; |
||
220 | buffer[buffer_pos++] = c; |
||
221 | printf("%c", c);
|
||
222 | } |
||
223 | 267 | up20180655 | |
224 | 261 | up20180642 | break;
|
225 | case GAME:
|
||
226 | if ((scancode[0]) == ESC_BREAK_CODE) { |
||
227 | game_state = MENU; |
||
228 | // reset game
|
||
229 | while(list_size(bullet_list) > 0){ |
||
230 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
231 | bullet_dtor(p); |
||
232 | 251 | up20180655 | } |
233 | 261 | up20180642 | list_node_t *it = list_begin(shooter_list); |
234 | while (it != list_end(shooter_list)) {
|
||
235 | gunner_t *p = *(gunner_t**)list_node_val(it); |
||
236 | get_random_spawn(map1, p); |
||
237 | gunner_set_curr_health(p, gunner_get_health(p)); |
||
238 | it = list_node_next(it); |
||
239 | } |
||
240 | timer_reset(in_game_timer); |
||
241 | 243 | up20180642 | } |
242 | 251 | up20180655 | break;
|
243 | 261 | up20180642 | } |
244 | break;
|
||
245 | 263 | up20180642 | #ifndef DIOGO
|
246 | 261 | up20180642 | case MOUSE_IRQ:
|
247 | if (counter_mouse_ih >= 3) { |
||
248 | mouse_parse_packet(packet_mouse_ih, &pp); |
||
249 | update_mouse(&pp); |
||
250 | 251 | up20180655 | switch (game_state) {
|
251 | case MENU:
|
||
252 | 261 | up20180642 | if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
253 | 251 | up20180655 | break;
|
254 | case GAME:
|
||
255 | 261 | up20180642 | if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
256 | shoot_bullet(shooter1, bullet_list, bsp_bullet); |
||
257 | 251 | up20180655 | break;
|
258 | 231 | up20180655 | } |
259 | 261 | up20180642 | last_lb = keys->lb_pressed; |
260 | counter_mouse_ih = 0;
|
||
261 | 251 | up20180655 | |
262 | 220 | up20180655 | } |
263 | 261 | up20180642 | break;
|
264 | #endif
|
||
265 | #ifdef DIOGO
|
||
266 | case COM1_IRQ:
|
||
267 | 269 | up20180642 | { |
268 | printf("You've got mail");
|
||
269 | int r = hltp_get_string(COM1_ADDR, &s);
|
||
270 | printf(" (return code %d)", r);
|
||
271 | printf(": -%s-\n", s);
|
||
272 | }break;
|
||
273 | 261 | up20180642 | #endif
|
274 | 220 | up20180655 | } |
275 | 251 | up20180655 | } |
276 | 261 | up20180642 | } |
277 | 167 | up20180655 | |
278 | 261 | up20180642 | break;
|
279 | default:
|
||
280 | break; /* no other notifications expected: do nothing */ |
||
281 | 147 | up20180655 | } |
282 | 261 | up20180642 | } else { /* received standart message, not a notification */ |
283 | /* no standart message expected: do nothing */
|
||
284 | 147 | up20180655 | } |
285 | 261 | up20180642 | } |
286 | 149 | up20180655 | |
287 | 269 | up20180642 | #ifdef DIOGO
|
288 | free(s); |
||
289 | #endif
|
||
290 | |||
291 | 261 | up20180642 | while(list_size(shooter_list) > 0){ |
292 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
293 | gunner_dtor(p); |
||
294 | } |
||
295 | 236 | up20180642 | |
296 | 261 | up20180642 | while(list_size(bullet_list) > 0){ |
297 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
298 | bullet_dtor(p); |
||
299 | } |
||
300 | if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
||
301 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
302 | 243 | up20180642 | |
303 | 261 | up20180642 | timer_dtor(in_game_timer); in_game_timer = NULL;
|
304 | 250 | up20180655 | |
305 | 261 | up20180642 | basic_sprite_dtor (bsp_crosshair); bsp_crosshair = NULL;
|
306 | basic_sprite_dtor (bsp_shooter ); bsp_shooter = NULL;
|
||
307 | sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
||
308 | basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL;
|
||
309 | basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL;
|
||
310 | map_dtor (map1 ); map1 = NULL;
|
||
311 | font_dtor (consolas ); consolas = NULL;
|
||
312 | 243 | up20180642 | |
313 | 261 | up20180642 | // Unsubscribe interrupts
|
314 | if (unsubscribe_all()) {
|
||
315 | if (cleanup())
|
||
316 | printf("%s: failed to cleanup.\n", __func__);
|
||
317 | return 1; |
||
318 | } |
||
319 | 188 | up20180642 | |
320 | 192 | up20180642 | |
321 | 261 | up20180642 | if (cleanup()) {
|
322 | printf("%s: failed to cleanup.\n", __func__);
|
||
323 | return 1; |
||
324 | } |
||
325 | 155 | up20180655 | |
326 | 149 | up20180655 | return 0; |
327 | 147 | up20180655 | } |