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