root / proj / src / proj.c @ 256
History | View | Annotate | Download (14.2 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 "kbc_macros.h" |
11 |
#include "i8254.h" |
12 |
#include "timer.h" |
13 |
#include "keyboard.h" |
14 |
#include "mouse.h" |
15 |
#include "graph.h" |
16 |
#include "rtc.h" |
17 |
#include "interrupts_func.h" |
18 |
|
19 |
#include "graph.h" |
20 |
#include "sprite.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 |
#ifdef DIOGO
|
35 |
#include "test7.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 |
#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 |
|
60 |
/// subscribe interrupts
|
61 |
if (subscribe_all()) { return 1; } |
62 |
|
63 |
/// 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 |
|
70 |
/// 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 |
|
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 |
#endif
|
95 |
|
96 |
#ifdef DIOGO
|
97 |
/*
|
98 |
graph_clear_screen();
|
99 |
|
100 |
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 |
rectangle_set_outline_width(rect, 2);
|
106 |
rectangle_set_outline_color(rect, WHITE);
|
107 |
rectangle_draw(rect);
|
108 |
|
109 |
text_t *txt = text_ctor(consolas, "Hello world!");
|
110 |
text_set_color(txt, 0x888888);
|
111 |
|
112 |
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 |
text_draw(txt);
|
117 |
text_dtor(txt);
|
118 |
|
119 |
graph_draw();
|
120 |
rectangle_dtor(rect);
|
121 |
|
122 |
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 |
tickdelay(micros_to_ticks(1000000));*/
|
147 |
|
148 |
// RTC
|
149 |
/*
|
150 |
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 |
*/
|
160 |
//UART
|
161 |
unsigned long bits = 8; |
162 |
unsigned long stop = 2; |
163 |
long parity = 0; |
164 |
unsigned long rate = 9600; |
165 |
unsigned char tx = 0; (void)tx; |
166 |
int stringc = 2; (void)stringc; |
167 |
char *strings[] = {"Hello", "world"}; (void)strings; |
168 |
//if((r = ser_test_set(COM1_ADDR, bits, stop, parity, rate))) return r;
|
169 |
//if((r = ser_test_conf(COM1_ADDR))) return r;
|
170 |
if((r = ser_test_poll(COM1_ADDR, tx, bits, stop, parity, rate, stringc, strings))) return r; |
171 |
printf("DONE\n");
|
172 |
#endif
|
173 |
#ifndef DIOGO
|
174 |
ent_set_scale(DEFAULT_SCALE); |
175 |
|
176 |
text_timer_t *in_game_timer = timer_ctor(consolas); |
177 |
|
178 |
menu_t *main_menu = menu_ctor(consolas); |
179 |
|
180 |
list_t *shooter_list = list_ctor(); |
181 |
|
182 |
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 |
|
186 |
gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing); |
187 |
gunner_set_spawn(shooter2, 975, 75); |
188 |
gunner_set_pos(shooter2, 775, 75); |
189 |
|
190 |
list_insert(shooter_list, list_end(shooter_list), shooter1); |
191 |
list_insert(shooter_list, list_end(shooter_list), shooter2); |
192 |
|
193 |
list_t *bullet_list = list_ctor(); |
194 |
|
195 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
196 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
197 |
|
198 |
//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 |
uint8_t last_lb = 0;
|
202 |
struct packet pp;
|
203 |
keys_t *keys = get_key_presses(); |
204 |
|
205 |
/// loop stuff
|
206 |
int ipc_status;
|
207 |
message msg; |
208 |
int game_state = MENU, click = 0; |
209 |
|
210 |
while (game_state != EXIT) {
|
211 |
/* 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 |
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 |
|
231 |
click = 0;
|
232 |
|
233 |
sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y); |
234 |
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 |
|
242 |
update_game_state(map1, shooter_list, bullet_list); |
243 |
|
244 |
//update_scale();
|
245 |
angle = get_mouse_angle(shooter1); |
246 |
gunner_set_angle(shooter1, angle - M_PI_2); |
247 |
|
248 |
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 |
} |
263 |
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 |
} |
277 |
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 |
} |
286 |
break;
|
287 |
} |
288 |
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 |
} |
308 |
} |
309 |
} |
310 |
|
311 |
break;
|
312 |
default:
|
313 |
break; /* no other notifications expected: do nothing */ |
314 |
} |
315 |
} else { /* received standart message, not a notification */ |
316 |
/* no standart message expected: do nothing */
|
317 |
} |
318 |
} |
319 |
|
320 |
while(list_size(shooter_list) > 0){ |
321 |
gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
322 |
gunner_dtor(p); |
323 |
} |
324 |
|
325 |
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 |
|
332 |
timer_dtor(in_game_timer); in_game_timer = NULL;
|
333 |
|
334 |
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 |
} |
348 |
|
349 |
|
350 |
if (cleanup()) {
|
351 |
printf("%s: failed to cleanup.\n", __func__);
|
352 |
return 1; |
353 |
} |
354 |
#endif
|
355 |
|
356 |
|
357 |
return 0; |
358 |
} |