root / proj / src / proj.c @ 323
History | View | Annotate | Download (37.2 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 | 291 | up20180642 | #include "menu.h" |
15 | 234 | up20180655 | #include "rtc.h" |
16 | 287 | up20180642 | #include "hltp.h" |
17 | 153 | up20180655 | #include "interrupts_func.h" |
18 | 270 | up20180655 | #include "makecode_map.h" |
19 | 149 | up20180655 | |
20 | 179 | up20180642 | #include "graph.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 | 303 | up20180642 | #include "zombie.h" |
28 | 192 | up20180642 | #include "pistol.h" |
29 | #include "nothing.h" |
||
30 | 237 | up20180642 | #include "bullet.h" |
31 | 216 | up20180642 | #include "map1.h" |
32 | 183 | up20180642 | |
33 | 294 | up20180642 | #include "errors.h" |
34 | |||
35 | 226 | up20180642 | #include "list.h" |
36 | |||
37 | 144 | up20180655 | int main(int argc, char* argv[]) { |
38 | |||
39 | lcf_set_language("EN-US");
|
||
40 | |||
41 | 323 | up20180642 | lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
|
42 | 144 | up20180655 | |
43 | 323 | up20180642 | 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 | 321 | up20180642 | static basic_sprite_t *bsp_crosshair = NULL; |
53 | static basic_sprite_t *bsp_shooter = NULL; |
||
54 | static basic_sprite_t *bsp_zombie = NULL; |
||
55 | static basic_sprite_t *bsp_pistol = NULL; |
||
56 | static basic_sprite_t *bsp_nothing = NULL; |
||
57 | static basic_sprite_t *bsp_bullet = NULL; |
||
58 | static map_t *map1 = NULL; |
||
59 | static sprite_t *sp_crosshair = NULL; |
||
60 | 294 | up20180642 | |
61 | 300 | up20180642 | static int (singleplayer)(void); |
62 | 311 | up20180655 | static int (multiplayer)(void); |
63 | 300 | up20180642 | static int (chat)(void); |
64 | 149 | up20180655 | int(proj_main_loop)(int argc, char *argv[]) { |
65 | 170 | up20180642 | |
66 | int r;
|
||
67 | |||
68 | 313 | up20180642 | if(font_init()){ printf("Failed to initialize fonts\n"); return 1; } |
69 | 192 | up20180642 | |
70 | 261 | up20180642 | /// subscribe interrupts
|
71 | if (subscribe_all()) { return 1; } |
||
72 | 170 | up20180642 | |
73 | 297 | up20180642 | /// initialize graphics
|
74 | if(graph_init(GRAPH_MODE)){
|
||
75 | printf("%s: failed to initalize graphics.\n", __func__);
|
||
76 | if (cleanup()) printf("%s: failed to cleanup.\n", __func__); |
||
77 | return 1; |
||
78 | } |
||
79 | 152 | up20180642 | |
80 | 261 | up20180642 | /// Load stuff
|
81 | { |
||
82 | 297 | up20180642 | graph_clear_screen(); |
83 | 314 | up20180642 | text_t *txt = text_ctor(font_get_default(), "Loading...");
|
84 | 309 | up20180642 | text_set_pos(txt, graph_get_XRes()/2, graph_get_YRes()/2); |
85 | text_set_valign(txt, text_valign_center); |
||
86 | text_set_halign(txt, text_halign_center); |
||
87 | text_set_color(txt, TEXT_COLOR); |
||
88 | 297 | up20180642 | text_draw(txt); |
89 | text_dtor(txt); |
||
90 | graph_draw(); |
||
91 | 183 | up20180642 | |
92 | 261 | up20180642 | bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n"); |
93 | bsp_shooter = get_shooter (); if(bsp_shooter == NULL) printf("Failed to get shooter\n"); |
||
94 | 303 | up20180642 | bsp_zombie = get_zombie (); if(bsp_zombie == NULL) printf("Failed to get zombie\n"); |
95 | 261 | up20180642 | bsp_pistol = get_pistol (); if(bsp_pistol == NULL) printf("Failed to get pistol\n"); |
96 | bsp_nothing = get_nothing (); if(bsp_nothing == NULL) printf("Failed to get nothing\n"); |
||
97 | bsp_bullet = get_bullet (); if(bsp_bullet == NULL) printf("Failed to get bullet\n"); |
||
98 | map1 = get_map1 (); if(map1 == NULL) printf("Failed to get map1\n"); |
||
99 | 226 | up20180642 | |
100 | 261 | up20180642 | sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n"); |
101 | } |
||
102 | 226 | up20180642 | |
103 | 314 | up20180642 | menu_t *main_menu = menu_ctor(font_get_default()); |
104 | 299 | up20180642 | menu_add_item(main_menu, "Single player");
|
105 | menu_add_item(main_menu, "Multiplayer");
|
||
106 | 297 | up20180642 | menu_add_item(main_menu, "Chat");
|
107 | menu_add_item(main_menu, "Exit");
|
||
108 | 234 | up20180655 | |
109 | 297 | up20180642 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
110 | uint8_t last_lb = 0;
|
||
111 | struct packet pp;
|
||
112 | keys_t *keys = get_key_presses(); |
||
113 | |||
114 | 261 | up20180642 | /// loop stuff
|
115 | 297 | up20180642 | int click = 0; |
116 | 323 | up20180642 | uint64_t int_vector = 0;
|
117 | 295 | up20180642 | int good = true; |
118 | while (good) {
|
||
119 | 261 | up20180642 | /* Get a request message. */
|
120 | 307 | up20180642 | if((r = get_interrupts_vector(&int_vector))) return r; |
121 | 321 | up20180642 | uint32_t n = 1;
|
122 | for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
||
123 | 307 | up20180642 | if (int_vector & n) {
|
124 | interrupt_handler(i); |
||
125 | switch (i) {
|
||
126 | case TIMER0_IRQ:
|
||
127 | 192 | up20180642 | |
128 | 307 | up20180642 | graph_clear_screen(); |
129 | switch(menu_update_state(main_menu, click)){
|
||
130 | case -1: break; |
||
131 | case 0: singleplayer(); break; //campaign(); break; |
||
132 | 311 | up20180655 | case 1: multiplayer() ; break; |
133 | 307 | up20180642 | case 2: chat(); break; |
134 | case 3: good = false; break; |
||
135 | } |
||
136 | menu_draw(main_menu); |
||
137 | 192 | up20180642 | |
138 | 307 | up20180642 | click = 0;
|
139 | 202 | up20180655 | |
140 | 307 | up20180642 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
141 | sprite_draw(sp_crosshair); |
||
142 | graph_draw(); |
||
143 | 231 | up20180655 | |
144 | 307 | up20180642 | break;
|
145 | case KBC_IRQ:
|
||
146 | 321 | up20180642 | if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false; |
147 | 307 | up20180642 | case MOUSE_IRQ:
|
148 | 323 | up20180642 | if (mouse_get_counter_mouse_ih() >= 3) { |
149 | mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
||
150 | 307 | up20180642 | update_mouse(&pp); |
151 | if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
||
152 | last_lb = keys->lb_pressed; |
||
153 | 323 | up20180642 | mouse_set_counter_mouse_ih(0);
|
154 | 261 | up20180642 | } |
155 | 307 | up20180642 | break;
|
156 | case COM1_IRQ: nctp_ih(); break; |
||
157 | 305 | up20180642 | } |
158 | 147 | up20180655 | } |
159 | } |
||
160 | 261 | up20180642 | } |
161 | 149 | up20180655 | |
162 | 261 | up20180642 | basic_sprite_dtor (bsp_crosshair); bsp_crosshair = NULL;
|
163 | basic_sprite_dtor (bsp_shooter ); bsp_shooter = NULL;
|
||
164 | 307 | up20180642 | basic_sprite_dtor (bsp_zombie ); bsp_zombie = NULL;
|
165 | 261 | up20180642 | sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
166 | basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL;
|
||
167 | basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL;
|
||
168 | map_dtor (map1 ); map1 = NULL;
|
||
169 | 319 | up20180642 | font_free(); |
170 | 243 | up20180642 | |
171 | 261 | up20180642 | // Unsubscribe interrupts
|
172 | if (unsubscribe_all()) {
|
||
173 | if (cleanup())
|
||
174 | 305 | up20180642 | printf("%s: failed to cleanup.\n", __func__);
|
175 | 261 | up20180642 | return 1; |
176 | } |
||
177 | 188 | up20180642 | |
178 | 261 | up20180642 | if (cleanup()) {
|
179 | printf("%s: failed to cleanup.\n", __func__);
|
||
180 | return 1; |
||
181 | } |
||
182 | 155 | up20180655 | |
183 | 149 | up20180655 | return 0; |
184 | 147 | up20180655 | } |
185 | 294 | up20180642 | |
186 | 321 | up20180642 | static host_info_t *host_info = NULL; |
187 | static remote_info_t *remote_info = NULL; |
||
188 | static bullet_info_t *bullet_info = NULL; |
||
189 | 308 | up20180655 | |
190 | 311 | up20180655 | static void multiplayer_process(const uint8_t *p, const size_t sz) { |
191 | void *dest = NULL; |
||
192 | hltp_type tp = hltp_interpret(p, sz, &dest); |
||
193 | switch(tp){
|
||
194 | case hltp_type_host:
|
||
195 | 320 | up20180655 | host_info_dtor(host_info); |
196 | host_info = (host_info_t*)dest; |
||
197 | 311 | up20180655 | break;
|
198 | case hltp_type_remote:
|
||
199 | 320 | up20180655 | remote_info_dtor(remote_info); |
200 | remote_info = (remote_info_t*)dest; |
||
201 | 311 | up20180655 | break;
|
202 | 320 | up20180655 | case hltp_type_bullet:
|
203 | bullet_info_dtor(bullet_info); |
||
204 | bullet_info = (bullet_info_t*)dest; |
||
205 | break;
|
||
206 | 321 | up20180642 | case hltp_type_invalid: break; |
207 | case hltp_type_string : break; |
||
208 | 311 | up20180655 | } |
209 | } |
||
210 | static int (multiplayer_host)(void); |
||
211 | static int (multiplayer_remote)(void); |
||
212 | static int (multiplayer)(void) { |
||
213 | int r;
|
||
214 | 308 | up20180655 | |
215 | 314 | up20180642 | menu_t *main_menu = menu_ctor(font_get_default()); |
216 | 311 | up20180655 | menu_add_item(main_menu, "Create");
|
217 | menu_add_item(main_menu, "Connect");
|
||
218 | menu_add_item(main_menu, "Back");
|
||
219 | 308 | up20180655 | |
220 | 311 | up20180655 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
221 | uint8_t last_lb = 0;
|
||
222 | struct packet pp;
|
||
223 | keys_t *keys = get_key_presses(); |
||
224 | 308 | up20180655 | |
225 | 311 | up20180655 | /// loop stuff
|
226 | int click = 0; |
||
227 | 323 | up20180642 | uint64_t int_vector = 0;
|
228 | 311 | up20180655 | int good = true; |
229 | while (good) {
|
||
230 | /* Get a request message. */
|
||
231 | if((r = get_interrupts_vector(&int_vector))) return r; |
||
232 | 321 | up20180642 | uint32_t n = 1;
|
233 | for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
||
234 | 311 | up20180655 | if (int_vector & n) {
|
235 | interrupt_handler(i); |
||
236 | switch (i) {
|
||
237 | case TIMER0_IRQ:
|
||
238 | 308 | up20180655 | |
239 | 311 | up20180655 | graph_clear_screen(); |
240 | switch(menu_update_state(main_menu, click)){
|
||
241 | case -1: break; |
||
242 | case 0: multiplayer_host(); break; |
||
243 | case 1: multiplayer_remote(); break; |
||
244 | case 2: good = false; break; |
||
245 | } |
||
246 | menu_draw(main_menu); |
||
247 | |||
248 | click = 0;
|
||
249 | |||
250 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
||
251 | sprite_draw(sp_crosshair); |
||
252 | graph_draw(); |
||
253 | |||
254 | break;
|
||
255 | case KBC_IRQ:
|
||
256 | 321 | up20180642 | if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false; |
257 | 311 | up20180655 | case MOUSE_IRQ:
|
258 | 323 | up20180642 | if (mouse_get_counter_mouse_ih() >= 3) { |
259 | mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
||
260 | 311 | up20180655 | update_mouse(&pp); |
261 | if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
||
262 | last_lb = keys->lb_pressed; |
||
263 | 323 | up20180642 | mouse_set_counter_mouse_ih(0);
|
264 | 311 | up20180655 | } |
265 | break;
|
||
266 | case COM1_IRQ: nctp_ih(); break; |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | return 0; |
||
272 | 308 | up20180655 | } |
273 | |||
274 | 311 | up20180655 | static int (multiplayer_host)(void) { |
275 | 320 | up20180655 | int r;
|
276 | 308 | up20180655 | |
277 | 311 | up20180655 | nctp_dump(); |
278 | 320 | up20180655 | nctp_set_processor(multiplayer_process); |
279 | 311 | up20180655 | |
280 | 308 | up20180655 | ent_set_scale(DEFAULT_SCALE); |
281 | 314 | up20180642 | text_timer_t *in_game_timer = timer_ctor(font_get_default()); |
282 | 308 | up20180655 | |
283 | list_t *shooter_list = list_ctor(); |
||
284 | |||
285 | 321 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
286 | 308 | up20180655 | gunner_set_spawn(shooter1, 75, 75); |
287 | |||
288 | 321 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 2); if(shooter2 == NULL) printf("Failed to get shooter2\n"); |
289 | 308 | up20180655 | gunner_set_spawn(shooter2, 975, 75); |
290 | |||
291 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
||
292 | list_insert(shooter_list, list_end(shooter_list), shooter2); |
||
293 | |||
294 | do {
|
||
295 | get_random_spawn(map1, shooter1, shooter_list); |
||
296 | get_random_spawn(map1, shooter2, shooter_list); |
||
297 | 320 | up20180655 | } while (distance_gunners(shooter1, shooter2) < 500); |
298 | 308 | up20180655 | |
299 | 320 | up20180655 | host_info = host_info_ctor(shooter1, shooter2); |
300 | remote_info = remote_info_ctor(); |
||
301 | bullet_info = bullet_info_ctor(); |
||
302 | |||
303 | 308 | up20180655 | list_t *bullet_list = list_ctor(); |
304 | |||
305 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
||
306 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
307 | |||
308 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
||
309 | uint8_t last_lb = 0;
|
||
310 | struct packet pp;
|
||
311 | keys_t *keys = get_key_presses(); |
||
312 | /// loop stuff
|
||
313 | 323 | up20180642 | uint64_t int_vector = 0;
|
314 | 308 | up20180655 | int good = true; |
315 | 311 | up20180655 | int state = 0; // -1 for remote win, 0 for draw, 1 for host win |
316 | 320 | up20180655 | list_node_t *p1, *p2; // player states
|
317 | int state_1, state_2;
|
||
318 | 311 | up20180655 | while (good) {
|
319 | if ((r = get_interrupts_vector(&int_vector))) return r; |
||
320 | 321 | up20180642 | uint32_t n = 1;
|
321 | for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
||
322 | 320 | up20180655 | if (int_vector & n) {
|
323 | interrupt_handler(i); |
||
324 | switch (i) {
|
||
325 | case TIMER0_IRQ:
|
||
326 | 321 | up20180642 | if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer); |
327 | 308 | up20180655 | |
328 | 320 | up20180655 | update_movement(map1, shooter1, keys, shooter_list); |
329 | update_movement(map1, shooter2, &(remote_info->remote_keys_pressed), shooter_list); |
||
330 | 308 | up20180655 | |
331 | 320 | up20180655 | update_game_state(map1, shooter_list, bullet_list); |
332 | 311 | up20180655 | |
333 | 320 | up20180655 | p1 = list_find(shooter_list, shooter1); |
334 | p2 = list_find(shooter_list, shooter2); |
||
335 | 311 | up20180655 | |
336 | 320 | up20180655 | if ((state_1 = (p1 == list_end(shooter_list))) || (state_2 = (p2 == list_end(shooter_list)))) {
|
337 | state = state_1 - state_2; |
||
338 | good = false;
|
||
339 | break;
|
||
340 | } |
||
341 | 311 | up20180655 | |
342 | 320 | up20180655 | double angle = get_mouse_angle(shooter1);
|
343 | gunner_set_angle(shooter1, angle - M_PI_2); |
||
344 | 311 | up20180655 | |
345 | 320 | up20180655 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
346 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
347 | 311 | up20180655 | |
348 | 320 | up20180655 | gunner_set_angle(shooter2, remote_info->remote_angle); |
349 | |||
350 | build_host_structure(host_info, shooter1, shooter2, bullet_list); |
||
351 | |||
352 | hltp_send_host_info(host_info); |
||
353 | |||
354 | graph_clear_screen(); |
||
355 | map_draw (map1); |
||
356 | bullet_draw_list(bullet_list); |
||
357 | gunner_draw_list(shooter_list); |
||
358 | |||
359 | text_draw(in_game_timer->text); |
||
360 | |||
361 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
||
362 | sprite_draw(sp_crosshair); |
||
363 | graph_draw(); |
||
364 | |||
365 | break;
|
||
366 | case KBC_IRQ:
|
||
367 | 321 | up20180642 | if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) { |
368 | 320 | up20180655 | good = false;
|
369 | } |
||
370 | break;
|
||
371 | case MOUSE_IRQ:
|
||
372 | 323 | up20180642 | if (mouse_get_counter_mouse_ih() >= 3) { |
373 | mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
||
374 | 320 | up20180655 | update_mouse(&pp); |
375 | if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
||
376 | shoot_bullet(shooter1, bullet_list, bsp_bullet); |
||
377 | last_lb = keys->lb_pressed; |
||
378 | 323 | up20180642 | mouse_set_counter_mouse_ih(0);
|
379 | 320 | up20180655 | } |
380 | break;
|
||
381 | |||
382 | case COM1_IRQ:
|
||
383 | nctp_ih(); |
||
384 | if (bullet_info->new_bullet) {
|
||
385 | shoot_bullet(shooter2, bullet_list, bsp_bullet); |
||
386 | bullet_info->new_bullet = false;
|
||
387 | } |
||
388 | break;
|
||
389 | } |
||
390 | 311 | up20180655 | } |
391 | } |
||
392 | 320 | up20180655 | } |
393 | 311 | up20180655 | |
394 | 320 | up20180655 | while(list_size(shooter_list) > 0){ |
395 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
396 | gunner_dtor(p); |
||
397 | } |
||
398 | if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
||
399 | |||
400 | while(list_size(bullet_list) > 0){ |
||
401 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
402 | bullet_dtor(p); |
||
403 | } |
||
404 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
405 | |||
406 | host_info_dtor(host_info); |
||
407 | remote_info_dtor(remote_info); |
||
408 | bullet_info_dtor(bullet_info); |
||
409 | |||
410 | timer_dtor(in_game_timer); in_game_timer = NULL;
|
||
411 | |||
412 | 308 | up20180655 | return 0; |
413 | 311 | up20180655 | } |
414 | 320 | up20180655 | static int (multiplayer_remote)(void) { |
415 | 311 | up20180655 | int r;
|
416 | 308 | up20180655 | |
417 | 311 | up20180655 | nctp_dump(); |
418 | nctp_set_processor(multiplayer_process); |
||
419 | |||
420 | ent_set_scale(DEFAULT_SCALE); |
||
421 | 314 | up20180642 | text_timer_t *in_game_timer = timer_ctor(font_get_default()); |
422 | 311 | up20180655 | |
423 | list_t *shooter_list = list_ctor(); |
||
424 | |||
425 | 321 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 2); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
426 | 311 | up20180655 | gunner_set_spawn(shooter1, 75, 75); |
427 | |||
428 | 321 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n"); |
429 | 311 | up20180655 | gunner_set_spawn(shooter2, 975, 75); |
430 | |||
431 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
||
432 | list_insert(shooter_list, list_end(shooter_list), shooter2); |
||
433 | |||
434 | 320 | up20180655 | host_info = host_info_ctor(shooter2, shooter1); |
435 | remote_info = remote_info_ctor(); |
||
436 | bullet_info = bullet_info_ctor(); |
||
437 | 311 | up20180655 | |
438 | list_t *bullet_list = list_ctor(); |
||
439 | |||
440 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
||
441 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
442 | |||
443 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
||
444 | uint8_t last_lb = 0;
|
||
445 | struct packet pp;
|
||
446 | keys_t *keys = get_key_presses(); |
||
447 | |||
448 | /// loop stuff
|
||
449 | 323 | up20180642 | uint64_t int_vector = 0;
|
450 | 311 | up20180655 | int good = true; |
451 | while (good) {
|
||
452 | if ((r = get_interrupts_vector(&int_vector))) return r; |
||
453 | 321 | up20180642 | uint32_t n = 1;
|
454 | for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
||
455 | 320 | up20180655 | if (int_vector & n) {
|
456 | interrupt_handler(i); |
||
457 | switch (i) {
|
||
458 | case TIMER0_IRQ:
|
||
459 | 321 | up20180642 | if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer); |
460 | 311 | up20180655 | |
461 | 320 | up20180655 | double angle = get_mouse_angle(shooter1);
|
462 | 311 | up20180655 | |
463 | 320 | up20180655 | build_remote_structure(remote_info, keys, angle); |
464 | 311 | up20180655 | |
465 | 320 | up20180655 | hltp_send_remote_info(remote_info); |
466 | 311 | up20180655 | |
467 | 320 | up20180655 | gunner_set_pos(shooter1, host_info->remote_x, host_info->remote_y); |
468 | gunner_set_angle(shooter1, host_info->remote_angle); |
||
469 | gunner_set_health(shooter1, host_info->remote_health); |
||
470 | gunner_set_curr_health(shooter1, host_info->remote_current_health); |
||
471 | 311 | up20180655 | |
472 | 320 | up20180655 | gunner_set_pos(shooter2, host_info->host_x, host_info->host_y); |
473 | gunner_set_angle(shooter2, host_info->host_angle); |
||
474 | gunner_set_health(shooter2, host_info->host_health); |
||
475 | gunner_set_curr_health(shooter2, host_info->host_current_health); |
||
476 | 311 | up20180655 | |
477 | 320 | up20180655 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
478 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
479 | 311 | up20180655 | |
480 | 321 | up20180642 | for (size_t j = 0; j < host_info->no_bullets; j++) { |
481 | if (host_info->bullets_shooter[j]) { // remote |
||
482 | bullet_t *bullet = bullet_ctor(shooter1, bsp_bullet, host_info->bullets_x[j], host_info->bullets_y[j], host_info->bullets_vx[j], host_info->bullets_vy[j]); |
||
483 | 320 | up20180655 | list_insert(bullet_list, list_end(bullet_list), bullet); |
484 | } else { // host |
||
485 | 321 | up20180642 | bullet_t *bullet = bullet_ctor(shooter2, bsp_bullet, host_info->bullets_x[j], host_info->bullets_y[j], host_info->bullets_vx[j], host_info->bullets_vy[j]); |
486 | 320 | up20180655 | list_insert(bullet_list, list_end(bullet_list), bullet); |
487 | } |
||
488 | } |
||
489 | |||
490 | graph_clear_screen(); |
||
491 | map_draw (map1); |
||
492 | bullet_draw_list(bullet_list); |
||
493 | gunner_draw_list(shooter_list); |
||
494 | |||
495 | text_draw(in_game_timer->text); |
||
496 | |||
497 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
||
498 | sprite_draw(sp_crosshair); |
||
499 | graph_draw(); |
||
500 | |||
501 | while(list_size(bullet_list) > 0){ |
||
502 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
503 | bullet_dtor(p); |
||
504 | } |
||
505 | |||
506 | break;
|
||
507 | case KBC_IRQ:
|
||
508 | 321 | up20180642 | if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) { |
509 | 320 | up20180655 | good = false;
|
510 | } |
||
511 | break;
|
||
512 | case MOUSE_IRQ:
|
||
513 | 323 | up20180642 | if (mouse_get_counter_mouse_ih() >= 3) { |
514 | mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
||
515 | 320 | up20180655 | update_mouse(&pp); |
516 | if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
|
||
517 | bullet_info->new_bullet = true;
|
||
518 | hltp_send_bullet_info(bullet_info); |
||
519 | } |
||
520 | last_lb = keys->lb_pressed; |
||
521 | 323 | up20180642 | mouse_set_counter_mouse_ih(0);
|
522 | 320 | up20180655 | } |
523 | break;
|
||
524 | |||
525 | case COM1_IRQ: nctp_ih(); break; |
||
526 | } |
||
527 | 311 | up20180655 | } |
528 | } |
||
529 | 320 | up20180655 | } |
530 | 311 | up20180655 | |
531 | 320 | up20180655 | while(list_size(shooter_list) > 0){ |
532 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
533 | gunner_dtor(p); |
||
534 | } |
||
535 | if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
||
536 | |||
537 | while(list_size(bullet_list) > 0){ |
||
538 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
539 | bullet_dtor(p); |
||
540 | } |
||
541 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
542 | |||
543 | host_info_dtor(host_info); |
||
544 | remote_info_dtor(remote_info); |
||
545 | bullet_info_dtor(bullet_info); |
||
546 | |||
547 | timer_dtor(in_game_timer); in_game_timer = NULL;
|
||
548 | |||
549 | 311 | up20180655 | return 0; |
550 | } |
||
551 | |||
552 | 300 | up20180642 | static int (campaign)(void); |
553 | 301 | up20180642 | static int (zombies)(void); |
554 | 300 | up20180642 | static int (singleplayer)(void) { |
555 | |||
556 | 295 | up20180642 | int r;
|
557 | |||
558 | 314 | up20180642 | menu_t *main_menu = menu_ctor(font_get_default()); |
559 | 300 | up20180642 | menu_add_item(main_menu, "Campaign");
|
560 | menu_add_item(main_menu, "Zombies");
|
||
561 | menu_add_item(main_menu, "Back");
|
||
562 | |||
563 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
||
564 | uint8_t last_lb = 0;
|
||
565 | struct packet pp;
|
||
566 | keys_t *keys = get_key_presses(); |
||
567 | |||
568 | /// loop stuff
|
||
569 | int click = 0; |
||
570 | 323 | up20180642 | uint64_t int_vector = 0;
|
571 | 300 | up20180642 | int good = true; |
572 | while (good) {
|
||
573 | /* Get a request message. */
|
||
574 | 304 | up20180642 | if((r = get_interrupts_vector(&int_vector))) return r; |
575 | 321 | up20180642 | uint32_t n = 1;
|
576 | for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
||
577 | 304 | up20180642 | if (int_vector & n) {
|
578 | interrupt_handler(i); |
||
579 | switch (i) {
|
||
580 | 305 | up20180642 | case TIMER0_IRQ:
|
581 | 300 | up20180642 | |
582 | 304 | up20180642 | graph_clear_screen(); |
583 | switch(menu_update_state(main_menu, click)){
|
||
584 | case -1: break; |
||
585 | case 0: campaign(); break; |
||
586 | case 1: zombies(); break; |
||
587 | case 2: good = false; break; |
||
588 | } |
||
589 | menu_draw(main_menu); |
||
590 | 300 | up20180642 | |
591 | 304 | up20180642 | click = 0;
|
592 | 300 | up20180642 | |
593 | 304 | up20180642 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
594 | sprite_draw(sp_crosshair); |
||
595 | graph_draw(); |
||
596 | 300 | up20180642 | |
597 | 304 | up20180642 | break;
|
598 | 305 | up20180642 | case KBC_IRQ:
|
599 | 321 | up20180642 | if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false; |
600 | 305 | up20180642 | case MOUSE_IRQ:
|
601 | 323 | up20180642 | if (mouse_get_counter_mouse_ih() >= 3) { |
602 | mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
||
603 | 304 | up20180642 | update_mouse(&pp); |
604 | if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
||
605 | last_lb = keys->lb_pressed; |
||
606 | 323 | up20180642 | mouse_set_counter_mouse_ih(0);
|
607 | 300 | up20180642 | } |
608 | break;
|
||
609 | 305 | up20180642 | case COM1_IRQ: nctp_ih(); break; |
610 | 304 | up20180642 | } |
611 | 300 | up20180642 | } |
612 | } |
||
613 | } |
||
614 | |||
615 | return 0; |
||
616 | } |
||
617 | |||
618 | static int (campaign)(void){ |
||
619 | |||
620 | int r;
|
||
621 | |||
622 | 295 | up20180642 | ent_set_scale(DEFAULT_SCALE); |
623 | 314 | up20180642 | text_timer_t *in_game_timer = timer_ctor(font_get_default()); |
624 | 295 | up20180642 | |
625 | list_t *shooter_list = list_ctor(); |
||
626 | |||
627 | 321 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
628 | 295 | up20180642 | gunner_set_spawn(shooter1, 75, 75); |
629 | gunner_set_pos(shooter1, 75, 75); |
||
630 | |||
631 | 321 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, GUNNER_PLAYER, 2);
|
632 | 295 | up20180642 | gunner_set_spawn(shooter2, 975, 75); |
633 | gunner_set_pos(shooter2, 775, 75); |
||
634 | |||
635 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
||
636 | list_insert(shooter_list, list_end(shooter_list), shooter2); |
||
637 | |||
638 | list_t *bullet_list = list_ctor(); |
||
639 | |||
640 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
||
641 | 305 | up20180642 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
642 | 295 | up20180642 | |
643 | 305 | up20180642 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
644 | uint8_t last_lb = 0;
|
||
645 | struct packet pp;
|
||
646 | keys_t *keys = get_key_presses(); |
||
647 | 295 | up20180642 | |
648 | /// loop stuff
|
||
649 | 323 | up20180642 | uint64_t int_vector = 0;
|
650 | 296 | up20180642 | int good = true; |
651 | while (good) {
|
||
652 | 304 | up20180642 | /* Get a request message. */
|
653 | if((r = get_interrupts_vector(&int_vector))) return r; |
||
654 | 321 | up20180642 | uint32_t n = 1;
|
655 | for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
||
656 | 309 | up20180642 | if(!good) break; |
657 | 305 | up20180642 | if (int_vector & n) {
|
658 | interrupt_handler(i); |
||
659 | switch (i) {
|
||
660 | case TIMER0_IRQ:
|
||
661 | 295 | up20180642 | |
662 | 321 | up20180642 | if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer); |
663 | 305 | up20180642 | update_movement(map1, shooter1, keys, shooter_list); |
664 | 295 | up20180642 | |
665 | 305 | up20180642 | update_game_state(map1, shooter_list, bullet_list); |
666 | 295 | up20180642 | |
667 | 305 | up20180642 | //update_scale();
|
668 | double angle = get_mouse_angle(shooter1);
|
||
669 | gunner_set_angle(shooter1, angle - M_PI_2); |
||
670 | 295 | up20180642 | |
671 | 305 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
672 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
673 | 295 | up20180642 | |
674 | 305 | up20180642 | graph_clear_screen(); |
675 | map_draw (map1); |
||
676 | bullet_draw_list(bullet_list); |
||
677 | gunner_draw_list(shooter_list); |
||
678 | 295 | up20180642 | |
679 | 305 | up20180642 | text_draw(in_game_timer->text); |
680 | 295 | up20180642 | |
681 | 305 | up20180642 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
682 | sprite_draw(sp_crosshair); |
||
683 | graph_draw(); |
||
684 | 295 | up20180642 | |
685 | 305 | up20180642 | break;
|
686 | case KBC_IRQ:
|
||
687 | 321 | up20180642 | if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) { |
688 | 305 | up20180642 | good = false;
|
689 | } |
||
690 | break;
|
||
691 | case MOUSE_IRQ:
|
||
692 | 323 | up20180642 | if (mouse_get_counter_mouse_ih() >= 3) { |
693 | mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
||
694 | 305 | up20180642 | update_mouse(&pp); |
695 | if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
||
696 | shoot_bullet(shooter1, bullet_list, bsp_bullet); |
||
697 | last_lb = keys->lb_pressed; |
||
698 | 323 | up20180642 | mouse_set_counter_mouse_ih(0);
|
699 | 304 | up20180642 | |
700 | 305 | up20180642 | } |
701 | break;
|
||
702 | case COM1_IRQ: nctp_ih(); break; |
||
703 | } |
||
704 | } |
||
705 | 304 | up20180642 | } |
706 | 295 | up20180642 | } |
707 | |||
708 | while(list_size(shooter_list) > 0){ |
||
709 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
710 | gunner_dtor(p); |
||
711 | } |
||
712 | 296 | up20180642 | if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
713 | 295 | up20180642 | |
714 | 301 | up20180642 | while(list_size(bullet_list) > 0){ |
715 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
716 | bullet_dtor(p); |
||
717 | } |
||
718 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
719 | |||
720 | timer_dtor(in_game_timer); in_game_timer = NULL;
|
||
721 | |||
722 | return SUCCESS;
|
||
723 | } |
||
724 | |||
725 | #define ZOMBIES_NUM 5 |
||
726 | #define ZOMBIE_HEALTH_FACTOR 1.1 |
||
727 | static int (zombies)(void){ |
||
728 | |||
729 | int r;
|
||
730 | |||
731 | ent_set_scale(DEFAULT_SCALE); |
||
732 | 314 | up20180642 | text_timer_t *in_game_timer = timer_ctor(font_get_default()); |
733 | 301 | up20180642 | |
734 | list_t *shooter_list = list_ctor(); |
||
735 | |||
736 | 321 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
737 | 305 | up20180642 | gunner_set_spawn(shooter1, 980, 790); |
738 | gunner_set_pos(shooter1, 980, 790); |
||
739 | 301 | up20180642 | |
740 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
||
741 | |||
742 | list_t *bullet_list = list_ctor(); |
||
743 | |||
744 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
||
745 | 305 | up20180642 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
746 | 301 | up20180642 | |
747 | 305 | up20180642 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
748 | uint8_t last_lb = 0;
|
||
749 | struct packet pp;
|
||
750 | keys_t *keys = get_key_presses(); |
||
751 | 301 | up20180642 | |
752 | /// loop stuff
|
||
753 | 323 | up20180642 | uint64_t int_vector = 0;
|
754 | 301 | up20180642 | int good = true; |
755 | 302 | up20180642 | int dead = false; |
756 | 301 | up20180642 | |
757 | int health = 50; |
||
758 | |||
759 | 309 | up20180642 | map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1)); |
760 | |||
761 | 302 | up20180642 | while (good && !dead) {
|
762 | 304 | up20180642 | /* Get a request message. */
|
763 | if((r = get_interrupts_vector(&int_vector))) return r; |
||
764 | 321 | up20180642 | uint32_t n = 1;
|
765 | for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
||
766 | 309 | up20180642 | if(!good || dead) break; |
767 | 304 | up20180642 | if (int_vector & n) {
|
768 | 305 | up20180642 | interrupt_handler(i); |
769 | switch (i) {
|
||
770 | case TIMER0_IRQ:
|
||
771 | 321 | up20180642 | if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer); |
772 | if (timer_get_no_interrupts() % 6 == 0){ |
||
773 | 309 | up20180642 | map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1)); |
774 | } |
||
775 | 301 | up20180642 | |
776 | 305 | up20180642 | update_movement(map1, shooter1, keys, shooter_list); |
777 | 301 | up20180642 | |
778 | 305 | up20180642 | update_game_state(map1, shooter_list, bullet_list); |
779 | 302 | up20180642 | |
780 | 305 | up20180642 | if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
|
781 | good = false;
|
||
782 | dead = true;
|
||
783 | break;
|
||
784 | } |
||
785 | 301 | up20180642 | |
786 | 305 | up20180642 | double angle = get_mouse_angle(shooter1);
|
787 | gunner_set_angle(shooter1, angle - M_PI_2); |
||
788 | 301 | up20180642 | |
789 | 305 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
790 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
791 | 301 | up20180642 | |
792 | 309 | up20180642 | while(list_size(shooter_list) < ZOMBIES_NUM+1){ |
793 | 321 | up20180642 | gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, GUNNER_MELEE | GUNNER_FOLLOW, 3);
|
794 | 305 | up20180642 | gunner_set_health(zombie, health); |
795 | gunner_set_curr_health(zombie, health); |
||
796 | health *= ZOMBIE_HEALTH_FACTOR; |
||
797 | 307 | up20180642 | get_random_spawn(map1, zombie, shooter_list); |
798 | 305 | up20180642 | list_push_back(shooter_list, zombie); |
799 | } |
||
800 | 301 | up20180642 | |
801 | 305 | up20180642 | graph_clear_screen(); |
802 | map_draw (map1); |
||
803 | bullet_draw_list(bullet_list); |
||
804 | gunner_draw_list(shooter_list); |
||
805 | 301 | up20180642 | |
806 | 305 | up20180642 | text_draw(in_game_timer->text); |
807 | 301 | up20180642 | |
808 | 305 | up20180642 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
809 | sprite_draw(sp_crosshair); |
||
810 | graph_draw(); |
||
811 | 304 | up20180642 | |
812 | 305 | up20180642 | break;
|
813 | case KBC_IRQ:
|
||
814 | 321 | up20180642 | if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) { |
815 | 305 | up20180642 | good = false;
|
816 | } |
||
817 | break;
|
||
818 | case MOUSE_IRQ:
|
||
819 | 323 | up20180642 | if (mouse_get_counter_mouse_ih() >= 3) { |
820 | mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
||
821 | 305 | up20180642 | update_mouse(&pp); |
822 | if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
||
823 | shoot_bullet(shooter1, bullet_list, bsp_bullet); |
||
824 | last_lb = keys->lb_pressed; |
||
825 | 323 | up20180642 | mouse_set_counter_mouse_ih(0);
|
826 | 305 | up20180642 | |
827 | } |
||
828 | break;
|
||
829 | case COM1_IRQ: nctp_ih(); break; |
||
830 | } |
||
831 | } |
||
832 | } |
||
833 | 301 | up20180642 | } |
834 | |||
835 | while(list_size(shooter_list) > 0){ |
||
836 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
837 | gunner_dtor(p); |
||
838 | } |
||
839 | if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
||
840 | |||
841 | 295 | up20180642 | while(list_size(bullet_list) > 0){ |
842 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
843 | bullet_dtor(p); |
||
844 | } |
||
845 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
846 | |||
847 | 302 | up20180642 | if(dead){
|
848 | printf("YOU DIED\n");
|
||
849 | } |
||
850 | |||
851 | 295 | up20180642 | timer_dtor(in_game_timer); in_game_timer = NULL;
|
852 | |||
853 | 294 | up20180642 | return SUCCESS;
|
854 | } |
||
855 | 296 | up20180642 | |
856 | 298 | up20180642 | #define CHAT_MAX_SIZE 75 |
857 | #define CHAT_MAX_NUM 19 |
||
858 | 321 | up20180642 | static text_t *t_text[CHAT_MAX_NUM] = {NULL}; |
859 | static rectangle_t *r_text = NULL; |
||
860 | 297 | up20180642 | static void chat_process(const uint8_t *p, const size_t sz){ |
861 | char buffer2[CHAT_MAX_NUM+3]; |
||
862 | void *dest = NULL; |
||
863 | hltp_type tp = hltp_interpret(p, sz, &dest); |
||
864 | switch(tp){
|
||
865 | case hltp_type_string:
|
||
866 | 305 | up20180642 | strcpy(buffer2, dest); |
867 | strncat(buffer2, " <", 2); |
||
868 | for(size_t i = CHAT_MAX_NUM-1; i; --i) |
||
869 | 314 | up20180642 | text_set_string(t_text[i], text_get_string(t_text[i-1]));
|
870 | text_set_string(t_text[0], buffer2);
|
||
871 | 305 | up20180642 | for(size_t i = 0; i < CHAT_MAX_NUM; ++i){ |
872 | if(text_get_string(t_text[i])[0] == '>'){ |
||
873 | text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
|
||
874 | text_set_halign(t_text[i], text_halign_left); |
||
875 | }else{
|
||
876 | text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
|
||
877 | text_set_halign(t_text[i], text_halign_right); |
||
878 | 297 | up20180642 | } |
879 | 305 | up20180642 | } |
880 | break;
|
||
881 | 321 | up20180642 | case hltp_type_invalid: break; |
882 | case hltp_type_bullet : break; |
||
883 | case hltp_type_host : break; |
||
884 | case hltp_type_remote : break; |
||
885 | 297 | up20180642 | } |
886 | } |
||
887 | 300 | up20180642 | static int (chat)(void){ |
888 | 297 | up20180642 | int r;
|
889 | |||
890 | 298 | up20180642 | nctp_dump(); |
891 | 297 | up20180642 | nctp_set_processor(chat_process); |
892 | |||
893 | struct packet pp;
|
||
894 | |||
895 | 298 | up20180642 | char buffer[CHAT_MAX_SIZE] = ""; |
896 | 297 | up20180642 | rectangle_t *r_buffer = NULL; {
|
897 | 298 | up20180642 | r_buffer = rectangle_ctor(0,0,900,70); |
898 | 297 | up20180642 | rectangle_set_pos(r_buffer, graph_get_XRes()/2 -rectangle_get_w(r_buffer)/2, |
899 | 321 | up20180642 | (int16_t)(graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2)); |
900 | 297 | up20180642 | rectangle_set_fill_color (r_buffer, GRAPH_BLACK); |
901 | rectangle_set_outline_width(r_buffer, 2);
|
||
902 | rectangle_set_outline_color(r_buffer, GRAPH_WHITE); |
||
903 | rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT); |
||
904 | } |
||
905 | text_t *t_buffer = NULL; {
|
||
906 | 314 | up20180642 | t_buffer = text_ctor(font_get_default(), "");
|
907 | 297 | up20180642 | text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
|
908 | 305 | up20180642 | rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
|
909 | 297 | up20180642 | text_set_halign(t_buffer, text_halign_left); |
910 | text_set_valign(t_buffer, text_valign_center); |
||
911 | text_set_color (t_buffer, TEXT_COLOR); |
||
912 | } |
||
913 | 298 | up20180642 | text_t *t_size = NULL; {
|
914 | 314 | up20180642 | t_size = text_ctor(font_get_default(), "");
|
915 | 298 | up20180642 | text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
|
916 | 305 | up20180642 | rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
|
917 | 298 | up20180642 | text_set_halign(t_size, text_halign_right); |
918 | text_set_valign(t_size, text_valign_bottom); |
||
919 | text_set_color (t_size, TEXT_COLOR); |
||
920 | text_set_size (t_size, 18);
|
||
921 | char buffer2[20]; |
||
922 | sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
|
||
923 | 314 | up20180642 | text_set_string(t_size, buffer2); |
924 | 298 | up20180642 | } |
925 | 297 | up20180642 | |
926 | /** r_text */ {
|
||
927 | 305 | up20180642 | r_text = rectangle_ctor(0,0,900,550); |
928 | rectangle_set_pos(r_text, graph_get_XRes()/2 -rectangle_get_w(r_buffer)/2, |
||
929 | 321 | up20180642 | (int16_t)(graph_get_YRes()*0.09)); |
930 | 305 | up20180642 | rectangle_set_fill_color (r_text, GRAPH_BLACK); |
931 | rectangle_set_outline_width(r_text, 2);
|
||
932 | rectangle_set_outline_color(r_text, GRAPH_WHITE); |
||
933 | rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT); |
||
934 | 311 | up20180655 | } |
935 | /** t_text */ {
|
||
936 | 321 | up20180642 | for(uint16_t i = 0; i < CHAT_MAX_NUM; ++i){ |
937 | 314 | up20180642 | t_text[i] = text_ctor(font_get_default(), " ");
|
938 | 311 | up20180655 | text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
|
939 | rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i); |
||
940 | text_set_halign(t_text[i], text_halign_left); |
||
941 | text_set_valign(t_text[i], text_valign_bottom); |
||
942 | text_set_color (t_text[i], TEXT_COLOR); |
||
943 | } |
||
944 | 305 | up20180642 | } |
945 | 297 | up20180642 | |
946 | 305 | up20180642 | /// loop stuff
|
947 | 323 | up20180642 | uint64_t int_vector = 0;
|
948 | 305 | up20180642 | int good = true; |
949 | while (good) {
|
||
950 | /* Get a request message. */
|
||
951 | if((r = get_interrupts_vector(&int_vector))) return r; |
||
952 | 321 | up20180642 | uint32_t n = 1;
|
953 | for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
||
954 | 305 | up20180642 | if (int_vector & n) {
|
955 | interrupt_handler(i); |
||
956 | switch (i) {
|
||
957 | 304 | up20180642 | case TIMER0_IRQ:
|
958 | 305 | up20180642 | graph_clear_screen(); |
959 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
||
960 | 297 | up20180642 | |
961 | 305 | up20180642 | rectangle_draw(r_buffer); |
962 | text_draw(t_buffer); |
||
963 | text_draw(t_size); |
||
964 | 297 | up20180642 | |
965 | 305 | up20180642 | rectangle_draw(r_text); |
966 | 321 | up20180642 | for(size_t j = 0; j < CHAT_MAX_NUM; ++j) text_draw(t_text[j]); |
967 | 297 | up20180642 | |
968 | 305 | up20180642 | sprite_draw(sp_crosshair); |
969 | graph_draw(); |
||
970 | break;
|
||
971 | 304 | up20180642 | case KBC_IRQ:
|
972 | 321 | up20180642 | if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false; |
973 | else if (keyboard_get_scancode()[0] == ENTER_MAKE_CODE) { |
||
974 | 305 | up20180642 | hltp_send_string(buffer); |
975 | char buffer2[CHAT_MAX_SIZE+3] = "> "; |
||
976 | strncat(buffer2, buffer, strlen(buffer)); |
||
977 | 321 | up20180642 | for(size_t j = CHAT_MAX_NUM-1; j; --j) text_set_string(t_text[i], text_get_string(t_text[i-1])); |
978 | 314 | up20180642 | text_set_string(t_text[0], buffer2);
|
979 | 321 | up20180642 | for(size_t j = 0; j < CHAT_MAX_NUM; ++j){ |
980 | if(text_get_string(t_text[j])[0] == '>'){ |
||
981 | text_set_pos(t_text[j], rectangle_get_x(r_text)+50, text_get_y(t_text[j]));
|
||
982 | text_set_halign(t_text[j], text_halign_left); |
||
983 | 305 | up20180642 | }else{
|
984 | 321 | up20180642 | text_set_pos(t_text[j], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[j]));
|
985 | text_set_halign(t_text[j], text_halign_right); |
||
986 | 297 | up20180642 | } |
987 | } |
||
988 | 305 | up20180642 | buffer[0] = '\0'; |
989 | 321 | up20180642 | } else if(keyboard_get_scancode()[0] == BACKSPACE_MAKE_CODE){ |
990 | 305 | up20180642 | buffer[strlen(buffer)-1] = '\0'; |
991 | } else {
|
||
992 | 321 | up20180642 | char c = map_makecode(keyboard_get_scancode()[0]); |
993 | 305 | up20180642 | if (c == ERROR_CODE) break; |
994 | if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1); |
||
995 | else printf("Char limit exceeded\n"); |
||
996 | } |
||
997 | 314 | up20180642 | text_set_string(t_buffer, buffer); |
998 | 305 | up20180642 | char buffer2[20]; |
999 | sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
|
||
1000 | 314 | up20180642 | text_set_string(t_size, buffer2); |
1001 | 304 | up20180642 | case MOUSE_IRQ:
|
1002 | 323 | up20180642 | if (mouse_get_counter_mouse_ih() >= 3) { |
1003 | mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
||
1004 | 305 | up20180642 | update_mouse(&pp); |
1005 | 323 | up20180642 | mouse_set_counter_mouse_ih(0);
|
1006 | 305 | up20180642 | } |
1007 | break;
|
||
1008 | 304 | up20180642 | case COM1_IRQ: nctp_ih(); break; |
1009 | 297 | up20180642 | } |
1010 | } |
||
1011 | } |
||
1012 | 305 | up20180642 | } |
1013 | 297 | up20180642 | |
1014 | 305 | up20180642 | rectangle_dtor(r_buffer); |
1015 | text_dtor (t_buffer); |
||
1016 | 297 | up20180642 | |
1017 | 305 | up20180642 | rectangle_dtor(r_text); |
1018 | for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]); |
||
1019 | 297 | up20180642 | |
1020 | 305 | up20180642 | nctp_set_processor(NULL);
|
1021 | 298 | up20180642 | |
1022 | 305 | up20180642 | return SUCCESS;
|
1023 | 296 | up20180642 | } |