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