Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 304

History | View | Annotate | Download (25.4 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 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 294 up20180642
font_t               *consolas      = NULL;
53
basic_sprite_t       *bsp_crosshair = NULL;
54
basic_sprite_t       *bsp_shooter   = NULL;
55 301 up20180642
basic_sprite_t       *bsp_zombie    = NULL;
56 294 up20180642
basic_sprite_t       *bsp_pistol    = NULL;
57
basic_sprite_t       *bsp_nothing   = NULL;
58
basic_sprite_t       *bsp_bullet    = NULL;
59
map_t                *map1          = NULL;
60
sprite_t             *sp_crosshair  = NULL;
61
62 300 up20180642
static int (singleplayer)(void);
63
static int (chat)(void);
64 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
65 170 up20180642
66
    int r;
67
68 294 up20180642
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
69 261 up20180642
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
70 192 up20180642
71 261 up20180642
    /// subscribe interrupts
72
    if (subscribe_all()) { return 1; }
73 170 up20180642
74 297 up20180642
    /// initialize graphics
75
    if(graph_init(GRAPH_MODE)){
76
        printf("%s: failed to initalize graphics.\n", __func__);
77
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
78
        return 1;
79
    }
80 152 up20180642
81 261 up20180642
    /// Load stuff
82
    {
83 297 up20180642
        graph_clear_screen();
84
        text_t *txt = text_ctor(consolas, "Loading...");
85
        text_draw(txt);
86
        text_dtor(txt);
87
        graph_draw();
88 183 up20180642
89 261 up20180642
        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 303 up20180642
        bsp_zombie    = get_zombie   (); if(bsp_zombie    == NULL) printf("Failed to get zombie\n");
92 261 up20180642
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
93
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
94
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
95
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
96 226 up20180642
97 261 up20180642
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
98
    }
99 226 up20180642
100 297 up20180642
    menu_t *main_menu = menu_ctor(consolas);
101 299 up20180642
    menu_add_item(main_menu, "Single player");
102
    menu_add_item(main_menu, "Multiplayer");
103 297 up20180642
    menu_add_item(main_menu, "Chat");
104
    menu_add_item(main_menu, "Exit");
105 234 up20180655
106 297 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
107
    uint8_t last_lb = 0;
108
    struct packet pp;
109
    keys_t *keys = get_key_presses();
110
111 261 up20180642
    /// loop stuff
112
    int ipc_status;
113
    message msg;
114 267 up20180655
115 297 up20180642
    int click = 0;
116 267 up20180655
117 295 up20180642
    int good = true;
118
119
    while (good) {
120 261 up20180642
        /* Get a request message. */
121
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
122
            printf("driver_receive failed with %d", r);
123
            continue;
124
        }
125
        if (is_ipc_notify(ipc_status)) { /* received notification */
126
            switch (_ENDPOINT_P(msg.m_source)) {
127
                case HARDWARE: /* hardware interrupt notification */
128
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
129
                        if (msg.m_notify.interrupts & n) {
130
                            interrupt_handler(i);
131
                            switch (i) {
132
                            case TIMER0_IRQ:
133 192 up20180642
134 295 up20180642
                                graph_clear_screen();
135
                                switch(menu_update_state(main_menu, click)){
136
                                    case -1: break;
137 300 up20180642
                                    case  0: singleplayer(); break; //campaign(); break;
138 299 up20180642
                                    case  1: break;
139
                                    case  2: chat(); break;
140
                                    case  3: good = false; break;
141 295 up20180642
                                }
142
                                menu_draw(main_menu);
143 192 up20180642
144 295 up20180642
                                click = 0;
145 202 up20180655
146 295 up20180642
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
147
                                sprite_draw(sp_crosshair);
148
                                graph_draw();
149 231 up20180655
150 261 up20180642
                                break;
151
                            case KBC_IRQ:
152 295 up20180642
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
153 261 up20180642
                            case MOUSE_IRQ:
154
                                if (counter_mouse_ih >= 3) {
155
                                    mouse_parse_packet(packet_mouse_ih, &pp);
156
                                    update_mouse(&pp);
157 295 up20180642
                                    if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
158 261 up20180642
                                    last_lb = keys->lb_pressed;
159
                                    counter_mouse_ih = 0;
160 220 up20180655
                                }
161 261 up20180642
                                break;
162 298 up20180642
                            case COM1_IRQ: nctp_ih(); break;
163 220 up20180655
                            }
164 251 up20180655
                        }
165 261 up20180642
                    }
166 167 up20180655
167 261 up20180642
                    break;
168
                default:
169
                    break; /* no other notifications expected: do nothing */
170 147 up20180655
            }
171 261 up20180642
        } else { /* received standart message, not a notification */
172
            /* no standart message expected: do nothing */
173 147 up20180655
        }
174 261 up20180642
    }
175 149 up20180655
176 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
177
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
178 301 up20180642
    /*basic_sprite_dtor      (bsp_zombie   );*/ bsp_zombie    = NULL;
179 261 up20180642
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
180
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
181
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
182
    map_dtor               (map1         ); map1          = NULL;
183
    font_dtor              (consolas     ); consolas      = NULL;
184 243 up20180642
185 261 up20180642
    // Unsubscribe interrupts
186
    if (unsubscribe_all()) {
187
        if (cleanup())
188
            printf("%s: failed to cleanup.\n", __func__);
189
        return 1;
190
    }
191 188 up20180642
192 261 up20180642
    if (cleanup()) {
193
        printf("%s: failed to cleanup.\n", __func__);
194
        return 1;
195
    }
196 155 up20180655
197 149 up20180655
    return 0;
198 147 up20180655
}
199 294 up20180642
200 300 up20180642
static int (campaign)(void);
201 301 up20180642
static int (zombies)(void);
202 300 up20180642
static int (singleplayer)(void) {
203
204 295 up20180642
    int r;
205
206 300 up20180642
    menu_t *main_menu = menu_ctor(consolas);
207
    menu_add_item(main_menu, "Campaign");
208
    menu_add_item(main_menu, "Zombies");
209
    menu_add_item(main_menu, "Back");
210
211
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
212
    uint8_t last_lb = 0;
213
    struct packet pp;
214
    keys_t *keys = get_key_presses();
215
216
    /// loop stuff
217
    int click = 0;
218 304 up20180642
    uint32_t int_vector = 0;
219 300 up20180642
    int good = true;
220
    while (good) {
221
        /* Get a request message. */
222 304 up20180642
        if((r = get_interrupts_vector(&int_vector))) return r;
223
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
224
            if (int_vector & n) {
225
                interrupt_handler(i);
226
                switch (i) {
227
                case TIMER0_IRQ:
228 300 up20180642
229 304 up20180642
                    graph_clear_screen();
230
                    switch(menu_update_state(main_menu, click)){
231
                        case -1: break;
232
                        case  0: campaign(); break;
233
                        case  1: zombies(); break;
234
                        case  2: good = false; break;
235
                    }
236
                    menu_draw(main_menu);
237 300 up20180642
238 304 up20180642
                    click = 0;
239 300 up20180642
240 304 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
241
                    sprite_draw(sp_crosshair);
242
                    graph_draw();
243 300 up20180642
244 304 up20180642
                    break;
245
                case KBC_IRQ:
246
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
247
                case MOUSE_IRQ:
248
                    if (counter_mouse_ih >= 3) {
249
                        mouse_parse_packet(packet_mouse_ih, &pp);
250
                        update_mouse(&pp);
251
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
252
                        last_lb = keys->lb_pressed;
253
                        counter_mouse_ih = 0;
254 300 up20180642
                    }
255
                    break;
256 304 up20180642
                case COM1_IRQ: nctp_ih(); break;
257
                }
258 300 up20180642
            }
259
        }
260
    }
261
262
    return 0;
263
}
264
265
static int (campaign)(void){
266
267
    int r;
268
269 295 up20180642
    ent_set_scale(DEFAULT_SCALE);
270
    text_timer_t *in_game_timer = timer_ctor(consolas);
271
272
    list_t *shooter_list = list_ctor();
273
274 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
275 295 up20180642
    gunner_set_spawn(shooter1, 75, 75);
276
    gunner_set_pos(shooter1, 75, 75);
277
278 302 up20180642
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
279 295 up20180642
    gunner_set_spawn(shooter2, 975, 75);
280
    gunner_set_pos(shooter2, 775, 75);
281
282
    list_insert(shooter_list, list_end(shooter_list), shooter1);
283
    list_insert(shooter_list, list_end(shooter_list), shooter2);
284
285
    list_t *bullet_list  = list_ctor();
286
287
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
288
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
289
290
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
291
   uint8_t last_lb = 0;
292
   struct packet pp;
293
   keys_t *keys = get_key_presses();
294
295
    /// loop stuff
296 304 up20180642
    uint32_t int_vector = 0;
297 296 up20180642
    int good = true;
298
    while (good) {
299 304 up20180642
        /* Get a request message. */
300
        if((r = get_interrupts_vector(&int_vector))) return r;
301
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
302
           if (int_vector & n) {
303
               interrupt_handler(i);
304
               switch (i) {
305
               case TIMER0_IRQ:
306 295 up20180642
307 304 up20180642
                   if (no_interrupts % 60 == 0) timer_update(in_game_timer);
308
                   update_movement(map1, shooter1, keys, shooter_list);
309 295 up20180642
310 304 up20180642
                   update_game_state(map1, shooter_list, bullet_list);
311 295 up20180642
312 304 up20180642
                   //update_scale();
313
                   double angle = get_mouse_angle(shooter1);
314
                   gunner_set_angle(shooter1, angle - M_PI_2);
315 295 up20180642
316 304 up20180642
                   ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
317
                                  gunner_get_y(shooter1)-ent_get_YLength()/2.0);
318 295 up20180642
319 304 up20180642
                   graph_clear_screen();
320
                   map_draw   (map1);
321
                   bullet_draw_list(bullet_list);
322
                   gunner_draw_list(shooter_list);
323 295 up20180642
324 304 up20180642
                   text_draw(in_game_timer->text);
325 295 up20180642
326 304 up20180642
                   sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
327
                   sprite_draw(sp_crosshair);
328
                   graph_draw();
329 295 up20180642
330 304 up20180642
                   break;
331
               case KBC_IRQ:
332
                   if ((scancode[0]) == ESC_BREAK_CODE) {
333
                       good = false;
334
                       // reset game
335
                       while(list_size(bullet_list) > 0){
336
                           bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
337
                           bullet_dtor(p);
338 295 up20180642
                       }
339 304 up20180642
                       list_node_t *it = list_begin(shooter_list);
340
                       while (it != list_end(shooter_list)) {
341
                           gunner_t *p = *(gunner_t**)list_node_val(it);
342
                           get_random_spawn(map1, p);
343
                           gunner_set_curr_health(p, gunner_get_health(p));
344
                           it = list_node_next(it);
345
                       }
346
                       timer_reset(in_game_timer);
347 295 up20180642
                   }
348
                   break;
349 304 up20180642
               case MOUSE_IRQ:
350
                   if (counter_mouse_ih >= 3) {
351
                       mouse_parse_packet(packet_mouse_ih, &pp);
352
                       update_mouse(&pp);
353
                       if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
354
                           shoot_bullet(shooter1, bullet_list, bsp_bullet);
355
                       last_lb = keys->lb_pressed;
356
                       counter_mouse_ih = 0;
357
358
                   }
359
                   break;
360
               case COM1_IRQ: nctp_ih(); break;
361
               }
362 295 up20180642
           }
363 304 up20180642
        }
364 295 up20180642
    }
365
366
    while(list_size(shooter_list) > 0){
367
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
368
        gunner_dtor(p);
369
    }
370 296 up20180642
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
371 295 up20180642
372 301 up20180642
    while(list_size(bullet_list) > 0){
373
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
374
        bullet_dtor(p);
375
    }
376
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
377
378
    timer_dtor(in_game_timer); in_game_timer = NULL;
379
380
    return SUCCESS;
381
}
382
383
#define ZOMBIES_NUM             5
384
#define ZOMBIE_HEALTH_FACTOR    1.1
385
386
static int (zombies)(void){
387
388
    int r;
389
390
    ent_set_scale(DEFAULT_SCALE);
391
    text_timer_t *in_game_timer = timer_ctor(consolas);
392
393
    list_t *shooter_list = list_ctor();
394
395 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
396 301 up20180642
    gunner_set_spawn(shooter1, 75, 75);
397
    gunner_set_pos(shooter1, 75, 75);
398
399
    list_insert(shooter_list, list_end(shooter_list), shooter1);
400
401
    list_t *bullet_list  = list_ctor();
402
403
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
404
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
405
406
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
407
   uint8_t last_lb = 0;
408
   struct packet pp;
409
   keys_t *keys = get_key_presses();
410
411
    /// loop stuff
412 304 up20180642
    uint32_t int_vector = 0;
413 301 up20180642
    int good = true;
414 302 up20180642
    int dead = false;
415 301 up20180642
416
    int health = 50;
417
418 302 up20180642
    while (good && !dead) {
419 304 up20180642
        /* Get a request message. */
420
        if((r = get_interrupts_vector(&int_vector))) return r;
421
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
422
            if (int_vector & n) {
423
               interrupt_handler(i);
424
               switch (i) {
425
               case TIMER0_IRQ: //printf("L463\n");
426 301 up20180642
427 304 up20180642
                   if (no_interrupts % 60 == 0) timer_update(in_game_timer);//printf("L465\n");
428
                   update_movement(map1, shooter1, keys, shooter_list);//printf("L466\n");
429 301 up20180642
430 304 up20180642
                   update_game_state(map1, shooter_list, bullet_list);//printf("L468\n");
431 301 up20180642
432 304 up20180642
                   if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
433
                       good = false;
434
                       dead = true;
435
                       break;
436
                   } //printf("L489\n");
437 302 up20180642
438 304 up20180642
                   //update_scale();
439
                   double angle = get_mouse_angle(shooter1);//printf("L471\n");
440
                   gunner_set_angle(shooter1, angle - M_PI_2); //printf("L472\n");
441 301 up20180642
442 304 up20180642
                   ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
443
                                  gunner_get_y(shooter1)-ent_get_YLength()/2.0);
444 301 up20180642
445 304 up20180642
                   while(list_size(shooter_list) < ZOMBIES_NUM){
446
                       gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_meelee, 3);
447
                       gunner_set_health(zombie, health);
448
                       gunner_set_curr_health(zombie, health);
449
                       health *= ZOMBIE_HEALTH_FACTOR;
450
                       get_random_spawn(map1, zombie);
451
                       list_push_back(shooter_list, zombie);
452
                   } //printf("L484\n");
453 301 up20180642
454 304 up20180642
                   graph_clear_screen();
455
                   map_draw   (map1);
456
                   bullet_draw_list(bullet_list);
457
                   gunner_draw_list(shooter_list);
458 301 up20180642
459 304 up20180642
                   text_draw(in_game_timer->text);
460 301 up20180642
461 304 up20180642
                   sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
462
                   sprite_draw(sp_crosshair);
463
                   graph_draw(); //printf("L508\n");
464 301 up20180642
465 304 up20180642
                   break;
466
               case KBC_IRQ:
467
                   if ((scancode[0]) == ESC_BREAK_CODE) {
468
                       good = false;
469
                       // reset game
470
                       while(list_size(bullet_list) > 0){
471
                           bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
472
                           bullet_dtor(p);
473 301 up20180642
                       }
474 304 up20180642
                       list_node_t *it = list_begin(shooter_list);
475
                       while (it != list_end(shooter_list)) {
476
                           gunner_t *p = *(gunner_t**)list_node_val(it);
477
                           get_random_spawn(map1, p);
478
                           gunner_set_curr_health(p, gunner_get_health(p));
479
                           it = list_node_next(it);
480
                       }
481
                       timer_reset(in_game_timer);
482 301 up20180642
                   }
483
                   break;
484 304 up20180642
               case MOUSE_IRQ:
485
                   if (counter_mouse_ih >= 3) {
486
                       mouse_parse_packet(packet_mouse_ih, &pp);
487
                       update_mouse(&pp);
488
                       if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
489
                           shoot_bullet(shooter1, bullet_list, bsp_bullet);
490
                       last_lb = keys->lb_pressed;
491
                       counter_mouse_ih = 0;
492
493
                   }
494
                   break;
495
               case COM1_IRQ: nctp_ih(); break;
496
               }
497 301 up20180642
           }
498
       }
499
    }
500
501
    while(list_size(shooter_list) > 0){
502
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
503
        gunner_dtor(p);
504
    }
505
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
506
507 295 up20180642
    while(list_size(bullet_list) > 0){
508
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
509
        bullet_dtor(p);
510
    }
511
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
512
513 302 up20180642
    if(dead){
514
        printf("YOU DIED\n");
515
    }
516
517 295 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
518
519 294 up20180642
    return SUCCESS;
520
}
521 296 up20180642
522 298 up20180642
#define CHAT_MAX_SIZE   75
523
#define CHAT_MAX_NUM    19
524 297 up20180642
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
525
rectangle_t *r_text               =  NULL;
526
static void chat_process(const uint8_t *p, const size_t sz){
527
    char buffer2[CHAT_MAX_NUM+3];
528
    void *dest = NULL;
529
    hltp_type tp = hltp_interpret(p, sz, &dest);
530
    switch(tp){
531
        case hltp_type_string:
532
            strcpy(buffer2, dest);
533
            strncat(buffer2, " <", 2);
534
            for(size_t i = CHAT_MAX_NUM-1; i; --i)
535
                text_set_text(t_text[i], text_get_string(t_text[i-1]));
536
            text_set_text(t_text[0], buffer2);
537
            for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
538
                if(text_get_string(t_text[i])[0] == '>'){
539
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
540
                    text_set_halign(t_text[i], text_halign_left);
541
                }else{
542
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
543
                    text_set_halign(t_text[i], text_halign_right);
544
                }
545
            }
546
            break;
547
        default: break;
548
    }
549
}
550 300 up20180642
static int (chat)(void){
551 297 up20180642
    int r;
552
553 298 up20180642
    nctp_dump();
554 297 up20180642
    nctp_set_processor(chat_process);
555
556
    struct packet pp;
557
558 298 up20180642
    char buffer[CHAT_MAX_SIZE] = "";
559 297 up20180642
    rectangle_t *r_buffer = NULL; {
560 298 up20180642
        r_buffer = rectangle_ctor(0,0,900,70);
561 297 up20180642
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
562 298 up20180642
                                    graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
563 297 up20180642
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
564
        rectangle_set_outline_width(r_buffer, 2);
565
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
566
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
567
    }
568
    text_t      *t_buffer = NULL; {
569
        t_buffer = text_ctor(consolas, "");
570
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
571
                               rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
572
        text_set_halign(t_buffer, text_halign_left);
573
        text_set_valign(t_buffer, text_valign_center);
574
        text_set_color (t_buffer, TEXT_COLOR);
575
    }
576 298 up20180642
    text_t      *t_size   = NULL; {
577
        t_size = text_ctor(consolas, "");
578
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
579
                             rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
580
        text_set_halign(t_size, text_halign_right);
581
        text_set_valign(t_size, text_valign_bottom);
582
        text_set_color (t_size, TEXT_COLOR);
583
        text_set_size  (t_size, 18);
584
        char buffer2[20];
585
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
586
        text_set_text(t_size, buffer2);
587
    }
588 297 up20180642
589
    /** r_text */ {
590 298 up20180642
        r_text = rectangle_ctor(0,0,900,550);
591 297 up20180642
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
592 298 up20180642
                                  graph_get_YRes()*0.09);
593 297 up20180642
        rectangle_set_fill_color   (r_text, GRAPH_BLACK);
594
        rectangle_set_outline_width(r_text, 2);
595
        rectangle_set_outline_color(r_text, GRAPH_WHITE);
596
        rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
597
    }
598
    /** t_text */ {
599
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
600
            t_text[i] = text_ctor(consolas, " ");
601
            text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
602 298 up20180642
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
603 297 up20180642
            text_set_halign(t_text[i], text_halign_left);
604
            text_set_valign(t_text[i], text_valign_bottom);
605
            text_set_color (t_text[i], TEXT_COLOR);
606
        }
607
    }
608
609
    /// loop stuff
610 304 up20180642
    uint32_t int_vector = 0;
611 297 up20180642
    int good = true;
612
    while (good) {
613
        /* Get a request message. */
614 304 up20180642
        if((r = get_interrupts_vector(&int_vector))) return r;
615
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
616
            if (int_vector & n) {
617
                interrupt_handler(i);
618
                switch (i) {
619
                case TIMER0_IRQ:
620
                    graph_clear_screen();
621
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
622 297 up20180642
623 304 up20180642
                    rectangle_draw(r_buffer);
624
                    text_draw(t_buffer);
625
                    text_draw(t_size);
626 297 up20180642
627 304 up20180642
                    rectangle_draw(r_text);
628
                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
629 297 up20180642
630 304 up20180642
                    sprite_draw(sp_crosshair);
631
                    graph_draw();
632
                    break;
633
                case KBC_IRQ:
634
                    if      (scancode[0] == ESC_BREAK_CODE) good = false;
635
                    else if (scancode[0] == ENTER_MAKE_CODE) {
636
                        hltp_send_string(buffer);
637
                        char buffer2[CHAT_MAX_SIZE+3] = "> ";
638
                        strncat(buffer2, buffer, strlen(buffer));
639
                        for(size_t i = CHAT_MAX_NUM-1; i; --i)
640
                            text_set_text(t_text[i], text_get_string(t_text[i-1]));
641
                        text_set_text(t_text[0], buffer2);
642
                        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
643
                            if(text_get_string(t_text[i])[0] == '>'){
644
                                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
645
                                text_set_halign(t_text[i], text_halign_left);
646
                            }else{
647
                                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
648
                                text_set_halign(t_text[i], text_halign_right);
649 297 up20180642
                            }
650
                        }
651 304 up20180642
                        buffer[0] = '\0';
652
                    } else if(scancode[0] == BACKSPACE_MAKE_CODE){
653
                        buffer[strlen(buffer)-1] = '\0';
654
                    } else {
655
                        char c = map_makecode(scancode[0]);
656
                        if (c == ERROR_CODE) break;
657
                        if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
658
                        else                               printf("Char limit exceeded\n");
659 297 up20180642
                    }
660 304 up20180642
                    text_set_text(t_buffer, buffer);
661
                    char buffer2[20];
662
                    sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
663
                    text_set_text(t_size, buffer2);
664
                case MOUSE_IRQ:
665
                    if (counter_mouse_ih >= 3) {
666
                        mouse_parse_packet(packet_mouse_ih, &pp);
667
                        update_mouse(&pp);
668
                        counter_mouse_ih = 0;
669
                    }
670 297 up20180642
                    break;
671 304 up20180642
                case COM1_IRQ: nctp_ih(); break;
672
                }
673 297 up20180642
            }
674
        }
675
    }
676
677
    rectangle_dtor(r_buffer);
678
    text_dtor     (t_buffer);
679
680
    rectangle_dtor(r_text);
681
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
682
683 298 up20180642
    nctp_set_processor(NULL);
684
685 296 up20180642
    return SUCCESS;
686
}