root / proj / src / proj.c @ 242
History | View | Annotate | Download (10.9 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 | #include "timer.h" |
||
11 | #include "keyboard.h" |
||
12 | 150 | up20180655 | #include "mouse.h" |
13 | 189 | up20180655 | #include "graph.h" |
14 | 234 | up20180655 | #include "rtc.h" |
15 | 153 | up20180655 | #include "interrupts_func.h" |
16 | 149 | up20180655 | |
17 | 179 | up20180642 | #include "graph.h" |
18 | #include "sprite.h" |
||
19 | #include "rectangle.h" |
||
20 | 182 | up20180642 | #include "font.h" |
21 | 192 | up20180642 | #include "ent.h" |
22 | 168 | up20180642 | |
23 | 192 | up20180642 | #include "crosshair.h" |
24 | #include "shooter.h" |
||
25 | #include "pistol.h" |
||
26 | #include "nothing.h" |
||
27 | 237 | up20180642 | #include "bullet.h" |
28 | 216 | up20180642 | #include "map1.h" |
29 | 183 | up20180642 | |
30 | 226 | up20180642 | #include "list.h" |
31 | |||
32 | 235 | up20180642 | #ifdef DIOGO
|
33 | #include "uart_macros.h" |
||
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 | |||
56 | 221 | up20180642 | font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
|
57 | 192 | up20180642 | if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; } |
58 | |||
59 | 170 | up20180642 | /// subscribe interrupts
|
60 | if (subscribe_all()) { return 1; } |
||
61 | |||
62 | /// initialize graphics
|
||
63 | 166 | up20180642 | 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 | 152 | up20180642 | return 1; |
67 | } |
||
68 | |||
69 | 188 | up20180642 | /// Load stuff
|
70 | 216 | up20180642 | 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 | 237 | up20180642 | basic_sprite_t *bsp_bullet = NULL;
|
75 | map_t *map1 = NULL;
|
||
76 | 216 | up20180642 | sprite_t *sp_crosshair = NULL;
|
77 | 188 | up20180642 | { |
78 | graph_clear_screen(); |
||
79 | text_t *txt = text_ctor(consolas, "Loading...");
|
||
80 | text_draw(txt); |
||
81 | text_dtor(txt); |
||
82 | graph_draw(); |
||
83 | 192 | up20180642 | |
84 | 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 | 237 | up20180642 | bsp_bullet = get_bullet (); if(bsp_bullet == NULL) printf("Failed to get bullet\n"); |
89 | 216 | up20180642 | map1 = get_map1 (); if(map1 == NULL) printf("Failed to get map1\n"); |
90 | 192 | up20180642 | |
91 | sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n"); |
||
92 | 188 | up20180642 | } |
93 | 192 | up20180642 | |
94 | 159 | up20180642 | #ifdef DIOGO
|
95 | 234 | up20180655 | /*
|
96 | 188 | up20180642 | graph_clear_screen();
|
97 | 183 | up20180642 | |
98 | 188 | up20180642 | rectangle_t *rect = rectangle_ctor(0,0,400,100);
|
99 | rectangle_set_pos(rect,
|
||
100 | graph_get_XRes()/2 - rectangle_get_w(rect)/2,
|
||
101 | graph_get_YRes()*0.25 - rectangle_get_h(rect)/2);
|
||
102 | rectangle_set_fill_color(rect, BLACK);
|
||
103 | 179 | up20180642 | rectangle_set_outline_width(rect, 2);
|
104 | 188 | up20180642 | rectangle_set_outline_color(rect, WHITE);
|
105 | 179 | up20180642 | rectangle_draw(rect);
|
106 | |||
107 | 188 | up20180642 | text_t *txt = text_ctor(consolas, "Hello world!");
|
108 | text_set_color(txt, 0x888888);
|
||
109 | 182 | up20180642 | |
110 | 188 | up20180642 | text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
|
111 | rectangle_get_y(rect)+rectangle_get_h(rect)/2);
|
||
112 | text_set_valign(txt, text_valign_center);
|
||
113 | text_set_halign(txt, text_halign_center);
|
||
114 | 183 | up20180642 | text_draw(txt);
|
115 | text_dtor(txt);
|
||
116 | |||
117 | graph_draw();
|
||
118 | 188 | up20180642 | rectangle_dtor(rect);
|
119 | 183 | up20180642 | |
120 | 226 | up20180642 | list_t *l = list_ctor();
|
121 | int *p = NULL;
|
||
122 | for(int i = 10; i < 20; ++i){
|
||
123 | p = malloc(sizeof(int));
|
||
124 | *p = i;
|
||
125 | printf("INSERTING %d\n", i);
|
||
126 | list_insert(l, list_end(l), p);
|
||
127 | printf("INSERTED, SIZE=%d\n", list_size(l));
|
||
128 | }
|
||
129 | list_node_t *it = list_begin(l);
|
||
130 | while(it != list_end(l)){
|
||
131 | printf("%d\n", **(int**)list_node_val(it));
|
||
132 | it = list_node_next(it);
|
||
133 | }
|
||
134 | while(list_size(l) > 0){
|
||
135 | printf("ERASING\n");
|
||
136 | void *p = list_erase(l, list_begin(l));
|
||
137 | printf("ERASED %d, SIZE=%d\n", *(int*)p, list_size(l));
|
||
138 | free(p);
|
||
139 | }
|
||
140 | printf("DONE\n");
|
||
141 | if(list_dtor(l)) printf("COULD NOT DESTRUCT LIST\n");
|
||
142 | |||
143 | |||
144 | 234 | up20180655 | tickdelay(micros_to_ticks(1000000));*/
|
145 | 183 | up20180642 | |
146 | 234 | up20180655 | // RTC
|
147 | 235 | up20180642 | /*
|
148 | 234 | up20180655 | uint8_t date[4], time[3];
|
149 | |||
150 | if (rtc_read_time(time)) return 1;
|
||
151 | |||
152 | if (rtc_read_date(date)) return 1;
|
||
153 | |||
154 | printf("Hour: %02d:%02d:%02d\n", time[2], time[1], time[0]);
|
||
155 | |||
156 | printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
|
||
157 | 235 | up20180642 | */
|
158 | //UART
|
||
159 | 241 | up20180642 | if((r = ser_test_conf(COM1_ADDR))) return r; |
160 | if((r = ser_test_set(COM1_ADDR, 6, 2, 0, 9800))) return r; |
||
161 | if((r = ser_test_conf(COM1_ADDR))) return r; |
||
162 | 159 | up20180642 | #endif
|
163 | 152 | up20180642 | |
164 | 171 | up20180655 | #ifdef TELMO
|
165 | 216 | up20180642 | ent_set_scale(DEFAULT_SCALE); |
166 | 192 | up20180642 | |
167 | 236 | up20180642 | list_t *shooter_list = list_ctor(); |
168 | |||
169 | 201 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
170 | 232 | up20180655 | gunner_set_spawn(shooter1, 75, 75); |
171 | 222 | up20180655 | gunner_set_pos(shooter1, 75, 75); |
172 | 192 | up20180642 | |
173 | 201 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing); |
174 | 232 | up20180655 | gunner_set_spawn(shooter2, 975, 75); |
175 | 231 | up20180655 | gunner_set_pos(shooter2, 775, 75); |
176 | 192 | up20180642 | |
177 | 236 | up20180642 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
178 | list_insert(shooter_list, list_end(shooter_list), shooter2); |
||
179 | |||
180 | 231 | up20180655 | //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
|
181 | 202 | up20180655 | |
182 | 236 | up20180642 | list_t *bullet_list = list_ctor(); |
183 | 231 | up20180655 | |
184 | 216 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
185 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
186 | 214 | up20180642 | |
187 | 200 | up20180655 | uint32_t refresh_count_value = sys_hz() / REFRESH_RATE; |
188 | 231 | up20180655 | |
189 | uint8_t last_lb = 0;
|
||
190 | 171 | up20180655 | #endif
|
191 | |||
192 | 149 | up20180655 | /// loop stuff
|
193 | 170 | up20180642 | int ipc_status;
|
194 | 149 | up20180655 | message msg; |
195 | 147 | up20180655 | int good = 1; |
196 | 168 | up20180642 | |
197 | 183 | up20180642 | #ifdef DIOGO
|
198 | good = 0;
|
||
199 | #endif
|
||
200 | |||
201 | 147 | up20180655 | while (good) {
|
202 | /* Get a request message. */
|
||
203 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
204 | printf("driver_receive failed with %d", r);
|
||
205 | continue;
|
||
206 | } |
||
207 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
208 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
209 | case HARDWARE: /* hardware interrupt notification */ |
||
210 | 153 | up20180655 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
211 | if (msg.m_notify.interrupts & n) {
|
||
212 | interrupt_handler(i); |
||
213 | 167 | up20180655 | if ((scancode[0]) == ESC_BREAK_CODE) good = 0; |
214 | 171 | up20180655 | #ifdef TELMO
|
215 | 187 | up20180655 | if (i == 0) { |
216 | if (no_interrupts % refresh_count_value == 0) { |
||
217 | 240 | up20180655 | update_movement(map1, shooter1, shooter2); |
218 | 228 | up20180655 | //bullet_update_movement(bullet);
|
219 | 232 | up20180655 | |
220 | if (no_interrupts % 180 == 0) gunner_set_pos(shooter2, 775, 75); |
||
221 | |||
222 | 236 | up20180642 | update_game_state(map1, shooter_list, bullet_list); |
223 | 216 | up20180642 | |
224 | 220 | up20180655 | if(map_collides_gunner(map1, shooter1)){
|
225 | 216 | up20180642 | printf("COLLIDING\n");
|
226 | } |
||
227 | |||
228 | 200 | up20180655 | update_scale(); |
229 | 192 | up20180642 | |
230 | 216 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
231 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
232 | 214 | up20180642 | |
233 | 192 | up20180642 | sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y()); |
234 | 187 | up20180655 | double angle = get_mouse_angle(shooter1);
|
235 | 201 | up20180642 | gunner_set_angle(shooter1, angle - M_PI_2); |
236 | 187 | up20180655 | graph_clear_screen(); |
237 | 207 | up20180642 | |
238 | clock_t t = clock(); |
||
239 | |||
240 | 216 | up20180642 | map_draw (map1); |
241 | 201 | up20180642 | gunner_draw(shooter2); |
242 | gunner_draw(shooter1); |
||
243 | 231 | up20180655 | bullet_draw_list(bullet_list); |
244 | 207 | up20180642 | |
245 | 222 | up20180655 | t = clock()-t; //printf("%d\n", (t*1000)/CLOCKS_PER_SEC);
|
246 | 207 | up20180642 | |
247 | 192 | up20180642 | sprite_draw(sp_crosshair); |
248 | 187 | up20180655 | graph_draw(); |
249 | } |
||
250 | 184 | up20180655 | } |
251 | 220 | up20180655 | if (i == 12) { |
252 | if (counter_mouse_ih >= 3) { |
||
253 | struct packet pp = mouse_parse_packet(packet_mouse_ih);
|
||
254 | 231 | up20180655 | update_mouse(&pp); |
255 | 220 | up20180655 | //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
|
256 | counter_mouse_ih = 0;
|
||
257 | 231 | up20180655 | |
258 | if (last_lb ^ get_key_presses()->lb_pressed && get_key_presses()->lb_pressed) {
|
||
259 | 237 | up20180642 | shoot_bullet(shooter1, bullet_list, bsp_bullet); |
260 | 231 | up20180655 | } |
261 | |||
262 | last_lb = get_key_presses()->lb_pressed; |
||
263 | 220 | up20180655 | } |
264 | } |
||
265 | 171 | up20180655 | #endif
|
266 | 153 | up20180655 | } |
267 | 147 | up20180655 | } |
268 | 167 | up20180655 | |
269 | 147 | up20180655 | 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 | 149 | up20180655 | |
278 | 192 | up20180642 | #ifdef TELMO
|
279 | 236 | up20180642 | while(list_size(shooter_list) > 0){ |
280 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
281 | gunner_dtor(p); |
||
282 | } |
||
283 | |||
284 | 231 | up20180655 | while(list_size(bullet_list) > 0){ |
285 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
286 | 236 | up20180642 | bullet_dtor(p); |
287 | 231 | up20180655 | } |
288 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
289 | 192 | up20180642 | #endif
|
290 | 188 | up20180642 | |
291 | 216 | up20180642 | basic_sprite_dtor (bsp_crosshair); bsp_crosshair = NULL;
|
292 | basic_sprite_dtor (bsp_shooter ); bsp_shooter = NULL;
|
||
293 | sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
||
294 | basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL;
|
||
295 | basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL;
|
||
296 | map_dtor (map1 ); map1 = NULL;
|
||
297 | font_dtor (consolas ); consolas = NULL;
|
||
298 | 192 | up20180642 | |
299 | 153 | up20180655 | // Unsubscribe interrupts
|
300 | 155 | up20180655 | if (unsubscribe_all()) {
|
301 | if (cleanup())
|
||
302 | printf("%s: failed to cleanup.\n", __func__);
|
||
303 | 152 | up20180642 | return 1; |
304 | } |
||
305 | |||
306 | 155 | up20180655 | |
307 | if (cleanup()) {
|
||
308 | printf("%s: failed to cleanup.\n", __func__);
|
||
309 | 152 | up20180642 | return 1; |
310 | } |
||
311 | |||
312 | 149 | up20180655 | return 0; |
313 | 147 | up20180655 | } |